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 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 36 | using namespace clang; |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 37 | using namespace sema; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 38 | |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 39 | // Exported for use by Parser. |
| 40 | SourceRange |
| 41 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
| 42 | unsigned N) { |
| 43 | if (!N) return SourceRange(); |
| 44 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
| 45 | } |
| 46 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 47 | /// \brief Determine whether the declaration found is acceptable as the name |
| 48 | /// of a template and, if so, return that template declaration. Otherwise, |
| 49 | /// returns NULL. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 50 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 51 | NamedDecl *Orig, |
| 52 | bool AllowFunctionTemplates) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 53 | NamedDecl *D = Orig->getUnderlyingDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 55 | if (isa<TemplateDecl>(D)) { |
| 56 | if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 57 | return nullptr; |
| 58 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 59 | return Orig; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 60 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 62 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 63 | // C++ [temp.local]p1: |
| 64 | // Like normal (non-template) classes, class templates have an |
| 65 | // injected-class-name (Clause 9). The injected-class-name |
| 66 | // can be used with or without a template-argument-list. When |
| 67 | // it is used without a template-argument-list, it is |
| 68 | // equivalent to the injected-class-name followed by the |
| 69 | // template-parameters of the class template enclosed in |
| 70 | // <>. When it is used with a template-argument-list, it |
| 71 | // refers to the specified class template specialization, |
| 72 | // which could be the current specialization or another |
| 73 | // specialization. |
| 74 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 568a071 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 75 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 76 | if (Record->getDescribedClassTemplate()) |
| 77 | return Record->getDescribedClassTemplate(); |
| 78 | |
| 79 | if (ClassTemplateSpecializationDecl *Spec |
| 80 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 81 | return Spec->getSpecializedTemplate(); |
| 82 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 84 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 85 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 87 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 90 | void Sema::FilterAcceptableTemplateNames(LookupResult &R, |
| 91 | bool AllowFunctionTemplates) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 92 | // The set of class templates we've already seen. |
| 93 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 94 | LookupResult::Filter filter = R.makeFilter(); |
| 95 | while (filter.hasNext()) { |
| 96 | NamedDecl *Orig = filter.next(); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 97 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig, |
| 98 | AllowFunctionTemplates); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 99 | if (!Repl) |
| 100 | filter.erase(); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 101 | else if (Repl != Orig) { |
| 102 | |
| 103 | // C++ [temp.local]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 104 | // 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] | 105 | // 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] | 106 | // one base class). If all of the injected-class-names that are found |
| 107 | // refer to specializations of the same class template, and if the name |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 108 | // is used as a template-name, the reference refers to the class |
| 109 | // template itself and not a specialization thereof, and is not |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 110 | // ambiguous. |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 111 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 112 | if (!ClassTemplates.insert(ClassTmpl).second) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 113 | filter.erase(); |
| 114 | continue; |
| 115 | } |
John McCall | bd8062d | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 116 | |
| 117 | // FIXME: we promote access to public here as a workaround to |
| 118 | // the fact that LookupResult doesn't let us remember that we |
| 119 | // found this template through a particular injected class name, |
| 120 | // which means we end up doing nasty things to the invariants. |
| 121 | // Pretending that access is public is *much* safer. |
| 122 | filter.replace(Repl, AS_public); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 123 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 124 | } |
| 125 | filter.done(); |
| 126 | } |
| 127 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 128 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R, |
| 129 | bool AllowFunctionTemplates) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 130 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 131 | if (isAcceptableTemplateName(Context, *I, AllowFunctionTemplates)) |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 132 | return true; |
| 133 | |
Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 134 | return false; |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 137 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 138 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 139 | bool hasTemplateKeyword, |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 140 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 141 | ParsedType ObjectTypePtr, |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 142 | bool EnteringContext, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 143 | TemplateTy &TemplateResult, |
| 144 | bool &MemberOfUnknownSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 145 | assert(getLangOpts().CPlusPlus && "No template names in C!"); |
Douglas Gregor | 411e5ac | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 146 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 147 | DeclarationName TName; |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 148 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 149 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 150 | switch (Name.getKind()) { |
| 151 | case UnqualifiedId::IK_Identifier: |
| 152 | TName = DeclarationName(Name.Identifier); |
| 153 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 154 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 155 | case UnqualifiedId::IK_OperatorFunctionId: |
| 156 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 157 | Name.OperatorFunctionId.Operator); |
| 158 | break; |
| 159 | |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 160 | case UnqualifiedId::IK_LiteralOperatorId: |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 161 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 162 | break; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 163 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 164 | default: |
| 165 | return TNK_Non_template; |
| 166 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 168 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 170 | LookupResult R(*this, TName, Name.getLocStart(), LookupOrdinaryName); |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 171 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 172 | MemberOfUnknownSpecialization); |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 173 | if (R.empty()) return TNK_Non_template; |
| 174 | if (R.isAmbiguous()) { |
| 175 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 176 | R.suppressDiagnostics(); |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 177 | |
| 178 | // FIXME: we might have ambiguous templates, in which case we |
| 179 | // should at least parse them properly! |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 180 | return TNK_Non_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 181 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 182 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 183 | TemplateName Template; |
| 184 | TemplateNameKind TemplateKind; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 186 | unsigned ResultCount = R.end() - R.begin(); |
| 187 | if (ResultCount > 1) { |
| 188 | // We assume that we'll preserve the qualifier from a function |
| 189 | // template name in other ways. |
| 190 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 191 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 192 | |
| 193 | // We'll do this lookup again later. |
| 194 | R.suppressDiagnostics(); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 195 | } else { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 196 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 197 | |
| 198 | if (SS.isSet() && !SS.isInvalid()) { |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 199 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 200 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 201 | hasTemplateKeyword, TD); |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 202 | } else { |
| 203 | Template = TemplateName(TD); |
| 204 | } |
| 205 | |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 206 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 207 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 208 | |
| 209 | // We'll do this lookup again later. |
| 210 | R.suppressDiagnostics(); |
| 211 | } else { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 212 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 213 | isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD) || |
| 214 | isa<BuiltinTemplateDecl>(TD)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 215 | TemplateKind = |
| 216 | isa<VarTemplateDecl>(TD) ? TNK_Var_template : TNK_Type_template; |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 217 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 218 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 220 | TemplateResult = TemplateTy::make(Template); |
| 221 | return TemplateKind; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 222 | } |
| 223 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 224 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 225 | SourceLocation IILoc, |
| 226 | Scope *S, |
| 227 | const CXXScopeSpec *SS, |
| 228 | TemplateTy &SuggestedTemplate, |
| 229 | TemplateNameKind &SuggestedKind) { |
| 230 | // We can't recover unless there's a dependent scope specifier preceding the |
| 231 | // template name. |
Douglas Gregor | 20c38a7 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 232 | // FIXME: Typo correction? |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 233 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 234 | computeDeclContext(*SS)) |
| 235 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 236 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 237 | // The code is missing a 'template' keyword prior to the dependent template |
| 238 | // name. |
| 239 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 240 | Diag(IILoc, diag::err_template_kw_missing) |
| 241 | << Qualifier << II.getName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 242 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 243 | SuggestedTemplate |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 244 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 245 | SuggestedKind = TNK_Dependent_template_name; |
| 246 | return true; |
| 247 | } |
| 248 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 249 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 250 | Scope *S, CXXScopeSpec &SS, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 251 | QualType ObjectType, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 252 | bool EnteringContext, |
| 253 | bool &MemberOfUnknownSpecialization) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 254 | // Determine where to perform name lookup |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 255 | MemberOfUnknownSpecialization = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 256 | DeclContext *LookupCtx = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 257 | bool isDependent = false; |
| 258 | if (!ObjectType.isNull()) { |
| 259 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 260 | // x->B::f, and we are looking into the type of the object. |
| 261 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 262 | LookupCtx = computeDeclContext(ObjectType); |
| 263 | isDependent = ObjectType->isDependentType(); |
Richard Smith | 5ed7956 | 2013-06-07 20:03:01 +0000 | [diff] [blame] | 264 | assert((isDependent || !ObjectType->isIncompleteType() || |
| 265 | ObjectType->castAs<TagType>()->isBeingDefined()) && |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 266 | "Caller should have completed object type"); |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 267 | |
| 268 | // Template names cannot appear inside an Objective-C class or object type. |
| 269 | if (ObjectType->isObjCObjectOrInterfaceType()) { |
| 270 | Found.clear(); |
| 271 | return; |
| 272 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 273 | } else if (SS.isSet()) { |
| 274 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 275 | // so long into the context associated with the prior nested-name-specifier. |
| 276 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 277 | isDependent = isDependentScopeSpecifier(SS); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 278 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 279 | // The declaration context must be complete. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 280 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 281 | return; |
| 282 | } |
| 283 | |
| 284 | bool ObjectTypeSearchedInScope = false; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 285 | bool AllowFunctionTemplatesInLookup = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 286 | if (LookupCtx) { |
| 287 | // Perform "qualified" name lookup into the declaration context we |
| 288 | // computed, which is either the type of the base of a member access |
| 289 | // expression or the declaration context associated with a prior |
| 290 | // nested-name-specifier. |
| 291 | LookupQualifiedName(Found, LookupCtx); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 292 | if (!ObjectType.isNull() && Found.empty()) { |
| 293 | // C++ [basic.lookup.classref]p1: |
| 294 | // In a class member access expression (5.2.5), if the . or -> token is |
| 295 | // immediately followed by an identifier followed by a <, the |
| 296 | // identifier must be looked up to determine whether the < is the |
| 297 | // beginning of a template argument list (14.2) or a less-than operator. |
| 298 | // The identifier is first looked up in the class of the object |
| 299 | // expression. If the identifier is not found, it is then looked up in |
| 300 | // the context of the entire postfix-expression and shall name a class |
| 301 | // or function template. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 302 | if (S) LookupName(Found, S); |
| 303 | ObjectTypeSearchedInScope = true; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 304 | AllowFunctionTemplatesInLookup = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 305 | } |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 306 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 307 | // We cannot look into a dependent object type or nested nme |
| 308 | // specifier. |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 309 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 310 | return; |
| 311 | } else { |
| 312 | // Perform unqualified name lookup in the current scope. |
| 313 | LookupName(Found, S); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 314 | |
| 315 | if (!ObjectType.isNull()) |
| 316 | AllowFunctionTemplatesInLookup = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 319 | if (Found.empty() && !isDependent) { |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 320 | // If we did not find any names, attempt to correct any typos. |
| 321 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 322 | Found.clear(); |
Kaelyn Uhrain | 637b5b3 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 323 | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 324 | auto FilterCCC = llvm::make_unique<CorrectionCandidateCallback>(); |
| 325 | FilterCCC->WantTypeSpecifiers = false; |
| 326 | FilterCCC->WantExpressionKeywords = false; |
| 327 | FilterCCC->WantRemainingKeywords = false; |
| 328 | FilterCCC->WantCXXNamedCasts = true; |
| 329 | if (TypoCorrection Corrected = CorrectTypo( |
| 330 | Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS, |
| 331 | std::move(FilterCCC), CTK_ErrorRecovery, LookupCtx)) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 332 | Found.setLookupName(Corrected.getCorrection()); |
Richard Smith | de6d6c4 | 2015-12-29 19:43:10 +0000 | [diff] [blame] | 333 | if (auto *ND = Corrected.getFoundDecl()) |
| 334 | Found.addDecl(ND); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 335 | FilterAcceptableTemplateNames(Found); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 336 | if (!Found.empty()) { |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 337 | if (LookupCtx) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 338 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 339 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 340 | Name.getAsString() == CorrectedStr; |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 341 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
| 342 | << Name << LookupCtx << DroppedSpecifier |
| 343 | << SS.getRange()); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 344 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 345 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 346 | } |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 347 | } |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 348 | } else { |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 349 | Found.setLookupName(Name); |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 353 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 354 | if (Found.empty()) { |
| 355 | if (isDependent) |
| 356 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 357 | return; |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 358 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 359 | |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 360 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && |
Richard Smith | e7d67f2 | 2013-09-03 21:22:41 +0000 | [diff] [blame] | 361 | !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 362 | // C++03 [basic.lookup.classref]p1: |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 363 | // [...] If the lookup in the class of the object expression finds a |
| 364 | // template, the name is also looked up in the context of the entire |
| 365 | // postfix-expression and [...] |
| 366 | // |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 367 | // Note: C++11 does not perform this second lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 368 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 369 | LookupOrdinaryName); |
| 370 | LookupName(FoundOuter, S); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 371 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 372 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 373 | if (FoundOuter.empty()) { |
| 374 | // - if the name is not found, the name found in the class of the |
| 375 | // object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 376 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 377 | FoundOuter.isAmbiguous()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 378 | // - if the name is found in the context of the entire |
| 379 | // postfix-expression and does not name a class template, the name |
| 380 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 381 | FoundOuter.clear(); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 382 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 383 | // - if the name found is a class template, it must refer to the same |
| 384 | // entity as the one found in the class of the object expression, |
| 385 | // otherwise the program is ill-formed. |
| 386 | if (!Found.isSingleResult() || |
| 387 | Found.getFoundDecl()->getCanonicalDecl() |
| 388 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 389 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 2f96e9f | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 390 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 391 | << Found.getLookupName() |
| 392 | << ObjectType; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 393 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 394 | diag::note_ambig_member_ref_object_type) |
| 395 | << ObjectType; |
| 396 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 397 | diag::note_ambig_member_ref_scope); |
| 398 | |
| 399 | // Recover by taking the template that we found in the object |
| 400 | // expression's type. |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 406 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 407 | /// was just parsed. This is only possible with an explicit scope |
| 408 | /// specifier naming a dependent type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 409 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 410 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 411 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 412 | const DeclarationNameInfo &NameInfo, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 413 | bool isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 414 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 415 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 416 | |
Reid Kleckner | 1af391df | 2016-03-11 18:59:12 +0000 | [diff] [blame] | 417 | // C++11 [expr.prim.general]p12: |
| 418 | // An id-expression that denotes a non-static data member or non-static |
| 419 | // member function of a class can only be used: |
| 420 | // (...) |
| 421 | // - if that id-expression denotes a non-static data member and it |
| 422 | // appears in an unevaluated operand. |
| 423 | // |
| 424 | // If this might be the case, form a DependentScopeDeclRefExpr instead of a |
| 425 | // CXXDependentScopeMemberExpr. The former can instantiate to either |
| 426 | // DeclRefExpr or MemberExpr depending on lookup results, while the latter is |
| 427 | // always a MemberExpr. |
| 428 | bool MightBeCxx11UnevalField = |
| 429 | getLangOpts().CPlusPlus11 && isUnevaluatedContext(); |
| 430 | |
| 431 | if (!MightBeCxx11UnevalField && !isAddressOfOperand && |
| 432 | isa<CXXMethodDecl>(DC) && cast<CXXMethodDecl>(DC)->isInstance()) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 433 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 434 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 435 | // Since the 'this' expression is synthesized, we don't need to |
| 436 | // perform the double-lookup check. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 437 | NamedDecl *FirstQualifierInScope = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 438 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 439 | return CXXDependentScopeMemberExpr::Create( |
| 440 | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
| 441 | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
| 442 | FirstQualifierInScope, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 445 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 446 | } |
| 447 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 448 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 449 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 450 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 451 | const DeclarationNameInfo &NameInfo, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 452 | const TemplateArgumentListInfo *TemplateArgs) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 453 | return DependentScopeDeclRefExpr::Create( |
| 454 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 455 | TemplateArgs); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 458 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 459 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 460 | /// declaration at location Loc. Returns true to indicate that this is |
| 461 | /// an error, and false otherwise. |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 462 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 463 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 464 | |
| 465 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 466 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 467 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 468 | |
| 469 | // C++ [temp.local]p4: |
| 470 | // A template-parameter shall not be redeclared within its |
| 471 | // scope (including nested scopes). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 473 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 474 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 477 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 478 | /// the parameter D to reference the templated declaration and return a pointer |
| 479 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 480 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 481 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 482 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 483 | return Temp; |
| 484 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 485 | return nullptr; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 488 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 489 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 490 | assert(Kind == Template && |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 491 | "Only template template arguments can be pack expansions here"); |
| 492 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 493 | "Template template argument pack expansion without packs"); |
| 494 | ParsedTemplateArgument Result(*this); |
| 495 | Result.EllipsisLoc = EllipsisLoc; |
| 496 | return Result; |
| 497 | } |
| 498 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 499 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 500 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 501 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 502 | switch (Arg.getKind()) { |
| 503 | case ParsedTemplateArgument::Type: { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 504 | TypeSourceInfo *DI; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 505 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 506 | if (!DI) |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 507 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 508 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 509 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 510 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 511 | case ParsedTemplateArgument::NonType: { |
| 512 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 513 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 514 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 515 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 516 | case ParsedTemplateArgument::Template: { |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 517 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 518 | TemplateArgument TArg; |
| 519 | if (Arg.getEllipsisLoc().isValid()) |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 520 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 521 | else |
| 522 | TArg = Template; |
| 523 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 524 | Arg.getScopeSpec().getWithLocInContext( |
| 525 | SemaRef.Context), |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 526 | Arg.getLocation(), |
| 527 | Arg.getEllipsisLoc()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 530 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 531 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 532 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 533 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 534 | /// \brief Translates template arguments as provided by the parser |
| 535 | /// into template arguments used by semantic analysis. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 536 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 537 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 538 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 539 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 540 | TemplateArgsIn[I])); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 541 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 542 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 543 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 544 | SourceLocation Loc, |
| 545 | IdentifierInfo *Name) { |
| 546 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
| 547 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 548 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 549 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 550 | } |
| 551 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 552 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 553 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 554 | /// the keyword "typename" was used to declare the type parameter |
| 555 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 556 | /// "class" or "typename" keyword. ParamName is the name of the |
| 557 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 558 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 559 | /// If the type parameter has a default argument, it will be added |
| 560 | /// later via ActOnTypeParameterDefault. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 561 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 562 | SourceLocation EllipsisLoc, |
| 563 | SourceLocation KeyLoc, |
| 564 | IdentifierInfo *ParamName, |
| 565 | SourceLocation ParamNameLoc, |
| 566 | unsigned Depth, unsigned Position, |
| 567 | SourceLocation EqualLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 568 | ParsedType DefaultArg) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | assert(S->isTemplateParamScope() && |
| 570 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 571 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 572 | SourceLocation Loc = ParamNameLoc; |
| 573 | if (!ParamName) |
| 574 | Loc = KeyLoc; |
| 575 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 576 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 577 | TemplateTypeParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 578 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 579 | KeyLoc, Loc, Depth, Position, ParamName, |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 580 | Typename, IsParameterPack); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 581 | Param->setAccess(AS_public); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 582 | |
| 583 | if (ParamName) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 584 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 585 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 586 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 587 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 588 | IdResolver.AddDecl(Param); |
| 589 | } |
| 590 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 591 | // C++0x [temp.param]p9: |
| 592 | // A default template-argument may be specified for any kind of |
| 593 | // template-parameter that is not a template parameter pack. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 594 | if (DefaultArg && IsParameterPack) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 595 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 596 | DefaultArg = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 599 | // Handle the default argument, if provided. |
| 600 | if (DefaultArg) { |
| 601 | TypeSourceInfo *DefaultTInfo; |
| 602 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 603 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 604 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 605 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 606 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 607 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 608 | UPPC_DefaultArgument)) |
| 609 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 610 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 611 | // Check the template argument itself. |
| 612 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 613 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 614 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 615 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 616 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 617 | Param->setDefaultArgument(DefaultTInfo); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 618 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 619 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 620 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 623 | /// \brief Check that the type of a non-type template parameter is |
| 624 | /// well-formed. |
| 625 | /// |
| 626 | /// \returns the (possibly-promoted) parameter type if valid; |
| 627 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 628 | QualType |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 629 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 630 | // We don't allow variably-modified types as the type of non-type template |
| 631 | // parameters. |
| 632 | if (T->isVariablyModifiedType()) { |
| 633 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 634 | << T; |
| 635 | return QualType(); |
| 636 | } |
| 637 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 638 | // C++ [temp.param]p4: |
| 639 | // |
| 640 | // A non-type template-parameter shall have one of the following |
| 641 | // (optionally cv-qualified) types: |
| 642 | // |
| 643 | // -- integral or enumeration type, |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 644 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 645 | // -- pointer to object or pointer to function, |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 646 | T->isPointerType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 647 | // -- reference to object or reference to function, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 648 | T->isReferenceType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 649 | // -- pointer to member, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 650 | T->isMemberPointerType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 651 | // -- std::nullptr_t. |
| 652 | T->isNullPtrType() || |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 653 | // If T is a dependent type, we can't do the check now, so we |
| 654 | // assume that it is well-formed. |
Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 655 | T->isDependentType()) { |
| 656 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 657 | // are ignored when determining its type. |
| 658 | return T.getUnqualifiedType(); |
| 659 | } |
| 660 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 661 | // C++ [temp.param]p8: |
| 662 | // |
| 663 | // A non-type template-parameter of type "array of T" or |
| 664 | // "function returning T" is adjusted to be of type "pointer to |
| 665 | // T" or "pointer to function returning T", respectively. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 666 | else if (T->isArrayType() || T->isFunctionType()) |
| 667 | return Context.getDecayedType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 668 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 669 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 670 | << T; |
| 671 | |
| 672 | return QualType(); |
| 673 | } |
| 674 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 675 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 676 | unsigned Depth, |
| 677 | unsigned Position, |
| 678 | SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 679 | Expr *Default) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 680 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 681 | QualType T = TInfo->getType(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 682 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 683 | assert(S->isTemplateParamScope() && |
| 684 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 685 | bool Invalid = false; |
| 686 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 687 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 688 | if (T.isNull()) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 689 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 690 | Invalid = true; |
| 691 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 692 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 693 | IdentifierInfo *ParamName = D.getIdentifier(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 694 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 695 | NonTypeTemplateParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 696 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 697 | D.getLocStart(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 698 | D.getIdentifierLoc(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 699 | Depth, Position, ParamName, T, |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 700 | IsParameterPack, TInfo); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 701 | Param->setAccess(AS_public); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 702 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 703 | if (Invalid) |
| 704 | Param->setInvalidDecl(); |
| 705 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 706 | if (ParamName) { |
| 707 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 708 | ParamName); |
| 709 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 710 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 711 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 712 | IdResolver.AddDecl(Param); |
| 713 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 714 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 715 | // C++0x [temp.param]p9: |
| 716 | // A default template-argument may be specified for any kind of |
| 717 | // template-parameter that is not a template parameter pack. |
| 718 | if (Default && IsParameterPack) { |
| 719 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 720 | Default = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 723 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 724 | if (Default) { |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 725 | // Check for unexpanded parameter packs. |
| 726 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 727 | return Param; |
| 728 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 729 | TemplateArgument Converted; |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 730 | ExprResult DefaultRes = |
| 731 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 732 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 733 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 734 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 735 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 736 | Default = DefaultRes.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 737 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 738 | Param->setDefaultArgument(Default); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 739 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 740 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 741 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 742 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 743 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 744 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 745 | /// parameter (e.g. T in template <template \<typename> class T> class array) |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 746 | /// has been parsed. S is the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 747 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 748 | SourceLocation TmpLoc, |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 749 | TemplateParameterList *Params, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 750 | SourceLocation EllipsisLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 751 | IdentifierInfo *Name, |
| 752 | SourceLocation NameLoc, |
| 753 | unsigned Depth, |
| 754 | unsigned Position, |
| 755 | SourceLocation EqualLoc, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 756 | ParsedTemplateArgument Default) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 757 | assert(S->isTemplateParamScope() && |
| 758 | "Template template parameter not in template parameter scope!"); |
| 759 | |
| 760 | // Construct the parameter object. |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 761 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 762 | TemplateTemplateParmDecl *Param = |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 763 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 764 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 765 | Depth, Position, IsParameterPack, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 766 | Name, Params); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 767 | Param->setAccess(AS_public); |
| 768 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 769 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 770 | // into the scope and lookup mechanisms. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 771 | if (Name) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 772 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 773 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 774 | S->AddDecl(Param); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 775 | IdResolver.AddDecl(Param); |
| 776 | } |
| 777 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 778 | if (Params->size() == 0) { |
| 779 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 780 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 781 | Param->setInvalidDecl(); |
| 782 | } |
| 783 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 784 | // C++0x [temp.param]p9: |
| 785 | // A default template-argument may be specified for any kind of |
| 786 | // template-parameter that is not a template parameter pack. |
| 787 | if (IsParameterPack && !Default.isInvalid()) { |
| 788 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 789 | Default = ParsedTemplateArgument(); |
| 790 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 791 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 792 | if (!Default.isInvalid()) { |
| 793 | // Check only that we have a template template argument. We don't want to |
| 794 | // try to check well-formedness now, because our template template parameter |
| 795 | // might have dependent types in its template parameters, which we wouldn't |
| 796 | // be able to match now. |
| 797 | // |
| 798 | // If none of the template template parameter's template arguments mention |
| 799 | // other template parameters, we could actually perform more checking here. |
| 800 | // However, it isn't worth doing. |
| 801 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 802 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 803 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_valid_template) |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 804 | << DefaultArg.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 805 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 806 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 807 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 808 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 809 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 810 | DefaultArg.getArgument().getAsTemplate(), |
| 811 | UPPC_DefaultArgument)) |
| 812 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 813 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 814 | Param->setDefaultArgument(Context, DefaultArg); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 815 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 816 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 817 | return Param; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 820 | /// ActOnTemplateParameterList - Builds a TemplateParameterList, optionally |
| 821 | /// constrained by RequiresClause, that contains the template parameters in |
| 822 | /// Params. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 823 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 824 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 825 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 826 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 827 | SourceLocation LAngleLoc, |
Craig Topper | 96225a5 | 2015-12-24 23:58:25 +0000 | [diff] [blame] | 828 | ArrayRef<Decl *> Params, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 829 | SourceLocation RAngleLoc, |
| 830 | Expr *RequiresClause) { |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 831 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 832 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 833 | |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 834 | // FIXME: store RequiresClause |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 835 | return TemplateParameterList::Create( |
| 836 | Context, TemplateLoc, LAngleLoc, |
| 837 | llvm::makeArrayRef((NamedDecl *const *)Params.data(), Params.size()), |
| 838 | RAngleLoc); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 839 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 840 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 841 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 842 | if (SS.isSet()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 843 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 844 | } |
| 845 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 846 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 847 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 848 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 849 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 850 | AttributeList *Attr, |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 851 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 852 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 853 | SourceLocation FriendLoc, |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 854 | unsigned NumOuterTemplateParamLists, |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 855 | TemplateParameterList** OuterTemplateParamLists, |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 856 | SkipBodyInfo *SkipBody) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 858 | "No template parameters"); |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 859 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 860 | bool Invalid = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 861 | |
| 862 | // Check that we can declare a template here. |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 863 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 864 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 865 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 866 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 867 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 868 | |
| 869 | // There is no such thing as an unnamed class template. |
| 870 | if (!Name) { |
| 871 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 872 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 875 | // Find any previous declaration with this name. For a friend with no |
| 876 | // scope explicitly specified, we only look for tag declarations (per |
| 877 | // C++11 [basic.lookup.elab]p2). |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 878 | DeclContext *SemanticContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 879 | LookupResult Previous(*this, Name, NameLoc, |
| 880 | (SS.isEmpty() && TUK == TUK_Friend) |
| 881 | ? LookupTagName : LookupOrdinaryName, |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 882 | ForRedeclaration); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 883 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 884 | SemanticContext = computeDeclContext(SS, true); |
| 885 | if (!SemanticContext) { |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 886 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 887 | // in the AST, and historically we have just ignored such friend |
| 888 | // class templates, so don't complain here. |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 889 | Diag(NameLoc, TUK == TUK_Friend |
| 890 | ? diag::warn_template_qualified_friend_ignored |
| 891 | : diag::err_template_qualified_declarator_no_match) |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 892 | << SS.getScopeRep() << SS.getRange(); |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 893 | return TUK != TUK_Friend; |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 894 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 895 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 896 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 897 | return true; |
| 898 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 899 | // If we're adding a template to a dependent context, we may need to |
| 900 | // rebuilding some of the types used within the template parameter list, |
| 901 | // now that we know what the current instantiation is. |
| 902 | if (SemanticContext->isDependentContext()) { |
| 903 | ContextRAII SavedContext(*this, SemanticContext); |
| 904 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 905 | Invalid = true; |
Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 906 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
| 907 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 908 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 909 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 910 | } else { |
| 911 | SemanticContext = CurContext; |
Richard Smith | 88fe69c | 2015-07-06 01:45:27 +0000 | [diff] [blame] | 912 | |
| 913 | // C++14 [class.mem]p14: |
| 914 | // If T is the name of a class, then each of the following shall have a |
| 915 | // name different from T: |
| 916 | // -- every member template of class T |
| 917 | if (TUK != TUK_Friend && |
| 918 | DiagnoseClassNameShadow(SemanticContext, |
| 919 | DeclarationNameInfo(Name, NameLoc))) |
| 920 | return true; |
| 921 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 922 | LookupName(Previous, S); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 923 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 924 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 925 | if (Previous.isAmbiguous()) |
| 926 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 927 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 928 | NamedDecl *PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 929 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 930 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 931 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 932 | // If there is a previous declaration with the same name, check |
| 933 | // whether this is a valid redeclaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 935 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 936 | |
| 937 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 938 | // class template partial specialization, or class template specialization. |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 939 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 940 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 941 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 942 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 943 | PrevClassTemplate |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 944 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 945 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 946 | PrevClassTemplate |
| 947 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 948 | ->getSpecializedTemplate(); |
| 949 | } |
| 950 | } |
| 951 | |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 952 | if (TUK == TUK_Friend) { |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 953 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 954 | // [...] When looking for a prior declaration of a class or a function |
| 955 | // 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] | 956 | // function is neither a qualified name nor a template-id, scopes outside |
| 957 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 958 | if (!SS.isSet()) { |
| 959 | DeclContext *OutermostContext = CurContext; |
| 960 | while (!OutermostContext->isFileContext()) |
| 961 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 962 | |
Richard Smith | 61e582f | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 963 | if (PrevDecl && |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 964 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 965 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 966 | SemanticContext = PrevDecl->getDeclContext(); |
| 967 | } else { |
| 968 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 969 | // context we computed is the semantic context for our new |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 970 | // declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 971 | PrevDecl = PrevClassTemplate = nullptr; |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 972 | SemanticContext = OutermostContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 973 | |
| 974 | // Check that the chosen semantic context doesn't already contain a |
| 975 | // declaration of this name as a non-tag type. |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 976 | Previous.clear(LookupOrdinaryName); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 977 | DeclContext *LookupContext = SemanticContext; |
| 978 | while (LookupContext->isTransparentContext()) |
| 979 | LookupContext = LookupContext->getLookupParent(); |
| 980 | LookupQualifiedName(Previous, LookupContext); |
| 981 | |
| 982 | if (Previous.isAmbiguous()) |
| 983 | return true; |
| 984 | |
| 985 | if (Previous.begin() != Previous.end()) |
| 986 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 987 | } |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 988 | } |
Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 989 | } else if (PrevDecl && |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 990 | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, |
| 991 | S, SS.isValid())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 992 | PrevDecl = PrevClassTemplate = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 993 | |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 994 | if (auto *Shadow = dyn_cast_or_null<UsingShadowDecl>( |
| 995 | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { |
| 996 | if (SS.isEmpty() && |
| 997 | !(PrevClassTemplate && |
| 998 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( |
| 999 | SemanticContext->getRedeclContext()))) { |
| 1000 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
| 1001 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 1002 | diag::note_using_decl_target); |
| 1003 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
| 1004 | // Recover by ignoring the old declaration. |
| 1005 | PrevDecl = PrevClassTemplate = nullptr; |
| 1006 | } |
| 1007 | } |
| 1008 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1009 | if (PrevClassTemplate) { |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1010 | // Ensure that the template parameter lists are compatible. Skip this check |
| 1011 | // for a friend in a dependent context: the template parameter list itself |
| 1012 | // could be dependent. |
| 1013 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 1014 | !TemplateParameterListsAreEqual(TemplateParams, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1015 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 1016 | /*Complain=*/true, |
| 1017 | TPL_TemplateMatch)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1018 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1019 | |
| 1020 | // C++ [temp.class]p4: |
| 1021 | // In a redeclaration, partial specialization, explicit |
| 1022 | // specialization or explicit instantiation of a class template, |
| 1023 | // the class-key shall agree in kind with the original class |
| 1024 | // template declaration (7.1.5.3). |
| 1025 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 1026 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 1027 | TUK == TUK_Definition, KWLoc, Name)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1029 | << Name |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1030 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1031 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1032 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1035 | // Check for redefinition of this class template. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1036 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1037 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1038 | // If we have a prior definition that is not visible, treat this as |
| 1039 | // simply making that previous definition visible. |
| 1040 | NamedDecl *Hidden = nullptr; |
| 1041 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1042 | SkipBody->ShouldSkip = true; |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1043 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
| 1044 | assert(Tmpl && "original definition of a class template is not a " |
| 1045 | "class template?"); |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1046 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 1047 | makeMergedDefinitionVisible(Tmpl, KWLoc); |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1048 | return Def; |
| 1049 | } |
| 1050 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1051 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1052 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1053 | // FIXME: Would it make sense to try to "forget" the previous |
| 1054 | // definition, as part of error recovery? |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1055 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1056 | } |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1057 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1058 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 1059 | // Maybe we will complain about the shadowed template parameter. |
| 1060 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 1061 | // Just pretend that we didn't see the previous declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1062 | PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1063 | } else if (PrevDecl) { |
| 1064 | // C++ [temp]p5: |
| 1065 | // A class template shall not have the same name as any other |
| 1066 | // template, class, function, object, enumeration, enumerator, |
| 1067 | // namespace, or type in the same scope (3.3), except as specified |
| 1068 | // in (14.5.4). |
| 1069 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1070 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1071 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1074 | // Check the template parameter list of this declaration, possibly |
| 1075 | // merging in the template parameter list from the previous class |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1076 | // template declaration. Skip this check for a friend in a dependent |
| 1077 | // context, because the template parameter list might be dependent. |
| 1078 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1079 | CheckTemplateParameterList( |
| 1080 | TemplateParams, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1081 | PrevClassTemplate ? PrevClassTemplate->getTemplateParameters() |
| 1082 | : nullptr, |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1083 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1084 | SemanticContext->isDependentContext()) |
| 1085 | ? TPC_ClassTemplateMember |
| 1086 | : TUK == TUK_Friend ? TPC_FriendClassTemplate |
| 1087 | : TPC_ClassTemplate)) |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1088 | Invalid = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1090 | if (SS.isSet()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1091 | // 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] | 1092 | // template out-of-line. |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1093 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1094 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1095 | : diag::err_member_decl_does_not_match) |
| 1096 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1097 | Invalid = true; |
| 1098 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1102 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1103 | PrevClassTemplate? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1104 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1105 | /*DelayTypeCreation=*/true); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1106 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1107 | if (NumOuterTemplateParamLists > 0) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1108 | NewClass->setTemplateParameterListsInfo( |
| 1109 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
| 1110 | NumOuterTemplateParamLists)); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1111 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1112 | // Add alignment attributes if necessary; these attributes are checked when |
| 1113 | // the ASTContext lays out the structure. |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1114 | if (TUK == TUK_Definition) { |
| 1115 | AddAlignmentAttributesForRecord(NewClass); |
| 1116 | AddMsStructLayoutForRecord(NewClass); |
| 1117 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1118 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1119 | ClassTemplateDecl *NewTemplate |
| 1120 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1121 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1122 | NewClass, PrevClassTemplate); |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1123 | NewClass->setDescribedClassTemplate(NewTemplate); |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1124 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1125 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1126 | NewTemplate->setModulePrivate(); |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1127 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1128 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1129 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1130 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1131 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1132 | (void)T; |
| 1133 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1134 | // 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] | 1135 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1136 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1137 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1138 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1139 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1140 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1141 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1142 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1143 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1144 | // Set the lexical context of these templates |
| 1145 | NewClass->setLexicalDeclContext(CurContext); |
| 1146 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1147 | |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1148 | if (TUK == TUK_Definition) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1149 | NewClass->startDefinition(); |
| 1150 | |
| 1151 | if (Attr) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1152 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1153 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1154 | if (PrevClassTemplate) |
| 1155 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1156 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1157 | AddPushedVisibilityAttribute(NewClass); |
| 1158 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1159 | if (TUK != TUK_Friend) { |
| 1160 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1161 | Scope *Outer = S; |
| 1162 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1163 | Outer = Outer->getParent(); |
| 1164 | PushOnScopeChains(NewTemplate, Outer); |
| 1165 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1166 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1167 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1168 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1169 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1170 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1171 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1172 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1173 | // Friend templates are visible in fairly strange ways. |
| 1174 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1175 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1176 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1177 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1178 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1179 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1180 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1181 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1182 | FriendDecl *Friend = FriendDecl::Create( |
| 1183 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1184 | Friend->setAccess(AS_public); |
| 1185 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1186 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1187 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1188 | if (Invalid) { |
| 1189 | NewTemplate->setInvalidDecl(); |
| 1190 | NewClass->setInvalidDecl(); |
| 1191 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1192 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1193 | ActOnDocumentableDecl(NewTemplate); |
| 1194 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1195 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1198 | /// \brief Diagnose the presence of a default template argument on a |
| 1199 | /// template parameter, which is ill-formed in certain contexts. |
| 1200 | /// |
| 1201 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1202 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1203 | Sema::TemplateParamListContext TPC, |
| 1204 | SourceLocation ParamLoc, |
| 1205 | SourceRange DefArgRange) { |
| 1206 | switch (TPC) { |
| 1207 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1208 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1209 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1210 | return false; |
| 1211 | |
| 1212 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1213 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1214 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1215 | // A default template-argument shall not be specified in a |
| 1216 | // function template declaration or a function template |
| 1217 | // definition [...] |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1218 | // If a friend function template declaration specifies a default |
| 1219 | // template-argument, that declaration shall be a definition and shall be |
| 1220 | // the only declaration of the function template in the translation unit. |
| 1221 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1222 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1223 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1224 | : diag::ext_template_parameter_default_in_function_template) |
| 1225 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1226 | return false; |
| 1227 | |
| 1228 | case Sema::TPC_ClassTemplateMember: |
| 1229 | // C++0x [temp.param]p9: |
| 1230 | // A default template-argument shall not be specified in the |
| 1231 | // template-parameter-lists of the definition of a member of a |
| 1232 | // class template that appears outside of the member's class. |
| 1233 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1234 | << DefArgRange; |
| 1235 | return true; |
| 1236 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1237 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1238 | case Sema::TPC_FriendFunctionTemplate: |
| 1239 | // C++ [temp.param]p9: |
| 1240 | // A default template-argument shall not be specified in a |
| 1241 | // friend template declaration. |
| 1242 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1243 | << DefArgRange; |
| 1244 | return true; |
| 1245 | |
| 1246 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1247 | // for friend function templates if there is only a single |
| 1248 | // declaration (and it is a definition). Strange! |
| 1249 | } |
| 1250 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1251 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1254 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1255 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1256 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1257 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1258 | // A template template parameter which is a parameter pack is also a pack |
| 1259 | // expansion. |
| 1260 | if (TTP->isParameterPack()) |
| 1261 | return false; |
| 1262 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1263 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1264 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1265 | NamedDecl *P = Params->getParam(I); |
| 1266 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1267 | if (!NTTP->isParameterPack() && |
| 1268 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1269 | NTTP->getTypeSourceInfo(), |
| 1270 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1271 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1272 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1273 | continue; |
| 1274 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1275 | |
| 1276 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1277 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1278 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1279 | return true; |
| 1280 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1281 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1282 | return false; |
| 1283 | } |
| 1284 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1285 | /// \brief Checks the validity of a template parameter list, possibly |
| 1286 | /// considering the template parameter list from a previous |
| 1287 | /// declaration. |
| 1288 | /// |
| 1289 | /// If an "old" template parameter list is provided, it must be |
| 1290 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1291 | /// template parameter list. |
| 1292 | /// |
| 1293 | /// \param NewParams Template parameter list for a new template |
| 1294 | /// declaration. This template parameter list will be updated with any |
| 1295 | /// default arguments that are carried through from the previous |
| 1296 | /// template parameter list. |
| 1297 | /// |
| 1298 | /// \param OldParams If provided, template parameter list from a |
| 1299 | /// previous declaration of the same template. Default template |
| 1300 | /// arguments will be merged from the old template parameter list to |
| 1301 | /// the new template parameter list. |
| 1302 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1303 | /// \param TPC Describes the context in which we are checking the given |
| 1304 | /// template parameter list. |
| 1305 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1306 | /// \returns true if an error occurred, false otherwise. |
| 1307 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1308 | TemplateParameterList *OldParams, |
| 1309 | TemplateParamListContext TPC) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1310 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1311 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1312 | // C++ [temp.param]p10: |
| 1313 | // The set of default template-arguments available for use with a |
| 1314 | // template declaration or definition is obtained by merging the |
| 1315 | // default arguments from the definition (if in scope) and all |
| 1316 | // declarations in scope in the same way default function |
| 1317 | // arguments are (8.3.6). |
| 1318 | bool SawDefaultArgument = false; |
| 1319 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1320 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1321 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1322 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1323 | if (OldParams) |
| 1324 | OldParam = OldParams->begin(); |
| 1325 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1326 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1327 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1328 | NewParamEnd = NewParams->end(); |
| 1329 | NewParam != NewParamEnd; ++NewParam) { |
| 1330 | // Variables used to diagnose redundant default arguments |
| 1331 | bool RedundantDefaultArg = false; |
| 1332 | SourceLocation OldDefaultLoc; |
| 1333 | SourceLocation NewDefaultLoc; |
| 1334 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1335 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1336 | bool MissingDefaultArg = false; |
| 1337 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1338 | // Variable used to diagnose non-final parameter packs |
| 1339 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1340 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1341 | if (TemplateTypeParmDecl *NewTypeParm |
| 1342 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1343 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1344 | if (NewTypeParm->hasDefaultArgument() && |
| 1345 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1346 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1347 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1348 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1349 | NewTypeParm->removeDefaultArgument(); |
| 1350 | |
| 1351 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1353 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1354 | if (NewTypeParm->isParameterPack()) { |
| 1355 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1356 | "Parameter packs can't have a default argument!"); |
| 1357 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1358 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1359 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1360 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1361 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1362 | SawDefaultArgument = true; |
| 1363 | RedundantDefaultArg = true; |
| 1364 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1365 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1366 | // Merge the default argument from the old declaration to the |
| 1367 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1368 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1369 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1370 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1371 | SawDefaultArgument = true; |
| 1372 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1373 | } else if (SawDefaultArgument) |
| 1374 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1375 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1376 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1377 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1378 | if (!NewNonTypeParm->isParameterPack() && |
| 1379 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1380 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1381 | UPPC_NonTypeTemplateParameterType)) { |
| 1382 | Invalid = true; |
| 1383 | continue; |
| 1384 | } |
| 1385 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1386 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1387 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1388 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1389 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1390 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1391 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1394 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1395 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1396 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1397 | if (NewNonTypeParm->isParameterPack()) { |
| 1398 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1399 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1400 | if (!NewNonTypeParm->isPackExpansion()) |
| 1401 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1402 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 1403 | NewNonTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1404 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1405 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1406 | SawDefaultArgument = true; |
| 1407 | RedundantDefaultArg = true; |
| 1408 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1409 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1410 | // Merge the default argument from the old declaration to the |
| 1411 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1412 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1413 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1414 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1415 | SawDefaultArgument = true; |
| 1416 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1417 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1419 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1420 | TemplateTemplateParmDecl *NewTemplateParm |
| 1421 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1422 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1423 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1424 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1425 | Invalid = true; |
| 1426 | continue; |
| 1427 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1428 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1429 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1430 | if (NewTemplateParm->hasDefaultArgument() && |
| 1431 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1432 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1433 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1434 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1435 | |
| 1436 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1437 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1438 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1439 | if (NewTemplateParm->isParameterPack()) { |
| 1440 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1441 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1442 | if (!NewTemplateParm->isPackExpansion()) |
| 1443 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1444 | } else if (OldTemplateParm && |
| 1445 | hasVisibleDefaultArgument(OldTemplateParm) && |
| 1446 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1447 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1448 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1449 | SawDefaultArgument = true; |
| 1450 | RedundantDefaultArg = true; |
| 1451 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1452 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1453 | // Merge the default argument from the old declaration to the |
| 1454 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1455 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1456 | PreviousDefaultArgLoc |
| 1457 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1458 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1459 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1460 | PreviousDefaultArgLoc |
| 1461 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1462 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1466 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1467 | // If a template parameter of a primary class template or alias template |
| 1468 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1469 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1470 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 1471 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1472 | Diag((*NewParam)->getLocation(), |
| 1473 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1474 | Invalid = true; |
| 1475 | } |
| 1476 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1477 | if (RedundantDefaultArg) { |
| 1478 | // C++ [temp.param]p12: |
| 1479 | // A template-parameter shall not be given default arguments |
| 1480 | // by two different declarations in the same scope. |
| 1481 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1482 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1483 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1484 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1485 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1486 | // If a template-parameter of a class template has a default |
| 1487 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1488 | // have a default template-argument supplied or be a template parameter |
| 1489 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1491 | diag::err_template_param_default_arg_missing); |
| 1492 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1493 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1494 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | // If we have an old template parameter list that we're merging |
| 1498 | // in, move on to the next parameter. |
| 1499 | if (OldParams) |
| 1500 | ++OldParam; |
| 1501 | } |
| 1502 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1503 | // We were missing some default arguments at the end of the list, so remove |
| 1504 | // all of the default arguments. |
| 1505 | if (RemoveDefaultArguments) { |
| 1506 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1507 | NewParamEnd = NewParams->end(); |
| 1508 | NewParam != NewParamEnd; ++NewParam) { |
| 1509 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1510 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1511 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1512 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1513 | NTTP->removeDefaultArgument(); |
| 1514 | else |
| 1515 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1516 | } |
| 1517 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1518 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1519 | return Invalid; |
| 1520 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1521 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1522 | namespace { |
| 1523 | |
| 1524 | /// A class which looks for a use of a certain level of template |
| 1525 | /// parameter. |
| 1526 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1527 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1528 | |
| 1529 | unsigned Depth; |
| 1530 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1531 | SourceLocation MatchLoc; |
| 1532 | |
| 1533 | DependencyChecker(unsigned Depth) : Depth(Depth), Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1534 | |
| 1535 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1536 | NamedDecl *ND = Params->getParam(0); |
| 1537 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1538 | Depth = PD->getDepth(); |
| 1539 | } else if (NonTypeTemplateParmDecl *PD = |
| 1540 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1541 | Depth = PD->getDepth(); |
| 1542 | } else { |
| 1543 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1544 | } |
| 1545 | } |
| 1546 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1547 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1548 | if (ParmDepth >= Depth) { |
| 1549 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1550 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1551 | return true; |
| 1552 | } |
| 1553 | return false; |
| 1554 | } |
| 1555 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1556 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1557 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 1558 | } |
| 1559 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1560 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1561 | return !Matches(T->getDepth()); |
| 1562 | } |
| 1563 | |
| 1564 | bool TraverseTemplateName(TemplateName N) { |
| 1565 | if (TemplateTemplateParmDecl *PD = |
| 1566 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1567 | if (Matches(PD->getDepth())) |
| 1568 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1569 | return super::TraverseTemplateName(N); |
| 1570 | } |
| 1571 | |
| 1572 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1573 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1574 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 1575 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1576 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1577 | return super::VisitDeclRefExpr(E); |
| 1578 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1579 | |
| 1580 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 1581 | return TraverseType(T->getReplacementType()); |
| 1582 | } |
| 1583 | |
| 1584 | bool |
| 1585 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 1586 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 1587 | } |
| 1588 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1589 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1590 | return TraverseType(T->getInjectedSpecializationType()); |
| 1591 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1592 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 1593 | } // end anonymous namespace |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1594 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1595 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1596 | /// list. |
| 1597 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1598 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1599 | DependencyChecker Checker(Params); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1600 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1601 | return Checker.Match; |
| 1602 | } |
| 1603 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1604 | // Find the source range corresponding to the named type in the given |
| 1605 | // nested-name-specifier, if any. |
| 1606 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1607 | QualType T, |
| 1608 | const CXXScopeSpec &SS) { |
| 1609 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1610 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1611 | if (const Type *CurType = NNS->getAsType()) { |
| 1612 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1613 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1614 | } else |
| 1615 | break; |
| 1616 | |
| 1617 | NNSLoc = NNSLoc.getPrefix(); |
| 1618 | } |
| 1619 | |
| 1620 | return SourceRange(); |
| 1621 | } |
| 1622 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1624 | /// specifier, returning the template parameter list that applies to the |
| 1625 | /// name. |
| 1626 | /// |
| 1627 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1628 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1629 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1630 | /// \param DeclLoc The location of the declaration itself. |
| 1631 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1632 | /// \param SS the scope specifier that will be matched to the given template |
| 1633 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1634 | /// being declared. |
| 1635 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1636 | /// \param TemplateId The template-id following the scope specifier, if there |
| 1637 | /// is one. Used to check for a missing 'template<>'. |
| 1638 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1639 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1640 | /// innermost template parameter lists. |
| 1641 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1642 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1643 | /// matching template parameters to scope specifiers in friend |
| 1644 | /// declarations. |
| 1645 | /// |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1646 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1647 | /// declared is an explicit specialization, false otherwise. |
| 1648 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1649 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1650 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1651 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1652 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1653 | /// 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] | 1654 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1655 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 1656 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1657 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1658 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
| 1659 | bool &IsExplicitSpecialization, bool &Invalid) { |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1660 | IsExplicitSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1661 | Invalid = false; |
| 1662 | |
| 1663 | // The sequence of nested types to which we will match up the template |
| 1664 | // parameter lists. We first build this list by starting with the type named |
| 1665 | // 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] | 1666 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1667 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1668 | if (SS.getScopeRep()) { |
| 1669 | if (CXXRecordDecl *Record |
| 1670 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1671 | T = Context.getTypeDeclType(Record); |
| 1672 | else |
| 1673 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1674 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1675 | |
| 1676 | // If we found an explicit specialization that prevents us from needing |
| 1677 | // 'template<>' headers, this will be set to the location of that |
| 1678 | // explicit specialization. |
| 1679 | SourceLocation ExplicitSpecLoc; |
| 1680 | |
| 1681 | while (!T.isNull()) { |
| 1682 | NestedTypes.push_back(T); |
| 1683 | |
| 1684 | // Retrieve the parent of a record type. |
| 1685 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1686 | // If this type is an explicit specialization, we're done. |
| 1687 | if (ClassTemplateSpecializationDecl *Spec |
| 1688 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1689 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1690 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1691 | ExplicitSpecLoc = Spec->getLocation(); |
| 1692 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1693 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1694 | } else if (Record->getTemplateSpecializationKind() |
| 1695 | == TSK_ExplicitSpecialization) { |
| 1696 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1697 | break; |
| 1698 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1699 | |
| 1700 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1701 | T = Context.getTypeDeclType(Parent); |
| 1702 | else |
| 1703 | T = QualType(); |
| 1704 | continue; |
| 1705 | } |
| 1706 | |
| 1707 | if (const TemplateSpecializationType *TST |
| 1708 | = T->getAs<TemplateSpecializationType>()) { |
| 1709 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1710 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1711 | T = Context.getTypeDeclType(Parent); |
| 1712 | else |
| 1713 | T = QualType(); |
| 1714 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1715 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | // Look one step prior in a dependent template specialization type. |
| 1719 | if (const DependentTemplateSpecializationType *DependentTST |
| 1720 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1721 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1722 | T = QualType(NNS->getAsType(), 0); |
| 1723 | else |
| 1724 | T = QualType(); |
| 1725 | continue; |
| 1726 | } |
| 1727 | |
| 1728 | // Look one step prior in a dependent name type. |
| 1729 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1730 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1731 | T = QualType(NNS->getAsType(), 0); |
| 1732 | else |
| 1733 | T = QualType(); |
| 1734 | continue; |
| 1735 | } |
| 1736 | |
| 1737 | // Retrieve the parent of an enumeration type. |
| 1738 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1739 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1740 | // check here. |
| 1741 | EnumDecl *Enum = EnumT->getDecl(); |
| 1742 | |
| 1743 | // Get to the parent type. |
| 1744 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1745 | T = Context.getTypeDeclType(Parent); |
| 1746 | else |
| 1747 | T = QualType(); |
| 1748 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1749 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1751 | T = QualType(); |
| 1752 | } |
| 1753 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1754 | // to the innermost while checking template-parameter-lists. |
| 1755 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1756 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1757 | // C++0x [temp.expl.spec]p17: |
| 1758 | // A member or a member template may be nested within many |
| 1759 | // enclosing class templates. In an explicit specialization for |
| 1760 | // such a member, the member declaration shall be preceded by a |
| 1761 | // template<> for each enclosing class template that is |
| 1762 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1763 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1764 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1765 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1766 | if (SawNonEmptyTemplateParameterList) { |
| 1767 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1768 | << !Recovery << Range; |
| 1769 | Invalid = true; |
| 1770 | IsExplicitSpecialization = false; |
| 1771 | return true; |
| 1772 | } |
| 1773 | |
| 1774 | return false; |
| 1775 | }; |
| 1776 | |
| 1777 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 1778 | // Check that we can have an explicit specialization here. |
| 1779 | if (CheckExplicitSpecialization(Range, true)) |
| 1780 | return true; |
| 1781 | |
| 1782 | // We don't have a template header, but we should. |
| 1783 | SourceLocation ExpectedTemplateLoc; |
| 1784 | if (!ParamLists.empty()) |
| 1785 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1786 | else |
| 1787 | ExpectedTemplateLoc = DeclStartLoc; |
| 1788 | |
| 1789 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1790 | << Range |
| 1791 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1792 | return false; |
| 1793 | }; |
| 1794 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1795 | unsigned ParamIdx = 0; |
| 1796 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1797 | ++TypeIdx) { |
| 1798 | T = NestedTypes[TypeIdx]; |
| 1799 | |
| 1800 | // Whether we expect a 'template<>' header. |
| 1801 | bool NeedEmptyTemplateHeader = false; |
| 1802 | |
| 1803 | // Whether we expect a template header with parameters. |
| 1804 | bool NeedNonemptyTemplateHeader = false; |
| 1805 | |
| 1806 | // For a dependent type, the set of template parameters that we |
| 1807 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1808 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1809 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1810 | // C++0x [temp.expl.spec]p15: |
| 1811 | // A member or a member template may be nested within many enclosing |
| 1812 | // class templates. In an explicit specialization for such a member, the |
| 1813 | // member declaration shall be preceded by a template<> for each |
| 1814 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1815 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1816 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1817 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1818 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1819 | NeedNonemptyTemplateHeader = true; |
| 1820 | } else if (Record->isDependentType()) { |
| 1821 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1822 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1823 | ->getTemplateParameters(); |
| 1824 | NeedNonemptyTemplateHeader = true; |
| 1825 | } |
| 1826 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1827 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1828 | // C++0x [temp.expl.spec]p4: |
| 1829 | // Members of an explicitly specialized class template are defined |
| 1830 | // in the same manner as members of normal classes, and not using |
| 1831 | // the template<> syntax. |
| 1832 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1833 | NeedEmptyTemplateHeader = true; |
| 1834 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1835 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1836 | } else if (Record->getTemplateSpecializationKind()) { |
| 1837 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1838 | != TSK_ExplicitSpecialization && |
| 1839 | TypeIdx == NumTypes - 1) |
| 1840 | IsExplicitSpecialization = true; |
| 1841 | |
| 1842 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1843 | } |
| 1844 | } else if (const TemplateSpecializationType *TST |
| 1845 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 1846 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1847 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1848 | NeedNonemptyTemplateHeader = true; |
| 1849 | } |
| 1850 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1851 | // FIXME: We actually could/should check the template arguments here |
| 1852 | // against the corresponding template parameter list. |
| 1853 | NeedNonemptyTemplateHeader = false; |
| 1854 | } |
| 1855 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1856 | // C++ [temp.expl.spec]p16: |
| 1857 | // In an explicit specialization declaration for a member of a class |
| 1858 | // template or a member template that ap- pears in namespace scope, the |
| 1859 | // member template and some of its enclosing class templates may remain |
| 1860 | // unspecialized, except that the declaration shall not explicitly |
| 1861 | // specialize a class member template if its en- closing class templates |
| 1862 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1863 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1864 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1865 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 1866 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1867 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1868 | } else |
| 1869 | SawNonEmptyTemplateParameterList = true; |
| 1870 | } |
| 1871 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1872 | if (NeedEmptyTemplateHeader) { |
| 1873 | // If we're on the last of the types, and we need a 'template<>' header |
| 1874 | // here, then it's an explicit specialization. |
| 1875 | if (TypeIdx == NumTypes - 1) |
| 1876 | IsExplicitSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1877 | |
| 1878 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1879 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1880 | // The header has template parameters when it shouldn't. Complain. |
| 1881 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1882 | diag::err_template_param_list_matches_nontemplate) |
| 1883 | << T |
| 1884 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1885 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1886 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1887 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1888 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1889 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1890 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1891 | // Consume this template header. |
| 1892 | ++ParamIdx; |
| 1893 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1894 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1895 | |
| 1896 | if (!IsFriend) |
| 1897 | if (DiagnoseMissingExplicitSpecialization( |
| 1898 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1899 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1900 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1901 | continue; |
| 1902 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1903 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1904 | if (NeedNonemptyTemplateHeader) { |
| 1905 | // In friend declarations we can have template-ids which don't |
| 1906 | // depend on the corresponding template parameter lists. But |
| 1907 | // assume that empty parameter lists are supposed to match this |
| 1908 | // template-id. |
| 1909 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1910 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1911 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1912 | ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1913 | else |
| 1914 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1915 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1916 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1917 | if (ParamIdx < ParamLists.size()) { |
| 1918 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1919 | if (ExpectedTemplateParams && |
| 1920 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1921 | ExpectedTemplateParams, |
| 1922 | true, TPL_TemplateMatch)) |
| 1923 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1924 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1925 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1926 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1927 | TPC_ClassTemplateMember)) |
| 1928 | Invalid = true; |
| 1929 | |
| 1930 | ++ParamIdx; |
| 1931 | continue; |
| 1932 | } |
| 1933 | |
| 1934 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1935 | << T |
| 1936 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1937 | Invalid = true; |
| 1938 | continue; |
| 1939 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1940 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1941 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1942 | // If there were at least as many template-ids as there were template |
| 1943 | // parameter lists, then there are no template parameter lists remaining for |
| 1944 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1945 | if (ParamIdx >= ParamLists.size()) { |
| 1946 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1947 | // We don't have a template header for the declaration itself, but we |
| 1948 | // should. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1949 | IsExplicitSpecialization = true; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1950 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 1951 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1952 | |
| 1953 | // Fabricate an empty template parameter list for the invented header. |
| 1954 | return TemplateParameterList::Create(Context, SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 1955 | SourceLocation(), None, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1956 | SourceLocation()); |
| 1957 | } |
| 1958 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1959 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1960 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1961 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1962 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1963 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1964 | bool HasAnyExplicitSpecHeader = false; |
| 1965 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1966 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1967 | if (ParamLists[I]->size() == 0) |
| 1968 | HasAnyExplicitSpecHeader = true; |
| 1969 | else |
| 1970 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1971 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1972 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1973 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1974 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 1975 | : diag::err_template_spec_extra_headers) |
| 1976 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1977 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1978 | |
| 1979 | // If there was a specialization somewhere, such that 'template<>' is |
| 1980 | // not required, and there were any 'template<>' headers, note where the |
| 1981 | // specialization occurred. |
| 1982 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1983 | Diag(ExplicitSpecLoc, |
| 1984 | diag::note_explicit_template_spec_does_not_need_header) |
| 1985 | << NestedTypes.back(); |
| 1986 | |
| 1987 | // We have a template parameter list with no corresponding scope, which |
| 1988 | // means that the resulting template declaration can't be instantiated |
| 1989 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1990 | if (!AllExplicitSpecHeaders) |
| 1991 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1992 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1994 | // C++ [temp.expl.spec]p16: |
| 1995 | // In an explicit specialization declaration for a member of a class |
| 1996 | // template or a member template that ap- pears in namespace scope, the |
| 1997 | // member template and some of its enclosing class templates may remain |
| 1998 | // unspecialized, except that the declaration shall not explicitly |
| 1999 | // specialize a class member template if its en- closing class templates |
| 2000 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2001 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 2002 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2003 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2004 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2005 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2006 | // Return the last template parameter list, which corresponds to the |
| 2007 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2008 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2011 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 2012 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2013 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2014 | << (isa<FunctionTemplateDecl>(Template) |
| 2015 | ? 0 |
| 2016 | : isa<ClassTemplateDecl>(Template) |
| 2017 | ? 1 |
| 2018 | : isa<VarTemplateDecl>(Template) |
| 2019 | ? 2 |
| 2020 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 2021 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2022 | return; |
| 2023 | } |
| 2024 | |
| 2025 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 2026 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 2027 | IEnd = OST->end(); |
| 2028 | I != IEnd; ++I) |
| 2029 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 2030 | << 0 << (*I)->getDeclName(); |
| 2031 | |
| 2032 | return; |
| 2033 | } |
| 2034 | } |
| 2035 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2036 | static QualType |
| 2037 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
| 2038 | const SmallVectorImpl<TemplateArgument> &Converted, |
| 2039 | SourceLocation TemplateLoc, |
| 2040 | TemplateArgumentListInfo &TemplateArgs) { |
| 2041 | ASTContext &Context = SemaRef.getASTContext(); |
| 2042 | switch (BTD->getBuiltinTemplateKind()) { |
| 2043 | case BTK__make_integer_seq: |
| 2044 | // Specializations of __make_integer_seq<S, T, N> are treated like |
| 2045 | // S<T, 0, ..., N-1>. |
| 2046 | |
| 2047 | // C++14 [inteseq.intseq]p1: |
| 2048 | // T shall be an integer type. |
| 2049 | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
| 2050 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
| 2051 | diag::err_integer_sequence_integral_element_type); |
| 2052 | return QualType(); |
| 2053 | } |
| 2054 | |
| 2055 | // C++14 [inteseq.make]p1: |
| 2056 | // If N is negative the program is ill-formed. |
| 2057 | TemplateArgument NumArgsArg = Converted[2]; |
| 2058 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
| 2059 | if (NumArgs < 0) { |
| 2060 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
| 2061 | diag::err_integer_sequence_negative_length); |
| 2062 | return QualType(); |
| 2063 | } |
| 2064 | |
| 2065 | QualType ArgTy = NumArgsArg.getIntegralType(); |
| 2066 | TemplateArgumentListInfo SyntheticTemplateArgs; |
| 2067 | // The type argument gets reused as the first template argument in the |
| 2068 | // synthetic template argument list. |
| 2069 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
| 2070 | // Expand N into 0 ... N-1. |
| 2071 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
| 2072 | I < NumArgs; ++I) { |
| 2073 | TemplateArgument TA(Context, I, ArgTy); |
| 2074 | Expr *E = SemaRef.BuildExpressionFromIntegralTemplateArgument( |
| 2075 | TA, TemplateArgs[2].getLocation()) |
| 2076 | .getAs<Expr>(); |
| 2077 | SyntheticTemplateArgs.addArgument( |
| 2078 | TemplateArgumentLoc(TemplateArgument(E), E)); |
| 2079 | } |
| 2080 | // The first template argument will be reused as the template decl that |
| 2081 | // our synthetic template arguments will be applied to. |
| 2082 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
| 2083 | TemplateLoc, SyntheticTemplateArgs); |
| 2084 | } |
| 2085 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
| 2086 | } |
| 2087 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2088 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 2089 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2090 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 2091 | DependentTemplateName *DTN |
| 2092 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2093 | if (DTN && DTN->isIdentifier()) |
| 2094 | // When building a template-id where the template-name is dependent, |
| 2095 | // assume the template is a type template. Either our assumption is |
| 2096 | // correct, or the code is ill-formed and will be diagnosed when the |
| 2097 | // dependent name is substituted. |
| 2098 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 2099 | DTN->getQualifier(), |
| 2100 | DTN->getIdentifier(), |
| 2101 | TemplateArgs); |
| 2102 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2103 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 2104 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
| 2105 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2106 | // We might have a substituted template template parameter pack. If so, |
| 2107 | // build a template specialization type for it. |
| 2108 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 2109 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2110 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2111 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 2112 | << Name; |
| 2113 | NoteAllFoundTemplates(Name); |
| 2114 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2115 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2116 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2117 | // Check that the template argument list is well-formed for this |
| 2118 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2119 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2120 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2121 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2122 | return QualType(); |
| 2123 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2124 | QualType CanonType; |
| 2125 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2126 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2127 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 2128 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2129 | // Find the canonical type for this type alias template specialization. |
| 2130 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 2131 | if (Pattern->isInvalidDecl()) |
| 2132 | return QualType(); |
| 2133 | |
| 2134 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2135 | Converted.data(), Converted.size()); |
| 2136 | |
| 2137 | // Only substitute for the innermost template argument list. |
| 2138 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 0c4a34b | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 2139 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 2140 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 2141 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 2142 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2143 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2144 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2145 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2146 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2147 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2148 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2149 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 2150 | TemplateArgLists, AliasTemplate->getLocation(), |
| 2151 | AliasTemplate->getDeclName()); |
| 2152 | if (CanonType.isNull()) |
| 2153 | return QualType(); |
| 2154 | } else if (Name.isDependent() || |
| 2155 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2156 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2157 | // This class template specialization is a dependent |
| 2158 | // type. Therefore, its canonical type is another class template |
| 2159 | // specialization type that contains all of the converted |
| 2160 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 2161 | // A<T, T> have identical types when A is declared as: |
| 2162 | // |
| 2163 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2164 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2165 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2166 | Converted.data(), |
| 2167 | Converted.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2169 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2170 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2171 | // In the future, we need to teach getTemplateSpecializationType to only |
| 2172 | // build the canonical type and return that to us. |
| 2173 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2174 | |
| 2175 | // This might work out to be a current instantiation, in which |
| 2176 | // case the canonical type needs to be the InjectedClassNameType. |
| 2177 | // |
| 2178 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2179 | // changes to CurContext don't change the set of current |
| 2180 | // instantiations. |
| 2181 | if (isa<ClassTemplateDecl>(Template)) { |
| 2182 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2183 | // If we get out to a namespace, we're done. |
| 2184 | if (Ctx->isFileContext()) break; |
| 2185 | |
| 2186 | // If this isn't a record, keep looking. |
| 2187 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2188 | if (!Record) continue; |
| 2189 | |
| 2190 | // Look for one of the two cases with InjectedClassNameTypes |
| 2191 | // and check whether it's the same template. |
| 2192 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2193 | !Record->getDescribedClassTemplate()) |
| 2194 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2195 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2196 | // Fetch the injected class name type and check whether its |
| 2197 | // injected type is equal to the type we just built. |
| 2198 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2199 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2200 | ->getInjectedSpecializationType(); |
| 2201 | |
| 2202 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2203 | continue; |
| 2204 | |
| 2205 | // If so, the canonical type of this TST is the injected |
| 2206 | // class name type of the record we just found. |
| 2207 | assert(ICNT.isCanonical()); |
| 2208 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2209 | break; |
| 2210 | } |
| 2211 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2212 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2213 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2214 | // Find the class template specialization declaration that |
| 2215 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2216 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2217 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2218 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2219 | if (!Decl) { |
| 2220 | // This is the first time we have referenced this class template |
| 2221 | // specialization. Create the canonical declaration and add it to |
| 2222 | // the set of specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2223 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2224 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2225 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2226 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2227 | ClassTemplate->getLocation(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2228 | ClassTemplate, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2229 | Converted.data(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2230 | Converted.size(), nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2231 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 2232 | if (ClassTemplate->isOutOfLine()) |
| 2233 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 2236 | // Diagnose uses of this specialization. |
| 2237 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 2238 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2239 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2240 | assert(isa<RecordType>(CanonType) && |
| 2241 | "type of non-dependent specialization is not a RecordType"); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2242 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { |
| 2243 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
| 2244 | TemplateArgs); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2245 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2246 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2247 | // Build the fully-sugared type for this class template |
| 2248 | // specialization, which refers back to the class template |
| 2249 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2250 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2253 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2254 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2255 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2256 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2257 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2258 | SourceLocation RAngleLoc, |
| 2259 | bool IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2260 | if (SS.isInvalid()) |
| 2261 | return true; |
| 2262 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2263 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2264 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2265 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2266 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2267 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2268 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2269 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2270 | QualType T |
| 2271 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2272 | DTN->getQualifier(), |
| 2273 | DTN->getIdentifier(), |
| 2274 | TemplateArgs); |
| 2275 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2276 | TypeLocBuilder TLB; |
| 2277 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2278 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2279 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2280 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2281 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2282 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2283 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2284 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2285 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2286 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2287 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2288 | } |
| 2289 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2290 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2291 | |
| 2292 | if (Result.isNull()) |
| 2293 | return true; |
| 2294 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2295 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2296 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2297 | TemplateSpecializationTypeLoc SpecTL |
| 2298 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2299 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2300 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2301 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2302 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2303 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2304 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2305 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2306 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2307 | // constructor or destructor name (in such a case, the scope specifier |
| 2308 | // will be attached to the enclosing Decl or Expr node). |
| 2309 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2310 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2311 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2312 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2313 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2314 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2315 | } |
| 2316 | |
| 2317 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2318 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2319 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2320 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2321 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2322 | SourceLocation TagLoc, |
| 2323 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2324 | SourceLocation TemplateKWLoc, |
| 2325 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2326 | SourceLocation TemplateLoc, |
| 2327 | SourceLocation LAngleLoc, |
| 2328 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2329 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2330 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2331 | |
| 2332 | // Translate the parser's template argument list in our AST format. |
| 2333 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2334 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2335 | |
| 2336 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2337 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2338 | ElaboratedTypeKeyword Keyword |
| 2339 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2340 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2341 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2342 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2343 | DTN->getQualifier(), |
| 2344 | DTN->getIdentifier(), |
| 2345 | TemplateArgs); |
| 2346 | |
| 2347 | // Build type-source information. |
| 2348 | TypeLocBuilder TLB; |
| 2349 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2350 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2351 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2352 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2353 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2354 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2355 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2356 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2357 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2358 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2359 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2360 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2361 | |
| 2362 | if (TypeAliasTemplateDecl *TAT = |
| 2363 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2364 | // C++0x [dcl.type.elab]p2: |
| 2365 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2366 | // resolves to an alias template specialization, the |
| 2367 | // elaborated-type-specifier is ill-formed. |
| 2368 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2369 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2370 | } |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2371 | |
| 2372 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2373 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2374 | return TypeResult(true); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2375 | |
| 2376 | // Check the tag kind |
| 2377 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2378 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2379 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2380 | IdentifierInfo *Id = D->getIdentifier(); |
| 2381 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2382 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2383 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 2384 | TagLoc, Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2385 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2386 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2387 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2388 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2389 | } |
| 2390 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2391 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2392 | // Provide source-location information for the template specialization. |
| 2393 | TypeLocBuilder TLB; |
| 2394 | TemplateSpecializationTypeLoc SpecTL |
| 2395 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2396 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2397 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2398 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2399 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2400 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2401 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2402 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2403 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2404 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2405 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2406 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2407 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2408 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2409 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2412 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2413 | Sema &S, SourceLocation NameLoc, TemplateParameterList *TemplateParams, |
| 2414 | unsigned ExplicitArgs, SmallVectorImpl<TemplateArgument> &TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2415 | |
| 2416 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 2417 | NamedDecl *PrevDecl, |
| 2418 | SourceLocation Loc, |
| 2419 | bool IsPartialSpecialization); |
| 2420 | |
| 2421 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2422 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2423 | static bool isTemplateArgumentTemplateParameter( |
| 2424 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 2425 | switch (Arg.getKind()) { |
| 2426 | case TemplateArgument::Null: |
| 2427 | case TemplateArgument::NullPtr: |
| 2428 | case TemplateArgument::Integral: |
| 2429 | case TemplateArgument::Declaration: |
| 2430 | case TemplateArgument::Pack: |
| 2431 | case TemplateArgument::TemplateExpansion: |
| 2432 | return false; |
| 2433 | |
| 2434 | case TemplateArgument::Type: { |
| 2435 | QualType Type = Arg.getAsType(); |
| 2436 | const TemplateTypeParmType *TPT = |
| 2437 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 2438 | return TPT && !Type.hasQualifiers() && |
| 2439 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 2440 | } |
| 2441 | |
| 2442 | case TemplateArgument::Expression: { |
| 2443 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 2444 | if (!DRE || !DRE->getDecl()) |
| 2445 | return false; |
| 2446 | const NonTypeTemplateParmDecl *NTTP = |
| 2447 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 2448 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 2449 | } |
| 2450 | |
| 2451 | case TemplateArgument::Template: |
| 2452 | const TemplateTemplateParmDecl *TTP = |
| 2453 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 2454 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 2455 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 2456 | } |
| 2457 | llvm_unreachable("unexpected kind of template argument"); |
| 2458 | } |
| 2459 | |
| 2460 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 2461 | ArrayRef<TemplateArgument> Args) { |
| 2462 | if (Params->size() != Args.size()) |
| 2463 | return false; |
| 2464 | |
| 2465 | unsigned Depth = Params->getDepth(); |
| 2466 | |
| 2467 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 2468 | TemplateArgument Arg = Args[I]; |
| 2469 | |
| 2470 | // If the parameter is a pack expansion, the argument must be a pack |
| 2471 | // whose only element is a pack expansion. |
| 2472 | if (Params->getParam(I)->isParameterPack()) { |
| 2473 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 2474 | !Arg.pack_begin()->isPackExpansion()) |
| 2475 | return false; |
| 2476 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 2477 | } |
| 2478 | |
| 2479 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 2480 | return false; |
| 2481 | } |
| 2482 | |
| 2483 | return true; |
| 2484 | } |
| 2485 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2486 | /// Convert the parser's template argument list representation into our form. |
| 2487 | static TemplateArgumentListInfo |
| 2488 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 2489 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 2490 | TemplateId.RAngleLoc); |
| 2491 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 2492 | TemplateId.NumArgs); |
| 2493 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 2494 | return TemplateArgs; |
| 2495 | } |
| 2496 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2497 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2498 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 2499 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2500 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2501 | // D must be variable template id. |
| 2502 | assert(D.getName().getKind() == UnqualifiedId::IK_TemplateId && |
| 2503 | "Variable template specialization is declared with a template it."); |
| 2504 | |
| 2505 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2506 | TemplateArgumentListInfo TemplateArgs = |
| 2507 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2508 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 2509 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 2510 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2511 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2512 | TemplateName Name = TemplateId->Template.get(); |
| 2513 | |
| 2514 | // The template-id must name a variable template. |
| 2515 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2516 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 2517 | if (!VarTemplate) { |
| 2518 | NamedDecl *FnTemplate; |
| 2519 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 2520 | FnTemplate = *OTS->begin(); |
| 2521 | else |
| 2522 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 2523 | if (FnTemplate) |
| 2524 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 2525 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2526 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 2527 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2528 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2529 | |
| 2530 | // Check for unexpanded parameter packs in any of the template arguments. |
| 2531 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 2532 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 2533 | UPPC_PartialSpecialization)) |
| 2534 | return true; |
| 2535 | |
| 2536 | // Check that the template argument list is well-formed for this |
| 2537 | // template. |
| 2538 | SmallVector<TemplateArgument, 4> Converted; |
| 2539 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 2540 | false, Converted)) |
| 2541 | return true; |
| 2542 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2543 | // Find the variable template (partial) specialization declaration that |
| 2544 | // corresponds to these arguments. |
| 2545 | if (IsPartialSpecialization) { |
| 2546 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2547 | *this, TemplateNameLoc, VarTemplate->getTemplateParameters(), |
| 2548 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2549 | return true; |
| 2550 | |
| 2551 | bool InstantiationDependent; |
| 2552 | if (!Name.isDependent() && |
| 2553 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2554 | TemplateArgs.getArgumentArray(), TemplateArgs.size(), |
| 2555 | InstantiationDependent)) { |
| 2556 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 2557 | << VarTemplate->getDeclName(); |
| 2558 | IsPartialSpecialization = false; |
| 2559 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2560 | |
| 2561 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 2562 | Converted)) { |
| 2563 | // C++ [temp.class.spec]p9b3: |
| 2564 | // |
| 2565 | // -- The argument list of the specialization shall not be identical |
| 2566 | // to the implicit argument list of the primary template. |
| 2567 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 2568 | << /*variable template*/ 1 |
| 2569 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 2570 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 2571 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 2572 | // of the primary template. |
| 2573 | return true; |
| 2574 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2577 | void *InsertPos = nullptr; |
| 2578 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2579 | |
| 2580 | if (IsPartialSpecialization) |
| 2581 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2582 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2583 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2584 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2585 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2586 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2587 | |
| 2588 | // Check whether we can declare a variable template specialization in |
| 2589 | // the current scope. |
| 2590 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 2591 | TemplateNameLoc, |
| 2592 | IsPartialSpecialization)) |
| 2593 | return true; |
| 2594 | |
| 2595 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 2596 | // Since the only prior variable template specialization with these |
| 2597 | // arguments was referenced but not declared, reuse that |
| 2598 | // declaration node as our own, updating its source location and |
| 2599 | // the list of outer template parameters to reflect our new declaration. |
| 2600 | Specialization = PrevDecl; |
| 2601 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2602 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2603 | } else if (IsPartialSpecialization) { |
| 2604 | // Create a new class template partial specialization declaration node. |
| 2605 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 2606 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2607 | VarTemplatePartialSpecializationDecl *Partial = |
| 2608 | VarTemplatePartialSpecializationDecl::Create( |
| 2609 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 2610 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 2611 | Converted.data(), Converted.size(), TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2612 | |
| 2613 | if (!PrevPartial) |
| 2614 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 2615 | Specialization = Partial; |
| 2616 | |
| 2617 | // If we are providing an explicit specialization of a member variable |
| 2618 | // template specialization, make a note of that. |
| 2619 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2620 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2621 | |
| 2622 | // Check that all of the template parameters of the variable template |
| 2623 | // partial specialization are deducible from the template |
| 2624 | // arguments. If not, this variable template partial specialization |
| 2625 | // will never be used. |
| 2626 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 2627 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 2628 | TemplateParams->getDepth(), DeducibleParams); |
| 2629 | |
| 2630 | if (!DeducibleParams.all()) { |
| 2631 | unsigned NumNonDeducible = |
| 2632 | DeducibleParams.size() - DeducibleParams.count(); |
| 2633 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2634 | << /*variable template*/ 1 << (NumNonDeducible > 1) |
| 2635 | << SourceRange(TemplateNameLoc, RAngleLoc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2636 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 2637 | if (!DeducibleParams[I]) { |
| 2638 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 2639 | if (Param->getDeclName()) |
| 2640 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
| 2641 | << Param->getDeclName(); |
| 2642 | else |
| 2643 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 2644 | << "(anonymous)"; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2645 | } |
| 2646 | } |
| 2647 | } |
| 2648 | } else { |
| 2649 | // Create a new class template specialization declaration node for |
| 2650 | // this explicit specialization or friend declaration. |
| 2651 | Specialization = VarTemplateSpecializationDecl::Create( |
| 2652 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
| 2653 | VarTemplate, DI->getType(), DI, SC, Converted.data(), Converted.size()); |
| 2654 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 2655 | |
| 2656 | if (!PrevDecl) |
| 2657 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 2658 | } |
| 2659 | |
| 2660 | // C++ [temp.expl.spec]p6: |
| 2661 | // If a template, a member template or the member of a class template is |
| 2662 | // explicitly specialized then that specialization shall be declared |
| 2663 | // before the first use of that specialization that would cause an implicit |
| 2664 | // instantiation to take place, in every translation unit in which such a |
| 2665 | // use occurs; no diagnostic is required. |
| 2666 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 2667 | bool Okay = false; |
| 2668 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 2669 | // Is there any previous explicit specialization declaration? |
| 2670 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 2671 | Okay = true; |
| 2672 | break; |
| 2673 | } |
| 2674 | } |
| 2675 | |
| 2676 | if (!Okay) { |
| 2677 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 2678 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 2679 | << Name << Range; |
| 2680 | |
| 2681 | Diag(PrevDecl->getPointOfInstantiation(), |
| 2682 | diag::note_instantiation_required_here) |
| 2683 | << (PrevDecl->getTemplateSpecializationKind() != |
| 2684 | TSK_ImplicitInstantiation); |
| 2685 | return true; |
| 2686 | } |
| 2687 | } |
| 2688 | |
| 2689 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 2690 | Specialization->setLexicalDeclContext(CurContext); |
| 2691 | |
| 2692 | // Add the specialization into its lexical context, so that it can |
| 2693 | // be seen when iterating through the list of declarations in that |
| 2694 | // context. However, specializations are not found by name lookup. |
| 2695 | CurContext->addDecl(Specialization); |
| 2696 | |
| 2697 | // Note that this is an explicit specialization. |
| 2698 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 2699 | |
| 2700 | if (PrevDecl) { |
| 2701 | // Check that this isn't a redefinition of this specialization, |
| 2702 | // merging with previous declarations. |
| 2703 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
| 2704 | ForRedeclaration); |
| 2705 | PrevSpec.addDecl(PrevDecl); |
| 2706 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2707 | } else if (Specialization->isStaticDataMember() && |
| 2708 | Specialization->isOutOfLine()) { |
| 2709 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | // Link instantiations of static data members back to the template from |
| 2713 | // which they were instantiated. |
| 2714 | if (Specialization->isStaticDataMember()) |
| 2715 | Specialization->setInstantiationOfStaticDataMember( |
| 2716 | VarTemplate->getTemplatedDecl(), |
| 2717 | Specialization->getSpecializationKind()); |
| 2718 | |
| 2719 | return Specialization; |
| 2720 | } |
| 2721 | |
| 2722 | namespace { |
| 2723 | /// \brief A partial specialization whose template arguments have matched |
| 2724 | /// a given template-id. |
| 2725 | struct PartialSpecMatchResult { |
| 2726 | VarTemplatePartialSpecializationDecl *Partial; |
| 2727 | TemplateArgumentList *Args; |
| 2728 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2729 | } // end anonymous namespace |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2730 | |
| 2731 | DeclResult |
| 2732 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2733 | SourceLocation TemplateNameLoc, |
| 2734 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2735 | assert(Template && "A variable template id without template?"); |
| 2736 | |
| 2737 | // Check that the template argument list is well-formed for this template. |
| 2738 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2739 | if (CheckTemplateArgumentList( |
| 2740 | Template, TemplateNameLoc, |
| 2741 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2742 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2743 | return true; |
| 2744 | |
| 2745 | // Find the variable template specialization declaration that |
| 2746 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2747 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2748 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2749 | Converted, InsertPos)) { |
| 2750 | checkSpecializationVisibility(TemplateNameLoc, Spec); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2751 | // If we already have a variable template specialization, return it. |
| 2752 | return Spec; |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2753 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2754 | |
| 2755 | // This is the first time we have referenced this variable template |
| 2756 | // specialization. Create the canonical declaration and add it to |
| 2757 | // the set of specializations, based on the closest partial specialization |
| 2758 | // that it represents. That is, |
| 2759 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 2760 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
| 2761 | Converted.data(), Converted.size()); |
| 2762 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 2763 | bool AmbiguousPartialSpec = false; |
| 2764 | typedef PartialSpecMatchResult MatchResult; |
| 2765 | SmallVector<MatchResult, 4> Matched; |
| 2766 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 2767 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
| 2768 | /*ForTakingAddress=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2769 | |
| 2770 | // 1. Attempt to find the closest partial specialization that this |
| 2771 | // specializes, if any. |
| 2772 | // If any of the template arguments is dependent, then this is probably |
| 2773 | // a placeholder for an incomplete declarative context; which must be |
| 2774 | // complete by instantiation time. Thus, do not search through the partial |
| 2775 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2776 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 2777 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 2778 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2779 | bool InstantiationDependent = false; |
| 2780 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 2781 | TemplateArgs, InstantiationDependent)) { |
| 2782 | |
| 2783 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 2784 | Template->getPartialSpecializations(PartialSpecs); |
| 2785 | |
| 2786 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 2787 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 2788 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 2789 | |
| 2790 | if (TemplateDeductionResult Result = |
| 2791 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 2792 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2793 | // TODO: Actually use the failed-deduction info? |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 2794 | FailedCandidates.addCandidate().set( |
| 2795 | DeclAccessPair::make(Template, AS_public), Partial, |
| 2796 | MakeDeductionFailureInfo(Context, Result, Info)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2797 | (void)Result; |
| 2798 | } else { |
| 2799 | Matched.push_back(PartialSpecMatchResult()); |
| 2800 | Matched.back().Partial = Partial; |
| 2801 | Matched.back().Args = Info.take(); |
| 2802 | } |
| 2803 | } |
| 2804 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2805 | if (Matched.size() >= 1) { |
| 2806 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 2807 | if (Matched.size() == 1) { |
| 2808 | // -- If exactly one matching specialization is found, the |
| 2809 | // instantiation is generated from that specialization. |
| 2810 | // We don't need to do anything for this. |
| 2811 | } else { |
| 2812 | // -- If more than one matching specialization is found, the |
| 2813 | // partial order rules (14.5.4.2) are used to determine |
| 2814 | // whether one of the specializations is more specialized |
| 2815 | // than the others. If none of the specializations is more |
| 2816 | // specialized than all of the other matching |
| 2817 | // specializations, then the use of the variable template is |
| 2818 | // ambiguous and the program is ill-formed. |
| 2819 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 2820 | PEnd = Matched.end(); |
| 2821 | P != PEnd; ++P) { |
| 2822 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 2823 | PointOfInstantiation) == |
| 2824 | P->Partial) |
| 2825 | Best = P; |
| 2826 | } |
| 2827 | |
| 2828 | // Determine if the best partial specialization is more specialized than |
| 2829 | // the others. |
| 2830 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2831 | PEnd = Matched.end(); |
| 2832 | P != PEnd; ++P) { |
| 2833 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 2834 | P->Partial, Best->Partial, |
| 2835 | PointOfInstantiation) != Best->Partial) { |
| 2836 | AmbiguousPartialSpec = true; |
| 2837 | break; |
| 2838 | } |
| 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | // Instantiate using the best variable template partial specialization. |
| 2843 | InstantiationPattern = Best->Partial; |
| 2844 | InstantiationArgs = Best->Args; |
| 2845 | } else { |
| 2846 | // -- If no match is found, the instantiation is generated |
| 2847 | // from the primary template. |
| 2848 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 2849 | } |
| 2850 | } |
| 2851 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2852 | // 2. Create the canonical declaration. |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2853 | // Note that we do not instantiate a definition until we see an odr-use |
| 2854 | // in DoMarkVarDeclReferenced(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2855 | // FIXME: LateAttrs et al.? |
| 2856 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 2857 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 2858 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 2859 | if (!Decl) |
| 2860 | return true; |
| 2861 | |
| 2862 | if (AmbiguousPartialSpec) { |
| 2863 | // Partial ordering did not produce a clear winner. Complain. |
| 2864 | Decl->setInvalidDecl(); |
| 2865 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 2866 | << Decl; |
| 2867 | |
| 2868 | // Print the matching partial specializations. |
| 2869 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2870 | PEnd = Matched.end(); |
| 2871 | P != PEnd; ++P) |
| 2872 | Diag(P->Partial->getLocation(), diag::note_partial_spec_match) |
| 2873 | << getTemplateArgumentBindingsText( |
| 2874 | P->Partial->getTemplateParameters(), *P->Args); |
| 2875 | return true; |
| 2876 | } |
| 2877 | |
| 2878 | if (VarTemplatePartialSpecializationDecl *D = |
| 2879 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 2880 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 2881 | |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 2882 | checkSpecializationVisibility(TemplateNameLoc, Decl); |
| 2883 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2884 | assert(Decl && "No variable template specialization?"); |
| 2885 | return Decl; |
| 2886 | } |
| 2887 | |
| 2888 | ExprResult |
| 2889 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 2890 | const DeclarationNameInfo &NameInfo, |
| 2891 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2892 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2893 | |
| 2894 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 2895 | *TemplateArgs); |
| 2896 | if (Decl.isInvalid()) |
| 2897 | return ExprError(); |
| 2898 | |
| 2899 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 2900 | if (!Var->getTemplateSpecializationKind()) |
| 2901 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 2902 | NameInfo.getLoc()); |
| 2903 | |
| 2904 | // Build an ordinary singleton decl ref. |
| 2905 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2906 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2909 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2910 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2911 | LookupResult &R, |
| 2912 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2913 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2914 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2915 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2916 | // 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] | 2917 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2918 | // foo<int> could identify a single function unambiguously |
| 2919 | // This approach does NOT work, since f<int>(1); |
| 2920 | // gets resolved prior to resorting to overload resolution |
| 2921 | // i.e., template<class T> void f(double); |
| 2922 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2923 | |
| 2924 | // These should be filtered out by our callers. |
| 2925 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2926 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2927 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2928 | // In C++1y, check variable template ids. |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 2929 | bool InstantiationDependent; |
| 2930 | if (R.getAsSingle<VarTemplateDecl>() && |
| 2931 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2932 | *TemplateArgs, InstantiationDependent)) { |
| 2933 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 2934 | R.getAsSingle<VarTemplateDecl>(), |
| 2935 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2938 | // We don't want lookup warnings at this point. |
| 2939 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2940 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2941 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2942 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2943 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2944 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2945 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2946 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2947 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2948 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2949 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2952 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2953 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2954 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2955 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2956 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2957 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2958 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2959 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2960 | DeclContext *DC; |
| 2961 | if (!(DC = computeDeclContext(SS, false)) || |
| 2962 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2963 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 2964 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2965 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2966 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2967 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2968 | LookupTemplateName(R, (Scope*)nullptr, SS, QualType(), /*Entering*/ false, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2969 | MemberOfUnknownSpecialization); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2970 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2971 | if (R.isAmbiguous()) |
| 2972 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2973 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2974 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2975 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2976 | << NameInfo.getName() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2977 | return ExprError(); |
| 2978 | } |
| 2979 | |
| 2980 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2981 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 2982 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 2983 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2984 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2985 | return ExprError(); |
| 2986 | } |
| 2987 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2988 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2989 | } |
| 2990 | |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2991 | /// \brief Form a dependent template name. |
| 2992 | /// |
| 2993 | /// This action forms a dependent template name given the template |
| 2994 | /// name and its (presumably dependent) scope specifier. For |
| 2995 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2996 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2997 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2998 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2999 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3000 | SourceLocation TemplateKWLoc, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3001 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3002 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3003 | bool EnteringContext, |
| 3004 | TemplateTy &Result) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3005 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 3006 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3007 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3008 | diag::warn_cxx98_compat_template_outside_of_template : |
| 3009 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3010 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 3011 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3012 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3013 | if (SS.isSet()) |
| 3014 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 3015 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3016 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3017 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3018 | // C++0x [temp.names]p5: |
| 3019 | // If a name prefixed by the keyword template is not the name of |
| 3020 | // a template, the program is ill-formed. [Note: the keyword |
| 3021 | // template may not be applied to non-template members of class |
| 3022 | // templates. -end note ] [ Note: as is the case with the |
| 3023 | // typename prefix, the template prefix is allowed in cases |
| 3024 | // where it is not strictly necessary; i.e., when the |
| 3025 | // nested-name-specifier or the expression on the left of the -> |
| 3026 | // or . is not dependent on a template-parameter, or the use |
| 3027 | // does not appear in the scope of a template. -end note] |
| 3028 | // |
| 3029 | // Note: C++03 was more strict here, because it banned the use of |
| 3030 | // the "template" keyword prior to a template-name that was not a |
| 3031 | // dependent name. C++ DR468 relaxed this requirement (the |
| 3032 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 3033 | // 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] | 3034 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 3035 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 3036 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3037 | MemberOfUnknownSpecialization); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3038 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 3039 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | 5ecbb1b | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 3040 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 3041 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3042 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3043 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3044 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3045 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3046 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3047 | << Name.getSourceRange() |
| 3048 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3049 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3050 | } else { |
| 3051 | // We found something; return it. |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3052 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3053 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 3056 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3057 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3058 | switch (Name.getKind()) { |
| 3059 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3060 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3061 | Name.Identifier)); |
| 3062 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3063 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3064 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3065 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3066 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 3067 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3068 | |
| 3069 | case UnqualifiedId::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 3070 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3071 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3072 | default: |
| 3073 | break; |
| 3074 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3075 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3076 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3077 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3078 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3079 | << Name.getSourceRange() |
| 3080 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3081 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3082 | } |
| 3083 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3084 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3085 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3086 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3087 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3088 | QualType ArgType; |
| 3089 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3090 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3091 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3092 | switch(Arg.getKind()) { |
| 3093 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3094 | // C++ [temp.arg.type]p1: |
| 3095 | // A template-argument for a template-parameter which is a |
| 3096 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3097 | ArgType = Arg.getAsType(); |
| 3098 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3099 | break; |
| 3100 | case TemplateArgument::Template: { |
| 3101 | // We have a template type parameter but the template argument |
| 3102 | // is a template without any arguments. |
| 3103 | SourceRange SR = AL.getSourceRange(); |
| 3104 | TemplateName Name = Arg.getAsTemplate(); |
| 3105 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 3106 | << Name << SR; |
| 3107 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 3108 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3109 | |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3110 | return true; |
| 3111 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3112 | case TemplateArgument::Expression: { |
| 3113 | // We have a template type parameter but the template argument is an |
| 3114 | // expression; see if maybe it is missing the "typename" keyword. |
| 3115 | CXXScopeSpec SS; |
| 3116 | DeclarationNameInfo NameInfo; |
| 3117 | |
| 3118 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 3119 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3120 | NameInfo = ArgExpr->getNameInfo(); |
| 3121 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 3122 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 3123 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3124 | NameInfo = ArgExpr->getNameInfo(); |
| 3125 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 3126 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3127 | if (ArgExpr->isImplicitAccess()) { |
| 3128 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3129 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 3130 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3133 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3134 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 3135 | LookupParsedName(Result, CurScope, &SS); |
| 3136 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3137 | if (Result.getAsSingle<TypeDecl>() || |
| 3138 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3139 | LookupResult::NotFoundInCurrentInstantiation) { |
| 3140 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3141 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3142 | Diag(Loc, getLangOpts().MSVCCompat |
| 3143 | ? diag::ext_ms_template_type_arg_missing_typename |
| 3144 | : diag::err_template_arg_must_be_type_suggest) |
| 3145 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3146 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3147 | |
| 3148 | // Recover by synthesizing a type using the location information that we |
| 3149 | // already have. |
| 3150 | ArgType = |
| 3151 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 3152 | TypeLocBuilder TLB; |
| 3153 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 3154 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 3155 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3156 | TL.setNameLoc(NameInfo.getLoc()); |
| 3157 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 3158 | |
| 3159 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 3160 | // properly. |
| 3161 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 3162 | TemplateArgumentLocInfo(TSI)); |
| 3163 | |
| 3164 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3165 | } |
| 3166 | } |
| 3167 | // fallthrough |
| 3168 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3169 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3170 | // We have a template type parameter but the template argument |
| 3171 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 3172 | SourceRange SR = AL.getSourceRange(); |
| 3173 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3174 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3175 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3176 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3177 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3178 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3179 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3180 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3181 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3182 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3183 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3184 | ArgType = Context.getCanonicalType(ArgType); |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3185 | |
| 3186 | // Objective-C ARC: |
| 3187 | // If an explicitly-specified template argument type is a lifetime type |
| 3188 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3189 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3190 | ArgType->isObjCLifetimeType() && |
| 3191 | !ArgType.getObjCLifetime()) { |
| 3192 | Qualifiers Qs; |
| 3193 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 3194 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 3195 | } |
| 3196 | |
| 3197 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3198 | return false; |
| 3199 | } |
| 3200 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3201 | /// \brief Substitute template arguments into the default template argument for |
| 3202 | /// the given template type parameter. |
| 3203 | /// |
| 3204 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3205 | /// the substitution. |
| 3206 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3207 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3208 | /// for. |
| 3209 | /// |
| 3210 | /// \param TemplateLoc the location of the template name that started the |
| 3211 | /// template-id we are checking. |
| 3212 | /// |
| 3213 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3214 | /// terminates the template-id. |
| 3215 | /// |
| 3216 | /// \param Param the template template parameter whose default we are |
| 3217 | /// substituting into. |
| 3218 | /// |
| 3219 | /// \param Converted the list of template arguments provided for template |
| 3220 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3221 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3222 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3223 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3224 | TemplateDecl *Template, |
| 3225 | SourceLocation TemplateLoc, |
| 3226 | SourceLocation RAngleLoc, |
| 3227 | TemplateTypeParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3228 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3229 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3230 | |
| 3231 | // If the argument type is dependent, instantiate it now based |
| 3232 | // on the previously-computed template arguments. |
| 3233 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3234 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3235 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3236 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3237 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3238 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3239 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3240 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3241 | Converted.data(), Converted.size()); |
| 3242 | |
| 3243 | // Only substitute for the innermost template argument list. |
| 3244 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3245 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3246 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3247 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3248 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3249 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3250 | ArgType = |
| 3251 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 3252 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | return ArgType; |
| 3256 | } |
| 3257 | |
| 3258 | /// \brief Substitute template arguments into the default template argument for |
| 3259 | /// the given non-type template parameter. |
| 3260 | /// |
| 3261 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3262 | /// the substitution. |
| 3263 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3264 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3265 | /// for. |
| 3266 | /// |
| 3267 | /// \param TemplateLoc the location of the template name that started the |
| 3268 | /// template-id we are checking. |
| 3269 | /// |
| 3270 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3271 | /// terminates the template-id. |
| 3272 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3273 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3274 | /// substituting into. |
| 3275 | /// |
| 3276 | /// \param Converted the list of template arguments provided for template |
| 3277 | /// parameters that precede \p Param in the template parameter list. |
| 3278 | /// |
| 3279 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3280 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3281 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3282 | TemplateDecl *Template, |
| 3283 | SourceLocation TemplateLoc, |
| 3284 | SourceLocation RAngleLoc, |
| 3285 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3286 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3287 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3288 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3289 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3290 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3291 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3292 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3293 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3294 | Converted.data(), Converted.size()); |
| 3295 | |
| 3296 | // Only substitute for the innermost template argument list. |
| 3297 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3298 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3299 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3300 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3301 | |
Faisal Vali | 48401eb | 2015-11-19 19:20:17 +0000 | [diff] [blame] | 3302 | EnterExpressionEvaluationContext ConstantEvaluated(SemaRef, |
| 3303 | Sema::ConstantEvaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3304 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3305 | } |
| 3306 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3307 | /// \brief Substitute template arguments into the default template argument for |
| 3308 | /// the given template template parameter. |
| 3309 | /// |
| 3310 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3311 | /// the substitution. |
| 3312 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3313 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3314 | /// for. |
| 3315 | /// |
| 3316 | /// \param TemplateLoc the location of the template name that started the |
| 3317 | /// template-id we are checking. |
| 3318 | /// |
| 3319 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3320 | /// terminates the template-id. |
| 3321 | /// |
| 3322 | /// \param Param the template template parameter whose default we are |
| 3323 | /// substituting into. |
| 3324 | /// |
| 3325 | /// \param Converted the list of template arguments provided for template |
| 3326 | /// parameters that precede \p Param in the template parameter list. |
| 3327 | /// |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3328 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 3329 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3330 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3331 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 3332 | static TemplateName |
| 3333 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3334 | TemplateDecl *Template, |
| 3335 | SourceLocation TemplateLoc, |
| 3336 | SourceLocation RAngleLoc, |
| 3337 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3338 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3339 | NestedNameSpecifierLoc &QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3340 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, Template, Converted, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3341 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3342 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3343 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3344 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3345 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3346 | Converted.data(), Converted.size()); |
| 3347 | |
| 3348 | // Only substitute for the innermost template argument list. |
| 3349 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3350 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3351 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3352 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3353 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3354 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3355 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3356 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3357 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3358 | QualifierLoc = |
| 3359 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3360 | if (!QualifierLoc) |
| 3361 | return TemplateName(); |
| 3362 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3363 | |
| 3364 | return SemaRef.SubstTemplateName( |
| 3365 | QualifierLoc, |
| 3366 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 3367 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 3368 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3371 | /// \brief If the given template parameter has a default template |
| 3372 | /// argument, substitute into that default template argument and |
| 3373 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3374 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3375 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 3376 | SourceLocation TemplateLoc, |
| 3377 | SourceLocation RAngleLoc, |
| 3378 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3379 | SmallVectorImpl<TemplateArgument> |
| 3380 | &Converted, |
| 3381 | bool &HasDefaultArg) { |
| 3382 | HasDefaultArg = false; |
| 3383 | |
| 3384 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3385 | if (!hasVisibleDefaultArgument(TypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3386 | return TemplateArgumentLoc(); |
| 3387 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3388 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3389 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3390 | TemplateLoc, |
| 3391 | RAngleLoc, |
| 3392 | TypeParm, |
| 3393 | Converted); |
| 3394 | if (DI) |
| 3395 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 3396 | |
| 3397 | return TemplateArgumentLoc(); |
| 3398 | } |
| 3399 | |
| 3400 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 3401 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3402 | if (!hasVisibleDefaultArgument(NonTypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3403 | return TemplateArgumentLoc(); |
| 3404 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3405 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3406 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3407 | TemplateLoc, |
| 3408 | RAngleLoc, |
| 3409 | NonTypeParm, |
| 3410 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3411 | if (Arg.isInvalid()) |
| 3412 | return TemplateArgumentLoc(); |
| 3413 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3414 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3415 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 3416 | } |
| 3417 | |
| 3418 | TemplateTemplateParmDecl *TempTempParm |
| 3419 | = cast<TemplateTemplateParmDecl>(Param); |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3420 | if (!hasVisibleDefaultArgument(TempTempParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3421 | return TemplateArgumentLoc(); |
| 3422 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3423 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3424 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3425 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3426 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3427 | RAngleLoc, |
| 3428 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3429 | Converted, |
| 3430 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3431 | if (TName.isNull()) |
| 3432 | return TemplateArgumentLoc(); |
| 3433 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3434 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3435 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3436 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 3437 | } |
| 3438 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3439 | /// \brief Check that the given template argument corresponds to the given |
| 3440 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3441 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3442 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3443 | /// checked. |
| 3444 | /// |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3445 | /// \param Arg The template argument, which may be updated due to conversions. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3446 | /// |
| 3447 | /// \param Template The template in which the template argument resides. |
| 3448 | /// |
| 3449 | /// \param TemplateLoc The location of the template name for the template |
| 3450 | /// whose argument list we're matching. |
| 3451 | /// |
| 3452 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 3453 | /// the template argument list. |
| 3454 | /// |
| 3455 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 3456 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 3457 | /// |
| 3458 | /// \param Converted The checked, converted argument will be added to the |
| 3459 | /// end of this small vector. |
| 3460 | /// |
| 3461 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 3462 | /// explicitly written, deduced, etc. |
| 3463 | /// |
| 3464 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3465 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3466 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 3467 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3468 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3469 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3470 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3471 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3472 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3473 | // Check template type parameters. |
| 3474 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3475 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3476 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3477 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3478 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3479 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3480 | // with the template arguments we've seen thus far. But if the |
| 3481 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3482 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3483 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 3484 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3485 | |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3486 | if (NTTPType->isDependentType() && |
| 3487 | !isa<TemplateTemplateParmDecl>(Template) && |
| 3488 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3489 | // Do substitution on the type of the non-type template parameter. |
| 3490 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3491 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3492 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3493 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3494 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3495 | |
| 3496 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3497 | Converted.data(), Converted.size()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3498 | NTTPType = SubstType(NTTPType, |
| 3499 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 3500 | NTTP->getLocation(), |
| 3501 | NTTP->getDeclName()); |
| 3502 | // If that worked, check the non-type template parameter type |
| 3503 | // for validity. |
| 3504 | if (!NTTPType.isNull()) |
| 3505 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 3506 | NTTP->getLocation()); |
| 3507 | if (NTTPType.isNull()) |
| 3508 | return true; |
| 3509 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3510 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3511 | switch (Arg.getArgument().getKind()) { |
| 3512 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3513 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3514 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3515 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3516 | TemplateArgument Result; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3517 | ExprResult Res = |
| 3518 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 3519 | Result, CTAK); |
| 3520 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3521 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3522 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3523 | // If the resulting expression is new, then use it in place of the |
| 3524 | // old expression in the template argument. |
| 3525 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 3526 | TemplateArgument TA(Res.get()); |
| 3527 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 3528 | } |
| 3529 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3530 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3531 | break; |
| 3532 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3533 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3534 | case TemplateArgument::Declaration: |
| 3535 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3536 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3537 | // We've already checked this template argument, so just copy |
| 3538 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3539 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3540 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3541 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3542 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3543 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3544 | // We were given a template template argument. It may not be ill-formed; |
| 3545 | // see below. |
| 3546 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3547 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 3548 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3549 | // We have a template argument such as \c T::template X, which we |
| 3550 | // parsed as a template template argument. However, since we now |
| 3551 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3552 | // template name into an expression. |
| 3553 | |
| 3554 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 3555 | Arg.getTemplateNameLoc()); |
| 3556 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 3557 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3558 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3559 | // FIXME: the template-template arg was a DependentTemplateName, |
| 3560 | // so it was provided with a template keyword. However, its source |
| 3561 | // location is not stored in the template argument structure. |
| 3562 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3563 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 3564 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 3565 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3566 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3567 | // If we parsed the template argument as a pack expansion, create a |
| 3568 | // pack expansion expression. |
| 3569 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3570 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3571 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3572 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3573 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3574 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3575 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3576 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3577 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3578 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3579 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3580 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3581 | break; |
| 3582 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3583 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3584 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3585 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3586 | // therefore cannot be a non-type template argument. |
| 3587 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 3588 | << Arg.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3589 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3590 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3591 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3592 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3593 | case TemplateArgument::Type: { |
| 3594 | // We have a non-type template parameter but the template |
| 3595 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3596 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3597 | // C++ [temp.arg]p2: |
| 3598 | // In a template-argument, an ambiguity between a type-id and |
| 3599 | // an expression is resolved to a type-id, regardless of the |
| 3600 | // form of the corresponding template-parameter. |
| 3601 | // |
| 3602 | // We warn specifically about this case, since it can be rather |
| 3603 | // confusing for users. |
| 3604 | QualType T = Arg.getArgument().getAsType(); |
| 3605 | SourceRange SR = Arg.getSourceRange(); |
| 3606 | if (T->isFunctionType()) |
| 3607 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 3608 | else |
| 3609 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 3610 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3611 | return true; |
| 3612 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3613 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3614 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3615 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3616 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3617 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3618 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3619 | } |
| 3620 | |
| 3621 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3622 | // Check template template parameters. |
| 3623 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3624 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3625 | // Substitute into the template parameter list of the template |
| 3626 | // template parameter, since previously-supplied template arguments |
| 3627 | // may appear within the template template parameter. |
| 3628 | { |
| 3629 | // Set up a template instantiation context. |
| 3630 | LocalInstantiationScope Scope(*this); |
| 3631 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3632 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3633 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3634 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3635 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3636 | |
| 3637 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3638 | Converted.data(), Converted.size()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3639 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3640 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3641 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 3642 | if (!TempParm) |
| 3643 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3644 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3645 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3646 | switch (Arg.getArgument().getKind()) { |
| 3647 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3648 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3649 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3650 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3651 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3652 | if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3653 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3654 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3655 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3656 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3657 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3658 | case TemplateArgument::Expression: |
| 3659 | case TemplateArgument::Type: |
| 3660 | // We have a template template parameter but the template |
| 3661 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3662 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3663 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3664 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3665 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3666 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3667 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3668 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3669 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3670 | case TemplateArgument::NullPtr: |
| 3671 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3672 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3673 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3674 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3675 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3676 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3677 | return false; |
| 3678 | } |
| 3679 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3680 | /// \brief Diagnose an arity mismatch in the |
| 3681 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 3682 | SourceLocation TemplateLoc, |
| 3683 | TemplateArgumentListInfo &TemplateArgs) { |
| 3684 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 3685 | unsigned NumParams = Params->size(); |
| 3686 | unsigned NumArgs = TemplateArgs.size(); |
| 3687 | |
| 3688 | SourceRange Range; |
| 3689 | if (NumArgs > NumParams) |
| 3690 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
| 3691 | TemplateArgs.getRAngleLoc()); |
| 3692 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3693 | << (NumArgs > NumParams) |
| 3694 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3695 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3696 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3697 | << Template << Range; |
| 3698 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3699 | << Params->getSourceRange(); |
| 3700 | return true; |
| 3701 | } |
| 3702 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3703 | /// \brief Check whether the template parameter is a pack expansion, and if so, |
| 3704 | /// determine the number of parameters produced by that expansion. For instance: |
| 3705 | /// |
| 3706 | /// \code |
| 3707 | /// template<typename ...Ts> struct A { |
| 3708 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 3709 | /// }; |
| 3710 | /// \endcode |
| 3711 | /// |
| 3712 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 3713 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3714 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3715 | if (NonTypeTemplateParmDecl *NTTP |
| 3716 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3717 | if (NTTP->isExpandedParameterPack()) |
| 3718 | return NTTP->getNumExpansionTypes(); |
| 3719 | } |
| 3720 | |
| 3721 | if (TemplateTemplateParmDecl *TTP |
| 3722 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 3723 | if (TTP->isExpandedParameterPack()) |
| 3724 | return TTP->getNumExpansionTemplateParameters(); |
| 3725 | } |
| 3726 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 3727 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3728 | } |
| 3729 | |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3730 | /// Diagnose a missing template argument. |
| 3731 | template<typename TemplateParmDecl> |
| 3732 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
| 3733 | TemplateDecl *TD, |
| 3734 | const TemplateParmDecl *D, |
| 3735 | TemplateArgumentListInfo &Args) { |
| 3736 | // Dig out the most recent declaration of the template parameter; there may be |
| 3737 | // declarations of the template that are more recent than TD. |
| 3738 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
| 3739 | ->getTemplateParameters() |
| 3740 | ->getParam(D->getIndex())); |
| 3741 | |
| 3742 | // If there's a default argument that's not visible, diagnose that we're |
| 3743 | // missing a module import. |
| 3744 | llvm::SmallVector<Module*, 8> Modules; |
| 3745 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { |
| 3746 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
| 3747 | D->getDefaultArgumentLoc(), Modules, |
| 3748 | Sema::MissingImportKind::DefaultArgument, |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 3749 | /*Recover*/true); |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3750 | return true; |
| 3751 | } |
| 3752 | |
| 3753 | // FIXME: If there's a more recent default argument that *is* visible, |
| 3754 | // diagnose that it was declared too late. |
| 3755 | |
| 3756 | return diagnoseArityMismatch(S, TD, Loc, Args); |
| 3757 | } |
| 3758 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3759 | /// \brief Check that the given template argument list is well-formed |
| 3760 | /// for specializing the given template. |
| 3761 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 3762 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3763 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 3764 | bool PartialTemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3765 | SmallVectorImpl<TemplateArgument> &Converted) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3766 | // Make a copy of the template arguments for processing. Only make the |
| 3767 | // changes at the end when successful in matching the arguments to the |
| 3768 | // template. |
| 3769 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 3770 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3771 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3772 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3773 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3774 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3775 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3776 | // [...] The type and form of each template-argument specified in |
| 3777 | // a template-id shall match the type and form specified for the |
| 3778 | // corresponding parameter declared by the template in its |
| 3779 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3780 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3781 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3782 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3783 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3784 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 3785 | ParamEnd = Params->end(); |
| 3786 | Param != ParamEnd; /* increment in loop */) { |
| 3787 | // If we have an expanded parameter pack, make sure we don't have too |
| 3788 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3789 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3790 | if (*Expansions == ArgumentPack.size()) { |
| 3791 | // We're done with this parameter pack. Pack up its arguments and add |
| 3792 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3793 | Converted.push_back( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 3794 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3795 | ArgumentPack.clear(); |
| 3796 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3797 | // This argument is assigned to the next parameter. |
| 3798 | ++Param; |
| 3799 | continue; |
| 3800 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 3801 | // Not enough arguments for this parameter pack. |
| 3802 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3803 | << false |
| 3804 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3805 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3806 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3807 | << Template; |
| 3808 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3809 | << Params->getSourceRange(); |
| 3810 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3811 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3812 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3813 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3814 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3815 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3816 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3817 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3818 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3819 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3820 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3821 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3822 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3823 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 3824 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3825 | // 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] | 3826 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3827 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3828 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3829 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3830 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3831 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 3832 | return true; |
| 3833 | } |
| 3834 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3835 | // We're now done with this argument. |
| 3836 | ++ArgIdx; |
| 3837 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3838 | if ((*Param)->isTemplateParameterPack()) { |
| 3839 | // The template parameter was a template parameter pack, so take the |
| 3840 | // deduced argument and place it on the argument pack. Note that we |
| 3841 | // stay on the same template parameter so that we can deduce more |
| 3842 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 3843 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3844 | } else { |
| 3845 | // Move to the next template parameter. |
| 3846 | ++Param; |
| 3847 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3848 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3849 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 3850 | // the remaining arguments, because we don't know what parameters they'll |
| 3851 | // match up with. |
| 3852 | if (PackExpansionIntoNonPack) { |
| 3853 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3854 | // If we were part way through filling in an expanded parameter pack, |
| 3855 | // fall back to just producing individual arguments. |
| 3856 | Converted.insert(Converted.end(), |
| 3857 | ArgumentPack.begin(), ArgumentPack.end()); |
| 3858 | ArgumentPack.clear(); |
| 3859 | } |
| 3860 | |
| 3861 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3862 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3863 | ++ArgIdx; |
| 3864 | } |
| 3865 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3866 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3867 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3868 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3869 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3870 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3871 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3872 | // If we're checking a partial template argument list, we're done. |
| 3873 | if (PartialTemplateArgs) { |
| 3874 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 3875 | Converted.push_back( |
| 3876 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 3877 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3878 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3879 | } |
| 3880 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3881 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3882 | // 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] | 3883 | if ((*Param)->isTemplateParameterPack()) { |
| 3884 | assert(!getExpandedPackSize(*Param) && |
| 3885 | "Should have dealt with this already"); |
| 3886 | |
| 3887 | // A non-expanded parameter pack before the end of the parameter list |
| 3888 | // only occurs for an ill-formed template parameter list, unless we've |
| 3889 | // got a partial argument list for a function template, so just bail out. |
| 3890 | if (Param + 1 != ParamEnd) |
| 3891 | return true; |
| 3892 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 3893 | Converted.push_back( |
| 3894 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3895 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3896 | |
| 3897 | ++Param; |
| 3898 | continue; |
| 3899 | } |
| 3900 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3901 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3902 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3903 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3904 | // Retrieve the default template argument from the template |
| 3905 | // parameter. For each kind of template parameter, we substitute the |
| 3906 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3907 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3908 | // the default argument. |
| 3909 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3910 | if (!hasVisibleDefaultArgument(TTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3911 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
| 3912 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3913 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3914 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3915 | Template, |
| 3916 | TemplateLoc, |
| 3917 | RAngleLoc, |
| 3918 | TTP, |
| 3919 | Converted); |
| 3920 | if (!ArgType) |
| 3921 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3922 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3923 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 3924 | ArgType); |
| 3925 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3926 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3927 | if (!hasVisibleDefaultArgument(NTTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3928 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
| 3929 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3930 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3931 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3932 | TemplateLoc, |
| 3933 | RAngleLoc, |
| 3934 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3935 | Converted); |
| 3936 | if (E.isInvalid()) |
| 3937 | return true; |
| 3938 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3939 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3940 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 3941 | } else { |
| 3942 | TemplateTemplateParmDecl *TempParm |
| 3943 | = cast<TemplateTemplateParmDecl>(*Param); |
| 3944 | |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3945 | if (!hasVisibleDefaultArgument(TempParm)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3946 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
| 3947 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3948 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3949 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3950 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3951 | TemplateLoc, |
| 3952 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3953 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3954 | Converted, |
| 3955 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3956 | if (Name.isNull()) |
| 3957 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3958 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3959 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3960 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3961 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3962 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3963 | // Introduce an instantiation record that describes where we are using |
| 3964 | // the default template argument. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3965 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 3966 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3967 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3968 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3969 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3970 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3971 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3972 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3973 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3974 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3975 | // Core issue 150 (assumed resolution): if this is a template template |
| 3976 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3977 | // template definition. |
| 3978 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3979 | NewArgs.addArgument(Arg); |
| 3980 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3981 | // Move to the next template parameter and argument. |
| 3982 | ++Param; |
| 3983 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3984 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3985 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 3986 | // If we're performing a partial argument substitution, allow any trailing |
| 3987 | // pack expansions; they might be empty. This can happen even if |
| 3988 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 3989 | // still dependent). |
| 3990 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 3991 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3992 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 3993 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 3994 | } |
| 3995 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3996 | // If we have any leftover arguments, then there were too many arguments. |
| 3997 | // Complain and fail. |
| 3998 | if (ArgIdx < NumArgs) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3999 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
| 4000 | |
| 4001 | // No problems found with the new argument list, propagate changes back |
| 4002 | // to caller. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 4003 | TemplateArgs = std::move(NewArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4004 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 4005 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4006 | } |
| 4007 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4008 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4009 | class UnnamedLocalNoLinkageFinder |
| 4010 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4011 | { |
| 4012 | Sema &S; |
| 4013 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4014 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4015 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4016 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4017 | public: |
| 4018 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 4019 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4020 | bool Visit(QualType T) { |
| 4021 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4022 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4023 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4024 | #define TYPE(Class, Parent) \ |
| 4025 | bool Visit##Class##Type(const Class##Type *); |
| 4026 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 4027 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4028 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 4029 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4030 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4031 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4032 | bool VisitTagDecl(const TagDecl *Tag); |
| 4033 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 4034 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4035 | } // end anonymous namespace |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4036 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4037 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4038 | return false; |
| 4039 | } |
| 4040 | |
| 4041 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 4042 | return Visit(T->getElementType()); |
| 4043 | } |
| 4044 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4045 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4046 | return Visit(T->getPointeeType()); |
| 4047 | } |
| 4048 | |
| 4049 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4050 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4051 | return Visit(T->getPointeeType()); |
| 4052 | } |
| 4053 | |
| 4054 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4055 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4056 | return Visit(T->getPointeeType()); |
| 4057 | } |
| 4058 | |
| 4059 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4060 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4061 | return Visit(T->getPointeeType()); |
| 4062 | } |
| 4063 | |
| 4064 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4065 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4066 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 4067 | } |
| 4068 | |
| 4069 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4070 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4071 | return Visit(T->getElementType()); |
| 4072 | } |
| 4073 | |
| 4074 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4075 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4076 | return Visit(T->getElementType()); |
| 4077 | } |
| 4078 | |
| 4079 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4080 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4081 | return Visit(T->getElementType()); |
| 4082 | } |
| 4083 | |
| 4084 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4085 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4086 | return Visit(T->getElementType()); |
| 4087 | } |
| 4088 | |
| 4089 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4090 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4091 | return Visit(T->getElementType()); |
| 4092 | } |
| 4093 | |
| 4094 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 4095 | return Visit(T->getElementType()); |
| 4096 | } |
| 4097 | |
| 4098 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 4099 | return Visit(T->getElementType()); |
| 4100 | } |
| 4101 | |
| 4102 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 4103 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4104 | for (const auto &A : T->param_types()) { |
| 4105 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4106 | return true; |
| 4107 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4108 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4109 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 4113 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4114 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
| 4117 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 4118 | const UnresolvedUsingType*) { |
| 4119 | return false; |
| 4120 | } |
| 4121 | |
| 4122 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 4123 | return false; |
| 4124 | } |
| 4125 | |
| 4126 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 4127 | return Visit(T->getUnderlyingType()); |
| 4128 | } |
| 4129 | |
| 4130 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 4131 | return false; |
| 4132 | } |
| 4133 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4134 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 4135 | const UnaryTransformType*) { |
| 4136 | return false; |
| 4137 | } |
| 4138 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4139 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 4140 | return Visit(T->getDeducedType()); |
| 4141 | } |
| 4142 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4143 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 4144 | return VisitTagDecl(T->getDecl()); |
| 4145 | } |
| 4146 | |
| 4147 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 4148 | return VisitTagDecl(T->getDecl()); |
| 4149 | } |
| 4150 | |
| 4151 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 4152 | const TemplateTypeParmType*) { |
| 4153 | return false; |
| 4154 | } |
| 4155 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4156 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 4157 | const SubstTemplateTypeParmPackType *) { |
| 4158 | return false; |
| 4159 | } |
| 4160 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4161 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 4162 | const TemplateSpecializationType*) { |
| 4163 | return false; |
| 4164 | } |
| 4165 | |
| 4166 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 4167 | const InjectedClassNameType* T) { |
| 4168 | return VisitTagDecl(T->getDecl()); |
| 4169 | } |
| 4170 | |
| 4171 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 4172 | const DependentNameType* T) { |
| 4173 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4174 | } |
| 4175 | |
| 4176 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 4177 | const DependentTemplateSpecializationType* T) { |
| 4178 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4179 | } |
| 4180 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4181 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 4182 | const PackExpansionType* T) { |
| 4183 | return Visit(T->getPattern()); |
| 4184 | } |
| 4185 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4186 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 4187 | return false; |
| 4188 | } |
| 4189 | |
| 4190 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 4191 | const ObjCInterfaceType *) { |
| 4192 | return false; |
| 4193 | } |
| 4194 | |
| 4195 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 4196 | const ObjCObjectPointerType *) { |
| 4197 | return false; |
| 4198 | } |
| 4199 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4200 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 4201 | return Visit(T->getValueType()); |
| 4202 | } |
| 4203 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 4204 | bool UnnamedLocalNoLinkageFinder::VisitPipeType(const PipeType* T) { |
| 4205 | return false; |
| 4206 | } |
| 4207 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4208 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 4209 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4210 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4211 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4212 | diag::warn_cxx98_compat_template_arg_local_type : |
| 4213 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4214 | << S.Context.getTypeDeclType(Tag) << SR; |
| 4215 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4216 | } |
| 4217 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 4218 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4219 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4220 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4221 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 4222 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4223 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 4224 | return true; |
| 4225 | } |
| 4226 | |
| 4227 | return false; |
| 4228 | } |
| 4229 | |
| 4230 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 4231 | NestedNameSpecifier *NNS) { |
| 4232 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 4233 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4234 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4235 | switch (NNS->getKind()) { |
| 4236 | case NestedNameSpecifier::Identifier: |
| 4237 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4238 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4239 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 4240 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4241 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4242 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4243 | case NestedNameSpecifier::TypeSpec: |
| 4244 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 4245 | return Visit(QualType(NNS->getAsType(), 0)); |
| 4246 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4247 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4248 | } |
| 4249 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4250 | /// \brief Check a template argument against its corresponding |
| 4251 | /// template type parameter. |
| 4252 | /// |
| 4253 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 4254 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4255 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4256 | TypeSourceInfo *ArgInfo) { |
| 4257 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4258 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 4259 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 4260 | |
| 4261 | if (Arg->isVariablyModifiedType()) { |
| 4262 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4263 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4264 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4265 | } |
| 4266 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4267 | // C++03 [temp.arg.type]p2: |
| 4268 | // A local type, a type with no linkage, an unnamed type or a type |
| 4269 | // compounded from any of these types shall not be used as a |
| 4270 | // template-argument for a template type-parameter. |
| 4271 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4272 | // 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] | 4273 | // a warning. |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 4274 | bool NeedsCheck; |
| 4275 | if (LangOpts.CPlusPlus11) |
| 4276 | NeedsCheck = |
| 4277 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 4278 | SR.getBegin()) || |
| 4279 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_local_type, |
| 4280 | SR.getBegin()); |
| 4281 | else |
| 4282 | NeedsCheck = Arg->hasUnnamedOrLocalType(); |
| 4283 | |
| 4284 | if (NeedsCheck) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4285 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 4286 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 4287 | } |
| 4288 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4289 | return false; |
| 4290 | } |
| 4291 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4292 | enum NullPointerValueKind { |
| 4293 | NPV_NotNullPointer, |
| 4294 | NPV_NullPointer, |
| 4295 | NPV_Error |
| 4296 | }; |
| 4297 | |
| 4298 | /// \brief Determine whether the given template argument is a null pointer |
| 4299 | /// value of the appropriate type. |
| 4300 | static NullPointerValueKind |
| 4301 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 4302 | QualType ParamType, Expr *Arg) { |
| 4303 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 4304 | return NPV_NotNullPointer; |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4305 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4306 | if (!S.isCompleteType(Arg->getExprLoc(), ParamType)) |
David Majnemer | b54368c | 2015-09-11 20:55:29 +0000 | [diff] [blame] | 4307 | llvm_unreachable( |
| 4308 | "Incomplete parameter type in isNullPointerValueTemplateArgument!"); |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4309 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 4310 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4311 | return NPV_NotNullPointer; |
| 4312 | |
| 4313 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4314 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 4315 | if (ArgRV.isInvalid()) |
| 4316 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4317 | Arg = ArgRV.get(); |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4318 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4319 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4320 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4321 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4322 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4323 | EvalResult.HasSideEffects) { |
| 4324 | SourceLocation DiagLoc = Arg->getExprLoc(); |
| 4325 | |
| 4326 | // If our only note is the usual "invalid subexpression" note, just point |
| 4327 | // the caret at its location rather than producing an essentially |
| 4328 | // redundant note. |
| 4329 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 4330 | diag::note_invalid_subexpr_in_const_expr) { |
| 4331 | DiagLoc = Notes[0].first; |
| 4332 | Notes.clear(); |
| 4333 | } |
| 4334 | |
| 4335 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 4336 | << Arg->getType() << Arg->getSourceRange(); |
| 4337 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 4338 | S.Diag(Notes[I].first, Notes[I].second); |
| 4339 | |
| 4340 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4341 | return NPV_Error; |
| 4342 | } |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4343 | |
| 4344 | // C++11 [temp.arg.nontype]p1: |
| 4345 | // - an address constant expression of type std::nullptr_t |
| 4346 | if (Arg->getType()->isNullPtrType()) |
| 4347 | return NPV_NullPointer; |
| 4348 | |
| 4349 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 4350 | // - a constant expression that evaluates to a null member pointer value |
| 4351 | // (4.11); or |
| 4352 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 4353 | (EvalResult.Val.isMemberPointer() && |
| 4354 | !EvalResult.Val.getMemberPointerDecl())) { |
| 4355 | // If our expression has an appropriate type, we've succeeded. |
| 4356 | bool ObjCLifetimeConversion; |
| 4357 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 4358 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 4359 | ObjCLifetimeConversion)) |
| 4360 | return NPV_NullPointer; |
| 4361 | |
| 4362 | // The types didn't match, but we know we got a null pointer; complain, |
| 4363 | // then recover as if the types were correct. |
| 4364 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 4365 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4366 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4367 | return NPV_NullPointer; |
| 4368 | } |
| 4369 | |
| 4370 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 4371 | // constant, suggest a cast to the appropriate type. |
| 4372 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 4373 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 4374 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4375 | << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 4376 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()), |
| 4377 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4378 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4379 | return NPV_NullPointer; |
| 4380 | } |
| 4381 | |
| 4382 | // FIXME: If we ever want to support general, address-constant expressions |
| 4383 | // as non-type template arguments, we should return the ExprResult here to |
| 4384 | // be interpreted by the caller. |
| 4385 | return NPV_NotNullPointer; |
| 4386 | } |
| 4387 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4388 | /// \brief Checks whether the given template argument is compatible with its |
| 4389 | /// template parameter. |
| 4390 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 4391 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 4392 | Expr *Arg, QualType ArgType) { |
| 4393 | bool ObjCLifetimeConversion; |
| 4394 | if (ParamType->isPointerType() && |
| 4395 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 4396 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 4397 | ObjCLifetimeConversion)) { |
| 4398 | // For pointer-to-object types, qualification conversions are |
| 4399 | // permitted. |
| 4400 | } else { |
| 4401 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 4402 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 4403 | // C++ [temp.arg.nontype]p5b3: |
| 4404 | // For a non-type template-parameter of type reference to |
| 4405 | // object, no conversions apply. The type referred to by the |
| 4406 | // reference may be more cv-qualified than the (otherwise |
| 4407 | // identical) type of the template- argument. The |
| 4408 | // template-parameter is bound directly to the |
| 4409 | // template-argument, which shall be an lvalue. |
| 4410 | |
| 4411 | // FIXME: Other qualifiers? |
| 4412 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 4413 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 4414 | |
| 4415 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 4416 | S.Diag(Arg->getLocStart(), |
| 4417 | diag::err_template_arg_ref_bind_ignores_quals) |
| 4418 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 4419 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4420 | return true; |
| 4421 | } |
| 4422 | } |
| 4423 | } |
| 4424 | |
| 4425 | // At this point, the template argument refers to an object or |
| 4426 | // function with external linkage. We now need to check whether the |
| 4427 | // argument and parameter types are compatible. |
| 4428 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 4429 | ParamType.getNonReferenceType())) { |
| 4430 | // We can't perform this conversion or binding. |
| 4431 | if (ParamType->isReferenceType()) |
| 4432 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 4433 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
| 4434 | else |
| 4435 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4436 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
| 4437 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4438 | return true; |
| 4439 | } |
| 4440 | } |
| 4441 | |
| 4442 | return false; |
| 4443 | } |
| 4444 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4445 | /// \brief Checks whether the given template argument is the address |
| 4446 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4447 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4448 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 4449 | NonTypeTemplateParmDecl *Param, |
| 4450 | QualType ParamType, |
| 4451 | Expr *ArgIn, |
| 4452 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4453 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4454 | Expr *Arg = ArgIn; |
| 4455 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4456 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4457 | bool AddressTaken = false; |
| 4458 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4459 | if (S.getLangOpts().MicrosoftExt) { |
| 4460 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 4461 | // dereference and address-of operators. |
| 4462 | Arg = Arg->IgnoreParenCasts(); |
| 4463 | |
| 4464 | bool ExtWarnMSTemplateArg = false; |
| 4465 | UnaryOperatorKind FirstOpKind; |
| 4466 | SourceLocation FirstOpLoc; |
| 4467 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4468 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 4469 | if (UnOpKind == UO_Deref) |
| 4470 | ExtWarnMSTemplateArg = true; |
| 4471 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 4472 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 4473 | if (!AddrOpLoc.isValid()) { |
| 4474 | FirstOpKind = UnOpKind; |
| 4475 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 4476 | } |
| 4477 | } else |
| 4478 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4479 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4480 | if (FirstOpLoc.isValid()) { |
| 4481 | if (ExtWarnMSTemplateArg) |
| 4482 | S.Diag(ArgIn->getLocStart(), diag::ext_ms_deref_template_argument) |
| 4483 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4484 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4485 | if (FirstOpKind == UO_AddrOf) |
| 4486 | AddressTaken = true; |
| 4487 | else if (Arg->getType()->isPointerType()) { |
| 4488 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 4489 | // constant expression. |
| 4490 | assert(FirstOpKind == UO_Deref); |
| 4491 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4492 | << Arg->getSourceRange(); |
| 4493 | } |
| 4494 | } |
| 4495 | } else { |
| 4496 | // See through any implicit casts we added to fix the type. |
| 4497 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4498 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4499 | // C++ [temp.arg.nontype]p1: |
| 4500 | // |
| 4501 | // A template-argument for a non-type, non-template |
| 4502 | // template-parameter shall be one of: [...] |
| 4503 | // |
| 4504 | // -- the address of an object or function with external |
| 4505 | // linkage, including function templates and function |
| 4506 | // template-ids but excluding non-static class members, |
| 4507 | // expressed as & id-expression where the & is optional if |
| 4508 | // the name refers to a function or array, or if the |
| 4509 | // corresponding template-parameter is a reference; or |
| 4510 | |
| 4511 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4512 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4513 | bool ExtraParens = false; |
| 4514 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 4515 | if (!Invalid && !ExtraParens) { |
| 4516 | S.Diag(Arg->getLocStart(), |
| 4517 | S.getLangOpts().CPlusPlus11 |
| 4518 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 4519 | : diag::ext_template_arg_extra_parens) |
| 4520 | << Arg->getSourceRange(); |
| 4521 | ExtraParens = true; |
| 4522 | } |
| 4523 | |
| 4524 | Arg = Parens->getSubExpr(); |
| 4525 | } |
| 4526 | |
| 4527 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4528 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4529 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4530 | |
| 4531 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4532 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 4533 | Arg = UnOp->getSubExpr(); |
| 4534 | AddressTaken = true; |
| 4535 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 4536 | } |
| 4537 | } |
| 4538 | |
| 4539 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4540 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4541 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4542 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4543 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4544 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 4545 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 4546 | |
| 4547 | // If our parameter has pointer type, check for a null template value. |
| 4548 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 4549 | NullPointerValueKind NPV; |
| 4550 | // dllimport'd entities aren't constant but are available inside of template |
| 4551 | // arguments. |
| 4552 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 4553 | NPV = NPV_NotNullPointer; |
| 4554 | else |
| 4555 | NPV = isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn); |
| 4556 | switch (NPV) { |
| 4557 | case NPV_NullPointer: |
| 4558 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4559 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4560 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4561 | return false; |
| 4562 | |
| 4563 | case NPV_Error: |
| 4564 | return true; |
| 4565 | |
| 4566 | case NPV_NotNullPointer: |
| 4567 | break; |
| 4568 | } |
| 4569 | } |
| 4570 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4571 | // Stop checking the precise nature of the argument if it is value dependent, |
| 4572 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4573 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4574 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4575 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4576 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4577 | |
| 4578 | if (isa<CXXUuidofExpr>(Arg)) { |
| 4579 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 4580 | ArgIn, Arg, ArgType)) |
| 4581 | return true; |
| 4582 | |
| 4583 | Converted = TemplateArgument(ArgIn); |
| 4584 | return false; |
| 4585 | } |
| 4586 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4587 | if (!DRE) { |
| 4588 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4589 | << Arg->getSourceRange(); |
| 4590 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4591 | return true; |
| 4592 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4593 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4594 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4595 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4596 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4597 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4598 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4599 | return true; |
| 4600 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4601 | |
| 4602 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4603 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4604 | if (!Method->isStatic()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4605 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4606 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4607 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4608 | return true; |
| 4609 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4610 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4611 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4612 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 4613 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4614 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4615 | // A non-type template argument must refer to an object or function. |
| 4616 | if (!Func && !Var) { |
| 4617 | // We found something, but we don't know specifically what it is. |
| 4618 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 4619 | << Arg->getSourceRange(); |
| 4620 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 4621 | return true; |
| 4622 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4623 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4624 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4625 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4626 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4627 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 4628 | diag::ext_template_arg_object_internal) |
| 4629 | << !Func << Entity << Arg->getSourceRange(); |
| 4630 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4631 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4632 | } else if (!Entity->hasLinkage()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4633 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 4634 | << !Func << Entity << Arg->getSourceRange(); |
| 4635 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4636 | << !Func; |
| 4637 | return true; |
| 4638 | } |
| 4639 | |
| 4640 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4641 | // If the template parameter has pointer type, the function decays. |
| 4642 | if (ParamType->isPointerType() && !AddressTaken) |
| 4643 | ArgType = S.Context.getPointerType(Func->getType()); |
| 4644 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 4645 | // If we originally had an address-of operator, but the |
| 4646 | // parameter has reference type, complain and (if things look |
| 4647 | // like they will work) drop the address-of operator. |
| 4648 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 4649 | ParamType.getNonReferenceType())) { |
| 4650 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4651 | << ParamType; |
| 4652 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4653 | return true; |
| 4654 | } |
| 4655 | |
| 4656 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4657 | << ParamType |
| 4658 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4659 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4660 | |
| 4661 | ArgType = Func->getType(); |
| 4662 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4663 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4664 | // A value of reference type is not an object. |
| 4665 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4666 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4667 | diag::err_template_arg_reference_var) |
| 4668 | << Var->getType() << Arg->getSourceRange(); |
| 4669 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4670 | return true; |
| 4671 | } |
| 4672 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4673 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 4674 | if (Var->getTLSKind()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4675 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 4676 | << Arg->getSourceRange(); |
| 4677 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 4678 | return true; |
| 4679 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4680 | |
| 4681 | // If the template parameter has pointer type, we must have taken |
| 4682 | // the address of this object. |
| 4683 | if (ParamType->isReferenceType()) { |
| 4684 | if (AddressTaken) { |
| 4685 | // If we originally had an address-of operator, but the |
| 4686 | // parameter has reference type, complain and (if things look |
| 4687 | // like they will work) drop the address-of operator. |
| 4688 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 4689 | ParamType.getNonReferenceType())) { |
| 4690 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4691 | << ParamType; |
| 4692 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4693 | return true; |
| 4694 | } |
| 4695 | |
| 4696 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4697 | << ParamType |
| 4698 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4699 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4700 | |
| 4701 | ArgType = Var->getType(); |
| 4702 | } |
| 4703 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 4704 | if (Var->getType()->isArrayType()) { |
| 4705 | // Array-to-pointer decay. |
| 4706 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 4707 | } else { |
| 4708 | // If the template parameter has pointer type but the address of |
| 4709 | // this object was not taken, complain and (possibly) recover by |
| 4710 | // taking the address of the entity. |
| 4711 | ArgType = S.Context.getPointerType(Var->getType()); |
| 4712 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 4713 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4714 | << ParamType; |
| 4715 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4716 | return true; |
| 4717 | } |
| 4718 | |
| 4719 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4720 | << ParamType |
| 4721 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 4722 | |
| 4723 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4724 | } |
| 4725 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4726 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4727 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4728 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 4729 | Arg, ArgType)) |
| 4730 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4731 | |
| 4732 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4733 | Converted = |
| 4734 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 4735 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4736 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4737 | } |
| 4738 | |
| 4739 | /// \brief Checks whether the given template argument is a pointer to |
| 4740 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4741 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 4742 | NonTypeTemplateParmDecl *Param, |
| 4743 | QualType ParamType, |
| 4744 | Expr *&ResultArg, |
| 4745 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4746 | bool Invalid = false; |
| 4747 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4748 | // Check for a null pointer value. |
| 4749 | Expr *Arg = ResultArg; |
| 4750 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 4751 | case NPV_Error: |
| 4752 | return true; |
| 4753 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 4754 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4755 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4756 | /*isNullPtr*/true); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4757 | return false; |
| 4758 | case NPV_NotNullPointer: |
| 4759 | break; |
| 4760 | } |
| 4761 | |
| 4762 | bool ObjCLifetimeConversion; |
| 4763 | if (S.IsQualificationConversion(Arg->getType(), |
| 4764 | ParamType.getNonReferenceType(), |
| 4765 | false, ObjCLifetimeConversion)) { |
| 4766 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4767 | Arg->getValueKind()).get(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4768 | ResultArg = Arg; |
| 4769 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), |
| 4770 | ParamType.getNonReferenceType())) { |
| 4771 | // We can't perform this conversion. |
| 4772 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4773 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4774 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4775 | return true; |
| 4776 | } |
| 4777 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4778 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4779 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4780 | Arg = Cast->getSubExpr(); |
| 4781 | |
| 4782 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4783 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4784 | // A template-argument for a non-type, non-template |
| 4785 | // template-parameter shall be one of: [...] |
| 4786 | // |
| 4787 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4788 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4789 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4790 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4791 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4792 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4793 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4794 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4795 | S.Diag(Arg->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4796 | S.getLangOpts().CPlusPlus11 ? |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4797 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 4798 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4799 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4800 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
| 4803 | Arg = Parens->getSubExpr(); |
| 4804 | } |
| 4805 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4806 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4807 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4808 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4809 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4810 | // A pointer-to-member constant written &Class::member. |
| 4811 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4812 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4813 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 4814 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4815 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4816 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4817 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4818 | // A constant of pointer-to-member type. |
| 4819 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 4820 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 4821 | if (VD->getType()->isMemberPointerType()) { |
David Majnemer | cd053cd | 2013-12-10 00:40:58 +0000 | [diff] [blame] | 4822 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4823 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4824 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4825 | } else { |
| 4826 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4827 | Converted = TemplateArgument(VD, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4828 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4829 | return Invalid; |
| 4830 | } |
| 4831 | } |
| 4832 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4833 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4834 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4835 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4836 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4837 | if (!DRE) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4838 | return S.Diag(Arg->getLocStart(), |
| 4839 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4840 | << Arg->getSourceRange(); |
| 4841 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4842 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 4843 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 4844 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4845 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4846 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4847 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 4848 | "Only non-static member pointers can make it here"); |
| 4849 | |
| 4850 | // Okay: this is the address of a non-static member, and therefore |
| 4851 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4852 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4853 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4854 | } else { |
| 4855 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4856 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4857 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4858 | return Invalid; |
| 4859 | } |
| 4860 | |
| 4861 | // 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] | 4862 | S.Diag(Arg->getLocStart(), |
| 4863 | diag::err_template_arg_not_pointer_to_member_form) |
| 4864 | << Arg->getSourceRange(); |
| 4865 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4866 | return true; |
| 4867 | } |
| 4868 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4869 | /// \brief Check a template argument against its corresponding |
| 4870 | /// non-type template parameter. |
| 4871 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4872 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4873 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4874 | /// returns the converted template argument. \p ParamType is the |
| 4875 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4876 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4877 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4878 | TemplateArgument &Converted, |
| 4879 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4880 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4881 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4882 | // If either the parameter has a dependent type or the argument is |
| 4883 | // type-dependent, there's nothing we can check now. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4884 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4885 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 4886 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4887 | return Arg; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4888 | } |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4889 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4890 | // We should have already dropped all cv-qualifiers by now. |
| 4891 | assert(!ParamType.hasQualifiers() && |
| 4892 | "non-type template parameter type cannot be qualified"); |
| 4893 | |
| 4894 | if (CTAK == CTAK_Deduced && |
| 4895 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 4896 | // C++ [temp.deduct.type]p17: |
| 4897 | // If, in the declaration of a function template with a non-type |
| 4898 | // template-parameter, the non-type template-parameter is used |
| 4899 | // in an expression in the function parameter-list and, if the |
| 4900 | // corresponding template-argument is deduced, the |
| 4901 | // template-argument type shall match the type of the |
| 4902 | // template-parameter exactly, except that a template-argument |
| 4903 | // deduced from an array bound may be of any integral type. |
| 4904 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 4905 | << Arg->getType().getUnqualifiedType() |
| 4906 | << ParamType.getUnqualifiedType(); |
| 4907 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4908 | return ExprError(); |
| 4909 | } |
| 4910 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4911 | if (getLangOpts().CPlusPlus1z) { |
| 4912 | // FIXME: We can do some limited checking for a value-dependent but not |
| 4913 | // type-dependent argument. |
| 4914 | if (Arg->isValueDependent()) { |
| 4915 | Converted = TemplateArgument(Arg); |
| 4916 | return Arg; |
| 4917 | } |
| 4918 | |
| 4919 | // C++1z [temp.arg.nontype]p1: |
| 4920 | // A template-argument for a non-type template parameter shall be |
| 4921 | // a converted constant expression of the type of the template-parameter. |
| 4922 | APValue Value; |
| 4923 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 4924 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 4925 | if (ArgResult.isInvalid()) |
| 4926 | return ExprError(); |
| 4927 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4928 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 4929 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4930 | // Convert the APValue to a TemplateArgument. |
| 4931 | switch (Value.getKind()) { |
| 4932 | case APValue::Uninitialized: |
| 4933 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4934 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4935 | break; |
| 4936 | case APValue::Int: |
| 4937 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4938 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4939 | break; |
| 4940 | case APValue::MemberPointer: { |
| 4941 | assert(ParamType->isMemberPointerType()); |
| 4942 | |
| 4943 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 4944 | if (!Value.getMemberPointerPath().empty()) { |
| 4945 | Diag(Arg->getLocStart(), |
| 4946 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 4947 | << Value.getMemberPointerDecl() << ParamType |
| 4948 | << Arg->getSourceRange(); |
| 4949 | return ExprError(); |
| 4950 | } |
| 4951 | |
| 4952 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4953 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 4954 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4955 | break; |
| 4956 | } |
| 4957 | case APValue::LValue: { |
| 4958 | // For a non-type template-parameter of pointer or reference type, |
| 4959 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4960 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 4961 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4962 | // -- a temporary object |
| 4963 | // -- a string literal |
| 4964 | // -- the result of a typeid expression, or |
| 4965 | // -- a predefind __func__ variable |
| 4966 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 4967 | if (isa<CXXUuidofExpr>(E)) { |
| 4968 | Converted = TemplateArgument(const_cast<Expr*>(E)); |
| 4969 | break; |
| 4970 | } |
| 4971 | Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4972 | << Arg->getSourceRange(); |
| 4973 | return ExprError(); |
| 4974 | } |
| 4975 | auto *VD = const_cast<ValueDecl *>( |
| 4976 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 4977 | // -- a subobject |
| 4978 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 4979 | VD && VD->getType()->isArrayType() && |
| 4980 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 4981 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 4982 | // Per defect report (no number yet): |
| 4983 | // ... other than a pointer to the first element of a complete array |
| 4984 | // object. |
| 4985 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 4986 | Value.isLValueOnePastTheEnd()) { |
| 4987 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 4988 | << Value.getAsString(Context, ParamType); |
| 4989 | return ExprError(); |
| 4990 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4991 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4992 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4993 | assert((!VD || !ParamType->isNullPtrType()) && |
| 4994 | "non-null value of type nullptr_t?"); |
| 4995 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 4996 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4997 | break; |
| 4998 | } |
| 4999 | case APValue::AddrLabelDiff: |
| 5000 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
| 5001 | case APValue::Float: |
| 5002 | case APValue::ComplexInt: |
| 5003 | case APValue::ComplexFloat: |
| 5004 | case APValue::Vector: |
| 5005 | case APValue::Array: |
| 5006 | case APValue::Struct: |
| 5007 | case APValue::Union: |
| 5008 | llvm_unreachable("invalid kind for template argument"); |
| 5009 | } |
| 5010 | |
| 5011 | return ArgResult.get(); |
| 5012 | } |
| 5013 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5014 | // C++ [temp.arg.nontype]p5: |
| 5015 | // The following conversions are performed on each expression used |
| 5016 | // as a non-type template-argument. If a non-type |
| 5017 | // template-argument cannot be converted to the type of the |
| 5018 | // corresponding template-parameter then the program is |
| 5019 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5020 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5021 | // C++11: |
| 5022 | // -- for a non-type template-parameter of integral or |
| 5023 | // enumeration type, conversions permitted in a converted |
| 5024 | // constant expression are applied. |
| 5025 | // |
| 5026 | // C++98: |
| 5027 | // -- for a non-type template-parameter of integral or |
| 5028 | // enumeration type, integral promotions (4.5) and integral |
| 5029 | // conversions (4.7) are applied. |
| 5030 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5031 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5032 | // We can't check arbitrary value-dependent arguments. |
| 5033 | // FIXME: If there's no viable conversion to the template parameter type, |
| 5034 | // we should be able to diagnose that prior to instantiation. |
| 5035 | if (Arg->isValueDependent()) { |
| 5036 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5037 | return Arg; |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5038 | } |
| 5039 | |
| 5040 | // C++ [temp.arg.nontype]p1: |
| 5041 | // A template-argument for a non-type, non-template template-parameter |
| 5042 | // shall be one of: |
| 5043 | // |
| 5044 | // -- for a non-type template-parameter of integral or enumeration |
| 5045 | // type, a converted constant expression of the type of the |
| 5046 | // template-parameter; or |
| 5047 | llvm::APSInt Value; |
| 5048 | ExprResult ArgResult = |
| 5049 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 5050 | CCEK_TemplateArg); |
| 5051 | if (ArgResult.isInvalid()) |
| 5052 | return ExprError(); |
| 5053 | |
| 5054 | // Widen the argument value to sizeof(parameter type). This is almost |
| 5055 | // always a no-op, except when the parameter type is bool. In |
| 5056 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 5057 | QualType IntegerType = ParamType; |
| 5058 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 5059 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 5060 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 5061 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5062 | Converted = TemplateArgument(Context, Value, |
| 5063 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5064 | return ArgResult; |
| 5065 | } |
| 5066 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5067 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 5068 | if (ArgResult.isInvalid()) |
| 5069 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5070 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5071 | |
| 5072 | QualType ArgType = Arg->getType(); |
| 5073 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5074 | // C++ [temp.arg.nontype]p1: |
| 5075 | // A template-argument for a non-type, non-template |
| 5076 | // template-parameter shall be one of: |
| 5077 | // |
| 5078 | // -- an integral constant-expression of integral or enumeration |
| 5079 | // type; or |
| 5080 | // -- the name of a non-type template-parameter; or |
| 5081 | SourceLocation NonConstantLoc; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5082 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5083 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5084 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5085 | diag::err_template_arg_not_integral_or_enumeral) |
| 5086 | << ArgType << Arg->getSourceRange(); |
| 5087 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5088 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5089 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5090 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 5091 | QualType T; |
| 5092 | |
| 5093 | public: |
| 5094 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 5095 | |
| 5096 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 5097 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5098 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 5099 | } |
| 5100 | } Diagnoser(ArgType); |
| 5101 | |
| 5102 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5103 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5104 | if (!Arg) |
| 5105 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5106 | } |
| 5107 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5108 | // From here on out, all we care about is the unqualified form |
| 5109 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5110 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5111 | |
| 5112 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 5113 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5114 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5115 | } else if (ParamType->isBooleanType()) { |
| 5116 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5117 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5118 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 5119 | !ParamType->isEnumeralType()) { |
| 5120 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5121 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5122 | } else { |
| 5123 | // We can't perform this conversion. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5124 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5125 | diag::err_template_arg_not_convertible) |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5126 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5127 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5128 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5129 | } |
| 5130 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5131 | // Add the value of this argument to the list of converted |
| 5132 | // arguments. We use the bitwidth and signedness of the template |
| 5133 | // parameter. |
| 5134 | if (Arg->isValueDependent()) { |
| 5135 | // The argument is value-dependent. Create a new |
| 5136 | // TemplateArgument with the converted expression. |
| 5137 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5138 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5139 | } |
| 5140 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5141 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5142 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 5143 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5144 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5145 | if (ParamType->isBooleanType()) { |
| 5146 | // Value must be zero or one. |
| 5147 | Value = Value != 0; |
| 5148 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 5149 | if (Value.getBitWidth() != AllowedBits) |
| 5150 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5151 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5152 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5153 | llvm::APSInt OldValue = Value; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5154 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5155 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5156 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5157 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5158 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5159 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5160 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5161 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5162 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5163 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5164 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5165 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5166 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5167 | << Arg->getSourceRange(); |
| 5168 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5169 | } |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5170 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5171 | // Complain if we overflowed the template parameter's type. |
| 5172 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5173 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5174 | RequiredBits = OldValue.getActiveBits(); |
| 5175 | else if (OldValue.isUnsigned()) |
| 5176 | RequiredBits = OldValue.getActiveBits() + 1; |
| 5177 | else |
| 5178 | RequiredBits = OldValue.getMinSignedBits(); |
| 5179 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5180 | Diag(Arg->getLocStart(), |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5181 | diag::warn_template_arg_too_large) |
| 5182 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5183 | << Arg->getSourceRange(); |
| 5184 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5185 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5186 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5187 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5188 | Converted = TemplateArgument(Context, Value, |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 5189 | ParamType->isEnumeralType() |
| 5190 | ? Context.getCanonicalType(ParamType) |
| 5191 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5192 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5193 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5194 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5195 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5196 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 5197 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5198 | // Handle pointer-to-function, reference-to-function, and |
| 5199 | // pointer-to-member-function all in (roughly) the same way. |
| 5200 | if (// -- For a non-type template-parameter of type pointer to |
| 5201 | // function, only the function-to-pointer conversion (4.3) is |
| 5202 | // applied. If the template-argument represents a set of |
| 5203 | // overloaded functions (or a pointer to such), the matching |
| 5204 | // function is selected from the set (13.4). |
| 5205 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5206 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5207 | // -- For a non-type template-parameter of type reference to |
| 5208 | // function, no conversions apply. If the template-argument |
| 5209 | // represents a set of overloaded functions, the matching |
| 5210 | // function is selected from the set (13.4). |
| 5211 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5212 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5213 | // -- For a non-type template-parameter of type pointer to |
| 5214 | // member function, no conversions apply. If the |
| 5215 | // template-argument represents a set of overloaded member |
| 5216 | // functions, the matching member function is selected from |
| 5217 | // the set (13.4). |
| 5218 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5219 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5220 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5221 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5222 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5223 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5224 | true, |
| 5225 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5226 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5227 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5228 | |
| 5229 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5230 | ArgType = Arg->getType(); |
| 5231 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5232 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5233 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5234 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5235 | if (!ParamType->isMemberPointerType()) { |
| 5236 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5237 | ParamType, |
| 5238 | Arg, Converted)) |
| 5239 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5240 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5241 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5242 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5243 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5244 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5245 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5246 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5247 | } |
| 5248 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 5249 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5250 | // -- for a non-type template-parameter of type pointer to |
| 5251 | // object, qualification conversions (4.4) and the |
| 5252 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5253 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5254 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5255 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5256 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5257 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5258 | ParamType, |
| 5259 | Arg, Converted)) |
| 5260 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5261 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5262 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5263 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5264 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5265 | // -- For a non-type template-parameter of type reference to |
| 5266 | // object, no conversions apply. The type referred to by the |
| 5267 | // reference may be more cv-qualified than the (otherwise |
| 5268 | // identical) type of the template-argument. The |
| 5269 | // template-parameter is bound directly to the |
| 5270 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5271 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5272 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5273 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5274 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5275 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 5276 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5277 | true, |
| 5278 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5279 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5280 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5281 | |
| 5282 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5283 | ArgType = Arg->getType(); |
| 5284 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5285 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5286 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5287 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5288 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5289 | ParamType, |
| 5290 | Arg, Converted)) |
| 5291 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5292 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5293 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5294 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5295 | // Deal with parameters of type std::nullptr_t. |
| 5296 | if (ParamType->isNullPtrType()) { |
| 5297 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 5298 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5299 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5300 | } |
| 5301 | |
| 5302 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 5303 | case NPV_NotNullPointer: |
| 5304 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 5305 | << Arg->getType() << ParamType; |
| 5306 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5307 | return ExprError(); |
| 5308 | |
| 5309 | case NPV_Error: |
| 5310 | return ExprError(); |
| 5311 | |
| 5312 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 5313 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5314 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 5315 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5316 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5317 | } |
| 5318 | } |
| 5319 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5320 | // -- For a non-type template-parameter of type pointer to data |
| 5321 | // member, qualification conversions (4.4) are applied. |
| 5322 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 5323 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5324 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5325 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5326 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5327 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5328 | } |
| 5329 | |
| 5330 | /// \brief Check a template argument against its corresponding |
| 5331 | /// template template parameter. |
| 5332 | /// |
| 5333 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 5334 | /// It returns true if an error occurred, and false otherwise. |
| 5335 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 5336 | TemplateArgumentLoc &Arg, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5337 | unsigned ArgumentPackIndex) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5338 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5339 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 5340 | if (!Template) { |
| 5341 | // Any dependent template name is fine. |
| 5342 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 5343 | return false; |
| 5344 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5345 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5346 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5347 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5348 | // the name of a class template or an alias template, expressed as an |
| 5349 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5350 | // primary class templates are considered when matching the |
| 5351 | // template template argument with the corresponding parameter; |
| 5352 | // partial specializations are not considered even if their |
| 5353 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5354 | // |
| 5355 | // Note that we also allow template template parameters here, which |
| 5356 | // will happen when we are dealing with, e.g., class template |
| 5357 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5358 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5359 | !isa<TemplateTemplateParmDecl>(Template) && |
| 5360 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5361 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5362 | "Only function templates are possible here"); |
Faisal Vali | b8b04f8 | 2016-03-26 20:46:45 +0000 | [diff] [blame] | 5363 | Diag(Arg.getLocation(), diag::err_template_arg_not_valid_template); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5364 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5365 | << Template; |
| 5366 | } |
| 5367 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5368 | TemplateParameterList *Params = Param->getTemplateParameters(); |
| 5369 | if (Param->isExpandedParameterPack()) |
| 5370 | Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); |
| 5371 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5372 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5373 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5374 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5375 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5376 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5377 | } |
| 5378 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5379 | /// \brief Given a non-type template argument that refers to a |
| 5380 | /// declaration and the type of its corresponding non-type template |
| 5381 | /// parameter, produce an expression that properly refers to that |
| 5382 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5383 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5384 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 5385 | QualType ParamType, |
| 5386 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 5387 | // C++ [temp.param]p8: |
| 5388 | // |
| 5389 | // A non-type template-parameter of type "array of T" or |
| 5390 | // "function returning T" is adjusted to be of type "pointer to |
| 5391 | // T" or "pointer to function returning T", respectively. |
| 5392 | if (ParamType->isArrayType()) |
| 5393 | ParamType = Context.getArrayDecayedType(ParamType); |
| 5394 | else if (ParamType->isFunctionType()) |
| 5395 | ParamType = Context.getPointerType(ParamType); |
| 5396 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5397 | // For a NULL non-type template argument, return nullptr casted to the |
| 5398 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5399 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5400 | return ImpCastExprToType( |
| 5401 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 5402 | ParamType, |
| 5403 | ParamType->getAs<MemberPointerType>() |
| 5404 | ? CK_NullToMemberPointer |
| 5405 | : CK_NullToPointer); |
| 5406 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5407 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 5408 | "Only declaration template arguments permitted here"); |
| 5409 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5410 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 5411 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5412 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 5413 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 5414 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5415 | // If the value is a class member, we might have a pointer-to-member. |
| 5416 | // Determine whether the non-type template template parameter is of |
| 5417 | // pointer-to-member type. If so, we need to build an appropriate |
| 5418 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 5419 | // would refer to the member itself. |
| 5420 | if (ParamType->isMemberPointerType()) { |
| 5421 | QualType ClassType |
| 5422 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 5423 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5424 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5425 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5426 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 5427 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5428 | |
| 5429 | // The actual value-ness of this is unimportant, but for |
| 5430 | // internal consistency's sake, references to instance methods |
| 5431 | // are r-values. |
| 5432 | ExprValueKind VK = VK_LValue; |
| 5433 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 5434 | VK = VK_RValue; |
| 5435 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5436 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5437 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5438 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5439 | Loc, |
| 5440 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5441 | if (RefExpr.isInvalid()) |
| 5442 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5443 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5444 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5445 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5446 | // We might need to perform a trailing qualification conversion, since |
| 5447 | // the element type on the parameter could be more qualified than the |
| 5448 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5449 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5450 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5451 | ParamType.getUnqualifiedType(), false, |
| 5452 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5453 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5454 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5455 | assert(!RefExpr.isInvalid() && |
| 5456 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5457 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5458 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5459 | } |
| 5460 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5461 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5462 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5463 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5464 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5465 | // When the non-type template parameter is a pointer, take the |
| 5466 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5467 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5468 | if (RefExpr.isInvalid()) |
| 5469 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5470 | |
| 5471 | if (T->isFunctionType() || T->isArrayType()) { |
| 5472 | // Decay functions and arrays. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5473 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5474 | if (RefExpr.isInvalid()) |
| 5475 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5476 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5477 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5478 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5479 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5480 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5481 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5482 | } |
| 5483 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5484 | ExprValueKind VK = VK_RValue; |
| 5485 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5486 | // If the non-type template parameter has reference type, qualify the |
| 5487 | // resulting declaration reference with the extra qualifiers on the |
| 5488 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5489 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 5490 | VK = VK_LValue; |
| 5491 | T = Context.getQualifiedType(T, |
| 5492 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5493 | } else if (isa<FunctionDecl>(VD)) { |
| 5494 | // References to functions are always lvalues. |
| 5495 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5496 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5497 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5498 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5499 | } |
| 5500 | |
| 5501 | /// \brief Construct a new expression that refers to the given |
| 5502 | /// integral template argument with the given source-location |
| 5503 | /// information. |
| 5504 | /// |
| 5505 | /// This routine takes care of the mapping from an integral template |
| 5506 | /// argument (which may have any integral type) to the appropriate |
| 5507 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5508 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5509 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 5510 | SourceLocation Loc) { |
| 5511 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 5512 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5513 | QualType OrigT = Arg.getIntegralType(); |
| 5514 | |
| 5515 | // If this is an enum type that we're instantiating, we need to use an integer |
| 5516 | // type the same size as the enumerator. We don't want to build an |
| 5517 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 5518 | // any integral type with C++11 enum classes, make sure we create the right |
| 5519 | // type of literal for it. |
| 5520 | QualType T = OrigT; |
| 5521 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 5522 | T = ET->getDecl()->getIntegerType(); |
| 5523 | |
| 5524 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5525 | if (T->isAnyCharacterType()) { |
Aaron Ballman | 9a17c85 | 2016-01-07 20:59:26 +0000 | [diff] [blame] | 5526 | // This does not need to handle u8 character literals because those are |
| 5527 | // 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] | 5528 | CharacterLiteral::CharacterKind Kind; |
| 5529 | if (T->isWideCharType()) |
| 5530 | Kind = CharacterLiteral::Wide; |
| 5531 | else if (T->isChar16Type()) |
| 5532 | Kind = CharacterLiteral::UTF16; |
| 5533 | else if (T->isChar32Type()) |
| 5534 | Kind = CharacterLiteral::UTF32; |
| 5535 | else |
| 5536 | Kind = CharacterLiteral::Ascii; |
| 5537 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5538 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 5539 | Kind, T, Loc); |
| 5540 | } else if (T->isBooleanType()) { |
| 5541 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 5542 | T, Loc); |
| 5543 | } else if (T->isNullPtrType()) { |
| 5544 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 5545 | } else { |
| 5546 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5547 | } |
| 5548 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5549 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5550 | // FIXME: This is a hack. We need a better way to handle substituted |
| 5551 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5552 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 5553 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5554 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5555 | Loc, Loc); |
| 5556 | } |
| 5557 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5558 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5559 | } |
| 5560 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5561 | /// \brief Match two template parameters within template parameter lists. |
| 5562 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 5563 | bool Complain, |
| 5564 | Sema::TemplateParameterListEqualKind Kind, |
| 5565 | SourceLocation TemplateArgLoc) { |
| 5566 | // Check the actual kind (type, non-type, template). |
| 5567 | if (Old->getKind() != New->getKind()) { |
| 5568 | if (Complain) { |
| 5569 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 5570 | if (TemplateArgLoc.isValid()) { |
| 5571 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5572 | NextDiag = diag::note_template_param_different_kind; |
| 5573 | } |
| 5574 | S.Diag(New->getLocation(), NextDiag) |
| 5575 | << (Kind != Sema::TPL_TemplateMatch); |
| 5576 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 5577 | << (Kind != Sema::TPL_TemplateMatch); |
| 5578 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5579 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5580 | return false; |
| 5581 | } |
| 5582 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5583 | // Check that both are parameter packs are neither are parameter packs. |
| 5584 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5585 | // template template parameter, the template template parameter can have |
| 5586 | // a parameter pack where the template template argument does not. |
| 5587 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 5588 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5589 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5590 | if (Complain) { |
| 5591 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 5592 | if (TemplateArgLoc.isValid()) { |
| 5593 | S.Diag(TemplateArgLoc, |
| 5594 | diag::err_template_arg_template_params_mismatch); |
| 5595 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 5596 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5597 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5598 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 5599 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 5600 | : 2; |
| 5601 | S.Diag(New->getLocation(), NextDiag) |
| 5602 | << ParamKind << New->isParameterPack(); |
| 5603 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 5604 | << ParamKind << Old->isParameterPack(); |
| 5605 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5606 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5607 | return false; |
| 5608 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5609 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5610 | // For non-type template parameters, check the type of the parameter. |
| 5611 | if (NonTypeTemplateParmDecl *OldNTTP |
| 5612 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 5613 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5614 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5615 | // If we are matching a template template argument to a template |
| 5616 | // template parameter and one of the non-type template parameter types |
| 5617 | // is dependent, then we must wait until template instantiation time |
| 5618 | // to actually compare the arguments. |
| 5619 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5620 | (OldNTTP->getType()->isDependentType() || |
| 5621 | NewNTTP->getType()->isDependentType())) |
| 5622 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5623 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5624 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 5625 | if (Complain) { |
| 5626 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 5627 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5628 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5629 | diag::err_template_arg_template_params_mismatch); |
| 5630 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 5631 | } |
| 5632 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 5633 | << NewNTTP->getType() |
| 5634 | << (Kind != Sema::TPL_TemplateMatch); |
| 5635 | S.Diag(OldNTTP->getLocation(), |
| 5636 | diag::note_template_nontype_parm_prev_declaration) |
| 5637 | << OldNTTP->getType(); |
| 5638 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5639 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5640 | return false; |
| 5641 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5642 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5643 | return true; |
| 5644 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5645 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5646 | // For template template parameters, check the template parameter types. |
| 5647 | // The template parameter lists of template template |
| 5648 | // parameters must agree. |
| 5649 | if (TemplateTemplateParmDecl *OldTTP |
| 5650 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5651 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5652 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 5653 | OldTTP->getTemplateParameters(), |
| 5654 | Complain, |
| 5655 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5656 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5657 | : Kind), |
| 5658 | TemplateArgLoc); |
| 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 true; |
| 5662 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5663 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5664 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 5665 | /// lists. |
| 5666 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5667 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5668 | TemplateParameterList *New, |
| 5669 | TemplateParameterList *Old, |
| 5670 | Sema::TemplateParameterListEqualKind Kind, |
| 5671 | SourceLocation TemplateArgLoc) { |
| 5672 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 5673 | if (TemplateArgLoc.isValid()) { |
| 5674 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5675 | NextDiag = diag::note_template_param_list_different_arity; |
| 5676 | } |
| 5677 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 5678 | << (New->size() > Old->size()) |
| 5679 | << (Kind != Sema::TPL_TemplateMatch) |
| 5680 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 5681 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 5682 | << (Kind != Sema::TPL_TemplateMatch) |
| 5683 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 5684 | } |
| 5685 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5686 | /// \brief Determine whether the given template parameter lists are |
| 5687 | /// equivalent. |
| 5688 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5689 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5690 | /// source code as part of a new template declaration. |
| 5691 | /// |
| 5692 | /// \param Old The old template parameter list, typically found via |
| 5693 | /// name lookup of the template declared with this template parameter |
| 5694 | /// list. |
| 5695 | /// |
| 5696 | /// \param Complain If true, this routine will produce a diagnostic if |
| 5697 | /// the template parameter lists are not equivalent. |
| 5698 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5699 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5700 | /// |
| 5701 | /// \param TemplateArgLoc If this source location is valid, then we |
| 5702 | /// are actually checking the template parameter list of a template |
| 5703 | /// argument (New) against the template parameter list of its |
| 5704 | /// corresponding template template parameter (Old). We produce |
| 5705 | /// slightly different diagnostics in this scenario. |
| 5706 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5707 | /// \returns True if the template parameter lists are equal, false |
| 5708 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5709 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5710 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 5711 | TemplateParameterList *Old, |
| 5712 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5713 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5714 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5715 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 5716 | if (Complain) |
| 5717 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5718 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5719 | |
| 5720 | return false; |
| 5721 | } |
| 5722 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5723 | // C++0x [temp.arg.template]p3: |
| 5724 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5725 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5726 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5727 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5728 | // template-parameter-list of P. [...] |
| 5729 | TemplateParameterList::iterator NewParm = New->begin(); |
| 5730 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5731 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5732 | OldParmEnd = Old->end(); |
| 5733 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 5734 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 5735 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5736 | if (NewParm == NewParmEnd) { |
| 5737 | if (Complain) |
| 5738 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5739 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5740 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5741 | return false; |
| 5742 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5743 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5744 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5745 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5746 | return false; |
| 5747 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5748 | ++NewParm; |
| 5749 | continue; |
| 5750 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5751 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5752 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5753 | // [...] When P's template- parameter-list contains a template parameter |
| 5754 | // pack (14.5.3), the template parameter pack will match zero or more |
| 5755 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5756 | // template-parameter-list of A with the same type and form as the |
| 5757 | // template parameter pack in P (ignoring whether those template |
| 5758 | // parameters are template parameter packs). |
| 5759 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 5760 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5761 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5762 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5763 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5764 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5765 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5766 | // Make sure we exhausted all of the arguments. |
| 5767 | if (NewParm != NewParmEnd) { |
| 5768 | if (Complain) |
| 5769 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5770 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5771 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5772 | return false; |
| 5773 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5774 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5775 | return true; |
| 5776 | } |
| 5777 | |
| 5778 | /// \brief Check whether a template can be declared within this scope. |
| 5779 | /// |
| 5780 | /// If the template declaration is valid in this scope, returns |
| 5781 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5782 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5783 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 5784 | if (!S) |
| 5785 | return false; |
| 5786 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5787 | // Find the nearest enclosing declaration scope. |
| 5788 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5789 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5790 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5791 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5792 | // C++ [temp]p4: |
| 5793 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 5794 | DeclContext *Ctx = S->getEntity(); |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5795 | if (Ctx && Ctx->isExternCContext()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5796 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5797 | << TemplateParams->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5798 | |
Eli Friedman | dfbd0c4 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 5799 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5800 | Ctx = Ctx->getParent(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5801 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5802 | // C++ [temp]p2: |
| 5803 | // A template-declaration can appear only as a namespace scope or |
| 5804 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 5805 | if (Ctx) { |
| 5806 | if (Ctx->isFileContext()) |
| 5807 | return false; |
| 5808 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 5809 | // C++ [temp.mem]p2: |
| 5810 | // A local class shall not have member templates. |
| 5811 | if (RD->isLocalClass()) |
| 5812 | return Diag(TemplateParams->getTemplateLoc(), |
| 5813 | diag::err_template_inside_local_class) |
| 5814 | << TemplateParams->getSourceRange(); |
| 5815 | else |
| 5816 | return false; |
| 5817 | } |
| 5818 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5819 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5820 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5821 | diag::err_template_outside_namespace_or_class_scope) |
| 5822 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5823 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5824 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5825 | /// \brief Determine what kind of template specialization the given declaration |
| 5826 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5827 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5828 | if (!D) |
| 5829 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5830 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5831 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 5832 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5833 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 5834 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5835 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 5836 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5837 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5838 | return TSK_Undeclared; |
| 5839 | } |
| 5840 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5841 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5842 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5843 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5844 | /// This routine determines whether a template specialization can be declared |
| 5845 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5846 | /// |
| 5847 | /// \param S the semantic analysis object for which this check is being |
| 5848 | /// performed. |
| 5849 | /// |
| 5850 | /// \param Specialized the entity being specialized or instantiated, which |
| 5851 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5852 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5853 | /// member class). |
| 5854 | /// |
| 5855 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 5856 | /// |
| 5857 | /// \param Loc the location of the explicit specialization or instantiation of |
| 5858 | /// this entity. |
| 5859 | /// |
| 5860 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 5861 | /// a class template. |
| 5862 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5863 | /// \returns true if there was an error that we cannot recover from, false |
| 5864 | /// otherwise. |
| 5865 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 5866 | NamedDecl *Specialized, |
| 5867 | NamedDecl *PrevDecl, |
| 5868 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5869 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5870 | // Keep these "kind" numbers in sync with the %select statements in the |
| 5871 | // various diagnostics emitted by this routine. |
| 5872 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5873 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5874 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5875 | else if (isa<VarTemplateDecl>(Specialized)) |
| 5876 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5877 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5878 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5879 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5880 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5881 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5882 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5883 | else if (isa<RecordDecl>(Specialized)) |
| 5884 | EntityKind = 7; |
| 5885 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 5886 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5887 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5888 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5889 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5890 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5891 | return true; |
| 5892 | } |
| 5893 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5894 | // C++ [temp.expl.spec]p2: |
| 5895 | // An explicit specialization shall be declared in the namespace |
| 5896 | // of which the template is a member, or, for member templates, in |
| 5897 | // the namespace of which the enclosing class or enclosing class |
| 5898 | // template is a member. An explicit specialization of a member |
| 5899 | // function, member class or static data member of a class |
| 5900 | // template shall be declared in the namespace of which the class |
| 5901 | // template is a member. Such a declaration may also be a |
| 5902 | // definition. If the declaration is not a definition, the |
| 5903 | // specialization may be defined later in the name- space in which |
| 5904 | // the explicit specialization was declared, or in a namespace |
| 5905 | // that encloses the one in which the explicit specialization was |
| 5906 | // declared. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5907 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5908 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5909 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5910 | return true; |
| 5911 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5912 | |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5913 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5914 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 5915 | // Do not warn for class scope explicit specialization during |
| 5916 | // instantiation, warning was already emitted during pattern |
| 5917 | // semantic analysis. |
| 5918 | if (!S.ActiveTemplateInstantiations.size()) |
| 5919 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 5920 | << Specialized; |
| 5921 | } else { |
| 5922 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5923 | << Specialized; |
| 5924 | return true; |
| 5925 | } |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5926 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5927 | |
Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 5928 | if (S.CurContext->isRecord() && |
| 5929 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 5930 | // Make sure that we're specializing in the right record context. |
| 5931 | // Otherwise, things can go horribly wrong. |
| 5932 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5933 | << Specialized; |
| 5934 | return true; |
| 5935 | } |
| 5936 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5937 | // C++ [temp.class.spec]p6: |
| 5938 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5939 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 5940 | // and 14.5.2). |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5941 | DeclContext *SpecializedContext |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5942 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5943 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5944 | |
| 5945 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
| 5946 | // namespace. |
| 5947 | // Note that HandleDeclarator() performs this check for explicit |
| 5948 | // specializations of function templates, static data members, and member |
| 5949 | // functions, so we skip the check here for those kinds of entities. |
| 5950 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
| 5951 | // Should we refactor that check, so that it occurs later? |
| 5952 | if (!DC->Encloses(SpecializedContext) && |
| 5953 | !(isa<FunctionTemplateDecl>(Specialized) || |
| 5954 | isa<FunctionDecl>(Specialized) || |
| 5955 | isa<VarTemplateDecl>(Specialized) || |
| 5956 | isa<VarDecl>(Specialized))) { |
| 5957 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 5958 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 5959 | << EntityKind << Specialized; |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 5960 | else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 5961 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
| 5962 | if (S.getLangOpts().MicrosoftExt) |
| 5963 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 5964 | S.Diag(Loc, Diag) << EntityKind << Specialized |
| 5965 | << cast<NamedDecl>(SpecializedContext); |
| 5966 | } else |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5967 | llvm_unreachable("unexpected namespace context for specialization"); |
| 5968 | |
| 5969 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 5970 | } else if ((!PrevDecl || |
| 5971 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 5972 | getTemplateSpecializationKind(PrevDecl) == |
| 5973 | TSK_ImplicitInstantiation)) { |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5974 | // C++ [temp.exp.spec]p2: |
| 5975 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5976 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5977 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5978 | // An explicit specialization of a member function, member class or |
| 5979 | // static data member of a class template shall be declared in the |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5980 | // namespace of which the class template is a member. |
| 5981 | // |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5982 | // C++11 [temp.expl.spec]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5983 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5984 | // the specialized template. |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5985 | // C++11 [temp.explicit]p3: |
| 5986 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 5987 | // template. |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5988 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5989 | bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext); |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5990 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5991 | assert(!IsCPlusPlus11Extension && |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5992 | "DC encloses TU but isn't in enclosing namespace set"); |
| 5993 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | 8ce6315 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 5994 | << EntityKind << Specialized; |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5995 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 5996 | int Diag; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5997 | if (!IsCPlusPlus11Extension) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5998 | Diag = diag::err_template_spec_decl_out_of_scope; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5999 | else if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6000 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 6001 | else |
| 6002 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 6003 | S.Diag(Loc, Diag) |
| 6004 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 6005 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6006 | |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6007 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6008 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6009 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6010 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6011 | return false; |
| 6012 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6013 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6014 | static SourceRange findTemplateParameter(unsigned Depth, Expr *E) { |
| 6015 | if (!E->isInstantiationDependent()) |
| 6016 | return SourceLocation(); |
| 6017 | DependencyChecker Checker(Depth); |
| 6018 | Checker.TraverseStmt(E); |
| 6019 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 6020 | return E->getSourceRange(); |
| 6021 | return Checker.MatchLoc; |
| 6022 | } |
| 6023 | |
| 6024 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 6025 | if (!TL.getType()->isDependentType()) |
| 6026 | return SourceLocation(); |
| 6027 | DependencyChecker Checker(Depth); |
| 6028 | Checker.TraverseTypeLoc(TL); |
| 6029 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 6030 | return TL.getSourceRange(); |
| 6031 | return Checker.MatchLoc; |
| 6032 | } |
| 6033 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6034 | /// \brief Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6035 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6036 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6037 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 6038 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6039 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 6040 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6041 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6042 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 6043 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6044 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6045 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6046 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6047 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6048 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6049 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6050 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6051 | |
| 6052 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6053 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 6054 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6055 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 6056 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6057 | |
| 6058 | // Strip off any implicit casts we added as part of type checking. |
| 6059 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 6060 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6061 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6062 | // C++ [temp.class.spec]p8: |
| 6063 | // A non-type argument is non-specialized if it is the name of a |
| 6064 | // non-type parameter. All other non-type arguments are |
| 6065 | // specialized. |
| 6066 | // |
| 6067 | // Below, we check the two conditions that only apply to |
| 6068 | // specialized non-type arguments, so skip any non-specialized |
| 6069 | // arguments. |
| 6070 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6071 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6072 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6073 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6074 | // C++ [temp.class.spec]p9: |
| 6075 | // Within the argument list of a class template partial |
| 6076 | // specialization, the following restrictions apply: |
| 6077 | // -- A partially specialized non-type argument expression |
| 6078 | // shall not involve a template parameter of the partial |
| 6079 | // specialization except when the argument expression is a |
| 6080 | // simple identifier. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6081 | SourceRange ParamUseRange = |
| 6082 | findTemplateParameter(Param->getDepth(), ArgExpr); |
| 6083 | if (ParamUseRange.isValid()) { |
| 6084 | if (IsDefaultArgument) { |
| 6085 | S.Diag(TemplateNameLoc, |
| 6086 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 6087 | S.Diag(ParamUseRange.getBegin(), |
| 6088 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 6089 | << ParamUseRange; |
| 6090 | } else { |
| 6091 | S.Diag(ParamUseRange.getBegin(), |
| 6092 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 6093 | << ParamUseRange; |
| 6094 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6095 | return true; |
| 6096 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6097 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6098 | // -- The type of a template parameter corresponding to a |
| 6099 | // specialized non-type argument shall not be dependent on a |
| 6100 | // parameter of the specialization. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6101 | // |
| 6102 | // FIXME: We need to delay this check until instantiation in some cases: |
| 6103 | // |
| 6104 | // template<template<typename> class X> struct A { |
| 6105 | // template<typename T, X<T> N> struct B; |
| 6106 | // template<typename T> struct B<T, 0>; |
| 6107 | // }; |
| 6108 | // template<typename> using X = int; |
| 6109 | // A<X>::B<int, 0> b; |
| 6110 | ParamUseRange = findTemplateParameter( |
| 6111 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
| 6112 | if (ParamUseRange.isValid()) { |
| 6113 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getLocStart(), |
| 6114 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 6115 | << Param->getType() << ParamUseRange; |
| 6116 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
| 6117 | << (IsDefaultArgument ? ParamUseRange : SourceRange()); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6118 | return true; |
| 6119 | } |
| 6120 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6121 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6122 | return false; |
| 6123 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6124 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6125 | /// \brief Check the non-type template arguments of a class template |
| 6126 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 6127 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6128 | /// \param TemplateNameLoc the location of the template name. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6129 | /// \param TemplateParams the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6130 | /// template. |
| 6131 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6132 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6133 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6134 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6135 | /// \returns \c true if there was an error, \c false otherwise. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6136 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6137 | Sema &S, SourceLocation TemplateNameLoc, |
| 6138 | TemplateParameterList *TemplateParams, unsigned NumExplicit, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6139 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6140 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 6141 | |
| 6142 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6143 | NonTypeTemplateParmDecl *Param |
| 6144 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 6145 | if (!Param) |
| 6146 | continue; |
| 6147 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6148 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
| 6149 | S, TemplateNameLoc, Param, &ArgList[I], 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6150 | return true; |
| 6151 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6152 | |
| 6153 | return false; |
| 6154 | } |
| 6155 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6156 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6157 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 6158 | TagUseKind TUK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6159 | SourceLocation KWLoc, |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6160 | SourceLocation ModulePrivateLoc, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6161 | TemplateIdAnnotation &TemplateId, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6162 | AttributeList *Attr, |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6163 | MultiTemplateParamsArg |
| 6164 | TemplateParameterLists, |
| 6165 | SkipBodyInfo *SkipBody) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6166 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 6167 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6168 | CXXScopeSpec &SS = TemplateId.SS; |
| 6169 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6170 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 6171 | // store the location of the outermost template keyword in the declaration. |
| 6172 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6173 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 6174 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 6175 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 6176 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6177 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6178 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6179 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6180 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6181 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 6182 | |
| 6183 | if (!ClassTemplate) { |
| 6184 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6185 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6186 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 6187 | return true; |
| 6188 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6189 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6190 | bool isExplicitSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6191 | bool isPartialSpecialization = false; |
| 6192 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6193 | // Check the validity of the template headers that introduce this |
| 6194 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6195 | // FIXME: We probably shouldn't complain about these headers for |
| 6196 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6197 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 6198 | TemplateParameterList *TemplateParams = |
| 6199 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6200 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
| 6201 | TemplateParameterLists, TUK == TUK_Friend, isExplicitSpecialization, |
| 6202 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6203 | if (Invalid) |
| 6204 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6205 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6206 | if (TemplateParams && TemplateParams->size() > 0) { |
| 6207 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6208 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 6209 | if (TUK == TUK_Friend) { |
| 6210 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 6211 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6212 | return true; |
| 6213 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6214 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6215 | // C++ [temp.class.spec]p10: |
| 6216 | // The template parameter list of a specialization shall not |
| 6217 | // contain default template argument values. |
| 6218 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6219 | Decl *Param = TemplateParams->getParam(I); |
| 6220 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 6221 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6222 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6223 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 6224 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6225 | } |
| 6226 | } else if (NonTypeTemplateParmDecl *NTTP |
| 6227 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 6228 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6229 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6230 | diag::err_default_arg_in_partial_spec) |
| 6231 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6232 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6233 | } |
| 6234 | } else { |
| 6235 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6236 | if (TTP->hasDefaultArgument()) { |
| 6237 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6238 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6239 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6240 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6241 | } |
| 6242 | } |
| 6243 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6244 | } else if (TemplateParams) { |
| 6245 | if (TUK == TUK_Friend) |
| 6246 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6247 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6248 | SourceRange(TemplateParams->getTemplateLoc(), |
| 6249 | TemplateParams->getRAngleLoc())) |
| 6250 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6251 | else |
| 6252 | isExplicitSpecialization = true; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6253 | } else { |
| 6254 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6255 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6256 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6257 | // Check that the specialization uses the same tag kind as the |
| 6258 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6259 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6260 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6261 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6262 | Kind, TUK == TUK_Definition, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 6263 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6264 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6265 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6266 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6267 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6268 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6269 | diag::note_previous_use); |
| 6270 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6271 | } |
| 6272 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6273 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6274 | TemplateArgumentListInfo TemplateArgs = |
| 6275 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6276 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6277 | // Check for unexpanded parameter packs in any of the template arguments. |
| 6278 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6279 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6280 | UPPC_PartialSpecialization)) |
| 6281 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6282 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6283 | // Check that the template argument list is well-formed for this |
| 6284 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6285 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6286 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6287 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6288 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6289 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6290 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6291 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6292 | if (isPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6293 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6294 | *this, TemplateNameLoc, ClassTemplate->getTemplateParameters(), |
| 6295 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6296 | return true; |
| 6297 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6298 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6299 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6300 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6301 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6302 | TemplateArgs.size(), |
| 6303 | InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6304 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 6305 | << ClassTemplate->getDeclName(); |
| 6306 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6307 | } |
| 6308 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6309 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6310 | void *InsertPos = nullptr; |
| 6311 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6312 | |
| 6313 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6314 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6315 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6316 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6317 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6318 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6319 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6320 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6321 | // Check whether we can declare a class template specialization in |
| 6322 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6323 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6324 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 6325 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6326 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6327 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6328 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6329 | // The canonical type |
| 6330 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 6331 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6332 | // Build the canonical type that describes the converted template |
| 6333 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6334 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6335 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6336 | Converted.data(), |
| 6337 | Converted.size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6338 | |
| 6339 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6340 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 6341 | // C++ [temp.class.spec]p9b3: |
| 6342 | // |
| 6343 | // -- The argument list of the specialization shall not be identical |
| 6344 | // to the implicit argument list of the primary template. |
| 6345 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6346 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6347 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6348 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 6349 | ClassTemplate->getIdentifier(), |
| 6350 | TemplateNameLoc, |
| 6351 | Attr, |
| 6352 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6353 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 6354 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6355 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6356 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6357 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6358 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6359 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6360 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 6361 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6362 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6363 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6364 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6365 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 6366 | TemplateParams, |
| 6367 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6368 | Converted.data(), |
| 6369 | Converted.size(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6370 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6371 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 6372 | PrevPartial); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6373 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6374 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6375 | Partial->setTemplateParameterListsInfo( |
| 6376 | Context, TemplateParameterLists.drop_back(1)); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6377 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6378 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6379 | if (!PrevPartial) |
| 6380 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6381 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6382 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6383 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6384 | // template specialization, make a note of that. |
| 6385 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 6386 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6387 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6388 | // Check that all of the template parameters of the class template |
| 6389 | // partial specialization are deducible from the template |
| 6390 | // arguments. If not, this class template partial specialization |
| 6391 | // will never be used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6392 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6393 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6394 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 6395 | DeducibleParams); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6396 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6397 | if (!DeducibleParams.all()) { |
| 6398 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6399 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6400 | << /*class template*/0 << (NumNonDeducible > 1) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6401 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 6402 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 6403 | if (!DeducibleParams[I]) { |
| 6404 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 6405 | if (Param->getDeclName()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6406 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6407 | diag::note_partial_spec_unused_parameter) |
| 6408 | << Param->getDeclName(); |
| 6409 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6410 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6411 | diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 6412 | << "(anonymous)"; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6413 | } |
| 6414 | } |
| 6415 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6416 | } else { |
| 6417 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6418 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6419 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6420 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6421 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6422 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6423 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6424 | Converted.data(), |
| 6425 | Converted.size(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6426 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6427 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6428 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6429 | Specialization->setTemplateParameterListsInfo(Context, |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6430 | TemplateParameterLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6431 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6432 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6433 | if (!PrevDecl) |
| 6434 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6435 | |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 6436 | if (CurContext->isDependentContext()) { |
| 6437 | // -fms-extensions permits specialization of nested classes without |
| 6438 | // fully specializing the outer class(es). |
| 6439 | assert(getLangOpts().MicrosoftExt && |
| 6440 | "Only possible with -fms-extensions!"); |
| 6441 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6442 | CanonType = Context.getTemplateSpecializationType( |
| 6443 | CanonTemplate, Converted.data(), Converted.size()); |
| 6444 | } else { |
| 6445 | CanonType = Context.getTypeDeclType(Specialization); |
| 6446 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6447 | } |
| 6448 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6449 | // C++ [temp.expl.spec]p6: |
| 6450 | // 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] | 6451 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6452 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6453 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6454 | // use occurs; no diagnostic is required. |
| 6455 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6456 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6457 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6458 | // Is there any previous explicit specialization declaration? |
| 6459 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6460 | Okay = true; |
| 6461 | break; |
| 6462 | } |
| 6463 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6464 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6465 | if (!Okay) { |
| 6466 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 6467 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 6468 | << Context.getTypeDeclType(Specialization) << Range; |
| 6469 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6470 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6471 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6472 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6473 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6474 | return true; |
| 6475 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6476 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6477 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6478 | // If this is not a friend, note that this is an explicit specialization. |
| 6479 | if (TUK != TUK_Friend) |
| 6480 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6481 | |
| 6482 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6483 | if (TUK == TUK_Definition) { |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6484 | RecordDecl *Def = Specialization->getDefinition(); |
| 6485 | NamedDecl *Hidden = nullptr; |
| 6486 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 6487 | SkipBody->ShouldSkip = true; |
| 6488 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 6489 | // From here on out, treat this as just a redeclaration. |
| 6490 | TUK = TUK_Declaration; |
| 6491 | } else if (Def) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6492 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6493 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6494 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6495 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 6496 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6497 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6498 | } |
| 6499 | } |
| 6500 | |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 6501 | if (Attr) |
| 6502 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6503 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 6504 | // Add alignment attributes if necessary; these attributes are checked when |
| 6505 | // the ASTContext lays out the structure. |
| 6506 | if (TUK == TUK_Definition) { |
| 6507 | AddAlignmentAttributesForRecord(Specialization); |
| 6508 | AddMsStructLayoutForRecord(Specialization); |
| 6509 | } |
| 6510 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6511 | if (ModulePrivateLoc.isValid()) |
| 6512 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 6513 | << (isPartialSpecialization? 1 : 0) |
| 6514 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 6515 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 6516 | // Build the fully-sugared type for this class template |
| 6517 | // specialization as the user wrote in the specialization |
| 6518 | // itself. This means that we'll pretty-print the type retrieved |
| 6519 | // from the specialization's declaration the way that the user |
| 6520 | // actually wrote the specialization, rather than formatting the |
| 6521 | // name based on the "canonical" representation used to store the |
| 6522 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6523 | TypeSourceInfo *WrittenTy |
| 6524 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6525 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6526 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6527 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6528 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6529 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6530 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 6531 | // C++ [temp.expl.spec]p9: |
| 6532 | // A template explicit specialization is in the scope of the |
| 6533 | // namespace in which the template was defined. |
| 6534 | // |
| 6535 | // We actually implement this paragraph where we set the semantic |
| 6536 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 6537 | // but we also maintain the lexical context where the actual |
| 6538 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6539 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6540 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6541 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6542 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6543 | Specialization->startDefinition(); |
| 6544 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6545 | if (TUK == TUK_Friend) { |
| 6546 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 6547 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6548 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6549 | /*FIXME:*/KWLoc); |
| 6550 | Friend->setAccess(AS_public); |
| 6551 | CurContext->addDecl(Friend); |
| 6552 | } else { |
| 6553 | // Add the specialization into its lexical context, so that it can |
| 6554 | // be seen when iterating through the list of declarations in that |
| 6555 | // context. However, specializations are not found by name lookup. |
| 6556 | CurContext->addDecl(Specialization); |
| 6557 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6558 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6559 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6560 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6561 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6562 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6563 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6564 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 6565 | ActOnDocumentableDecl(NewDecl); |
| 6566 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6567 | } |
| 6568 | |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6569 | /// \brief Strips various properties off an implicit instantiation |
| 6570 | /// that has just been explicitly specialized. |
| 6571 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6572 | D->dropAttr<DLLImportAttr>(); |
| 6573 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6574 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6575 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6576 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6577 | } |
| 6578 | |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6579 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 6580 | // declaration or definition. |
| 6581 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6582 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6583 | // Explicit instantiations following a specialization have no effect and |
| 6584 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 6585 | // until a valid name loc is found. |
| 6586 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6587 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 6588 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6589 | PrevDiagLoc = Prev->getLocation(); |
| 6590 | } |
| 6591 | assert(PrevDiagLoc.isValid() && |
| 6592 | "Explicit instantiation without point of instantiation?"); |
| 6593 | return PrevDiagLoc; |
| 6594 | } |
| 6595 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6596 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6597 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6598 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6599 | /// new specialization/instantiation will have any effect. |
| 6600 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6601 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6602 | /// instantiation. |
| 6603 | /// |
| 6604 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 6605 | /// |
| 6606 | /// \param PrevDecl the previous declaration of the entity. |
| 6607 | /// |
| 6608 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 6609 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6610 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6611 | /// declaration was instantiated (either implicitly or explicitly). |
| 6612 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6613 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6614 | /// specialization or instantiation has no effect and should be ignored. |
| 6615 | /// |
| 6616 | /// \returns true if there was an error that should prevent the introduction of |
| 6617 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6618 | bool |
| 6619 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 6620 | TemplateSpecializationKind NewTSK, |
| 6621 | NamedDecl *PrevDecl, |
| 6622 | TemplateSpecializationKind PrevTSK, |
| 6623 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6624 | bool &HasNoEffect) { |
| 6625 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6626 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6627 | switch (NewTSK) { |
| 6628 | case TSK_Undeclared: |
| 6629 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 6630 | assert( |
| 6631 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 6632 | "previous declaration must be implicit!"); |
| 6633 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6634 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6635 | case TSK_ExplicitSpecialization: |
| 6636 | switch (PrevTSK) { |
| 6637 | case TSK_Undeclared: |
| 6638 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6639 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6640 | // explicitly specialized or has merely been mentioned without any |
| 6641 | // instantiation. |
| 6642 | return false; |
| 6643 | |
| 6644 | case TSK_ImplicitInstantiation: |
| 6645 | if (PrevPointOfInstantiation.isInvalid()) { |
| 6646 | // The declaration itself has not actually been instantiated, so it is |
| 6647 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6648 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6649 | return false; |
| 6650 | } |
| 6651 | // Fall through |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6652 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6653 | case TSK_ExplicitInstantiationDeclaration: |
| 6654 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6655 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 6656 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6657 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6658 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6659 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6660 | // 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] | 6661 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6662 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6663 | // implicit instantiation to take place, in every translation unit in |
| 6664 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6665 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6666 | // Is there any previous explicit specialization declaration? |
| 6667 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 6668 | return false; |
| 6669 | } |
| 6670 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6671 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6672 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6673 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6674 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6675 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6676 | return true; |
| 6677 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6678 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6679 | case TSK_ExplicitInstantiationDeclaration: |
| 6680 | switch (PrevTSK) { |
| 6681 | case TSK_ExplicitInstantiationDeclaration: |
| 6682 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6683 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6684 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6685 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6686 | case TSK_Undeclared: |
| 6687 | case TSK_ImplicitInstantiation: |
| 6688 | // We're explicitly instantiating something that may have already been |
| 6689 | // implicitly instantiated; that's fine. |
| 6690 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6691 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6692 | case TSK_ExplicitSpecialization: |
| 6693 | // C++0x [temp.explicit]p4: |
| 6694 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6695 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6696 | // specialization for that template, the explicit instantiation has no |
| 6697 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6698 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6699 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6700 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6701 | case TSK_ExplicitInstantiationDefinition: |
| 6702 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6703 | // If an entity is the subject of both an explicit instantiation |
| 6704 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6705 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6706 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6707 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6708 | |
| 6709 | // Explicit instantiations following a specialization have no effect and |
| 6710 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 6711 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6712 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 6713 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6714 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6715 | return false; |
| 6716 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6717 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6718 | case TSK_ExplicitInstantiationDefinition: |
| 6719 | switch (PrevTSK) { |
| 6720 | case TSK_Undeclared: |
| 6721 | case TSK_ImplicitInstantiation: |
| 6722 | // We're explicitly instantiating something that may have already been |
| 6723 | // implicitly instantiated; that's fine. |
| 6724 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6725 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6726 | case TSK_ExplicitSpecialization: |
| 6727 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 6728 | // For a given set of template parameters, if an explicit |
| 6729 | // instantiation of a template appears after a declaration of |
| 6730 | // an explicit specialization for that template, the explicit |
| 6731 | // instantiation has no effect. |
| 6732 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6733 | // 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] | 6734 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6735 | // has been explicitly specialized. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6736 | Diag(NewLoc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6737 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 6738 | diag::ext_explicit_instantiation_after_specialization) |
| 6739 | << PrevDecl; |
| 6740 | Diag(PrevDecl->getLocation(), |
| 6741 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6742 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6743 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6744 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6745 | case TSK_ExplicitInstantiationDeclaration: |
| 6746 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6747 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6748 | |
| 6749 | // C++0x [temp.explicit]p4: |
| 6750 | // For a given set of template parameters, if an explicit instantiation |
| 6751 | // of a template appears after a declaration of an explicit |
| 6752 | // specialization for that template, the explicit instantiation has no |
| 6753 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6754 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6755 | // Is there any previous explicit specialization declaration? |
| 6756 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6757 | HasNoEffect = true; |
| 6758 | break; |
| 6759 | } |
| 6760 | } |
| 6761 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6762 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6763 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6764 | case TSK_ExplicitInstantiationDefinition: |
| 6765 | // C++0x [temp.spec]p5: |
| 6766 | // For a given template and a given set of template-arguments, |
| 6767 | // - an explicit instantiation definition shall appear at most once |
| 6768 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6769 | |
| 6770 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 6771 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 6772 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6773 | : diag::err_explicit_instantiation_duplicate) |
| 6774 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6775 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6776 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6777 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6778 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6779 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6780 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6781 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6782 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6783 | } |
| 6784 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6785 | /// \brief Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6786 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6787 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6788 | /// The only possible way to get a dependent function template specialization |
| 6789 | /// is with a friend declaration, like so: |
| 6790 | /// |
| 6791 | /// \code |
| 6792 | /// template \<class T> void foo(T); |
| 6793 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6794 | /// friend void foo<>(T); |
| 6795 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6796 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6797 | /// |
| 6798 | /// There really isn't any useful analysis we can do here, so we |
| 6799 | /// just store the information. |
| 6800 | bool |
| 6801 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 6802 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 6803 | LookupResult &Previous) { |
| 6804 | // Remove anything from Previous that isn't a function template in |
| 6805 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6806 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6807 | LookupResult::Filter F = Previous.makeFilter(); |
| 6808 | while (F.hasNext()) { |
| 6809 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 6810 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6811 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 6812 | D->getDeclContext()->getRedeclContext())) |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6813 | F.erase(); |
| 6814 | } |
| 6815 | F.done(); |
| 6816 | |
| 6817 | // Should this be diagnosed here? |
| 6818 | if (Previous.empty()) return true; |
| 6819 | |
| 6820 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 6821 | ExplicitTemplateArgs); |
| 6822 | return false; |
| 6823 | } |
| 6824 | |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6825 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6826 | /// specialization. |
| 6827 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6828 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6829 | /// explicit function template specialization. On successful completion, |
| 6830 | /// the function declaration \p FD will become a function template |
| 6831 | /// specialization. |
| 6832 | /// |
| 6833 | /// \param FD the function declaration, which will be updated to become a |
| 6834 | /// function template specialization. |
| 6835 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6836 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 6837 | /// if any. Note that this may be valid info even when 0 arguments are |
| 6838 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 6839 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6840 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6841 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6842 | /// this function specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6843 | bool Sema::CheckFunctionTemplateSpecialization( |
| 6844 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 6845 | LookupResult &Previous) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6846 | // The set of function template specializations that could match this |
| 6847 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6848 | UnresolvedSet<8> Candidates; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 6849 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), |
| 6850 | /*ForTakingAddress=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6851 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6852 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
| 6853 | ConvertedTemplateArgs; |
| 6854 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6855 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6856 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6857 | I != E; ++I) { |
| 6858 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 6859 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6860 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6861 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6862 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 6863 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6864 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6865 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6866 | // When matching a constexpr member function template specialization |
| 6867 | // against the primary template, we don't yet know whether the |
| 6868 | // specialization has an implicit 'const' (because we don't know whether |
| 6869 | // it will be a static member function until we know which template it |
| 6870 | // specializes), so adjust it now assuming it specializes this template. |
| 6871 | QualType FT = FD->getType(); |
| 6872 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6873 | CXXMethodDecl *OldMD = |
| 6874 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6875 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6876 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6877 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6878 | EPI.TypeQuals |= Qualifiers::Const; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6879 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6880 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6881 | } |
| 6882 | } |
| 6883 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6884 | TemplateArgumentListInfo Args; |
| 6885 | if (ExplicitTemplateArgs) |
| 6886 | Args = *ExplicitTemplateArgs; |
| 6887 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6888 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6889 | // A trailing template-argument can be left unspecified in the |
| 6890 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6891 | // provided it can be deduced from the function argument type. |
| 6892 | // Perform template argument deduction to determine whether we may be |
| 6893 | // specializing this template. |
| 6894 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6895 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6896 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 6897 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 6898 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6899 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, |
| 6900 | Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6901 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6902 | // that we can provide nifty diagnostics. |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 6903 | FailedCandidates.addCandidate().set( |
| 6904 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 6905 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6906 | (void)TDK; |
| 6907 | continue; |
| 6908 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6909 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6910 | // Record this candidate. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6911 | if (ExplicitTemplateArgs) |
| 6912 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6913 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6914 | } |
| 6915 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6916 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 6917 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6918 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 6919 | Candidates.begin(), Candidates.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6920 | FD->getLocation(), |
| 6921 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 6922 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6923 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6924 | PDiag(diag::note_function_template_spec_matched)); |
| 6925 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6926 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6927 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6928 | |
| 6929 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 6930 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 6931 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 6932 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare [...] |
| 6933 | // an explicit specialization (14.8.3) [...] of a concept definition. |
| 6934 | if (Specialization->getPrimaryTemplate()->isConcept()) { |
| 6935 | Diag(FD->getLocation(), diag::err_concept_specialized) |
| 6936 | << 0 /*function*/ << 1 /*explicitly specialized*/; |
| 6937 | Diag(Specialization->getLocation(), diag::note_previous_declaration); |
| 6938 | return true; |
| 6939 | } |
| 6940 | |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 6941 | FunctionTemplateSpecializationInfo *SpecInfo |
| 6942 | = Specialization->getTemplateSpecializationInfo(); |
| 6943 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6944 | |
| 6945 | // Note: do not overwrite location info if previous template |
| 6946 | // specialization kind was explicit. |
| 6947 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6948 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6949 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6950 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 6951 | // function can differ from the template declaration with respect to |
| 6952 | // the constexpr specifier. |
| 6953 | Specialization->setConstexpr(FD->isConstexpr()); |
| 6954 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6955 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6956 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6957 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6958 | |
| 6959 | // If this is a friend declaration, then we're not really declaring |
| 6960 | // an explicit specialization. |
| 6961 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6962 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6963 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6964 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6965 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6966 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6967 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6968 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6969 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6970 | |
| 6971 | // C++ [temp.expl.spec]p6: |
| 6972 | // 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] | 6973 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6974 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6975 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6976 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6977 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6978 | if (!isFriend && |
| 6979 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6980 | TSK_ExplicitSpecialization, |
| 6981 | Specialization, |
| 6982 | SpecInfo->getTemplateSpecializationKind(), |
| 6983 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6984 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6985 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6986 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6987 | // Mark the prior declaration as an explicit specialization, so that later |
| 6988 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6989 | if (!isFriend) { |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 6990 | // Explicit specializations do not inherit '=delete' from their primary
|
| 6991 | // function template.
|
| 6992 | if (Specialization->isDeleted()) {
|
| 6993 | assert(!SpecInfo->isExplicitSpecialization());
|
| 6994 | assert(Specialization->getCanonicalDecl() == Specialization);
|
| 6995 | Specialization->setDeletedAsWritten(false);
|
| 6996 | } |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6997 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6998 | MarkUnusedFileScopedDecl(Specialization); |
| 6999 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7000 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7001 | // Turn the given function declaration into a function template |
| 7002 | // specialization, with the template arguments from the previous |
| 7003 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 7004 | // Take copies of (semantic and syntactic) template argument lists. |
| 7005 | const TemplateArgumentList* TemplArgs = new (Context) |
| 7006 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 7007 | FD->setFunctionTemplateSpecialization( |
| 7008 | Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr, |
| 7009 | SpecInfo->getTemplateSpecializationKind(), |
| 7010 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 7011 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7012 | // The "previous declaration" for this function template specialization is |
| 7013 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7014 | Previous.clear(); |
| 7015 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 7016 | return false; |
| 7017 | } |
| 7018 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7019 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7020 | /// specialization. |
| 7021 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7022 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7023 | /// explicit member function specialization. On successful completion, |
| 7024 | /// the function declaration \p FD will become a member function |
| 7025 | /// specialization. |
| 7026 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7027 | /// \param Member the member declaration, which will be updated to become a |
| 7028 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7029 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7030 | /// \param Previous the set of declarations, one of which may be specialized |
| 7031 | /// by this function specialization; the set will be modified to contain the |
| 7032 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7033 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7034 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7035 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7036 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7037 | // Try to find the member we are instantiating. |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7038 | NamedDecl *FoundInstantiation = nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7039 | NamedDecl *Instantiation = nullptr; |
| 7040 | NamedDecl *InstantiatedFrom = nullptr; |
| 7041 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7042 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7043 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7044 | // Nowhere to look anyway. |
| 7045 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7046 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 7047 | I != E; ++I) { |
| 7048 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 7049 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 7050 | QualType Adjusted = Function->getType(); |
| 7051 | if (!hasExplicitCallingConv(Adjusted)) |
| 7052 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
| 7053 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7054 | FoundInstantiation = *I; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7055 | Instantiation = Method; |
| 7056 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7057 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7058 | break; |
| 7059 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7060 | } |
| 7061 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7062 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7063 | VarDecl *PrevVar; |
| 7064 | if (Previous.isSingleResult() && |
| 7065 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7066 | if (PrevVar->isStaticDataMember()) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7067 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7068 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7069 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7070 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7071 | } |
| 7072 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7073 | CXXRecordDecl *PrevRecord; |
| 7074 | if (Previous.isSingleResult() && |
| 7075 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7076 | FoundInstantiation = Previous.getRepresentativeDecl(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7077 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7078 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7079 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7080 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7081 | } else if (isa<EnumDecl>(Member)) { |
| 7082 | EnumDecl *PrevEnum; |
| 7083 | if (Previous.isSingleResult() && |
| 7084 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7085 | FoundInstantiation = Previous.getRepresentativeDecl(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7086 | Instantiation = PrevEnum; |
| 7087 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 7088 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 7089 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7090 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7091 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7092 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7093 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7094 | // specializations are always out-of-line, the caller will complain about |
| 7095 | // this mismatch later. |
| 7096 | return false; |
| 7097 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7098 | |
| 7099 | // If this is a friend, just bail out here before we start turning |
| 7100 | // things into explicit specializations. |
| 7101 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 7102 | // Preserve instantiation information. |
| 7103 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 7104 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 7105 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7106 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7107 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 7108 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 7109 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7110 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7111 | } |
| 7112 | |
| 7113 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7114 | Previous.addDecl(FoundInstantiation); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7115 | return false; |
| 7116 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7117 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7118 | // Make sure that this is a specialization of a member. |
| 7119 | if (!InstantiatedFrom) { |
| 7120 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 7121 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7122 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 7123 | return true; |
| 7124 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7125 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7126 | // C++ [temp.expl.spec]p6: |
| 7127 | // 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] | 7128 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7129 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7130 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7131 | // use occurs; no diagnostic is required. |
| 7132 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7133 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7134 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7135 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 7136 | TSK_ExplicitSpecialization, |
| 7137 | Instantiation, |
| 7138 | MSInfo->getTemplateSpecializationKind(), |
| 7139 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7140 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7141 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7142 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7143 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7144 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7145 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7146 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7147 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7148 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 7149 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7150 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7151 | // the original declaration to note that it is an explicit specialization |
| 7152 | // (if it was previously an implicit instantiation). This latter step |
| 7153 | // makes bookkeeping easier. |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7154 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7155 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 7156 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 7157 | TSK_ImplicitInstantiation) { |
| 7158 | InstantiationFunction->setTemplateSpecializationKind( |
| 7159 | TSK_ExplicitSpecialization); |
| 7160 | InstantiationFunction->setLocation(Member->getLocation()); |
Faisal Vali | 5e9e8ac | 2016-04-17 17:32:04 +0000 | [diff] [blame] | 7161 | // Explicit specializations of member functions of class templates do not |
| 7162 | // inherit '=delete' from the member function they are specializing. |
| 7163 | if (InstantiationFunction->isDeleted()) { |
| 7164 | assert(InstantiationFunction->getCanonicalDecl() == |
| 7165 | InstantiationFunction); |
| 7166 | InstantiationFunction->setDeletedAsWritten(false);
|
| 7167 | } |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7168 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7169 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7170 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 7171 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7172 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7173 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7174 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7175 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 7176 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 7177 | TSK_ImplicitInstantiation) { |
| 7178 | InstantiationVar->setTemplateSpecializationKind( |
| 7179 | TSK_ExplicitSpecialization); |
| 7180 | InstantiationVar->setLocation(Member->getLocation()); |
| 7181 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7182 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7183 | cast<VarDecl>(Member)->setInstantiationOfStaticDataMember( |
| 7184 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7185 | MarkUnusedFileScopedDecl(InstantiationVar); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7186 | } else if (isa<CXXRecordDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7187 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 7188 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 7189 | TSK_ImplicitInstantiation) { |
| 7190 | InstantiationClass->setTemplateSpecializationKind( |
| 7191 | TSK_ExplicitSpecialization); |
| 7192 | InstantiationClass->setLocation(Member->getLocation()); |
| 7193 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7194 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7195 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7196 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7197 | TSK_ExplicitSpecialization); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7198 | } else { |
| 7199 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); |
| 7200 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); |
| 7201 | if (InstantiationEnum->getTemplateSpecializationKind() == |
| 7202 | TSK_ImplicitInstantiation) { |
| 7203 | InstantiationEnum->setTemplateSpecializationKind( |
| 7204 | TSK_ExplicitSpecialization); |
| 7205 | InstantiationEnum->setLocation(Member->getLocation()); |
| 7206 | } |
| 7207 | |
| 7208 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( |
| 7209 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7210 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7211 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7212 | // Save the caller the trouble of having to figure out which declaration |
| 7213 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7214 | Previous.clear(); |
Richard Smith | 22e7cc6 | 2016-05-24 00:01:49 +0000 | [diff] [blame] | 7215 | Previous.addDecl(FoundInstantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7216 | return false; |
| 7217 | } |
| 7218 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7219 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7220 | /// |
| 7221 | /// \returns true if a serious error occurs, false otherwise. |
| 7222 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7223 | SourceLocation InstLoc, |
| 7224 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7225 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 7226 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7227 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7228 | if (CurContext->isRecord()) { |
| 7229 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 7230 | << D; |
| 7231 | return true; |
| 7232 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7233 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7234 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7235 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7236 | // template. If the name declared in the explicit instantiation is an |
| 7237 | // unqualified name, the explicit instantiation shall appear in the |
| 7238 | // namespace where its template is declared or, if that namespace is inline |
| 7239 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7240 | // |
| 7241 | // 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] | 7242 | if (WasQualifiedName) { |
| 7243 | if (CurContext->Encloses(OrigContext)) |
| 7244 | return false; |
| 7245 | } else { |
| 7246 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 7247 | return false; |
| 7248 | } |
| 7249 | |
| 7250 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 7251 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7252 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7253 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7254 | diag::err_explicit_instantiation_out_of_scope : |
| 7255 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7256 | << D << NS; |
| 7257 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7258 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7259 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7260 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 7261 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 7262 | << D << NS; |
| 7263 | } else |
| 7264 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7265 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7266 | diag::err_explicit_instantiation_must_be_global : |
| 7267 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 7268 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7269 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7270 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7271 | } |
| 7272 | |
| 7273 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 7274 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 7275 | if (!SS.isSet()) |
| 7276 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7277 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7278 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7279 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7280 | // or a static data member of a class template specialization, the name of |
| 7281 | // the class template specialization in the qualified-id for the member |
| 7282 | // name shall be a simple-template-id. |
| 7283 | // |
| 7284 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7285 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 7286 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7287 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7288 | if (isa<TemplateSpecializationType>(T)) |
| 7289 | return true; |
| 7290 | |
| 7291 | return false; |
| 7292 | } |
| 7293 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7294 | // Explicit instantiation of a class template specialization |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7295 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7296 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7297 | SourceLocation ExternLoc, |
| 7298 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7299 | unsigned TagSpec, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7300 | SourceLocation KWLoc, |
| 7301 | const CXXScopeSpec &SS, |
| 7302 | TemplateTy TemplateD, |
| 7303 | SourceLocation TemplateNameLoc, |
| 7304 | SourceLocation LAngleLoc, |
| 7305 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7306 | SourceLocation RAngleLoc, |
| 7307 | AttributeList *Attr) { |
| 7308 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 7309 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7310 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7311 | // Check that the specialization uses the same tag kind as the |
| 7312 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7313 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7314 | assert(Kind != TTK_Enum && |
| 7315 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7316 | |
Richard Trieu | 265c344 | 2016-04-05 21:13:54 +0000 | [diff] [blame] | 7317 | ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(TD); |
| 7318 | |
| 7319 | if (!ClassTemplate) { |
| 7320 | unsigned ErrorKind = 0; |
| 7321 | if (isa<TypeAliasTemplateDecl>(TD)) { |
| 7322 | ErrorKind = 4; |
| 7323 | } else if (isa<TemplateTemplateParmDecl>(TD)) { |
| 7324 | ErrorKind = 5; |
| 7325 | } |
| 7326 | |
| 7327 | Diag(TemplateNameLoc, diag::err_tag_reference_non_tag) << ErrorKind; |
| 7328 | Diag(TD->getLocation(), diag::note_previous_use); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7329 | return true; |
| 7330 | } |
| 7331 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7332 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7333 | Kind, /*isDefinition*/false, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 7334 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7335 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7336 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7337 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7338 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7339 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7340 | diag::note_previous_use); |
| 7341 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7342 | } |
| 7343 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7344 | // C++0x [temp.explicit]p2: |
| 7345 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7346 | // definition and an explicit instantiation declaration. An explicit |
| 7347 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 7348 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 7349 | ? TSK_ExplicitInstantiationDefinition |
| 7350 | : TSK_ExplicitInstantiationDeclaration; |
| 7351 | |
| 7352 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 7353 | // Check for dllexport class template instantiation declarations. |
| 7354 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7355 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7356 | Diag(ExternLoc, |
| 7357 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7358 | Diag(A->getLoc(), diag::note_attribute); |
| 7359 | break; |
| 7360 | } |
| 7361 | } |
| 7362 | |
| 7363 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 7364 | Diag(ExternLoc, |
| 7365 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7366 | Diag(A->getLocation(), diag::note_attribute); |
| 7367 | } |
| 7368 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7369 | |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame^] | 7370 | // In MSVC mode, dllimported explicit instantiation definitions are treated as |
| 7371 | // instantiation declarations for most purposes. |
| 7372 | bool DLLImportExplicitInstantiationDef = false; |
| 7373 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 7374 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 7375 | // Check for dllimport class template instantiation definitions. |
| 7376 | bool DLLImport = |
| 7377 | ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>(); |
| 7378 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7379 | if (A->getKind() == AttributeList::AT_DLLImport) |
| 7380 | DLLImport = true; |
| 7381 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7382 | // dllexport trumps dllimport here. |
| 7383 | DLLImport = false; |
| 7384 | break; |
| 7385 | } |
| 7386 | } |
| 7387 | if (DLLImport) { |
| 7388 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 7389 | DLLImportExplicitInstantiationDef = true; |
| 7390 | } |
| 7391 | } |
| 7392 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7393 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7394 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 7395 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7396 | |
| 7397 | // Check that the template argument list is well-formed for this |
| 7398 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7399 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7400 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7401 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7402 | return true; |
| 7403 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7404 | // Find the class template specialization declaration that |
| 7405 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7406 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7407 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7408 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7409 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7410 | TemplateSpecializationKind PrevDecl_TSK |
| 7411 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 7412 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7413 | // C++0x [temp.explicit]p2: |
| 7414 | // [...] An explicit instantiation shall appear in an enclosing |
| 7415 | // namespace of its template. [...] |
| 7416 | // |
| 7417 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7418 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 7419 | SS.isSet())) |
| 7420 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7421 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7422 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7423 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7424 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7425 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7426 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7427 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7428 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7429 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7430 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7431 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7432 | // Even though HasNoEffect == true means that this explicit instantiation |
| 7433 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 7434 | |
| 7435 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 7436 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7437 | // Since the only prior class template specialization with these |
| 7438 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7439 | // declaration node as our own, updating the source location |
| 7440 | // for the template name to reflect our new declaration. |
| 7441 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7442 | Specialization = PrevDecl; |
| 7443 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7444 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7445 | } |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame^] | 7446 | |
| 7447 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
| 7448 | DLLImportExplicitInstantiationDef) { |
| 7449 | // The new specialization might add a dllimport attribute. |
| 7450 | HasNoEffect = false; |
| 7451 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7452 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7453 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7454 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7455 | // Create a new class template specialization declaration node for |
| 7456 | // this explicit specialization. |
| 7457 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7458 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7459 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7460 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7461 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7462 | Converted.data(), |
| 7463 | Converted.size(), |
| 7464 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7465 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7466 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7467 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7468 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7469 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7470 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7471 | } |
| 7472 | |
| 7473 | // Build the fully-sugared type for this explicit instantiation as |
| 7474 | // the user wrote in the explicit instantiation itself. This means |
| 7475 | // that we'll pretty-print the type retrieved from the |
| 7476 | // specialization's declaration the way that the user actually wrote |
| 7477 | // the explicit instantiation, rather than formatting the name based |
| 7478 | // on the "canonical" representation used to store the template |
| 7479 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7480 | TypeSourceInfo *WrittenTy |
| 7481 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7482 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7483 | Context.getTypeDeclType(Specialization)); |
| 7484 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7485 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7486 | // Set source locations for keywords. |
| 7487 | Specialization->setExternLoc(ExternLoc); |
| 7488 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | 40bcfd7 | 2013-04-22 23:23:42 +0000 | [diff] [blame] | 7489 | Specialization->setRBraceLoc(SourceLocation()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7490 | |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 7491 | if (Attr) |
| 7492 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 7493 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7494 | // Add the explicit instantiation into its lexical context. However, |
| 7495 | // since explicit instantiations are never found by name lookup, we |
| 7496 | // just put it into the declaration context directly. |
| 7497 | Specialization->setLexicalDeclContext(CurContext); |
| 7498 | CurContext->addDecl(Specialization); |
| 7499 | |
| 7500 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 7501 | if (HasNoEffect) { |
| 7502 | // Set the template specialization kind. |
| 7503 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7504 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 7505 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7506 | |
| 7507 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7508 | // A definition of a class template or class member template |
| 7509 | // shall be in scope at the point of the explicit instantiation of |
| 7510 | // the class template or class member template. |
| 7511 | // |
| 7512 | // This check comes when we actually try to perform the |
| 7513 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7514 | ClassTemplateSpecializationDecl *Def |
| 7515 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7516 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7517 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7518 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7519 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7520 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7521 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 7522 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7523 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7524 | // Instantiate the members of this class template specialization. |
| 7525 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7526 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7527 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7528 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7529 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 7530 | // TSK_ExplicitInstantiationDefinition |
| 7531 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
Hans Wennborg | a86a83b | 2016-05-26 19:42:56 +0000 | [diff] [blame^] | 7532 | (TSK == TSK_ExplicitInstantiationDefinition || |
| 7533 | DLLImportExplicitInstantiationDef)) { |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7534 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7535 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7536 | |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 7537 | if (!getDLLAttr(Def) && getDLLAttr(Specialization) && |
| 7538 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 7539 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 7540 | // attribute to a template with a previous instantiation declaration. |
| 7541 | // MinGW doesn't allow this. |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7542 | auto *A = cast<InheritableAttr>( |
| 7543 | getDLLAttr(Specialization)->clone(getASTContext())); |
| 7544 | A->setInherited(true); |
| 7545 | Def->addAttr(A); |
Reid Kleckner | 5b64034 | 2016-02-26 19:51:02 +0000 | [diff] [blame] | 7546 | |
| 7547 | // We reject explicit instantiations in class scope, so there should |
| 7548 | // never be any delayed exported classes to worry about. |
| 7549 | assert(DelayedDllExportClasses.empty() && |
| 7550 | "delayed exports present at explicit instantiation"); |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7551 | checkClassLevelDLLAttribute(Def); |
Reid Kleckner | 5b64034 | 2016-02-26 19:51:02 +0000 | [diff] [blame] | 7552 | referenceDLLExportedClassMethods(); |
Hans Wennborg | fce87ca | 2015-06-09 00:39:09 +0000 | [diff] [blame] | 7553 | |
| 7554 | // Propagate attribute to base class templates. |
| 7555 | for (auto &B : Def->bases()) { |
| 7556 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 7557 | B.getType()->getAsCXXRecordDecl())) |
| 7558 | propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getLocStart()); |
| 7559 | } |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7560 | } |
| 7561 | } |
| 7562 | |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 7563 | // Set the template specialization kind. Make sure it is set before |
| 7564 | // instantiating the members which will trigger ASTConsumer callbacks. |
| 7565 | Specialization->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7566 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 7567 | } else { |
| 7568 | |
| 7569 | // Set the template specialization kind. |
| 7570 | Specialization->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7571 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7572 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7573 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7574 | } |
| 7575 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7576 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7577 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7578 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7579 | SourceLocation ExternLoc, |
| 7580 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7581 | unsigned TagSpec, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7582 | SourceLocation KWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7583 | CXXScopeSpec &SS, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7584 | IdentifierInfo *Name, |
| 7585 | SourceLocation NameLoc, |
| 7586 | AttributeList *Attr) { |
| 7587 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 7588 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7589 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7590 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7591 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7592 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7593 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 7594 | SourceLocation(), false, TypeResult(), |
| 7595 | /*IsTypeSpecifier*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7596 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 7597 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7598 | if (!TagD) |
| 7599 | return true; |
| 7600 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7601 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7602 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7603 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 7604 | if (Tag->isInvalidDecl()) |
| 7605 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7606 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7607 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 7608 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 7609 | if (!Pattern) { |
| 7610 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 7611 | << Context.getTypeDeclType(Record); |
| 7612 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 7613 | return true; |
| 7614 | } |
| 7615 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7616 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7617 | // If the explicit instantiation is for a class or member class, the |
| 7618 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7619 | // simple-template-id. |
| 7620 | // |
| 7621 | // C++98 has the same restriction, just worded differently. |
| 7622 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7623 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7624 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7625 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7626 | // C++0x [temp.explicit]p2: |
| 7627 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7628 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7629 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 7630 | TemplateSpecializationKind TSK |
| 7631 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7632 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7633 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7634 | // C++0x [temp.explicit]p2: |
| 7635 | // [...] An explicit instantiation shall appear in an enclosing |
| 7636 | // namespace of its template. [...] |
| 7637 | // |
| 7638 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7639 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7640 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7641 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7642 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7643 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7644 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7645 | PrevDecl = Record; |
| 7646 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7647 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7648 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7649 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7650 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7651 | PrevDecl, |
| 7652 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7653 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7654 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7655 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7656 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7657 | return TagD; |
| 7658 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7659 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7660 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7661 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7662 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7663 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7664 | // 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] | 7665 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7666 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7667 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7668 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 7669 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 7670 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7671 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 7672 | << Pattern; |
| 7673 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7674 | } else { |
| 7675 | if (InstantiateClass(NameLoc, Record, Def, |
| 7676 | getTemplateInstantiationArgs(Record), |
| 7677 | TSK)) |
| 7678 | return true; |
| 7679 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7680 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7681 | if (!RecordDef) |
| 7682 | return true; |
| 7683 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7684 | } |
| 7685 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7686 | // Instantiate all of the members of the class. |
| 7687 | InstantiateClassMembers(NameLoc, RecordDef, |
| 7688 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7689 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7690 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7691 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 7692 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7693 | // FIXME: We don't have any representation for explicit instantiations of |
| 7694 | // member classes. Such a representation is not needed for compilation, but it |
| 7695 | // should be available for clients that want to see all of the declarations in |
| 7696 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7697 | return TagD; |
| 7698 | } |
| 7699 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7700 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 7701 | SourceLocation ExternLoc, |
| 7702 | SourceLocation TemplateLoc, |
| 7703 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7704 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7705 | // TODO: check if/when DNInfo should replace Name. |
| 7706 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 7707 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7708 | if (!Name) { |
| 7709 | if (!D.isInvalidType()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7710 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7711 | diag::err_explicit_instantiation_requires_name) |
| 7712 | << D.getDeclSpec().getSourceRange() |
| 7713 | << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7714 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7715 | return true; |
| 7716 | } |
| 7717 | |
| 7718 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 7719 | // we find one that is. |
| 7720 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 7721 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 7722 | S = S->getParent(); |
| 7723 | |
| 7724 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7725 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 7726 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7727 | if (R.isNull()) |
| 7728 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7729 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7730 | // C++ [dcl.stc]p1: |
| 7731 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 7732 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7733 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7734 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 7735 | << Name; |
| 7736 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7737 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 7738 | != DeclSpec::SCS_unspecified) { |
| 7739 | // Complain about then remove the storage class specifier. |
| 7740 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 7741 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 7742 | |
| 7743 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7744 | } |
| 7745 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 7746 | // C++0x [temp.explicit]p1: |
| 7747 | // [...] An explicit instantiation of a function template shall not use the |
| 7748 | // inline or constexpr specifiers. |
| 7749 | // Presumably, this also applies to member functions of class templates as |
| 7750 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7751 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7752 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7753 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7754 | diag::err_explicit_instantiation_inline : |
| 7755 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7756 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7757 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7758 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 7759 | // not already specified. |
| 7760 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 7761 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7762 | |
Nathan Wilson | de49845 | 2016-02-08 05:34:00 +0000 | [diff] [blame] | 7763 | // C++ Concepts TS [dcl.spec.concept]p1: The concept specifier shall be |
| 7764 | // applied only to the definition of a function template or variable template, |
| 7765 | // declared in namespace scope. |
| 7766 | if (D.getDeclSpec().isConceptSpecified()) { |
| 7767 | Diag(D.getDeclSpec().getConceptSpecLoc(), |
| 7768 | diag::err_concept_specified_specialization) << 0; |
| 7769 | return true; |
| 7770 | } |
| 7771 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7772 | // C++0x [temp.explicit]p2: |
| 7773 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7774 | // definition and an explicit instantiation declaration. An explicit |
| 7775 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7776 | TemplateSpecializationKind TSK |
| 7777 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7778 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7779 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7780 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7781 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7782 | |
| 7783 | if (!R->isFunctionType()) { |
| 7784 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7785 | // A [...] static data member of a class template can be explicitly |
| 7786 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7787 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7788 | // C++1y [temp.explicit]p1: |
| 7789 | // A [...] variable [...] template specialization can be explicitly |
| 7790 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7791 | if (Previous.isAmbiguous()) |
| 7792 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7793 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 7794 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7795 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7796 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7797 | if (!PrevTemplate) { |
| 7798 | if (!Prev || !Prev->isStaticDataMember()) { |
| 7799 | // We expect to see a data data member here. |
| 7800 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 7801 | << Name; |
| 7802 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7803 | P != PEnd; ++P) |
| 7804 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 7805 | return true; |
| 7806 | } |
| 7807 | |
| 7808 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 7809 | // FIXME: Check for explicit specialization? |
| 7810 | Diag(D.getIdentifierLoc(), |
| 7811 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 7812 | << Prev; |
| 7813 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 7814 | // FIXME: Can we provide a note showing where this was declared? |
| 7815 | return true; |
| 7816 | } |
| 7817 | } else { |
| 7818 | // Explicitly instantiate a variable template. |
| 7819 | |
| 7820 | // C++1y [dcl.spec.auto]p6: |
| 7821 | // ... A program that uses auto or decltype(auto) in a context not |
| 7822 | // explicitly allowed in this section is ill-formed. |
| 7823 | // |
| 7824 | // This includes auto-typed variable template instantiations. |
| 7825 | if (R->isUndeducedType()) { |
| 7826 | Diag(T->getTypeLoc().getLocStart(), |
| 7827 | diag::err_auto_not_allowed_var_inst); |
| 7828 | return true; |
| 7829 | } |
| 7830 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7831 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 7832 | // C++1y [temp.explicit]p3: |
| 7833 | // If the explicit instantiation is for a variable, the unqualified-id |
| 7834 | // in the declaration shall be a template-id. |
| 7835 | Diag(D.getIdentifierLoc(), |
| 7836 | diag::err_explicit_instantiation_without_template_id) |
| 7837 | << PrevTemplate; |
| 7838 | Diag(PrevTemplate->getLocation(), |
| 7839 | diag::note_explicit_instantiation_here); |
| 7840 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7841 | } |
| 7842 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 7843 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
| 7844 | // explicit instantiation (14.8.2) [...] of a concept definition. |
| 7845 | if (PrevTemplate->isConcept()) { |
| 7846 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
| 7847 | << 1 /*variable*/ << 0 /*explicitly instantiated*/; |
| 7848 | Diag(PrevTemplate->getLocation(), diag::note_previous_declaration); |
| 7849 | return true; |
| 7850 | } |
| 7851 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7852 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7853 | TemplateArgumentListInfo TemplateArgs = |
| 7854 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7855 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7856 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 7857 | D.getIdentifierLoc(), TemplateArgs); |
| 7858 | if (Res.isInvalid()) |
| 7859 | return true; |
| 7860 | |
| 7861 | // Ignore access control bits, we don't need them for redeclaration |
| 7862 | // checking. |
| 7863 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7864 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7865 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7866 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7867 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7868 | // or a static data member of a class template specialization, the name of |
| 7869 | // the class template specialization in the qualified-id for the member |
| 7870 | // name shall be a simple-template-id. |
| 7871 | // |
| 7872 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7873 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 7874 | // This does not apply to variable template specializations, where the |
| 7875 | // template-id is in the unqualified-id instead. |
| 7876 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7877 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7878 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7879 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7880 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7881 | // Check the scope of this explicit instantiation. |
| 7882 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7883 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7884 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 7885 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 7886 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7887 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7888 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7889 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7890 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7891 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7892 | if (!HasNoEffect) { |
| 7893 | // Instantiate static data member or variable template. |
| 7894 | |
| 7895 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 7896 | if (PrevTemplate) { |
| 7897 | // Merge attributes. |
| 7898 | if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList()) |
| 7899 | ProcessDeclAttributeList(S, Prev, Attr); |
| 7900 | } |
| 7901 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7902 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 7903 | } |
| 7904 | |
| 7905 | // Check the new variable specialization against the parsed input. |
| 7906 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
| 7907 | Diag(T->getTypeLoc().getLocStart(), |
| 7908 | diag::err_invalid_var_template_spec_type) |
| 7909 | << 0 << PrevTemplate << R << Prev->getType(); |
| 7910 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 7911 | << 2 << PrevTemplate->getDeclName(); |
| 7912 | return true; |
| 7913 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7914 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7915 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7916 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7917 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7918 | |
| 7919 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 7920 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7921 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7922 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7923 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7924 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7925 | HasExplicitTemplateArgs = true; |
| 7926 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7927 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7928 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7929 | // A [...] function [...] can be explicitly instantiated from its template. |
| 7930 | // A member function [...] of a class template can be explicitly |
| 7931 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7932 | // template. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7933 | UnresolvedSet<8> Matches; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7934 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7935 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7936 | P != PEnd; ++P) { |
| 7937 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7938 | if (!HasExplicitTemplateArgs) { |
| 7939 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 7940 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType()); |
| 7941 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7942 | Matches.clear(); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7943 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7944 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7945 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 7946 | break; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7947 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7948 | } |
| 7949 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7950 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7951 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 7952 | if (!FunTmpl) |
| 7953 | continue; |
| 7954 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7955 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7956 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7957 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7958 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7959 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 7960 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7961 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7962 | // Keep track of almost-matches. |
| 7963 | FailedCandidates.addCandidate() |
Richard Smith | c2bebe9 | 2016-05-11 20:37:46 +0000 | [diff] [blame] | 7964 | .set(P.getPair(), FunTmpl->getTemplatedDecl(), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7965 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7966 | (void)TDK; |
| 7967 | continue; |
| 7968 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7969 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7970 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7971 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7972 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7973 | // Find the most specialized function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7974 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 7975 | Matches.begin(), Matches.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7976 | D.getIdentifierLoc(), |
| 7977 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 7978 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 7979 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7980 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7981 | if (Result == Matches.end()) |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7982 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7983 | |
| 7984 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 7985 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7986 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 7987 | // C++11 [except.spec]p4 |
| 7988 | // In an explicit instantiation an exception-specification may be specified, |
| 7989 | // but is not required. |
| 7990 | // If an exception-specification is specified in an explicit instantiation |
| 7991 | // directive, it shall be compatible with the exception-specifications of |
| 7992 | // other declarations of that function. |
| 7993 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 7994 | if (FPT->hasExceptionSpec()) { |
| 7995 | unsigned DiagID = |
| 7996 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 7997 | if (getLangOpts().MicrosoftExt) |
| 7998 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 7999 | bool Result = CheckEquivalentExceptionSpec( |
| 8000 | PDiag(DiagID) << Specialization->getType(), |
| 8001 | PDiag(diag::note_explicit_instantiation_here), |
| 8002 | Specialization->getType()->getAs<FunctionProtoType>(), |
| 8003 | Specialization->getLocation(), FPT, D.getLocStart()); |
| 8004 | // In Microsoft mode, mismatching exception specifications just cause a |
| 8005 | // warning. |
| 8006 | if (!getLangOpts().MicrosoftExt && Result) |
| 8007 | return true; |
| 8008 | } |
| 8009 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8010 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8011 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8012 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 8013 | << Specialization |
| 8014 | << (Specialization->getTemplateSpecializationKind() == |
| 8015 | TSK_ExplicitSpecialization); |
| 8016 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 8017 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8018 | } |
| 8019 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 8020 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 8021 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 8022 | PrevDecl = Specialization; |
| 8023 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8024 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8025 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 8026 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8027 | PrevDecl, |
| 8028 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8029 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8030 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8031 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8032 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8033 | // FIXME: We may still want to build some representation of this |
| 8034 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 8035 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8036 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8037 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 8038 | |
| 8039 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 2aa7acf | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 8040 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 8041 | if (Attr) |
| 8042 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8043 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 8044 | if (Specialization->isDefined()) { |
| 8045 | // Let the ASTConsumer know that this function has been explicitly |
| 8046 | // instantiated now, and its linkage might have changed. |
| 8047 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 8048 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 8049 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8050 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8051 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8052 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8053 | // or a static data member of a class template specialization, the name of |
| 8054 | // the class template specialization in the qualified-id for the member |
| 8055 | // name shall be a simple-template-id. |
| 8056 | // |
| 8057 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 8058 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8059 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8060 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8061 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8062 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 8063 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8064 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8065 | |
Nathan Wilson | 8383912 | 2016-04-09 02:55:27 +0000 | [diff] [blame] | 8066 | // C++ Concepts TS [dcl.spec.concept]p7: A program shall not declare an |
| 8067 | // explicit instantiation (14.8.2) [...] of a concept definition. |
| 8068 | if (FunTmpl && FunTmpl->isConcept() && |
| 8069 | !D.getDeclSpec().isConceptSpecified()) { |
| 8070 | Diag(D.getIdentifierLoc(), diag::err_concept_specialized) |
| 8071 | << 0 /*function*/ << 0 /*explicitly instantiated*/; |
| 8072 | Diag(FunTmpl->getLocation(), diag::note_previous_declaration); |
| 8073 | return true; |
| 8074 | } |
| 8075 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8076 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8077 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8078 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8079 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 8080 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8081 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8082 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8083 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 8084 | } |
| 8085 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8086 | TypeResult |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8087 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 8088 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 8089 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 8090 | // This has to hold, because SS is expected to be defined. |
| 8091 | assert(Name && "Expected a name in a dependent tag"); |
| 8092 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8093 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8094 | if (!NNS) |
| 8095 | return true; |
| 8096 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8097 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 8098 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 8099 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 8100 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8101 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 8102 | return true; |
| 8103 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8104 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8105 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8106 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8107 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 8108 | |
| 8109 | // Create type-source location information for this type. |
| 8110 | TypeLocBuilder TLB; |
| 8111 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8112 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8113 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 8114 | TL.setNameLoc(NameLoc); |
| 8115 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8116 | } |
| 8117 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8118 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8119 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 8120 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 8121 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8122 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8123 | return true; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8124 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8125 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8126 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8127 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8128 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8129 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8130 | << FixItHint::CreateRemoval(TypenameLoc); |
| 8131 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8132 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8133 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 8134 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 8135 | if (T.isNull()) |
| 8136 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8137 | |
| 8138 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 8139 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8140 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8141 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8142 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8143 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8144 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8145 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8146 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8147 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8148 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8149 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8150 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8151 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8152 | } |
| 8153 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8154 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8155 | Sema::ActOnTypenameType(Scope *S, |
| 8156 | SourceLocation TypenameLoc, |
| 8157 | const CXXScopeSpec &SS, |
| 8158 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8159 | TemplateTy TemplateIn, |
| 8160 | SourceLocation TemplateNameLoc, |
| 8161 | SourceLocation LAngleLoc, |
| 8162 | ASTTemplateArgsPtr TemplateArgsIn, |
| 8163 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8164 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8165 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8166 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8167 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8168 | diag::ext_typename_outside_of_template) |
| 8169 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8170 | |
| 8171 | // Translate the parser's template argument list in our AST format. |
| 8172 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 8173 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 8174 | |
| 8175 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8176 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 8177 | // Construct a dependent template specialization type. |
| 8178 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8179 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8180 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 8181 | DTN->getQualifier(), |
| 8182 | DTN->getIdentifier(), |
| 8183 | TemplateArgs); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8184 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8185 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8186 | TypeLocBuilder Builder; |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8187 | DependentTemplateSpecializationTypeLoc SpecTL |
| 8188 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8189 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 8190 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 8191 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8192 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8193 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8194 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8195 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8196 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8197 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 8198 | } |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8199 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8200 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 8201 | if (T.isNull()) |
| 8202 | return true; |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8203 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8204 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8205 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8206 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8207 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8208 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 8209 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8210 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8211 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8212 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8213 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 8214 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8215 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 8216 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8217 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8218 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 8219 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8220 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 8221 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 8222 | } |
| 8223 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8224 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8225 | /// Determine whether this failed name lookup should be treated as being |
| 8226 | /// disabled by a usage of std::enable_if. |
| 8227 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
| 8228 | SourceRange &CondRange) { |
| 8229 | // We must be looking for a ::type... |
| 8230 | if (!II.isStr("type")) |
| 8231 | return false; |
| 8232 | |
| 8233 | // ... within an explicitly-written template specialization... |
| 8234 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 8235 | return false; |
| 8236 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8237 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 8238 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 8239 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8240 | return false; |
| 8241 | const TemplateSpecializationType *EnableIfTST = |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8242 | cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr()); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8243 | |
| 8244 | // ... which names a complete class template declaration... |
| 8245 | const TemplateDecl *EnableIfDecl = |
| 8246 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 8247 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 8248 | return false; |
| 8249 | |
| 8250 | // ... called "enable_if". |
| 8251 | const IdentifierInfo *EnableIfII = |
| 8252 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 8253 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 8254 | return false; |
| 8255 | |
| 8256 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8257 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8258 | return true; |
| 8259 | } |
| 8260 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8261 | /// \brief Build the type that describes a C++ typename specifier, |
| 8262 | /// e.g., "typename T::type". |
| 8263 | QualType |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8264 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 8265 | SourceLocation KeywordLoc, |
| 8266 | NestedNameSpecifierLoc QualifierLoc, |
| 8267 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8268 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8269 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8270 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8271 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8272 | DeclContext *Ctx = computeDeclContext(SS); |
| 8273 | if (!Ctx) { |
| 8274 | // If the nested-name-specifier is dependent and couldn't be |
| 8275 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8276 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 8277 | return Context.getDependentNameType(Keyword, |
| 8278 | QualifierLoc.getNestedNameSpecifier(), |
| 8279 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 8280 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8281 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8282 | // If the nested-name-specifier refers to the current instantiation, |
| 8283 | // the "typename" keyword itself is superfluous. In C++03, the |
| 8284 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 8285 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 8286 | // 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] | 8287 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8288 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 8289 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8290 | |
| 8291 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8292 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 8293 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8294 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8295 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8296 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8297 | case LookupResult::NotFound: { |
| 8298 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 8299 | // a more specific diagnostic. |
| 8300 | SourceRange CondRange; |
| 8301 | if (isEnableIf(QualifierLoc, II, CondRange)) { |
| 8302 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
| 8303 | << Ctx << CondRange; |
| 8304 | return QualType(); |
| 8305 | } |
| 8306 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 8307 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8308 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8309 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8310 | |
| 8311 | case LookupResult::FoundUnresolvedValue: { |
| 8312 | // We found a using declaration that is a value. Most likely, the using |
| 8313 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8314 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8315 | IILoc); |
| 8316 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 8317 | << Name << Ctx << FullRange; |
| 8318 | if (UnresolvedUsingValueDecl *Using |
| 8319 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8320 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8321 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 8322 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 8323 | } |
| 8324 | } |
| 8325 | // Fall through to create a dependent typename type, from which we can recover |
| 8326 | // better. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8327 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 8328 | case LookupResult::NotFoundInCurrentInstantiation: |
| 8329 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8330 | return Context.getDependentNameType(Keyword, |
| 8331 | QualifierLoc.getNestedNameSpecifier(), |
| 8332 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8333 | |
| 8334 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8335 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8336 | // We found a type. Build an ElaboratedType, since the |
| 8337 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 8338 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8339 | return Context.getElaboratedType(ETK_Typename, |
| 8340 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8341 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8342 | } |
| 8343 | |
| 8344 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 8345 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8346 | break; |
| 8347 | |
| 8348 | case LookupResult::FoundOverloaded: |
| 8349 | DiagID = diag::err_typename_nested_not_type; |
| 8350 | Referenced = *Result.begin(); |
| 8351 | break; |
| 8352 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 8353 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8354 | return QualType(); |
| 8355 | } |
| 8356 | |
| 8357 | // If we get here, it's because name lookup did not find a |
| 8358 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8359 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8360 | IILoc); |
| 8361 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8362 | if (Referenced) |
| 8363 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 8364 | << Name; |
| 8365 | return QualType(); |
| 8366 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8367 | |
| 8368 | namespace { |
| 8369 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 8370 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8371 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8372 | SourceLocation Loc; |
| 8373 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8374 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8375 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 8376 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8377 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8378 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8379 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8380 | DeclarationName Entity) |
| 8381 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8382 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8383 | |
| 8384 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8385 | /// transformed. |
| 8386 | /// |
| 8387 | /// For the purposes of type reconstruction, a type has already been |
| 8388 | /// transformed if it is NULL or if it is not dependent. |
| 8389 | bool AlreadyTransformed(QualType T) { |
| 8390 | return T.isNull() || !T->isDependentType(); |
| 8391 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8392 | |
| 8393 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8394 | /// rebuilt. |
| 8395 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8396 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8397 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 8398 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8399 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 8400 | /// \brief Sets the "base" location and entity when that |
| 8401 | /// information is known based on another transformation. |
| 8402 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 8403 | this->Loc = Loc; |
| 8404 | this->Entity = Entity; |
| 8405 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 8406 | |
| 8407 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 8408 | // Lambdas never need to be transformed. |
| 8409 | return E; |
| 8410 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8411 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 8412 | } // end anonymous namespace |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8413 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8414 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 8415 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8416 | /// 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] | 8417 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8418 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8419 | /// partial specialization thereof). This routine will rebuild that type now |
| 8420 | /// that we have entered the declarator's scope, which may produce different |
| 8421 | /// canonical types, e.g., |
| 8422 | /// |
| 8423 | /// \code |
| 8424 | /// template<typename T> |
| 8425 | /// struct X { |
| 8426 | /// typedef T* pointer; |
| 8427 | /// pointer data(); |
| 8428 | /// }; |
| 8429 | /// |
| 8430 | /// template<typename T> |
| 8431 | /// typename X<T>::pointer X<T>::data() { ... } |
| 8432 | /// \endcode |
| 8433 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 8434 | /// 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] | 8435 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 8436 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8437 | /// 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] | 8438 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 8439 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8440 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 8441 | SourceLocation Loc, |
| 8442 | DeclarationName Name) { |
| 8443 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8444 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8445 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8446 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 8447 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 8448 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8449 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8450 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8451 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 8452 | DeclarationName()); |
| 8453 | return Rebuilder.TransformExpr(E); |
| 8454 | } |
| 8455 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8456 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8457 | if (SS.isInvalid()) |
| 8458 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8459 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8460 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8461 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 8462 | DeclarationName()); |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8463 | NestedNameSpecifierLoc Rebuilt |
| 8464 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 8465 | if (!Rebuilt) |
| 8466 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8467 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8468 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8469 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8470 | } |
| 8471 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8472 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 8473 | /// instantiation. |
| 8474 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 8475 | TemplateParameterList *Params) { |
| 8476 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 8477 | Decl *Param = Params->getParam(I); |
| 8478 | |
| 8479 | // There is nothing to rebuild in a type parameter. |
| 8480 | if (isa<TemplateTypeParmDecl>(Param)) |
| 8481 | continue; |
| 8482 | |
| 8483 | // Rebuild the template parameter list of a template template parameter. |
| 8484 | if (TemplateTemplateParmDecl *TTP |
| 8485 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 8486 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 8487 | TTP->getTemplateParameters())) |
| 8488 | return true; |
| 8489 | |
| 8490 | continue; |
| 8491 | } |
| 8492 | |
| 8493 | // Rebuild the type of a non-type template parameter. |
| 8494 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 8495 | TypeSourceInfo *NewTSI |
| 8496 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 8497 | NTTP->getLocation(), |
| 8498 | NTTP->getDeclName()); |
| 8499 | if (!NewTSI) |
| 8500 | return true; |
| 8501 | |
| 8502 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 8503 | NTTP->setTypeSourceInfo(NewTSI); |
| 8504 | NTTP->setType(NewTSI->getType()); |
| 8505 | } |
| 8506 | } |
| 8507 | |
| 8508 | return false; |
| 8509 | } |
| 8510 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8511 | /// \brief Produces a formatted string that describes the binding of |
| 8512 | /// template parameters to template arguments. |
| 8513 | std::string |
| 8514 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8515 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8516 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8517 | } |
| 8518 | |
| 8519 | std::string |
| 8520 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8521 | const TemplateArgument *Args, |
| 8522 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8523 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8524 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8525 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8526 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8527 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8528 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8529 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8530 | if (I >= NumArgs) |
| 8531 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8532 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8533 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8534 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8535 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8536 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8537 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8538 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8539 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8540 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8541 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8542 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8543 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8544 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 8545 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8546 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8547 | |
| 8548 | Out << ']'; |
| 8549 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8550 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8551 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8552 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 8553 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8554 | if (!FD) |
| 8555 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8556 | |
| 8557 | LateParsedTemplate *LPT = new LateParsedTemplate; |
| 8558 | |
| 8559 | // Take tokens to avoid allocations |
| 8560 | LPT->Toks.swap(Toks); |
| 8561 | LPT->D = FnD; |
Chandler Carruth | 52cee4d | 2015-03-26 09:08:15 +0000 | [diff] [blame] | 8562 | LateParsedTemplateMap.insert(std::make_pair(FD, LPT)); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8563 | |
| 8564 | FD->setLateTemplateParsed(true); |
| 8565 | } |
| 8566 | |
| 8567 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 8568 | if (!FD) |
| 8569 | return; |
| 8570 | FD->setLateTemplateParsed(false); |
| 8571 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8572 | |
| 8573 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 8574 | DeclContext *DC = CurContext; |
| 8575 | |
| 8576 | while (DC) { |
| 8577 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 8578 | const FunctionDecl *FD = RD->isLocalClass(); |
| 8579 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 8580 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 8581 | return false; |
| 8582 | |
| 8583 | DC = DC->getParent(); |
| 8584 | } |
| 8585 | return false; |
| 8586 | } |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 8587 | |
| 8588 | /// \brief Walk the path from which a declaration was instantiated, and check |
| 8589 | /// that every explicit specialization along that path is visible. This enforces |
| 8590 | /// C++ [temp.expl.spec]/6: |
| 8591 | /// |
| 8592 | /// If a template, a member template or a member of a class template is |
| 8593 | /// explicitly specialized then that specialization shall be declared before |
| 8594 | /// the first use of that specialization that would cause an implicit |
| 8595 | /// instantiation to take place, in every translation unit in which such a |
| 8596 | /// use occurs; no diagnostic is required. |
| 8597 | /// |
| 8598 | /// and also C++ [temp.class.spec]/1: |
| 8599 | /// |
| 8600 | /// A partial specialization shall be declared before the first use of a |
| 8601 | /// class template specialization that would make use of the partial |
| 8602 | /// specialization as the result of an implicit or explicit instantiation |
| 8603 | /// in every translation unit in which such a use occurs; no diagnostic is |
| 8604 | /// required. |
| 8605 | class ExplicitSpecializationVisibilityChecker { |
| 8606 | Sema &S; |
| 8607 | SourceLocation Loc; |
| 8608 | llvm::SmallVector<Module *, 8> Modules; |
| 8609 | |
| 8610 | public: |
| 8611 | ExplicitSpecializationVisibilityChecker(Sema &S, SourceLocation Loc) |
| 8612 | : S(S), Loc(Loc) {} |
| 8613 | |
| 8614 | void check(NamedDecl *ND) { |
| 8615 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 8616 | return checkImpl(FD); |
| 8617 | if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) |
| 8618 | return checkImpl(RD); |
| 8619 | if (auto *VD = dyn_cast<VarDecl>(ND)) |
| 8620 | return checkImpl(VD); |
| 8621 | if (auto *ED = dyn_cast<EnumDecl>(ND)) |
| 8622 | return checkImpl(ED); |
| 8623 | } |
| 8624 | |
| 8625 | private: |
| 8626 | void diagnose(NamedDecl *D, bool IsPartialSpec) { |
| 8627 | auto Kind = IsPartialSpec ? Sema::MissingImportKind::PartialSpecialization |
| 8628 | : Sema::MissingImportKind::ExplicitSpecialization; |
| 8629 | const bool Recover = true; |
| 8630 | |
| 8631 | // If we got a custom set of modules (because only a subset of the |
| 8632 | // declarations are interesting), use them, otherwise let |
| 8633 | // diagnoseMissingImport intelligently pick some. |
| 8634 | if (Modules.empty()) |
| 8635 | S.diagnoseMissingImport(Loc, D, Kind, Recover); |
| 8636 | else |
| 8637 | S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover); |
| 8638 | } |
| 8639 | |
| 8640 | // Check a specific declaration. There are three problematic cases: |
| 8641 | // |
| 8642 | // 1) The declaration is an explicit specialization of a template |
| 8643 | // specialization. |
| 8644 | // 2) The declaration is an explicit specialization of a member of an |
| 8645 | // templated class. |
| 8646 | // 3) The declaration is an instantiation of a template, and that template |
| 8647 | // is an explicit specialization of a member of a templated class. |
| 8648 | // |
| 8649 | // We don't need to go any deeper than that, as the instantiation of the |
| 8650 | // surrounding class / etc is not triggered by whatever triggered this |
| 8651 | // instantiation, and thus should be checked elsewhere. |
| 8652 | template<typename SpecDecl> |
| 8653 | void checkImpl(SpecDecl *Spec) { |
| 8654 | bool IsHiddenExplicitSpecialization = false; |
| 8655 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
| 8656 | IsHiddenExplicitSpecialization = |
| 8657 | Spec->getMemberSpecializationInfo() |
| 8658 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) |
| 8659 | : !S.hasVisibleDeclaration(Spec); |
| 8660 | } else { |
| 8661 | checkInstantiated(Spec); |
| 8662 | } |
| 8663 | |
| 8664 | if (IsHiddenExplicitSpecialization) |
| 8665 | diagnose(Spec->getMostRecentDecl(), false); |
| 8666 | } |
| 8667 | |
| 8668 | void checkInstantiated(FunctionDecl *FD) { |
| 8669 | if (auto *TD = FD->getPrimaryTemplate()) |
| 8670 | checkTemplate(TD); |
| 8671 | } |
| 8672 | |
| 8673 | void checkInstantiated(CXXRecordDecl *RD) { |
| 8674 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 8675 | if (!SD) |
| 8676 | return; |
| 8677 | |
| 8678 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 8679 | if (auto *TD = From.dyn_cast<ClassTemplateDecl *>()) |
| 8680 | checkTemplate(TD); |
| 8681 | else if (auto *TD = |
| 8682 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
| 8683 | if (!S.hasVisibleDeclaration(TD)) |
| 8684 | diagnose(TD, true); |
| 8685 | checkTemplate(TD); |
| 8686 | } |
| 8687 | } |
| 8688 | |
| 8689 | void checkInstantiated(VarDecl *RD) { |
| 8690 | auto *SD = dyn_cast<VarTemplateSpecializationDecl>(RD); |
| 8691 | if (!SD) |
| 8692 | return; |
| 8693 | |
| 8694 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 8695 | if (auto *TD = From.dyn_cast<VarTemplateDecl *>()) |
| 8696 | checkTemplate(TD); |
| 8697 | else if (auto *TD = |
| 8698 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 8699 | if (!S.hasVisibleDeclaration(TD)) |
| 8700 | diagnose(TD, true); |
| 8701 | checkTemplate(TD); |
| 8702 | } |
| 8703 | } |
| 8704 | |
| 8705 | void checkInstantiated(EnumDecl *FD) {} |
| 8706 | |
| 8707 | template<typename TemplDecl> |
| 8708 | void checkTemplate(TemplDecl *TD) { |
| 8709 | if (TD->isMemberSpecialization()) { |
| 8710 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) |
| 8711 | diagnose(TD->getMostRecentDecl(), false); |
| 8712 | } |
| 8713 | } |
| 8714 | }; |
| 8715 | |
| 8716 | void Sema::checkSpecializationVisibility(SourceLocation Loc, NamedDecl *Spec) { |
| 8717 | if (!getLangOpts().Modules) |
| 8718 | return; |
| 8719 | |
| 8720 | ExplicitSpecializationVisibilityChecker(*this, Loc).check(Spec); |
| 8721 | } |
| 8722 | |
| 8723 | /// \brief Check whether a template partial specialization that we've discovered |
| 8724 | /// is hidden, and produce suitable diagnostics if so. |
| 8725 | void Sema::checkPartialSpecializationVisibility(SourceLocation Loc, |
| 8726 | NamedDecl *Spec) { |
| 8727 | llvm::SmallVector<Module *, 8> Modules; |
| 8728 | if (!hasVisibleDeclaration(Spec, &Modules)) |
| 8729 | diagnoseMissingImport(Loc, Spec, Spec->getLocation(), Modules, |
| 8730 | MissingImportKind::PartialSpecialization, |
| 8731 | /*Recover*/true); |
| 8732 | } |