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()) { |
| 803 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 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 | |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 820 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 821 | /// contains the template parameters in Params/NumParams. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 822 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 823 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 824 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 826 | SourceLocation LAngleLoc, |
Craig Topper | 96225a5 | 2015-12-24 23:58:25 +0000 | [diff] [blame] | 827 | ArrayRef<Decl *> Params, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 828 | SourceLocation RAngleLoc) { |
| 829 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 830 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 831 | |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 832 | return TemplateParameterList::Create( |
| 833 | Context, TemplateLoc, LAngleLoc, |
| 834 | llvm::makeArrayRef((NamedDecl *const *)Params.data(), Params.size()), |
| 835 | RAngleLoc); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 836 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 837 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 838 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 839 | if (SS.isSet()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 840 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 841 | } |
| 842 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 843 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 844 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 845 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 846 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 847 | AttributeList *Attr, |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 848 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 849 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 850 | SourceLocation FriendLoc, |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 851 | unsigned NumOuterTemplateParamLists, |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 852 | TemplateParameterList** OuterTemplateParamLists, |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 853 | SkipBodyInfo *SkipBody) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 855 | "No template parameters"); |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 856 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 857 | bool Invalid = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 858 | |
| 859 | // Check that we can declare a template here. |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 860 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 861 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 862 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 863 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 864 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 865 | |
| 866 | // There is no such thing as an unnamed class template. |
| 867 | if (!Name) { |
| 868 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 869 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 872 | // Find any previous declaration with this name. For a friend with no |
| 873 | // scope explicitly specified, we only look for tag declarations (per |
| 874 | // C++11 [basic.lookup.elab]p2). |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 875 | DeclContext *SemanticContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 876 | LookupResult Previous(*this, Name, NameLoc, |
| 877 | (SS.isEmpty() && TUK == TUK_Friend) |
| 878 | ? LookupTagName : LookupOrdinaryName, |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 879 | ForRedeclaration); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 880 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 881 | SemanticContext = computeDeclContext(SS, true); |
| 882 | if (!SemanticContext) { |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 883 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 884 | // in the AST, and historically we have just ignored such friend |
| 885 | // class templates, so don't complain here. |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 886 | Diag(NameLoc, TUK == TUK_Friend |
| 887 | ? diag::warn_template_qualified_friend_ignored |
| 888 | : diag::err_template_qualified_declarator_no_match) |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 889 | << SS.getScopeRep() << SS.getRange(); |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 890 | return TUK != TUK_Friend; |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 891 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 892 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 893 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 894 | return true; |
| 895 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 896 | // If we're adding a template to a dependent context, we may need to |
| 897 | // rebuilding some of the types used within the template parameter list, |
| 898 | // now that we know what the current instantiation is. |
| 899 | if (SemanticContext->isDependentContext()) { |
| 900 | ContextRAII SavedContext(*this, SemanticContext); |
| 901 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 902 | Invalid = true; |
Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 903 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
| 904 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 905 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 906 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 907 | } else { |
| 908 | SemanticContext = CurContext; |
Richard Smith | 88fe69c | 2015-07-06 01:45:27 +0000 | [diff] [blame] | 909 | |
| 910 | // C++14 [class.mem]p14: |
| 911 | // If T is the name of a class, then each of the following shall have a |
| 912 | // name different from T: |
| 913 | // -- every member template of class T |
| 914 | if (TUK != TUK_Friend && |
| 915 | DiagnoseClassNameShadow(SemanticContext, |
| 916 | DeclarationNameInfo(Name, NameLoc))) |
| 917 | return true; |
| 918 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 919 | LookupName(Previous, S); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 920 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 921 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 922 | if (Previous.isAmbiguous()) |
| 923 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 924 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 925 | NamedDecl *PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 926 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 927 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 928 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 929 | // If there is a previous declaration with the same name, check |
| 930 | // whether this is a valid redeclaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 931 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 932 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 933 | |
| 934 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 935 | // class template partial specialization, or class template specialization. |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 936 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 937 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 938 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 939 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 940 | PrevClassTemplate |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 941 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 942 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 943 | PrevClassTemplate |
| 944 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 945 | ->getSpecializedTemplate(); |
| 946 | } |
| 947 | } |
| 948 | |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 949 | if (TUK == TUK_Friend) { |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 950 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 951 | // [...] When looking for a prior declaration of a class or a function |
| 952 | // 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] | 953 | // function is neither a qualified name nor a template-id, scopes outside |
| 954 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 955 | if (!SS.isSet()) { |
| 956 | DeclContext *OutermostContext = CurContext; |
| 957 | while (!OutermostContext->isFileContext()) |
| 958 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 959 | |
Richard Smith | 61e582f | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 960 | if (PrevDecl && |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 961 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 962 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 963 | SemanticContext = PrevDecl->getDeclContext(); |
| 964 | } else { |
| 965 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 966 | // context we computed is the semantic context for our new |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 967 | // declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 968 | PrevDecl = PrevClassTemplate = nullptr; |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 969 | SemanticContext = OutermostContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 970 | |
| 971 | // Check that the chosen semantic context doesn't already contain a |
| 972 | // declaration of this name as a non-tag type. |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 973 | Previous.clear(LookupOrdinaryName); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 974 | DeclContext *LookupContext = SemanticContext; |
| 975 | while (LookupContext->isTransparentContext()) |
| 976 | LookupContext = LookupContext->getLookupParent(); |
| 977 | LookupQualifiedName(Previous, LookupContext); |
| 978 | |
| 979 | if (Previous.isAmbiguous()) |
| 980 | return true; |
| 981 | |
| 982 | if (Previous.begin() != Previous.end()) |
| 983 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 984 | } |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 985 | } |
Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 986 | } else if (PrevDecl && |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 987 | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, |
| 988 | S, SS.isValid())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 989 | PrevDecl = PrevClassTemplate = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 990 | |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 991 | if (auto *Shadow = dyn_cast_or_null<UsingShadowDecl>( |
| 992 | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { |
| 993 | if (SS.isEmpty() && |
| 994 | !(PrevClassTemplate && |
| 995 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( |
| 996 | SemanticContext->getRedeclContext()))) { |
| 997 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
| 998 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 999 | diag::note_using_decl_target); |
| 1000 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
| 1001 | // Recover by ignoring the old declaration. |
| 1002 | PrevDecl = PrevClassTemplate = nullptr; |
| 1003 | } |
| 1004 | } |
| 1005 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1006 | if (PrevClassTemplate) { |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1007 | // Ensure that the template parameter lists are compatible. Skip this check |
| 1008 | // for a friend in a dependent context: the template parameter list itself |
| 1009 | // could be dependent. |
| 1010 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 1011 | !TemplateParameterListsAreEqual(TemplateParams, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1012 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 1013 | /*Complain=*/true, |
| 1014 | TPL_TemplateMatch)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1015 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1016 | |
| 1017 | // C++ [temp.class]p4: |
| 1018 | // In a redeclaration, partial specialization, explicit |
| 1019 | // specialization or explicit instantiation of a class template, |
| 1020 | // the class-key shall agree in kind with the original class |
| 1021 | // template declaration (7.1.5.3). |
| 1022 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 1023 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 1024 | TUK == TUK_Definition, KWLoc, Name)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1025 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1026 | << Name |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1027 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1028 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1029 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1032 | // Check for redefinition of this class template. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1033 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1034 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1035 | // If we have a prior definition that is not visible, treat this as |
| 1036 | // simply making that previous definition visible. |
| 1037 | NamedDecl *Hidden = nullptr; |
| 1038 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1039 | SkipBody->ShouldSkip = true; |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1040 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
| 1041 | assert(Tmpl && "original definition of a class template is not a " |
| 1042 | "class template?"); |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1043 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 1044 | makeMergedDefinitionVisible(Tmpl, KWLoc); |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1045 | return Def; |
| 1046 | } |
| 1047 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1048 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1049 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1050 | // FIXME: Would it make sense to try to "forget" the previous |
| 1051 | // definition, as part of error recovery? |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1052 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1053 | } |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1054 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1055 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 1056 | // Maybe we will complain about the shadowed template parameter. |
| 1057 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 1058 | // Just pretend that we didn't see the previous declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1059 | PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1060 | } else if (PrevDecl) { |
| 1061 | // C++ [temp]p5: |
| 1062 | // A class template shall not have the same name as any other |
| 1063 | // template, class, function, object, enumeration, enumerator, |
| 1064 | // namespace, or type in the same scope (3.3), except as specified |
| 1065 | // in (14.5.4). |
| 1066 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1067 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1068 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1071 | // Check the template parameter list of this declaration, possibly |
| 1072 | // merging in the template parameter list from the previous class |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1073 | // template declaration. Skip this check for a friend in a dependent |
| 1074 | // context, because the template parameter list might be dependent. |
| 1075 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1076 | CheckTemplateParameterList( |
| 1077 | TemplateParams, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1078 | PrevClassTemplate ? PrevClassTemplate->getTemplateParameters() |
| 1079 | : nullptr, |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1080 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1081 | SemanticContext->isDependentContext()) |
| 1082 | ? TPC_ClassTemplateMember |
| 1083 | : TUK == TUK_Friend ? TPC_FriendClassTemplate |
| 1084 | : TPC_ClassTemplate)) |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1085 | Invalid = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1087 | if (SS.isSet()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1088 | // 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] | 1089 | // template out-of-line. |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1090 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1091 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1092 | : diag::err_member_decl_does_not_match) |
| 1093 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1094 | Invalid = true; |
| 1095 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1099 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | PrevClassTemplate? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1101 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1102 | /*DelayTypeCreation=*/true); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1103 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1104 | if (NumOuterTemplateParamLists > 0) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1105 | NewClass->setTemplateParameterListsInfo( |
| 1106 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
| 1107 | NumOuterTemplateParamLists)); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1108 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1109 | // Add alignment attributes if necessary; these attributes are checked when |
| 1110 | // the ASTContext lays out the structure. |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1111 | if (TUK == TUK_Definition) { |
| 1112 | AddAlignmentAttributesForRecord(NewClass); |
| 1113 | AddMsStructLayoutForRecord(NewClass); |
| 1114 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1115 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1116 | ClassTemplateDecl *NewTemplate |
| 1117 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1118 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1119 | NewClass, PrevClassTemplate); |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1120 | NewClass->setDescribedClassTemplate(NewTemplate); |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1121 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1122 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1123 | NewTemplate->setModulePrivate(); |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1124 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1125 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1126 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1127 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1128 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1129 | (void)T; |
| 1130 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1131 | // 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] | 1132 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1133 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1134 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1135 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1136 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1137 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1138 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1139 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1140 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1141 | // Set the lexical context of these templates |
| 1142 | NewClass->setLexicalDeclContext(CurContext); |
| 1143 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1144 | |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1145 | if (TUK == TUK_Definition) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1146 | NewClass->startDefinition(); |
| 1147 | |
| 1148 | if (Attr) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1149 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1150 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1151 | if (PrevClassTemplate) |
| 1152 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1153 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1154 | AddPushedVisibilityAttribute(NewClass); |
| 1155 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1156 | if (TUK != TUK_Friend) { |
| 1157 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1158 | Scope *Outer = S; |
| 1159 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1160 | Outer = Outer->getParent(); |
| 1161 | PushOnScopeChains(NewTemplate, Outer); |
| 1162 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1163 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1164 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1165 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1166 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1167 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1168 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1169 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1170 | // Friend templates are visible in fairly strange ways. |
| 1171 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1172 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1173 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1174 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1175 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1176 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1177 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1178 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1179 | FriendDecl *Friend = FriendDecl::Create( |
| 1180 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1181 | Friend->setAccess(AS_public); |
| 1182 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1183 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1184 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1185 | if (Invalid) { |
| 1186 | NewTemplate->setInvalidDecl(); |
| 1187 | NewClass->setInvalidDecl(); |
| 1188 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1189 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1190 | ActOnDocumentableDecl(NewTemplate); |
| 1191 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1192 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1195 | /// \brief Diagnose the presence of a default template argument on a |
| 1196 | /// template parameter, which is ill-formed in certain contexts. |
| 1197 | /// |
| 1198 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1199 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1200 | Sema::TemplateParamListContext TPC, |
| 1201 | SourceLocation ParamLoc, |
| 1202 | SourceRange DefArgRange) { |
| 1203 | switch (TPC) { |
| 1204 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1205 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1206 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1207 | return false; |
| 1208 | |
| 1209 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1210 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1211 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1212 | // A default template-argument shall not be specified in a |
| 1213 | // function template declaration or a function template |
| 1214 | // definition [...] |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1215 | // If a friend function template declaration specifies a default |
| 1216 | // template-argument, that declaration shall be a definition and shall be |
| 1217 | // the only declaration of the function template in the translation unit. |
| 1218 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1219 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1220 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1221 | : diag::ext_template_parameter_default_in_function_template) |
| 1222 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1223 | return false; |
| 1224 | |
| 1225 | case Sema::TPC_ClassTemplateMember: |
| 1226 | // C++0x [temp.param]p9: |
| 1227 | // A default template-argument shall not be specified in the |
| 1228 | // template-parameter-lists of the definition of a member of a |
| 1229 | // class template that appears outside of the member's class. |
| 1230 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1231 | << DefArgRange; |
| 1232 | return true; |
| 1233 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1234 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1235 | case Sema::TPC_FriendFunctionTemplate: |
| 1236 | // C++ [temp.param]p9: |
| 1237 | // A default template-argument shall not be specified in a |
| 1238 | // friend template declaration. |
| 1239 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1240 | << DefArgRange; |
| 1241 | return true; |
| 1242 | |
| 1243 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1244 | // for friend function templates if there is only a single |
| 1245 | // declaration (and it is a definition). Strange! |
| 1246 | } |
| 1247 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1248 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1251 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1252 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1253 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1254 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1255 | // A template template parameter which is a parameter pack is also a pack |
| 1256 | // expansion. |
| 1257 | if (TTP->isParameterPack()) |
| 1258 | return false; |
| 1259 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1260 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1261 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1262 | NamedDecl *P = Params->getParam(I); |
| 1263 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1264 | if (!NTTP->isParameterPack() && |
| 1265 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1266 | NTTP->getTypeSourceInfo(), |
| 1267 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1268 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1269 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1270 | continue; |
| 1271 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1272 | |
| 1273 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1274 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1275 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1276 | return true; |
| 1277 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1278 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1279 | return false; |
| 1280 | } |
| 1281 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1282 | /// \brief Checks the validity of a template parameter list, possibly |
| 1283 | /// considering the template parameter list from a previous |
| 1284 | /// declaration. |
| 1285 | /// |
| 1286 | /// If an "old" template parameter list is provided, it must be |
| 1287 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1288 | /// template parameter list. |
| 1289 | /// |
| 1290 | /// \param NewParams Template parameter list for a new template |
| 1291 | /// declaration. This template parameter list will be updated with any |
| 1292 | /// default arguments that are carried through from the previous |
| 1293 | /// template parameter list. |
| 1294 | /// |
| 1295 | /// \param OldParams If provided, template parameter list from a |
| 1296 | /// previous declaration of the same template. Default template |
| 1297 | /// arguments will be merged from the old template parameter list to |
| 1298 | /// the new template parameter list. |
| 1299 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1300 | /// \param TPC Describes the context in which we are checking the given |
| 1301 | /// template parameter list. |
| 1302 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1303 | /// \returns true if an error occurred, false otherwise. |
| 1304 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1305 | TemplateParameterList *OldParams, |
| 1306 | TemplateParamListContext TPC) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1307 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1309 | // C++ [temp.param]p10: |
| 1310 | // The set of default template-arguments available for use with a |
| 1311 | // template declaration or definition is obtained by merging the |
| 1312 | // default arguments from the definition (if in scope) and all |
| 1313 | // declarations in scope in the same way default function |
| 1314 | // arguments are (8.3.6). |
| 1315 | bool SawDefaultArgument = false; |
| 1316 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1317 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1318 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1319 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1320 | if (OldParams) |
| 1321 | OldParam = OldParams->begin(); |
| 1322 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1323 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1324 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1325 | NewParamEnd = NewParams->end(); |
| 1326 | NewParam != NewParamEnd; ++NewParam) { |
| 1327 | // Variables used to diagnose redundant default arguments |
| 1328 | bool RedundantDefaultArg = false; |
| 1329 | SourceLocation OldDefaultLoc; |
| 1330 | SourceLocation NewDefaultLoc; |
| 1331 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1332 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1333 | bool MissingDefaultArg = false; |
| 1334 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1335 | // Variable used to diagnose non-final parameter packs |
| 1336 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1337 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1338 | if (TemplateTypeParmDecl *NewTypeParm |
| 1339 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1340 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1341 | if (NewTypeParm->hasDefaultArgument() && |
| 1342 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1343 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1344 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1345 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1346 | NewTypeParm->removeDefaultArgument(); |
| 1347 | |
| 1348 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1349 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1350 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1351 | if (NewTypeParm->isParameterPack()) { |
| 1352 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1353 | "Parameter packs can't have a default argument!"); |
| 1354 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1355 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1356 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1357 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1358 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1359 | SawDefaultArgument = true; |
| 1360 | RedundantDefaultArg = true; |
| 1361 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1362 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1363 | // Merge the default argument from the old declaration to the |
| 1364 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1365 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1366 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1367 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1368 | SawDefaultArgument = true; |
| 1369 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1370 | } else if (SawDefaultArgument) |
| 1371 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1372 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1373 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1374 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1375 | if (!NewNonTypeParm->isParameterPack() && |
| 1376 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1377 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1378 | UPPC_NonTypeTemplateParameterType)) { |
| 1379 | Invalid = true; |
| 1380 | continue; |
| 1381 | } |
| 1382 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1383 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1384 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1385 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1386 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1387 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1388 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1391 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1392 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1393 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1394 | if (NewNonTypeParm->isParameterPack()) { |
| 1395 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1396 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1397 | if (!NewNonTypeParm->isPackExpansion()) |
| 1398 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1399 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 1400 | NewNonTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1401 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1402 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1403 | SawDefaultArgument = true; |
| 1404 | RedundantDefaultArg = true; |
| 1405 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1406 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1407 | // Merge the default argument from the old declaration to the |
| 1408 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1409 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1410 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1411 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1412 | SawDefaultArgument = true; |
| 1413 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1414 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1416 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1417 | TemplateTemplateParmDecl *NewTemplateParm |
| 1418 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1419 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1420 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1421 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1422 | Invalid = true; |
| 1423 | continue; |
| 1424 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1425 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1426 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1427 | if (NewTemplateParm->hasDefaultArgument() && |
| 1428 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1429 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1430 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1431 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1432 | |
| 1433 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1434 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1435 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1436 | if (NewTemplateParm->isParameterPack()) { |
| 1437 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1438 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1439 | if (!NewTemplateParm->isPackExpansion()) |
| 1440 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1441 | } else if (OldTemplateParm && |
| 1442 | hasVisibleDefaultArgument(OldTemplateParm) && |
| 1443 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1444 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1445 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1446 | SawDefaultArgument = true; |
| 1447 | RedundantDefaultArg = true; |
| 1448 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1449 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1450 | // Merge the default argument from the old declaration to the |
| 1451 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1452 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1453 | PreviousDefaultArgLoc |
| 1454 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1455 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1456 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1457 | PreviousDefaultArgLoc |
| 1458 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1459 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1460 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1463 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1464 | // If a template parameter of a primary class template or alias template |
| 1465 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1466 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1467 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 1468 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1469 | Diag((*NewParam)->getLocation(), |
| 1470 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1471 | Invalid = true; |
| 1472 | } |
| 1473 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1474 | if (RedundantDefaultArg) { |
| 1475 | // C++ [temp.param]p12: |
| 1476 | // A template-parameter shall not be given default arguments |
| 1477 | // by two different declarations in the same scope. |
| 1478 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1479 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1480 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1481 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1482 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1483 | // If a template-parameter of a class template has a default |
| 1484 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1485 | // have a default template-argument supplied or be a template parameter |
| 1486 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1488 | diag::err_template_param_default_arg_missing); |
| 1489 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1490 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1491 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | // If we have an old template parameter list that we're merging |
| 1495 | // in, move on to the next parameter. |
| 1496 | if (OldParams) |
| 1497 | ++OldParam; |
| 1498 | } |
| 1499 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1500 | // We were missing some default arguments at the end of the list, so remove |
| 1501 | // all of the default arguments. |
| 1502 | if (RemoveDefaultArguments) { |
| 1503 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1504 | NewParamEnd = NewParams->end(); |
| 1505 | NewParam != NewParamEnd; ++NewParam) { |
| 1506 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1507 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1508 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1509 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1510 | NTTP->removeDefaultArgument(); |
| 1511 | else |
| 1512 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1513 | } |
| 1514 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1515 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1516 | return Invalid; |
| 1517 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1518 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1519 | namespace { |
| 1520 | |
| 1521 | /// A class which looks for a use of a certain level of template |
| 1522 | /// parameter. |
| 1523 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1524 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1525 | |
| 1526 | unsigned Depth; |
| 1527 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1528 | SourceLocation MatchLoc; |
| 1529 | |
| 1530 | DependencyChecker(unsigned Depth) : Depth(Depth), Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1531 | |
| 1532 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1533 | NamedDecl *ND = Params->getParam(0); |
| 1534 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1535 | Depth = PD->getDepth(); |
| 1536 | } else if (NonTypeTemplateParmDecl *PD = |
| 1537 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1538 | Depth = PD->getDepth(); |
| 1539 | } else { |
| 1540 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1541 | } |
| 1542 | } |
| 1543 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1544 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1545 | if (ParmDepth >= Depth) { |
| 1546 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1547 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1548 | return true; |
| 1549 | } |
| 1550 | return false; |
| 1551 | } |
| 1552 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1553 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1554 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 1555 | } |
| 1556 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1557 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1558 | return !Matches(T->getDepth()); |
| 1559 | } |
| 1560 | |
| 1561 | bool TraverseTemplateName(TemplateName N) { |
| 1562 | if (TemplateTemplateParmDecl *PD = |
| 1563 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1564 | if (Matches(PD->getDepth())) |
| 1565 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1566 | return super::TraverseTemplateName(N); |
| 1567 | } |
| 1568 | |
| 1569 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1570 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1571 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 1572 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1573 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1574 | return super::VisitDeclRefExpr(E); |
| 1575 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1576 | |
| 1577 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 1578 | return TraverseType(T->getReplacementType()); |
| 1579 | } |
| 1580 | |
| 1581 | bool |
| 1582 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 1583 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 1584 | } |
| 1585 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1586 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1587 | return TraverseType(T->getInjectedSpecializationType()); |
| 1588 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1589 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 1590 | } // end anonymous namespace |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1591 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1592 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1593 | /// list. |
| 1594 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1595 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1596 | DependencyChecker Checker(Params); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1597 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1598 | return Checker.Match; |
| 1599 | } |
| 1600 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1601 | // Find the source range corresponding to the named type in the given |
| 1602 | // nested-name-specifier, if any. |
| 1603 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1604 | QualType T, |
| 1605 | const CXXScopeSpec &SS) { |
| 1606 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1607 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1608 | if (const Type *CurType = NNS->getAsType()) { |
| 1609 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1610 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1611 | } else |
| 1612 | break; |
| 1613 | |
| 1614 | NNSLoc = NNSLoc.getPrefix(); |
| 1615 | } |
| 1616 | |
| 1617 | return SourceRange(); |
| 1618 | } |
| 1619 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1620 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1621 | /// specifier, returning the template parameter list that applies to the |
| 1622 | /// name. |
| 1623 | /// |
| 1624 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1625 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1627 | /// \param DeclLoc The location of the declaration itself. |
| 1628 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1629 | /// \param SS the scope specifier that will be matched to the given template |
| 1630 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1631 | /// being declared. |
| 1632 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1633 | /// \param TemplateId The template-id following the scope specifier, if there |
| 1634 | /// is one. Used to check for a missing 'template<>'. |
| 1635 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1636 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1637 | /// innermost template parameter lists. |
| 1638 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1639 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1640 | /// matching template parameters to scope specifiers in friend |
| 1641 | /// declarations. |
| 1642 | /// |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1643 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1644 | /// declared is an explicit specialization, false otherwise. |
| 1645 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1647 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1648 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1649 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1650 | /// 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] | 1651 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1652 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 1653 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1654 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1655 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
| 1656 | bool &IsExplicitSpecialization, bool &Invalid) { |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1657 | IsExplicitSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1658 | Invalid = false; |
| 1659 | |
| 1660 | // The sequence of nested types to which we will match up the template |
| 1661 | // parameter lists. We first build this list by starting with the type named |
| 1662 | // 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] | 1663 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1664 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1665 | if (SS.getScopeRep()) { |
| 1666 | if (CXXRecordDecl *Record |
| 1667 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1668 | T = Context.getTypeDeclType(Record); |
| 1669 | else |
| 1670 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1671 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1672 | |
| 1673 | // If we found an explicit specialization that prevents us from needing |
| 1674 | // 'template<>' headers, this will be set to the location of that |
| 1675 | // explicit specialization. |
| 1676 | SourceLocation ExplicitSpecLoc; |
| 1677 | |
| 1678 | while (!T.isNull()) { |
| 1679 | NestedTypes.push_back(T); |
| 1680 | |
| 1681 | // Retrieve the parent of a record type. |
| 1682 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1683 | // If this type is an explicit specialization, we're done. |
| 1684 | if (ClassTemplateSpecializationDecl *Spec |
| 1685 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1686 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1687 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1688 | ExplicitSpecLoc = Spec->getLocation(); |
| 1689 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1690 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1691 | } else if (Record->getTemplateSpecializationKind() |
| 1692 | == TSK_ExplicitSpecialization) { |
| 1693 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1694 | break; |
| 1695 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1696 | |
| 1697 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1698 | T = Context.getTypeDeclType(Parent); |
| 1699 | else |
| 1700 | T = QualType(); |
| 1701 | continue; |
| 1702 | } |
| 1703 | |
| 1704 | if (const TemplateSpecializationType *TST |
| 1705 | = T->getAs<TemplateSpecializationType>()) { |
| 1706 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1707 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1708 | T = Context.getTypeDeclType(Parent); |
| 1709 | else |
| 1710 | T = QualType(); |
| 1711 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1712 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | // Look one step prior in a dependent template specialization type. |
| 1716 | if (const DependentTemplateSpecializationType *DependentTST |
| 1717 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1718 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1719 | T = QualType(NNS->getAsType(), 0); |
| 1720 | else |
| 1721 | T = QualType(); |
| 1722 | continue; |
| 1723 | } |
| 1724 | |
| 1725 | // Look one step prior in a dependent name type. |
| 1726 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1727 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1728 | T = QualType(NNS->getAsType(), 0); |
| 1729 | else |
| 1730 | T = QualType(); |
| 1731 | continue; |
| 1732 | } |
| 1733 | |
| 1734 | // Retrieve the parent of an enumeration type. |
| 1735 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1736 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1737 | // check here. |
| 1738 | EnumDecl *Enum = EnumT->getDecl(); |
| 1739 | |
| 1740 | // Get to the parent type. |
| 1741 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1742 | T = Context.getTypeDeclType(Parent); |
| 1743 | else |
| 1744 | T = QualType(); |
| 1745 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1746 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1747 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1748 | T = QualType(); |
| 1749 | } |
| 1750 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1751 | // to the innermost while checking template-parameter-lists. |
| 1752 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1753 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1754 | // C++0x [temp.expl.spec]p17: |
| 1755 | // A member or a member template may be nested within many |
| 1756 | // enclosing class templates. In an explicit specialization for |
| 1757 | // such a member, the member declaration shall be preceded by a |
| 1758 | // template<> for each enclosing class template that is |
| 1759 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1760 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1761 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1762 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1763 | if (SawNonEmptyTemplateParameterList) { |
| 1764 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1765 | << !Recovery << Range; |
| 1766 | Invalid = true; |
| 1767 | IsExplicitSpecialization = false; |
| 1768 | return true; |
| 1769 | } |
| 1770 | |
| 1771 | return false; |
| 1772 | }; |
| 1773 | |
| 1774 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 1775 | // Check that we can have an explicit specialization here. |
| 1776 | if (CheckExplicitSpecialization(Range, true)) |
| 1777 | return true; |
| 1778 | |
| 1779 | // We don't have a template header, but we should. |
| 1780 | SourceLocation ExpectedTemplateLoc; |
| 1781 | if (!ParamLists.empty()) |
| 1782 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1783 | else |
| 1784 | ExpectedTemplateLoc = DeclStartLoc; |
| 1785 | |
| 1786 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1787 | << Range |
| 1788 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1789 | return false; |
| 1790 | }; |
| 1791 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1792 | unsigned ParamIdx = 0; |
| 1793 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1794 | ++TypeIdx) { |
| 1795 | T = NestedTypes[TypeIdx]; |
| 1796 | |
| 1797 | // Whether we expect a 'template<>' header. |
| 1798 | bool NeedEmptyTemplateHeader = false; |
| 1799 | |
| 1800 | // Whether we expect a template header with parameters. |
| 1801 | bool NeedNonemptyTemplateHeader = false; |
| 1802 | |
| 1803 | // For a dependent type, the set of template parameters that we |
| 1804 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1805 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1806 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1807 | // C++0x [temp.expl.spec]p15: |
| 1808 | // A member or a member template may be nested within many enclosing |
| 1809 | // class templates. In an explicit specialization for such a member, the |
| 1810 | // member declaration shall be preceded by a template<> for each |
| 1811 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1812 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1813 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1814 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1815 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1816 | NeedNonemptyTemplateHeader = true; |
| 1817 | } else if (Record->isDependentType()) { |
| 1818 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1819 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1820 | ->getTemplateParameters(); |
| 1821 | NeedNonemptyTemplateHeader = true; |
| 1822 | } |
| 1823 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1824 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1825 | // C++0x [temp.expl.spec]p4: |
| 1826 | // Members of an explicitly specialized class template are defined |
| 1827 | // in the same manner as members of normal classes, and not using |
| 1828 | // the template<> syntax. |
| 1829 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1830 | NeedEmptyTemplateHeader = true; |
| 1831 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1832 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1833 | } else if (Record->getTemplateSpecializationKind()) { |
| 1834 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1835 | != TSK_ExplicitSpecialization && |
| 1836 | TypeIdx == NumTypes - 1) |
| 1837 | IsExplicitSpecialization = true; |
| 1838 | |
| 1839 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1840 | } |
| 1841 | } else if (const TemplateSpecializationType *TST |
| 1842 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 1843 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1844 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1845 | NeedNonemptyTemplateHeader = true; |
| 1846 | } |
| 1847 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1848 | // FIXME: We actually could/should check the template arguments here |
| 1849 | // against the corresponding template parameter list. |
| 1850 | NeedNonemptyTemplateHeader = false; |
| 1851 | } |
| 1852 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1853 | // C++ [temp.expl.spec]p16: |
| 1854 | // In an explicit specialization declaration for a member of a class |
| 1855 | // template or a member template that ap- pears in namespace scope, the |
| 1856 | // member template and some of its enclosing class templates may remain |
| 1857 | // unspecialized, except that the declaration shall not explicitly |
| 1858 | // specialize a class member template if its en- closing class templates |
| 1859 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1860 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1861 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1862 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 1863 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1864 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1865 | } else |
| 1866 | SawNonEmptyTemplateParameterList = true; |
| 1867 | } |
| 1868 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1869 | if (NeedEmptyTemplateHeader) { |
| 1870 | // If we're on the last of the types, and we need a 'template<>' header |
| 1871 | // here, then it's an explicit specialization. |
| 1872 | if (TypeIdx == NumTypes - 1) |
| 1873 | IsExplicitSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1874 | |
| 1875 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1876 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1877 | // The header has template parameters when it shouldn't. Complain. |
| 1878 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1879 | diag::err_template_param_list_matches_nontemplate) |
| 1880 | << T |
| 1881 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1882 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1883 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1884 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1885 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1886 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1887 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1888 | // Consume this template header. |
| 1889 | ++ParamIdx; |
| 1890 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1891 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1892 | |
| 1893 | if (!IsFriend) |
| 1894 | if (DiagnoseMissingExplicitSpecialization( |
| 1895 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1896 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1897 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1898 | continue; |
| 1899 | } |
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 | if (NeedNonemptyTemplateHeader) { |
| 1902 | // In friend declarations we can have template-ids which don't |
| 1903 | // depend on the corresponding template parameter lists. But |
| 1904 | // assume that empty parameter lists are supposed to match this |
| 1905 | // template-id. |
| 1906 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1907 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1908 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1909 | ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1910 | else |
| 1911 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1912 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1913 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1914 | if (ParamIdx < ParamLists.size()) { |
| 1915 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1916 | if (ExpectedTemplateParams && |
| 1917 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1918 | ExpectedTemplateParams, |
| 1919 | true, TPL_TemplateMatch)) |
| 1920 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1921 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1922 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1923 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1924 | TPC_ClassTemplateMember)) |
| 1925 | Invalid = true; |
| 1926 | |
| 1927 | ++ParamIdx; |
| 1928 | continue; |
| 1929 | } |
| 1930 | |
| 1931 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1932 | << T |
| 1933 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1934 | Invalid = true; |
| 1935 | continue; |
| 1936 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1937 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1938 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1939 | // If there were at least as many template-ids as there were template |
| 1940 | // parameter lists, then there are no template parameter lists remaining for |
| 1941 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1942 | if (ParamIdx >= ParamLists.size()) { |
| 1943 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1944 | // We don't have a template header for the declaration itself, but we |
| 1945 | // should. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1946 | IsExplicitSpecialization = true; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1947 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 1948 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1949 | |
| 1950 | // Fabricate an empty template parameter list for the invented header. |
| 1951 | return TemplateParameterList::Create(Context, SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 1952 | SourceLocation(), None, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1953 | SourceLocation()); |
| 1954 | } |
| 1955 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1956 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1957 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1959 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1960 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1961 | bool HasAnyExplicitSpecHeader = false; |
| 1962 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1963 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1964 | if (ParamLists[I]->size() == 0) |
| 1965 | HasAnyExplicitSpecHeader = true; |
| 1966 | else |
| 1967 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1968 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1969 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1970 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1971 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 1972 | : diag::err_template_spec_extra_headers) |
| 1973 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1974 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1975 | |
| 1976 | // If there was a specialization somewhere, such that 'template<>' is |
| 1977 | // not required, and there were any 'template<>' headers, note where the |
| 1978 | // specialization occurred. |
| 1979 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1980 | Diag(ExplicitSpecLoc, |
| 1981 | diag::note_explicit_template_spec_does_not_need_header) |
| 1982 | << NestedTypes.back(); |
| 1983 | |
| 1984 | // We have a template parameter list with no corresponding scope, which |
| 1985 | // means that the resulting template declaration can't be instantiated |
| 1986 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1987 | if (!AllExplicitSpecHeaders) |
| 1988 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1989 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1990 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1991 | // C++ [temp.expl.spec]p16: |
| 1992 | // In an explicit specialization declaration for a member of a class |
| 1993 | // template or a member template that ap- pears in namespace scope, the |
| 1994 | // member template and some of its enclosing class templates may remain |
| 1995 | // unspecialized, except that the declaration shall not explicitly |
| 1996 | // specialize a class member template if its en- closing class templates |
| 1997 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1998 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1999 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2000 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2001 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 2002 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2003 | // Return the last template parameter list, which corresponds to the |
| 2004 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 2005 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2008 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 2009 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2010 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2011 | << (isa<FunctionTemplateDecl>(Template) |
| 2012 | ? 0 |
| 2013 | : isa<ClassTemplateDecl>(Template) |
| 2014 | ? 1 |
| 2015 | : isa<VarTemplateDecl>(Template) |
| 2016 | ? 2 |
| 2017 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 2018 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2019 | return; |
| 2020 | } |
| 2021 | |
| 2022 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 2023 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 2024 | IEnd = OST->end(); |
| 2025 | I != IEnd; ++I) |
| 2026 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 2027 | << 0 << (*I)->getDeclName(); |
| 2028 | |
| 2029 | return; |
| 2030 | } |
| 2031 | } |
| 2032 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2033 | static QualType |
| 2034 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
| 2035 | const SmallVectorImpl<TemplateArgument> &Converted, |
| 2036 | SourceLocation TemplateLoc, |
| 2037 | TemplateArgumentListInfo &TemplateArgs) { |
| 2038 | ASTContext &Context = SemaRef.getASTContext(); |
| 2039 | switch (BTD->getBuiltinTemplateKind()) { |
| 2040 | case BTK__make_integer_seq: |
| 2041 | // Specializations of __make_integer_seq<S, T, N> are treated like |
| 2042 | // S<T, 0, ..., N-1>. |
| 2043 | |
| 2044 | // C++14 [inteseq.intseq]p1: |
| 2045 | // T shall be an integer type. |
| 2046 | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
| 2047 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
| 2048 | diag::err_integer_sequence_integral_element_type); |
| 2049 | return QualType(); |
| 2050 | } |
| 2051 | |
| 2052 | // C++14 [inteseq.make]p1: |
| 2053 | // If N is negative the program is ill-formed. |
| 2054 | TemplateArgument NumArgsArg = Converted[2]; |
| 2055 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
| 2056 | if (NumArgs < 0) { |
| 2057 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
| 2058 | diag::err_integer_sequence_negative_length); |
| 2059 | return QualType(); |
| 2060 | } |
| 2061 | |
| 2062 | QualType ArgTy = NumArgsArg.getIntegralType(); |
| 2063 | TemplateArgumentListInfo SyntheticTemplateArgs; |
| 2064 | // The type argument gets reused as the first template argument in the |
| 2065 | // synthetic template argument list. |
| 2066 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
| 2067 | // Expand N into 0 ... N-1. |
| 2068 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
| 2069 | I < NumArgs; ++I) { |
| 2070 | TemplateArgument TA(Context, I, ArgTy); |
| 2071 | Expr *E = SemaRef.BuildExpressionFromIntegralTemplateArgument( |
| 2072 | TA, TemplateArgs[2].getLocation()) |
| 2073 | .getAs<Expr>(); |
| 2074 | SyntheticTemplateArgs.addArgument( |
| 2075 | TemplateArgumentLoc(TemplateArgument(E), E)); |
| 2076 | } |
| 2077 | // The first template argument will be reused as the template decl that |
| 2078 | // our synthetic template arguments will be applied to. |
| 2079 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
| 2080 | TemplateLoc, SyntheticTemplateArgs); |
| 2081 | } |
| 2082 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
| 2083 | } |
| 2084 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2085 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 2086 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2087 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 2088 | DependentTemplateName *DTN |
| 2089 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2090 | if (DTN && DTN->isIdentifier()) |
| 2091 | // When building a template-id where the template-name is dependent, |
| 2092 | // assume the template is a type template. Either our assumption is |
| 2093 | // correct, or the code is ill-formed and will be diagnosed when the |
| 2094 | // dependent name is substituted. |
| 2095 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 2096 | DTN->getQualifier(), |
| 2097 | DTN->getIdentifier(), |
| 2098 | TemplateArgs); |
| 2099 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2100 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 2101 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
| 2102 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2103 | // We might have a substituted template template parameter pack. If so, |
| 2104 | // build a template specialization type for it. |
| 2105 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 2106 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2107 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2108 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 2109 | << Name; |
| 2110 | NoteAllFoundTemplates(Name); |
| 2111 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2112 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2113 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2114 | // Check that the template argument list is well-formed for this |
| 2115 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2116 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2117 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2118 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2119 | return QualType(); |
| 2120 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2121 | QualType CanonType; |
| 2122 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2123 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2124 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 2125 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2126 | // Find the canonical type for this type alias template specialization. |
| 2127 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 2128 | if (Pattern->isInvalidDecl()) |
| 2129 | return QualType(); |
| 2130 | |
| 2131 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2132 | Converted.data(), Converted.size()); |
| 2133 | |
| 2134 | // Only substitute for the innermost template argument list. |
| 2135 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 0c4a34b | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 2136 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 2137 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 2138 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 2139 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2140 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2141 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2142 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2143 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2144 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2145 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2146 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 2147 | TemplateArgLists, AliasTemplate->getLocation(), |
| 2148 | AliasTemplate->getDeclName()); |
| 2149 | if (CanonType.isNull()) |
| 2150 | return QualType(); |
| 2151 | } else if (Name.isDependent() || |
| 2152 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2153 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2154 | // This class template specialization is a dependent |
| 2155 | // type. Therefore, its canonical type is another class template |
| 2156 | // specialization type that contains all of the converted |
| 2157 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 2158 | // A<T, T> have identical types when A is declared as: |
| 2159 | // |
| 2160 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2161 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2162 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2163 | Converted.data(), |
| 2164 | Converted.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2165 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2166 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2167 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2168 | // In the future, we need to teach getTemplateSpecializationType to only |
| 2169 | // build the canonical type and return that to us. |
| 2170 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2171 | |
| 2172 | // This might work out to be a current instantiation, in which |
| 2173 | // case the canonical type needs to be the InjectedClassNameType. |
| 2174 | // |
| 2175 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2176 | // changes to CurContext don't change the set of current |
| 2177 | // instantiations. |
| 2178 | if (isa<ClassTemplateDecl>(Template)) { |
| 2179 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2180 | // If we get out to a namespace, we're done. |
| 2181 | if (Ctx->isFileContext()) break; |
| 2182 | |
| 2183 | // If this isn't a record, keep looking. |
| 2184 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2185 | if (!Record) continue; |
| 2186 | |
| 2187 | // Look for one of the two cases with InjectedClassNameTypes |
| 2188 | // and check whether it's the same template. |
| 2189 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2190 | !Record->getDescribedClassTemplate()) |
| 2191 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2192 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2193 | // Fetch the injected class name type and check whether its |
| 2194 | // injected type is equal to the type we just built. |
| 2195 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2196 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2197 | ->getInjectedSpecializationType(); |
| 2198 | |
| 2199 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2200 | continue; |
| 2201 | |
| 2202 | // If so, the canonical type of this TST is the injected |
| 2203 | // class name type of the record we just found. |
| 2204 | assert(ICNT.isCanonical()); |
| 2205 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2206 | break; |
| 2207 | } |
| 2208 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2209 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2210 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2211 | // Find the class template specialization declaration that |
| 2212 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2213 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2214 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2215 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2216 | if (!Decl) { |
| 2217 | // This is the first time we have referenced this class template |
| 2218 | // specialization. Create the canonical declaration and add it to |
| 2219 | // the set of specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2220 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2221 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2222 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2223 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2224 | ClassTemplate->getLocation(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2225 | ClassTemplate, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2226 | Converted.data(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2227 | Converted.size(), nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2228 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 2229 | if (ClassTemplate->isOutOfLine()) |
| 2230 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 2233 | // Diagnose uses of this specialization. |
| 2234 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 2235 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2236 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2237 | assert(isa<RecordType>(CanonType) && |
| 2238 | "type of non-dependent specialization is not a RecordType"); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2239 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { |
| 2240 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
| 2241 | TemplateArgs); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2242 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2243 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2244 | // Build the fully-sugared type for this class template |
| 2245 | // specialization, which refers back to the class template |
| 2246 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2247 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2250 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2251 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2252 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2253 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2254 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2255 | SourceLocation RAngleLoc, |
| 2256 | bool IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2257 | if (SS.isInvalid()) |
| 2258 | return true; |
| 2259 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2260 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2261 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2262 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2263 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2264 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2265 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2266 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2267 | QualType T |
| 2268 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2269 | DTN->getQualifier(), |
| 2270 | DTN->getIdentifier(), |
| 2271 | TemplateArgs); |
| 2272 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2273 | TypeLocBuilder TLB; |
| 2274 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2275 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2276 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2277 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2278 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2279 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2280 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2281 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2282 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2283 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2284 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2285 | } |
| 2286 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2287 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2288 | |
| 2289 | if (Result.isNull()) |
| 2290 | return true; |
| 2291 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2292 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2293 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2294 | TemplateSpecializationTypeLoc SpecTL |
| 2295 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2296 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2297 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2298 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2299 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2300 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2301 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2302 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2303 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2304 | // constructor or destructor name (in such a case, the scope specifier |
| 2305 | // will be attached to the enclosing Decl or Expr node). |
| 2306 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2307 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2308 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2309 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2310 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2311 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2312 | } |
| 2313 | |
| 2314 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2315 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2316 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2317 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2318 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2319 | SourceLocation TagLoc, |
| 2320 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2321 | SourceLocation TemplateKWLoc, |
| 2322 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2323 | SourceLocation TemplateLoc, |
| 2324 | SourceLocation LAngleLoc, |
| 2325 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2326 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2327 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2328 | |
| 2329 | // Translate the parser's template argument list in our AST format. |
| 2330 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2331 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2332 | |
| 2333 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2334 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2335 | ElaboratedTypeKeyword Keyword |
| 2336 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2337 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2338 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2339 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2340 | DTN->getQualifier(), |
| 2341 | DTN->getIdentifier(), |
| 2342 | TemplateArgs); |
| 2343 | |
| 2344 | // Build type-source information. |
| 2345 | TypeLocBuilder TLB; |
| 2346 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2347 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2348 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2349 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2350 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2351 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2352 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2353 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2354 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2355 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2356 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2357 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2358 | |
| 2359 | if (TypeAliasTemplateDecl *TAT = |
| 2360 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2361 | // C++0x [dcl.type.elab]p2: |
| 2362 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2363 | // resolves to an alias template specialization, the |
| 2364 | // elaborated-type-specifier is ill-formed. |
| 2365 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2366 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2367 | } |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2368 | |
| 2369 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2370 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2371 | return TypeResult(true); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2372 | |
| 2373 | // Check the tag kind |
| 2374 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2375 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2376 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2377 | IdentifierInfo *Id = D->getIdentifier(); |
| 2378 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2379 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2380 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 2381 | TagLoc, Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2382 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2383 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2384 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2385 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2386 | } |
| 2387 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2388 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2389 | // Provide source-location information for the template specialization. |
| 2390 | TypeLocBuilder TLB; |
| 2391 | TemplateSpecializationTypeLoc SpecTL |
| 2392 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2393 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2394 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2395 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2396 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2397 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2398 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2399 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2400 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2401 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2402 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2403 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2404 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2405 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2406 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2409 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2410 | Sema &S, SourceLocation NameLoc, TemplateParameterList *TemplateParams, |
| 2411 | unsigned ExplicitArgs, SmallVectorImpl<TemplateArgument> &TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2412 | |
| 2413 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 2414 | NamedDecl *PrevDecl, |
| 2415 | SourceLocation Loc, |
| 2416 | bool IsPartialSpecialization); |
| 2417 | |
| 2418 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2419 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2420 | static bool isTemplateArgumentTemplateParameter( |
| 2421 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 2422 | switch (Arg.getKind()) { |
| 2423 | case TemplateArgument::Null: |
| 2424 | case TemplateArgument::NullPtr: |
| 2425 | case TemplateArgument::Integral: |
| 2426 | case TemplateArgument::Declaration: |
| 2427 | case TemplateArgument::Pack: |
| 2428 | case TemplateArgument::TemplateExpansion: |
| 2429 | return false; |
| 2430 | |
| 2431 | case TemplateArgument::Type: { |
| 2432 | QualType Type = Arg.getAsType(); |
| 2433 | const TemplateTypeParmType *TPT = |
| 2434 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 2435 | return TPT && !Type.hasQualifiers() && |
| 2436 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 2437 | } |
| 2438 | |
| 2439 | case TemplateArgument::Expression: { |
| 2440 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 2441 | if (!DRE || !DRE->getDecl()) |
| 2442 | return false; |
| 2443 | const NonTypeTemplateParmDecl *NTTP = |
| 2444 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 2445 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 2446 | } |
| 2447 | |
| 2448 | case TemplateArgument::Template: |
| 2449 | const TemplateTemplateParmDecl *TTP = |
| 2450 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 2451 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 2452 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 2453 | } |
| 2454 | llvm_unreachable("unexpected kind of template argument"); |
| 2455 | } |
| 2456 | |
| 2457 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 2458 | ArrayRef<TemplateArgument> Args) { |
| 2459 | if (Params->size() != Args.size()) |
| 2460 | return false; |
| 2461 | |
| 2462 | unsigned Depth = Params->getDepth(); |
| 2463 | |
| 2464 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 2465 | TemplateArgument Arg = Args[I]; |
| 2466 | |
| 2467 | // If the parameter is a pack expansion, the argument must be a pack |
| 2468 | // whose only element is a pack expansion. |
| 2469 | if (Params->getParam(I)->isParameterPack()) { |
| 2470 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 2471 | !Arg.pack_begin()->isPackExpansion()) |
| 2472 | return false; |
| 2473 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 2474 | } |
| 2475 | |
| 2476 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 2477 | return false; |
| 2478 | } |
| 2479 | |
| 2480 | return true; |
| 2481 | } |
| 2482 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2483 | /// Convert the parser's template argument list representation into our form. |
| 2484 | static TemplateArgumentListInfo |
| 2485 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 2486 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 2487 | TemplateId.RAngleLoc); |
| 2488 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 2489 | TemplateId.NumArgs); |
| 2490 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 2491 | return TemplateArgs; |
| 2492 | } |
| 2493 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2494 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2495 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 2496 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2497 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2498 | // D must be variable template id. |
| 2499 | assert(D.getName().getKind() == UnqualifiedId::IK_TemplateId && |
| 2500 | "Variable template specialization is declared with a template it."); |
| 2501 | |
| 2502 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2503 | TemplateArgumentListInfo TemplateArgs = |
| 2504 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2505 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 2506 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 2507 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2508 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2509 | TemplateName Name = TemplateId->Template.get(); |
| 2510 | |
| 2511 | // The template-id must name a variable template. |
| 2512 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2513 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 2514 | if (!VarTemplate) { |
| 2515 | NamedDecl *FnTemplate; |
| 2516 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 2517 | FnTemplate = *OTS->begin(); |
| 2518 | else |
| 2519 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 2520 | if (FnTemplate) |
| 2521 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 2522 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2523 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 2524 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2525 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2526 | |
| 2527 | // Check for unexpanded parameter packs in any of the template arguments. |
| 2528 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 2529 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 2530 | UPPC_PartialSpecialization)) |
| 2531 | return true; |
| 2532 | |
| 2533 | // Check that the template argument list is well-formed for this |
| 2534 | // template. |
| 2535 | SmallVector<TemplateArgument, 4> Converted; |
| 2536 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 2537 | false, Converted)) |
| 2538 | return true; |
| 2539 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2540 | // Find the variable template (partial) specialization declaration that |
| 2541 | // corresponds to these arguments. |
| 2542 | if (IsPartialSpecialization) { |
| 2543 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2544 | *this, TemplateNameLoc, VarTemplate->getTemplateParameters(), |
| 2545 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2546 | return true; |
| 2547 | |
| 2548 | bool InstantiationDependent; |
| 2549 | if (!Name.isDependent() && |
| 2550 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2551 | TemplateArgs.getArgumentArray(), TemplateArgs.size(), |
| 2552 | InstantiationDependent)) { |
| 2553 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 2554 | << VarTemplate->getDeclName(); |
| 2555 | IsPartialSpecialization = false; |
| 2556 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2557 | |
| 2558 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 2559 | Converted)) { |
| 2560 | // C++ [temp.class.spec]p9b3: |
| 2561 | // |
| 2562 | // -- The argument list of the specialization shall not be identical |
| 2563 | // to the implicit argument list of the primary template. |
| 2564 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 2565 | << /*variable template*/ 1 |
| 2566 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 2567 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 2568 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 2569 | // of the primary template. |
| 2570 | return true; |
| 2571 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2572 | } |
| 2573 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2574 | void *InsertPos = nullptr; |
| 2575 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2576 | |
| 2577 | if (IsPartialSpecialization) |
| 2578 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2579 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2580 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2581 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2582 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2583 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2584 | |
| 2585 | // Check whether we can declare a variable template specialization in |
| 2586 | // the current scope. |
| 2587 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 2588 | TemplateNameLoc, |
| 2589 | IsPartialSpecialization)) |
| 2590 | return true; |
| 2591 | |
| 2592 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 2593 | // Since the only prior variable template specialization with these |
| 2594 | // arguments was referenced but not declared, reuse that |
| 2595 | // declaration node as our own, updating its source location and |
| 2596 | // the list of outer template parameters to reflect our new declaration. |
| 2597 | Specialization = PrevDecl; |
| 2598 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2599 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2600 | } else if (IsPartialSpecialization) { |
| 2601 | // Create a new class template partial specialization declaration node. |
| 2602 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 2603 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2604 | VarTemplatePartialSpecializationDecl *Partial = |
| 2605 | VarTemplatePartialSpecializationDecl::Create( |
| 2606 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 2607 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 2608 | Converted.data(), Converted.size(), TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2609 | |
| 2610 | if (!PrevPartial) |
| 2611 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 2612 | Specialization = Partial; |
| 2613 | |
| 2614 | // If we are providing an explicit specialization of a member variable |
| 2615 | // template specialization, make a note of that. |
| 2616 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2617 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2618 | |
| 2619 | // Check that all of the template parameters of the variable template |
| 2620 | // partial specialization are deducible from the template |
| 2621 | // arguments. If not, this variable template partial specialization |
| 2622 | // will never be used. |
| 2623 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 2624 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 2625 | TemplateParams->getDepth(), DeducibleParams); |
| 2626 | |
| 2627 | if (!DeducibleParams.all()) { |
| 2628 | unsigned NumNonDeducible = |
| 2629 | DeducibleParams.size() - DeducibleParams.count(); |
| 2630 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2631 | << /*variable template*/ 1 << (NumNonDeducible > 1) |
| 2632 | << SourceRange(TemplateNameLoc, RAngleLoc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2633 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 2634 | if (!DeducibleParams[I]) { |
| 2635 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 2636 | if (Param->getDeclName()) |
| 2637 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
| 2638 | << Param->getDeclName(); |
| 2639 | else |
| 2640 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 2641 | << "(anonymous)"; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2642 | } |
| 2643 | } |
| 2644 | } |
| 2645 | } else { |
| 2646 | // Create a new class template specialization declaration node for |
| 2647 | // this explicit specialization or friend declaration. |
| 2648 | Specialization = VarTemplateSpecializationDecl::Create( |
| 2649 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
| 2650 | VarTemplate, DI->getType(), DI, SC, Converted.data(), Converted.size()); |
| 2651 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 2652 | |
| 2653 | if (!PrevDecl) |
| 2654 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 2655 | } |
| 2656 | |
| 2657 | // C++ [temp.expl.spec]p6: |
| 2658 | // If a template, a member template or the member of a class template is |
| 2659 | // explicitly specialized then that specialization shall be declared |
| 2660 | // before the first use of that specialization that would cause an implicit |
| 2661 | // instantiation to take place, in every translation unit in which such a |
| 2662 | // use occurs; no diagnostic is required. |
| 2663 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 2664 | bool Okay = false; |
| 2665 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 2666 | // Is there any previous explicit specialization declaration? |
| 2667 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 2668 | Okay = true; |
| 2669 | break; |
| 2670 | } |
| 2671 | } |
| 2672 | |
| 2673 | if (!Okay) { |
| 2674 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 2675 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 2676 | << Name << Range; |
| 2677 | |
| 2678 | Diag(PrevDecl->getPointOfInstantiation(), |
| 2679 | diag::note_instantiation_required_here) |
| 2680 | << (PrevDecl->getTemplateSpecializationKind() != |
| 2681 | TSK_ImplicitInstantiation); |
| 2682 | return true; |
| 2683 | } |
| 2684 | } |
| 2685 | |
| 2686 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 2687 | Specialization->setLexicalDeclContext(CurContext); |
| 2688 | |
| 2689 | // Add the specialization into its lexical context, so that it can |
| 2690 | // be seen when iterating through the list of declarations in that |
| 2691 | // context. However, specializations are not found by name lookup. |
| 2692 | CurContext->addDecl(Specialization); |
| 2693 | |
| 2694 | // Note that this is an explicit specialization. |
| 2695 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 2696 | |
| 2697 | if (PrevDecl) { |
| 2698 | // Check that this isn't a redefinition of this specialization, |
| 2699 | // merging with previous declarations. |
| 2700 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
| 2701 | ForRedeclaration); |
| 2702 | PrevSpec.addDecl(PrevDecl); |
| 2703 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2704 | } else if (Specialization->isStaticDataMember() && |
| 2705 | Specialization->isOutOfLine()) { |
| 2706 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | // Link instantiations of static data members back to the template from |
| 2710 | // which they were instantiated. |
| 2711 | if (Specialization->isStaticDataMember()) |
| 2712 | Specialization->setInstantiationOfStaticDataMember( |
| 2713 | VarTemplate->getTemplatedDecl(), |
| 2714 | Specialization->getSpecializationKind()); |
| 2715 | |
| 2716 | return Specialization; |
| 2717 | } |
| 2718 | |
| 2719 | namespace { |
| 2720 | /// \brief A partial specialization whose template arguments have matched |
| 2721 | /// a given template-id. |
| 2722 | struct PartialSpecMatchResult { |
| 2723 | VarTemplatePartialSpecializationDecl *Partial; |
| 2724 | TemplateArgumentList *Args; |
| 2725 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2726 | } // end anonymous namespace |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2727 | |
| 2728 | DeclResult |
| 2729 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2730 | SourceLocation TemplateNameLoc, |
| 2731 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2732 | assert(Template && "A variable template id without template?"); |
| 2733 | |
| 2734 | // Check that the template argument list is well-formed for this template. |
| 2735 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2736 | if (CheckTemplateArgumentList( |
| 2737 | Template, TemplateNameLoc, |
| 2738 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2739 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2740 | return true; |
| 2741 | |
| 2742 | // Find the variable template specialization declaration that |
| 2743 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2744 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2745 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2746 | Converted, InsertPos)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2747 | // If we already have a variable template specialization, return it. |
| 2748 | return Spec; |
| 2749 | |
| 2750 | // This is the first time we have referenced this variable template |
| 2751 | // specialization. Create the canonical declaration and add it to |
| 2752 | // the set of specializations, based on the closest partial specialization |
| 2753 | // that it represents. That is, |
| 2754 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 2755 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
| 2756 | Converted.data(), Converted.size()); |
| 2757 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 2758 | bool AmbiguousPartialSpec = false; |
| 2759 | typedef PartialSpecMatchResult MatchResult; |
| 2760 | SmallVector<MatchResult, 4> Matched; |
| 2761 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 2762 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
| 2763 | /*ForTakingAddress=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2764 | |
| 2765 | // 1. Attempt to find the closest partial specialization that this |
| 2766 | // specializes, if any. |
| 2767 | // If any of the template arguments is dependent, then this is probably |
| 2768 | // a placeholder for an incomplete declarative context; which must be |
| 2769 | // complete by instantiation time. Thus, do not search through the partial |
| 2770 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2771 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 2772 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 2773 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2774 | bool InstantiationDependent = false; |
| 2775 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 2776 | TemplateArgs, InstantiationDependent)) { |
| 2777 | |
| 2778 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 2779 | Template->getPartialSpecializations(PartialSpecs); |
| 2780 | |
| 2781 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 2782 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 2783 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 2784 | |
| 2785 | if (TemplateDeductionResult Result = |
| 2786 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 2787 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2788 | // TODO: Actually use the failed-deduction info? |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2789 | FailedCandidates.addCandidate() |
| 2790 | .set(Partial, MakeDeductionFailureInfo(Context, Result, Info)); |
| 2791 | (void)Result; |
| 2792 | } else { |
| 2793 | Matched.push_back(PartialSpecMatchResult()); |
| 2794 | Matched.back().Partial = Partial; |
| 2795 | Matched.back().Args = Info.take(); |
| 2796 | } |
| 2797 | } |
| 2798 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2799 | if (Matched.size() >= 1) { |
| 2800 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 2801 | if (Matched.size() == 1) { |
| 2802 | // -- If exactly one matching specialization is found, the |
| 2803 | // instantiation is generated from that specialization. |
| 2804 | // We don't need to do anything for this. |
| 2805 | } else { |
| 2806 | // -- If more than one matching specialization is found, the |
| 2807 | // partial order rules (14.5.4.2) are used to determine |
| 2808 | // whether one of the specializations is more specialized |
| 2809 | // than the others. If none of the specializations is more |
| 2810 | // specialized than all of the other matching |
| 2811 | // specializations, then the use of the variable template is |
| 2812 | // ambiguous and the program is ill-formed. |
| 2813 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 2814 | PEnd = Matched.end(); |
| 2815 | P != PEnd; ++P) { |
| 2816 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 2817 | PointOfInstantiation) == |
| 2818 | P->Partial) |
| 2819 | Best = P; |
| 2820 | } |
| 2821 | |
| 2822 | // Determine if the best partial specialization is more specialized than |
| 2823 | // the others. |
| 2824 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2825 | PEnd = Matched.end(); |
| 2826 | P != PEnd; ++P) { |
| 2827 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 2828 | P->Partial, Best->Partial, |
| 2829 | PointOfInstantiation) != Best->Partial) { |
| 2830 | AmbiguousPartialSpec = true; |
| 2831 | break; |
| 2832 | } |
| 2833 | } |
| 2834 | } |
| 2835 | |
| 2836 | // Instantiate using the best variable template partial specialization. |
| 2837 | InstantiationPattern = Best->Partial; |
| 2838 | InstantiationArgs = Best->Args; |
| 2839 | } else { |
| 2840 | // -- If no match is found, the instantiation is generated |
| 2841 | // from the primary template. |
| 2842 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 2843 | } |
| 2844 | } |
| 2845 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2846 | // 2. Create the canonical declaration. |
| 2847 | // Note that we do not instantiate the variable just yet, since |
| 2848 | // instantiation is handled in DoMarkVarDeclReferenced(). |
| 2849 | // FIXME: LateAttrs et al.? |
| 2850 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 2851 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 2852 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 2853 | if (!Decl) |
| 2854 | return true; |
| 2855 | |
| 2856 | if (AmbiguousPartialSpec) { |
| 2857 | // Partial ordering did not produce a clear winner. Complain. |
| 2858 | Decl->setInvalidDecl(); |
| 2859 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 2860 | << Decl; |
| 2861 | |
| 2862 | // Print the matching partial specializations. |
| 2863 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2864 | PEnd = Matched.end(); |
| 2865 | P != PEnd; ++P) |
| 2866 | Diag(P->Partial->getLocation(), diag::note_partial_spec_match) |
| 2867 | << getTemplateArgumentBindingsText( |
| 2868 | P->Partial->getTemplateParameters(), *P->Args); |
| 2869 | return true; |
| 2870 | } |
| 2871 | |
| 2872 | if (VarTemplatePartialSpecializationDecl *D = |
| 2873 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 2874 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 2875 | |
| 2876 | assert(Decl && "No variable template specialization?"); |
| 2877 | return Decl; |
| 2878 | } |
| 2879 | |
| 2880 | ExprResult |
| 2881 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 2882 | const DeclarationNameInfo &NameInfo, |
| 2883 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2884 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2885 | |
| 2886 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 2887 | *TemplateArgs); |
| 2888 | if (Decl.isInvalid()) |
| 2889 | return ExprError(); |
| 2890 | |
| 2891 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 2892 | if (!Var->getTemplateSpecializationKind()) |
| 2893 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 2894 | NameInfo.getLoc()); |
| 2895 | |
| 2896 | // Build an ordinary singleton decl ref. |
| 2897 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2898 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2901 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2902 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2903 | LookupResult &R, |
| 2904 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2905 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2906 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2907 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2908 | // 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] | 2909 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2910 | // foo<int> could identify a single function unambiguously |
| 2911 | // This approach does NOT work, since f<int>(1); |
| 2912 | // gets resolved prior to resorting to overload resolution |
| 2913 | // i.e., template<class T> void f(double); |
| 2914 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2915 | |
| 2916 | // These should be filtered out by our callers. |
| 2917 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2918 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2919 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2920 | // In C++1y, check variable template ids. |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 2921 | bool InstantiationDependent; |
| 2922 | if (R.getAsSingle<VarTemplateDecl>() && |
| 2923 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2924 | *TemplateArgs, InstantiationDependent)) { |
| 2925 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 2926 | R.getAsSingle<VarTemplateDecl>(), |
| 2927 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2930 | // We don't want lookup warnings at this point. |
| 2931 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2932 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2933 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2934 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2935 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2936 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2937 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2938 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2939 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2940 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2941 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2944 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2945 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2946 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2947 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2948 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2949 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2950 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2951 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2952 | DeclContext *DC; |
| 2953 | if (!(DC = computeDeclContext(SS, false)) || |
| 2954 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2955 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 2956 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2957 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2958 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2959 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2960 | LookupTemplateName(R, (Scope*)nullptr, SS, QualType(), /*Entering*/ false, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2961 | MemberOfUnknownSpecialization); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2962 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2963 | if (R.isAmbiguous()) |
| 2964 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2965 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2966 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2967 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2968 | << NameInfo.getName() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2969 | return ExprError(); |
| 2970 | } |
| 2971 | |
| 2972 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2973 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 2974 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 2975 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2976 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2977 | return ExprError(); |
| 2978 | } |
| 2979 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2980 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2983 | /// \brief Form a dependent template name. |
| 2984 | /// |
| 2985 | /// This action forms a dependent template name given the template |
| 2986 | /// name and its (presumably dependent) scope specifier. For |
| 2987 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2988 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2989 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2990 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2991 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2992 | SourceLocation TemplateKWLoc, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2993 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2994 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2995 | bool EnteringContext, |
| 2996 | TemplateTy &Result) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2997 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 2998 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2999 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3000 | diag::warn_cxx98_compat_template_outside_of_template : |
| 3001 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3002 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 3003 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3004 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3005 | if (SS.isSet()) |
| 3006 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 3007 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3008 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3009 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3010 | // C++0x [temp.names]p5: |
| 3011 | // If a name prefixed by the keyword template is not the name of |
| 3012 | // a template, the program is ill-formed. [Note: the keyword |
| 3013 | // template may not be applied to non-template members of class |
| 3014 | // templates. -end note ] [ Note: as is the case with the |
| 3015 | // typename prefix, the template prefix is allowed in cases |
| 3016 | // where it is not strictly necessary; i.e., when the |
| 3017 | // nested-name-specifier or the expression on the left of the -> |
| 3018 | // or . is not dependent on a template-parameter, or the use |
| 3019 | // does not appear in the scope of a template. -end note] |
| 3020 | // |
| 3021 | // Note: C++03 was more strict here, because it banned the use of |
| 3022 | // the "template" keyword prior to a template-name that was not a |
| 3023 | // dependent name. C++ DR468 relaxed this requirement (the |
| 3024 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 3025 | // 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] | 3026 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 3027 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 3028 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3029 | MemberOfUnknownSpecialization); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3030 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 3031 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | 5ecbb1b | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 3032 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 3033 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3034 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3035 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3036 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3037 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3038 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3039 | << Name.getSourceRange() |
| 3040 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3041 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3042 | } else { |
| 3043 | // We found something; return it. |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3044 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3045 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3046 | } |
| 3047 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 3048 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3049 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3050 | switch (Name.getKind()) { |
| 3051 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3052 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3053 | Name.Identifier)); |
| 3054 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3055 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3056 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3057 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3058 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 3059 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3060 | |
| 3061 | case UnqualifiedId::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 3062 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3063 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3064 | default: |
| 3065 | break; |
| 3066 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3067 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3068 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3069 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3070 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3071 | << Name.getSourceRange() |
| 3072 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3073 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3074 | } |
| 3075 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3076 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3077 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3078 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3079 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3080 | QualType ArgType; |
| 3081 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3082 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3083 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3084 | switch(Arg.getKind()) { |
| 3085 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3086 | // C++ [temp.arg.type]p1: |
| 3087 | // A template-argument for a template-parameter which is a |
| 3088 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3089 | ArgType = Arg.getAsType(); |
| 3090 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3091 | break; |
| 3092 | case TemplateArgument::Template: { |
| 3093 | // We have a template type parameter but the template argument |
| 3094 | // is a template without any arguments. |
| 3095 | SourceRange SR = AL.getSourceRange(); |
| 3096 | TemplateName Name = Arg.getAsTemplate(); |
| 3097 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 3098 | << Name << SR; |
| 3099 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 3100 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3101 | |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3102 | return true; |
| 3103 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3104 | case TemplateArgument::Expression: { |
| 3105 | // We have a template type parameter but the template argument is an |
| 3106 | // expression; see if maybe it is missing the "typename" keyword. |
| 3107 | CXXScopeSpec SS; |
| 3108 | DeclarationNameInfo NameInfo; |
| 3109 | |
| 3110 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 3111 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3112 | NameInfo = ArgExpr->getNameInfo(); |
| 3113 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 3114 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 3115 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3116 | NameInfo = ArgExpr->getNameInfo(); |
| 3117 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 3118 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3119 | if (ArgExpr->isImplicitAccess()) { |
| 3120 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3121 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 3122 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3125 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3126 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 3127 | LookupParsedName(Result, CurScope, &SS); |
| 3128 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3129 | if (Result.getAsSingle<TypeDecl>() || |
| 3130 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3131 | LookupResult::NotFoundInCurrentInstantiation) { |
| 3132 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3133 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3134 | Diag(Loc, getLangOpts().MSVCCompat |
| 3135 | ? diag::ext_ms_template_type_arg_missing_typename |
| 3136 | : diag::err_template_arg_must_be_type_suggest) |
| 3137 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3138 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3139 | |
| 3140 | // Recover by synthesizing a type using the location information that we |
| 3141 | // already have. |
| 3142 | ArgType = |
| 3143 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 3144 | TypeLocBuilder TLB; |
| 3145 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 3146 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 3147 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3148 | TL.setNameLoc(NameInfo.getLoc()); |
| 3149 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 3150 | |
| 3151 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 3152 | // properly. |
| 3153 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 3154 | TemplateArgumentLocInfo(TSI)); |
| 3155 | |
| 3156 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3157 | } |
| 3158 | } |
| 3159 | // fallthrough |
| 3160 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3161 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3162 | // We have a template type parameter but the template argument |
| 3163 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 3164 | SourceRange SR = AL.getSourceRange(); |
| 3165 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3166 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3167 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3168 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3169 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3170 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3171 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3172 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3173 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3174 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3175 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3176 | ArgType = Context.getCanonicalType(ArgType); |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3177 | |
| 3178 | // Objective-C ARC: |
| 3179 | // If an explicitly-specified template argument type is a lifetime type |
| 3180 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3181 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3182 | ArgType->isObjCLifetimeType() && |
| 3183 | !ArgType.getObjCLifetime()) { |
| 3184 | Qualifiers Qs; |
| 3185 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 3186 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 3187 | } |
| 3188 | |
| 3189 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3190 | return false; |
| 3191 | } |
| 3192 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3193 | /// \brief Substitute template arguments into the default template argument for |
| 3194 | /// the given template type parameter. |
| 3195 | /// |
| 3196 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3197 | /// the substitution. |
| 3198 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3199 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3200 | /// for. |
| 3201 | /// |
| 3202 | /// \param TemplateLoc the location of the template name that started the |
| 3203 | /// template-id we are checking. |
| 3204 | /// |
| 3205 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3206 | /// terminates the template-id. |
| 3207 | /// |
| 3208 | /// \param Param the template template parameter whose default we are |
| 3209 | /// substituting into. |
| 3210 | /// |
| 3211 | /// \param Converted the list of template arguments provided for template |
| 3212 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3213 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3214 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3215 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3216 | TemplateDecl *Template, |
| 3217 | SourceLocation TemplateLoc, |
| 3218 | SourceLocation RAngleLoc, |
| 3219 | TemplateTypeParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3220 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3221 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3222 | |
| 3223 | // If the argument type is dependent, instantiate it now based |
| 3224 | // on the previously-computed template arguments. |
| 3225 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3226 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3227 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3228 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3229 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3230 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3231 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3232 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3233 | Converted.data(), Converted.size()); |
| 3234 | |
| 3235 | // Only substitute for the innermost template argument list. |
| 3236 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3237 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3238 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3239 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3240 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3241 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3242 | ArgType = |
| 3243 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 3244 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | return ArgType; |
| 3248 | } |
| 3249 | |
| 3250 | /// \brief Substitute template arguments into the default template argument for |
| 3251 | /// the given non-type template parameter. |
| 3252 | /// |
| 3253 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3254 | /// the substitution. |
| 3255 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3256 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3257 | /// for. |
| 3258 | /// |
| 3259 | /// \param TemplateLoc the location of the template name that started the |
| 3260 | /// template-id we are checking. |
| 3261 | /// |
| 3262 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3263 | /// terminates the template-id. |
| 3264 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3265 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3266 | /// substituting into. |
| 3267 | /// |
| 3268 | /// \param Converted the list of template arguments provided for template |
| 3269 | /// parameters that precede \p Param in the template parameter list. |
| 3270 | /// |
| 3271 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3272 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3273 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3274 | TemplateDecl *Template, |
| 3275 | SourceLocation TemplateLoc, |
| 3276 | SourceLocation RAngleLoc, |
| 3277 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3278 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3279 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3280 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3281 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3282 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3283 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3284 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3285 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3286 | Converted.data(), Converted.size()); |
| 3287 | |
| 3288 | // Only substitute for the innermost template argument list. |
| 3289 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3290 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3291 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3292 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3293 | |
Faisal Vali | 48401eb | 2015-11-19 19:20:17 +0000 | [diff] [blame] | 3294 | EnterExpressionEvaluationContext ConstantEvaluated(SemaRef, |
| 3295 | Sema::ConstantEvaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3296 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3299 | /// \brief Substitute template arguments into the default template argument for |
| 3300 | /// the given template template parameter. |
| 3301 | /// |
| 3302 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3303 | /// the substitution. |
| 3304 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3305 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3306 | /// for. |
| 3307 | /// |
| 3308 | /// \param TemplateLoc the location of the template name that started the |
| 3309 | /// template-id we are checking. |
| 3310 | /// |
| 3311 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3312 | /// terminates the template-id. |
| 3313 | /// |
| 3314 | /// \param Param the template template parameter whose default we are |
| 3315 | /// substituting into. |
| 3316 | /// |
| 3317 | /// \param Converted the list of template arguments provided for template |
| 3318 | /// parameters that precede \p Param in the template parameter list. |
| 3319 | /// |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3320 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 3321 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3322 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3323 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 3324 | static TemplateName |
| 3325 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3326 | TemplateDecl *Template, |
| 3327 | SourceLocation TemplateLoc, |
| 3328 | SourceLocation RAngleLoc, |
| 3329 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3330 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3331 | NestedNameSpecifierLoc &QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3332 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, Template, Converted, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3333 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3334 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3335 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3336 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3337 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3338 | Converted.data(), Converted.size()); |
| 3339 | |
| 3340 | // Only substitute for the innermost template argument list. |
| 3341 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3342 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3343 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3344 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3345 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3346 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3347 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3348 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3349 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3350 | QualifierLoc = |
| 3351 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3352 | if (!QualifierLoc) |
| 3353 | return TemplateName(); |
| 3354 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3355 | |
| 3356 | return SemaRef.SubstTemplateName( |
| 3357 | QualifierLoc, |
| 3358 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 3359 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 3360 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3361 | } |
| 3362 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3363 | /// \brief If the given template parameter has a default template |
| 3364 | /// argument, substitute into that default template argument and |
| 3365 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3366 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3367 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 3368 | SourceLocation TemplateLoc, |
| 3369 | SourceLocation RAngleLoc, |
| 3370 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3371 | SmallVectorImpl<TemplateArgument> |
| 3372 | &Converted, |
| 3373 | bool &HasDefaultArg) { |
| 3374 | HasDefaultArg = false; |
| 3375 | |
| 3376 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3377 | if (!hasVisibleDefaultArgument(TypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3378 | return TemplateArgumentLoc(); |
| 3379 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3380 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3381 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3382 | TemplateLoc, |
| 3383 | RAngleLoc, |
| 3384 | TypeParm, |
| 3385 | Converted); |
| 3386 | if (DI) |
| 3387 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 3388 | |
| 3389 | return TemplateArgumentLoc(); |
| 3390 | } |
| 3391 | |
| 3392 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 3393 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3394 | if (!hasVisibleDefaultArgument(NonTypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3395 | return TemplateArgumentLoc(); |
| 3396 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3397 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3398 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3399 | TemplateLoc, |
| 3400 | RAngleLoc, |
| 3401 | NonTypeParm, |
| 3402 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3403 | if (Arg.isInvalid()) |
| 3404 | return TemplateArgumentLoc(); |
| 3405 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3406 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3407 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 3408 | } |
| 3409 | |
| 3410 | TemplateTemplateParmDecl *TempTempParm |
| 3411 | = cast<TemplateTemplateParmDecl>(Param); |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3412 | if (!hasVisibleDefaultArgument(TempTempParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3413 | return TemplateArgumentLoc(); |
| 3414 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3415 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3416 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3417 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3418 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3419 | RAngleLoc, |
| 3420 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3421 | Converted, |
| 3422 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3423 | if (TName.isNull()) |
| 3424 | return TemplateArgumentLoc(); |
| 3425 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3426 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3427 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3428 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 3429 | } |
| 3430 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3431 | /// \brief Check that the given template argument corresponds to the given |
| 3432 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3433 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3434 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3435 | /// checked. |
| 3436 | /// |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3437 | /// \param Arg The template argument, which may be updated due to conversions. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3438 | /// |
| 3439 | /// \param Template The template in which the template argument resides. |
| 3440 | /// |
| 3441 | /// \param TemplateLoc The location of the template name for the template |
| 3442 | /// whose argument list we're matching. |
| 3443 | /// |
| 3444 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 3445 | /// the template argument list. |
| 3446 | /// |
| 3447 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 3448 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 3449 | /// |
| 3450 | /// \param Converted The checked, converted argument will be added to the |
| 3451 | /// end of this small vector. |
| 3452 | /// |
| 3453 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 3454 | /// explicitly written, deduced, etc. |
| 3455 | /// |
| 3456 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3457 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3458 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 3459 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3460 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3461 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3462 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3463 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3464 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3465 | // Check template type parameters. |
| 3466 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3467 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3468 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3469 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3470 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3471 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3472 | // with the template arguments we've seen thus far. But if the |
| 3473 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3474 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3475 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 3476 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3477 | |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3478 | if (NTTPType->isDependentType() && |
| 3479 | !isa<TemplateTemplateParmDecl>(Template) && |
| 3480 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3481 | // Do substitution on the type of the non-type template parameter. |
| 3482 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3483 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3484 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3485 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3486 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3487 | |
| 3488 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3489 | Converted.data(), Converted.size()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3490 | NTTPType = SubstType(NTTPType, |
| 3491 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 3492 | NTTP->getLocation(), |
| 3493 | NTTP->getDeclName()); |
| 3494 | // If that worked, check the non-type template parameter type |
| 3495 | // for validity. |
| 3496 | if (!NTTPType.isNull()) |
| 3497 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 3498 | NTTP->getLocation()); |
| 3499 | if (NTTPType.isNull()) |
| 3500 | return true; |
| 3501 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3502 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3503 | switch (Arg.getArgument().getKind()) { |
| 3504 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3505 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3506 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3507 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3508 | TemplateArgument Result; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3509 | ExprResult Res = |
| 3510 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 3511 | Result, CTAK); |
| 3512 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3513 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3514 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3515 | // If the resulting expression is new, then use it in place of the |
| 3516 | // old expression in the template argument. |
| 3517 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 3518 | TemplateArgument TA(Res.get()); |
| 3519 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 3520 | } |
| 3521 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3522 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3523 | break; |
| 3524 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3525 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3526 | case TemplateArgument::Declaration: |
| 3527 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3528 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3529 | // We've already checked this template argument, so just copy |
| 3530 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3531 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3532 | break; |
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::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3535 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3536 | // We were given a template template argument. It may not be ill-formed; |
| 3537 | // see below. |
| 3538 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3539 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 3540 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3541 | // We have a template argument such as \c T::template X, which we |
| 3542 | // parsed as a template template argument. However, since we now |
| 3543 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3544 | // template name into an expression. |
| 3545 | |
| 3546 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 3547 | Arg.getTemplateNameLoc()); |
| 3548 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 3549 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3550 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3551 | // FIXME: the template-template arg was a DependentTemplateName, |
| 3552 | // so it was provided with a template keyword. However, its source |
| 3553 | // location is not stored in the template argument structure. |
| 3554 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3555 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 3556 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 3557 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3558 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3559 | // If we parsed the template argument as a pack expansion, create a |
| 3560 | // pack expansion expression. |
| 3561 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3562 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3563 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3564 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3565 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3566 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3567 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3568 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3569 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3570 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3571 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3572 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3573 | break; |
| 3574 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3575 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3576 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3577 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3578 | // therefore cannot be a non-type template argument. |
| 3579 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 3580 | << Arg.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3581 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3582 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3583 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3584 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3585 | case TemplateArgument::Type: { |
| 3586 | // We have a non-type template parameter but the template |
| 3587 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3588 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3589 | // C++ [temp.arg]p2: |
| 3590 | // In a template-argument, an ambiguity between a type-id and |
| 3591 | // an expression is resolved to a type-id, regardless of the |
| 3592 | // form of the corresponding template-parameter. |
| 3593 | // |
| 3594 | // We warn specifically about this case, since it can be rather |
| 3595 | // confusing for users. |
| 3596 | QualType T = Arg.getArgument().getAsType(); |
| 3597 | SourceRange SR = Arg.getSourceRange(); |
| 3598 | if (T->isFunctionType()) |
| 3599 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 3600 | else |
| 3601 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 3602 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3603 | return true; |
| 3604 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3605 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3606 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3607 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3608 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3609 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3610 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3614 | // Check template template parameters. |
| 3615 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3616 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3617 | // Substitute into the template parameter list of the template |
| 3618 | // template parameter, since previously-supplied template arguments |
| 3619 | // may appear within the template template parameter. |
| 3620 | { |
| 3621 | // Set up a template instantiation context. |
| 3622 | LocalInstantiationScope Scope(*this); |
| 3623 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3624 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3625 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3626 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3627 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3628 | |
| 3629 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3630 | Converted.data(), Converted.size()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3631 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3632 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3633 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 3634 | if (!TempParm) |
| 3635 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3636 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3637 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3638 | switch (Arg.getArgument().getKind()) { |
| 3639 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3640 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3641 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3642 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3643 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3644 | if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3645 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3646 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3647 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3648 | break; |
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::Expression: |
| 3651 | case TemplateArgument::Type: |
| 3652 | // We have a template template parameter but the template |
| 3653 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3654 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3655 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3656 | return true; |
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::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3659 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3660 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3661 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3662 | case TemplateArgument::NullPtr: |
| 3663 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3664 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3665 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3666 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3667 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3668 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3669 | return false; |
| 3670 | } |
| 3671 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3672 | /// \brief Diagnose an arity mismatch in the |
| 3673 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 3674 | SourceLocation TemplateLoc, |
| 3675 | TemplateArgumentListInfo &TemplateArgs) { |
| 3676 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 3677 | unsigned NumParams = Params->size(); |
| 3678 | unsigned NumArgs = TemplateArgs.size(); |
| 3679 | |
| 3680 | SourceRange Range; |
| 3681 | if (NumArgs > NumParams) |
| 3682 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
| 3683 | TemplateArgs.getRAngleLoc()); |
| 3684 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3685 | << (NumArgs > NumParams) |
| 3686 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3687 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3688 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3689 | << Template << Range; |
| 3690 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3691 | << Params->getSourceRange(); |
| 3692 | return true; |
| 3693 | } |
| 3694 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3695 | /// \brief Check whether the template parameter is a pack expansion, and if so, |
| 3696 | /// determine the number of parameters produced by that expansion. For instance: |
| 3697 | /// |
| 3698 | /// \code |
| 3699 | /// template<typename ...Ts> struct A { |
| 3700 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 3701 | /// }; |
| 3702 | /// \endcode |
| 3703 | /// |
| 3704 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 3705 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3706 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3707 | if (NonTypeTemplateParmDecl *NTTP |
| 3708 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3709 | if (NTTP->isExpandedParameterPack()) |
| 3710 | return NTTP->getNumExpansionTypes(); |
| 3711 | } |
| 3712 | |
| 3713 | if (TemplateTemplateParmDecl *TTP |
| 3714 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 3715 | if (TTP->isExpandedParameterPack()) |
| 3716 | return TTP->getNumExpansionTemplateParameters(); |
| 3717 | } |
| 3718 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 3719 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3722 | /// Diagnose a missing template argument. |
| 3723 | template<typename TemplateParmDecl> |
| 3724 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
| 3725 | TemplateDecl *TD, |
| 3726 | const TemplateParmDecl *D, |
| 3727 | TemplateArgumentListInfo &Args) { |
| 3728 | // Dig out the most recent declaration of the template parameter; there may be |
| 3729 | // declarations of the template that are more recent than TD. |
| 3730 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
| 3731 | ->getTemplateParameters() |
| 3732 | ->getParam(D->getIndex())); |
| 3733 | |
| 3734 | // If there's a default argument that's not visible, diagnose that we're |
| 3735 | // missing a module import. |
| 3736 | llvm::SmallVector<Module*, 8> Modules; |
| 3737 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { |
| 3738 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
| 3739 | D->getDefaultArgumentLoc(), Modules, |
| 3740 | Sema::MissingImportKind::DefaultArgument, |
| 3741 | /*Recover*/ true); |
| 3742 | return true; |
| 3743 | } |
| 3744 | |
| 3745 | // FIXME: If there's a more recent default argument that *is* visible, |
| 3746 | // diagnose that it was declared too late. |
| 3747 | |
| 3748 | return diagnoseArityMismatch(S, TD, Loc, Args); |
| 3749 | } |
| 3750 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3751 | /// \brief Check that the given template argument list is well-formed |
| 3752 | /// for specializing the given template. |
| 3753 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 3754 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3755 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 3756 | bool PartialTemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3757 | SmallVectorImpl<TemplateArgument> &Converted) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3758 | // Make a copy of the template arguments for processing. Only make the |
| 3759 | // changes at the end when successful in matching the arguments to the |
| 3760 | // template. |
| 3761 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 3762 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3763 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3764 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3765 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3766 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3767 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3768 | // [...] The type and form of each template-argument specified in |
| 3769 | // a template-id shall match the type and form specified for the |
| 3770 | // corresponding parameter declared by the template in its |
| 3771 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3772 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3773 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3774 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3775 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3776 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 3777 | ParamEnd = Params->end(); |
| 3778 | Param != ParamEnd; /* increment in loop */) { |
| 3779 | // If we have an expanded parameter pack, make sure we don't have too |
| 3780 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3781 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3782 | if (*Expansions == ArgumentPack.size()) { |
| 3783 | // We're done with this parameter pack. Pack up its arguments and add |
| 3784 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3785 | Converted.push_back( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 3786 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3787 | ArgumentPack.clear(); |
| 3788 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3789 | // This argument is assigned to the next parameter. |
| 3790 | ++Param; |
| 3791 | continue; |
| 3792 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 3793 | // Not enough arguments for this parameter pack. |
| 3794 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3795 | << false |
| 3796 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3797 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3798 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3799 | << Template; |
| 3800 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3801 | << Params->getSourceRange(); |
| 3802 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3803 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3804 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3805 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3806 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3807 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3808 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3809 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3810 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3811 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3812 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3813 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3814 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3815 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 3816 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3817 | // 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] | 3818 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3819 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3820 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3821 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3822 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3823 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 3824 | return true; |
| 3825 | } |
| 3826 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3827 | // We're now done with this argument. |
| 3828 | ++ArgIdx; |
| 3829 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3830 | if ((*Param)->isTemplateParameterPack()) { |
| 3831 | // The template parameter was a template parameter pack, so take the |
| 3832 | // deduced argument and place it on the argument pack. Note that we |
| 3833 | // stay on the same template parameter so that we can deduce more |
| 3834 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 3835 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3836 | } else { |
| 3837 | // Move to the next template parameter. |
| 3838 | ++Param; |
| 3839 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3840 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3841 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 3842 | // the remaining arguments, because we don't know what parameters they'll |
| 3843 | // match up with. |
| 3844 | if (PackExpansionIntoNonPack) { |
| 3845 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3846 | // If we were part way through filling in an expanded parameter pack, |
| 3847 | // fall back to just producing individual arguments. |
| 3848 | Converted.insert(Converted.end(), |
| 3849 | ArgumentPack.begin(), ArgumentPack.end()); |
| 3850 | ArgumentPack.clear(); |
| 3851 | } |
| 3852 | |
| 3853 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3854 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3855 | ++ArgIdx; |
| 3856 | } |
| 3857 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3858 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3859 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3860 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3861 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3862 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3863 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3864 | // If we're checking a partial template argument list, we're done. |
| 3865 | if (PartialTemplateArgs) { |
| 3866 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 3867 | Converted.push_back( |
| 3868 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 3869 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3870 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3873 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3874 | // 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] | 3875 | if ((*Param)->isTemplateParameterPack()) { |
| 3876 | assert(!getExpandedPackSize(*Param) && |
| 3877 | "Should have dealt with this already"); |
| 3878 | |
| 3879 | // A non-expanded parameter pack before the end of the parameter list |
| 3880 | // only occurs for an ill-formed template parameter list, unless we've |
| 3881 | // got a partial argument list for a function template, so just bail out. |
| 3882 | if (Param + 1 != ParamEnd) |
| 3883 | return true; |
| 3884 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 3885 | Converted.push_back( |
| 3886 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3887 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3888 | |
| 3889 | ++Param; |
| 3890 | continue; |
| 3891 | } |
| 3892 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3893 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3894 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3895 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3896 | // Retrieve the default template argument from the template |
| 3897 | // parameter. For each kind of template parameter, we substitute the |
| 3898 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3899 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3900 | // the default argument. |
| 3901 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3902 | if (!hasVisibleDefaultArgument(TTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3903 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
| 3904 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3905 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3906 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3907 | Template, |
| 3908 | TemplateLoc, |
| 3909 | RAngleLoc, |
| 3910 | TTP, |
| 3911 | Converted); |
| 3912 | if (!ArgType) |
| 3913 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3914 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3915 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 3916 | ArgType); |
| 3917 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3918 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3919 | if (!hasVisibleDefaultArgument(NTTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3920 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
| 3921 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3922 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3923 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3924 | TemplateLoc, |
| 3925 | RAngleLoc, |
| 3926 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3927 | Converted); |
| 3928 | if (E.isInvalid()) |
| 3929 | return true; |
| 3930 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3931 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3932 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 3933 | } else { |
| 3934 | TemplateTemplateParmDecl *TempParm |
| 3935 | = cast<TemplateTemplateParmDecl>(*Param); |
| 3936 | |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3937 | if (!hasVisibleDefaultArgument(TempParm)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3938 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
| 3939 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3940 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3941 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3942 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3943 | TemplateLoc, |
| 3944 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3945 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3946 | Converted, |
| 3947 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3948 | if (Name.isNull()) |
| 3949 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3950 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3951 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3952 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3953 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3954 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3955 | // Introduce an instantiation record that describes where we are using |
| 3956 | // the default template argument. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3957 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 3958 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3959 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3960 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3961 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3962 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3963 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3964 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3965 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3966 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3967 | // Core issue 150 (assumed resolution): if this is a template template |
| 3968 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3969 | // template definition. |
| 3970 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3971 | NewArgs.addArgument(Arg); |
| 3972 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3973 | // Move to the next template parameter and argument. |
| 3974 | ++Param; |
| 3975 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3976 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3977 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 3978 | // If we're performing a partial argument substitution, allow any trailing |
| 3979 | // pack expansions; they might be empty. This can happen even if |
| 3980 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 3981 | // still dependent). |
| 3982 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 3983 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3984 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 3985 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 3986 | } |
| 3987 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3988 | // If we have any leftover arguments, then there were too many arguments. |
| 3989 | // Complain and fail. |
| 3990 | if (ArgIdx < NumArgs) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3991 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
| 3992 | |
| 3993 | // No problems found with the new argument list, propagate changes back |
| 3994 | // to caller. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 3995 | TemplateArgs = std::move(NewArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3996 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3997 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3998 | } |
| 3999 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4000 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4001 | class UnnamedLocalNoLinkageFinder |
| 4002 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4003 | { |
| 4004 | Sema &S; |
| 4005 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4006 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4007 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4008 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4009 | public: |
| 4010 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 4011 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4012 | bool Visit(QualType T) { |
| 4013 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4014 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4015 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4016 | #define TYPE(Class, Parent) \ |
| 4017 | bool Visit##Class##Type(const Class##Type *); |
| 4018 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 4019 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4020 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 4021 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4022 | #include "clang/AST/TypeNodes.def" |
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 | bool VisitTagDecl(const TagDecl *Tag); |
| 4025 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 4026 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4027 | } // end anonymous namespace |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4028 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4029 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4030 | return false; |
| 4031 | } |
| 4032 | |
| 4033 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 4034 | return Visit(T->getElementType()); |
| 4035 | } |
| 4036 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4037 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4038 | return Visit(T->getPointeeType()); |
| 4039 | } |
| 4040 | |
| 4041 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4042 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4043 | return Visit(T->getPointeeType()); |
| 4044 | } |
| 4045 | |
| 4046 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4047 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4048 | return Visit(T->getPointeeType()); |
| 4049 | } |
| 4050 | |
| 4051 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4052 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4053 | return Visit(T->getPointeeType()); |
| 4054 | } |
| 4055 | |
| 4056 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4057 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4058 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 4059 | } |
| 4060 | |
| 4061 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4062 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4063 | return Visit(T->getElementType()); |
| 4064 | } |
| 4065 | |
| 4066 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4067 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4068 | return Visit(T->getElementType()); |
| 4069 | } |
| 4070 | |
| 4071 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4072 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4073 | return Visit(T->getElementType()); |
| 4074 | } |
| 4075 | |
| 4076 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4077 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4078 | return Visit(T->getElementType()); |
| 4079 | } |
| 4080 | |
| 4081 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4082 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4083 | return Visit(T->getElementType()); |
| 4084 | } |
| 4085 | |
| 4086 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 4087 | return Visit(T->getElementType()); |
| 4088 | } |
| 4089 | |
| 4090 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 4091 | return Visit(T->getElementType()); |
| 4092 | } |
| 4093 | |
| 4094 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 4095 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4096 | for (const auto &A : T->param_types()) { |
| 4097 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4098 | return true; |
| 4099 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4100 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4101 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 4105 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4106 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 4110 | const UnresolvedUsingType*) { |
| 4111 | return false; |
| 4112 | } |
| 4113 | |
| 4114 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 4115 | return false; |
| 4116 | } |
| 4117 | |
| 4118 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 4119 | return Visit(T->getUnderlyingType()); |
| 4120 | } |
| 4121 | |
| 4122 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 4123 | return false; |
| 4124 | } |
| 4125 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4126 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 4127 | const UnaryTransformType*) { |
| 4128 | return false; |
| 4129 | } |
| 4130 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4131 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 4132 | return Visit(T->getDeducedType()); |
| 4133 | } |
| 4134 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4135 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 4136 | return VisitTagDecl(T->getDecl()); |
| 4137 | } |
| 4138 | |
| 4139 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 4140 | return VisitTagDecl(T->getDecl()); |
| 4141 | } |
| 4142 | |
| 4143 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 4144 | const TemplateTypeParmType*) { |
| 4145 | return false; |
| 4146 | } |
| 4147 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4148 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 4149 | const SubstTemplateTypeParmPackType *) { |
| 4150 | return false; |
| 4151 | } |
| 4152 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4153 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 4154 | const TemplateSpecializationType*) { |
| 4155 | return false; |
| 4156 | } |
| 4157 | |
| 4158 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 4159 | const InjectedClassNameType* T) { |
| 4160 | return VisitTagDecl(T->getDecl()); |
| 4161 | } |
| 4162 | |
| 4163 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 4164 | const DependentNameType* T) { |
| 4165 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4166 | } |
| 4167 | |
| 4168 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 4169 | const DependentTemplateSpecializationType* T) { |
| 4170 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4171 | } |
| 4172 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4173 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 4174 | const PackExpansionType* T) { |
| 4175 | return Visit(T->getPattern()); |
| 4176 | } |
| 4177 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4178 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 4179 | return false; |
| 4180 | } |
| 4181 | |
| 4182 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 4183 | const ObjCInterfaceType *) { |
| 4184 | return false; |
| 4185 | } |
| 4186 | |
| 4187 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 4188 | const ObjCObjectPointerType *) { |
| 4189 | return false; |
| 4190 | } |
| 4191 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4192 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 4193 | return Visit(T->getValueType()); |
| 4194 | } |
| 4195 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 4196 | bool UnnamedLocalNoLinkageFinder::VisitPipeType(const PipeType* T) { |
| 4197 | return false; |
| 4198 | } |
| 4199 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4200 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 4201 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4202 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4203 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4204 | diag::warn_cxx98_compat_template_arg_local_type : |
| 4205 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4206 | << S.Context.getTypeDeclType(Tag) << SR; |
| 4207 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4208 | } |
| 4209 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 4210 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4211 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4212 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4213 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 4214 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4215 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 4216 | return true; |
| 4217 | } |
| 4218 | |
| 4219 | return false; |
| 4220 | } |
| 4221 | |
| 4222 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 4223 | NestedNameSpecifier *NNS) { |
| 4224 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 4225 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4226 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4227 | switch (NNS->getKind()) { |
| 4228 | case NestedNameSpecifier::Identifier: |
| 4229 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4230 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4231 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 4232 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4233 | return false; |
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 | case NestedNameSpecifier::TypeSpec: |
| 4236 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 4237 | return Visit(QualType(NNS->getAsType(), 0)); |
| 4238 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4239 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4240 | } |
| 4241 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4242 | /// \brief Check a template argument against its corresponding |
| 4243 | /// template type parameter. |
| 4244 | /// |
| 4245 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 4246 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4247 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4248 | TypeSourceInfo *ArgInfo) { |
| 4249 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4250 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 4251 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 4252 | |
| 4253 | if (Arg->isVariablyModifiedType()) { |
| 4254 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4255 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4256 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4257 | } |
| 4258 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4259 | // C++03 [temp.arg.type]p2: |
| 4260 | // A local type, a type with no linkage, an unnamed type or a type |
| 4261 | // compounded from any of these types shall not be used as a |
| 4262 | // template-argument for a template type-parameter. |
| 4263 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4264 | // 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] | 4265 | // a warning. |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 4266 | bool NeedsCheck; |
| 4267 | if (LangOpts.CPlusPlus11) |
| 4268 | NeedsCheck = |
| 4269 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 4270 | SR.getBegin()) || |
| 4271 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_local_type, |
| 4272 | SR.getBegin()); |
| 4273 | else |
| 4274 | NeedsCheck = Arg->hasUnnamedOrLocalType(); |
| 4275 | |
| 4276 | if (NeedsCheck) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4277 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 4278 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 4279 | } |
| 4280 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4281 | return false; |
| 4282 | } |
| 4283 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4284 | enum NullPointerValueKind { |
| 4285 | NPV_NotNullPointer, |
| 4286 | NPV_NullPointer, |
| 4287 | NPV_Error |
| 4288 | }; |
| 4289 | |
| 4290 | /// \brief Determine whether the given template argument is a null pointer |
| 4291 | /// value of the appropriate type. |
| 4292 | static NullPointerValueKind |
| 4293 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 4294 | QualType ParamType, Expr *Arg) { |
| 4295 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 4296 | return NPV_NotNullPointer; |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4297 | |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 4298 | if (!S.isCompleteType(Arg->getExprLoc(), ParamType)) |
David Majnemer | b54368c | 2015-09-11 20:55:29 +0000 | [diff] [blame] | 4299 | llvm_unreachable( |
| 4300 | "Incomplete parameter type in isNullPointerValueTemplateArgument!"); |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4301 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 4302 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4303 | return NPV_NotNullPointer; |
| 4304 | |
| 4305 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4306 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 4307 | if (ArgRV.isInvalid()) |
| 4308 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4309 | Arg = ArgRV.get(); |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4310 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4311 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4312 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4313 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4314 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4315 | EvalResult.HasSideEffects) { |
| 4316 | SourceLocation DiagLoc = Arg->getExprLoc(); |
| 4317 | |
| 4318 | // If our only note is the usual "invalid subexpression" note, just point |
| 4319 | // the caret at its location rather than producing an essentially |
| 4320 | // redundant note. |
| 4321 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 4322 | diag::note_invalid_subexpr_in_const_expr) { |
| 4323 | DiagLoc = Notes[0].first; |
| 4324 | Notes.clear(); |
| 4325 | } |
| 4326 | |
| 4327 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 4328 | << Arg->getType() << Arg->getSourceRange(); |
| 4329 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 4330 | S.Diag(Notes[I].first, Notes[I].second); |
| 4331 | |
| 4332 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4333 | return NPV_Error; |
| 4334 | } |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4335 | |
| 4336 | // C++11 [temp.arg.nontype]p1: |
| 4337 | // - an address constant expression of type std::nullptr_t |
| 4338 | if (Arg->getType()->isNullPtrType()) |
| 4339 | return NPV_NullPointer; |
| 4340 | |
| 4341 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 4342 | // - a constant expression that evaluates to a null member pointer value |
| 4343 | // (4.11); or |
| 4344 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 4345 | (EvalResult.Val.isMemberPointer() && |
| 4346 | !EvalResult.Val.getMemberPointerDecl())) { |
| 4347 | // If our expression has an appropriate type, we've succeeded. |
| 4348 | bool ObjCLifetimeConversion; |
| 4349 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 4350 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 4351 | ObjCLifetimeConversion)) |
| 4352 | return NPV_NullPointer; |
| 4353 | |
| 4354 | // The types didn't match, but we know we got a null pointer; complain, |
| 4355 | // then recover as if the types were correct. |
| 4356 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 4357 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4358 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4359 | return NPV_NullPointer; |
| 4360 | } |
| 4361 | |
| 4362 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 4363 | // constant, suggest a cast to the appropriate type. |
| 4364 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 4365 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 4366 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4367 | << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 4368 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()), |
| 4369 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4370 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4371 | return NPV_NullPointer; |
| 4372 | } |
| 4373 | |
| 4374 | // FIXME: If we ever want to support general, address-constant expressions |
| 4375 | // as non-type template arguments, we should return the ExprResult here to |
| 4376 | // be interpreted by the caller. |
| 4377 | return NPV_NotNullPointer; |
| 4378 | } |
| 4379 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4380 | /// \brief Checks whether the given template argument is compatible with its |
| 4381 | /// template parameter. |
| 4382 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 4383 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 4384 | Expr *Arg, QualType ArgType) { |
| 4385 | bool ObjCLifetimeConversion; |
| 4386 | if (ParamType->isPointerType() && |
| 4387 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 4388 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 4389 | ObjCLifetimeConversion)) { |
| 4390 | // For pointer-to-object types, qualification conversions are |
| 4391 | // permitted. |
| 4392 | } else { |
| 4393 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 4394 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 4395 | // C++ [temp.arg.nontype]p5b3: |
| 4396 | // For a non-type template-parameter of type reference to |
| 4397 | // object, no conversions apply. The type referred to by the |
| 4398 | // reference may be more cv-qualified than the (otherwise |
| 4399 | // identical) type of the template- argument. The |
| 4400 | // template-parameter is bound directly to the |
| 4401 | // template-argument, which shall be an lvalue. |
| 4402 | |
| 4403 | // FIXME: Other qualifiers? |
| 4404 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 4405 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 4406 | |
| 4407 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 4408 | S.Diag(Arg->getLocStart(), |
| 4409 | diag::err_template_arg_ref_bind_ignores_quals) |
| 4410 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 4411 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4412 | return true; |
| 4413 | } |
| 4414 | } |
| 4415 | } |
| 4416 | |
| 4417 | // At this point, the template argument refers to an object or |
| 4418 | // function with external linkage. We now need to check whether the |
| 4419 | // argument and parameter types are compatible. |
| 4420 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 4421 | ParamType.getNonReferenceType())) { |
| 4422 | // We can't perform this conversion or binding. |
| 4423 | if (ParamType->isReferenceType()) |
| 4424 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 4425 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
| 4426 | else |
| 4427 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4428 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
| 4429 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4430 | return true; |
| 4431 | } |
| 4432 | } |
| 4433 | |
| 4434 | return false; |
| 4435 | } |
| 4436 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4437 | /// \brief Checks whether the given template argument is the address |
| 4438 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4439 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4440 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 4441 | NonTypeTemplateParmDecl *Param, |
| 4442 | QualType ParamType, |
| 4443 | Expr *ArgIn, |
| 4444 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4445 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4446 | Expr *Arg = ArgIn; |
| 4447 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4448 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4449 | bool AddressTaken = false; |
| 4450 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4451 | if (S.getLangOpts().MicrosoftExt) { |
| 4452 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 4453 | // dereference and address-of operators. |
| 4454 | Arg = Arg->IgnoreParenCasts(); |
| 4455 | |
| 4456 | bool ExtWarnMSTemplateArg = false; |
| 4457 | UnaryOperatorKind FirstOpKind; |
| 4458 | SourceLocation FirstOpLoc; |
| 4459 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4460 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 4461 | if (UnOpKind == UO_Deref) |
| 4462 | ExtWarnMSTemplateArg = true; |
| 4463 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 4464 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 4465 | if (!AddrOpLoc.isValid()) { |
| 4466 | FirstOpKind = UnOpKind; |
| 4467 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 4468 | } |
| 4469 | } else |
| 4470 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4471 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4472 | if (FirstOpLoc.isValid()) { |
| 4473 | if (ExtWarnMSTemplateArg) |
| 4474 | S.Diag(ArgIn->getLocStart(), diag::ext_ms_deref_template_argument) |
| 4475 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4476 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4477 | if (FirstOpKind == UO_AddrOf) |
| 4478 | AddressTaken = true; |
| 4479 | else if (Arg->getType()->isPointerType()) { |
| 4480 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 4481 | // constant expression. |
| 4482 | assert(FirstOpKind == UO_Deref); |
| 4483 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4484 | << Arg->getSourceRange(); |
| 4485 | } |
| 4486 | } |
| 4487 | } else { |
| 4488 | // See through any implicit casts we added to fix the type. |
| 4489 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4490 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4491 | // C++ [temp.arg.nontype]p1: |
| 4492 | // |
| 4493 | // A template-argument for a non-type, non-template |
| 4494 | // template-parameter shall be one of: [...] |
| 4495 | // |
| 4496 | // -- the address of an object or function with external |
| 4497 | // linkage, including function templates and function |
| 4498 | // template-ids but excluding non-static class members, |
| 4499 | // expressed as & id-expression where the & is optional if |
| 4500 | // the name refers to a function or array, or if the |
| 4501 | // corresponding template-parameter is a reference; or |
| 4502 | |
| 4503 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4504 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4505 | bool ExtraParens = false; |
| 4506 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 4507 | if (!Invalid && !ExtraParens) { |
| 4508 | S.Diag(Arg->getLocStart(), |
| 4509 | S.getLangOpts().CPlusPlus11 |
| 4510 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 4511 | : diag::ext_template_arg_extra_parens) |
| 4512 | << Arg->getSourceRange(); |
| 4513 | ExtraParens = true; |
| 4514 | } |
| 4515 | |
| 4516 | Arg = Parens->getSubExpr(); |
| 4517 | } |
| 4518 | |
| 4519 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4520 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4521 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4522 | |
| 4523 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4524 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 4525 | Arg = UnOp->getSubExpr(); |
| 4526 | AddressTaken = true; |
| 4527 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 4528 | } |
| 4529 | } |
| 4530 | |
| 4531 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4532 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4533 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4534 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4535 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4536 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 4537 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 4538 | |
| 4539 | // If our parameter has pointer type, check for a null template value. |
| 4540 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 4541 | NullPointerValueKind NPV; |
| 4542 | // dllimport'd entities aren't constant but are available inside of template |
| 4543 | // arguments. |
| 4544 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 4545 | NPV = NPV_NotNullPointer; |
| 4546 | else |
| 4547 | NPV = isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn); |
| 4548 | switch (NPV) { |
| 4549 | case NPV_NullPointer: |
| 4550 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4551 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4552 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4553 | return false; |
| 4554 | |
| 4555 | case NPV_Error: |
| 4556 | return true; |
| 4557 | |
| 4558 | case NPV_NotNullPointer: |
| 4559 | break; |
| 4560 | } |
| 4561 | } |
| 4562 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4563 | // Stop checking the precise nature of the argument if it is value dependent, |
| 4564 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4565 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4566 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4567 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4568 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4569 | |
| 4570 | if (isa<CXXUuidofExpr>(Arg)) { |
| 4571 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 4572 | ArgIn, Arg, ArgType)) |
| 4573 | return true; |
| 4574 | |
| 4575 | Converted = TemplateArgument(ArgIn); |
| 4576 | return false; |
| 4577 | } |
| 4578 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4579 | if (!DRE) { |
| 4580 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4581 | << Arg->getSourceRange(); |
| 4582 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4583 | return true; |
| 4584 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4585 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4586 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4587 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4588 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4589 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4590 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4591 | return true; |
| 4592 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4593 | |
| 4594 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4595 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4596 | if (!Method->isStatic()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4597 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4598 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4599 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4600 | return true; |
| 4601 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4602 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4603 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4604 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 4605 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4606 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4607 | // A non-type template argument must refer to an object or function. |
| 4608 | if (!Func && !Var) { |
| 4609 | // We found something, but we don't know specifically what it is. |
| 4610 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 4611 | << Arg->getSourceRange(); |
| 4612 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 4613 | return true; |
| 4614 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4615 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4616 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4617 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4618 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4619 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 4620 | diag::ext_template_arg_object_internal) |
| 4621 | << !Func << Entity << Arg->getSourceRange(); |
| 4622 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4623 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4624 | } else if (!Entity->hasLinkage()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4625 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 4626 | << !Func << Entity << Arg->getSourceRange(); |
| 4627 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4628 | << !Func; |
| 4629 | return true; |
| 4630 | } |
| 4631 | |
| 4632 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4633 | // If the template parameter has pointer type, the function decays. |
| 4634 | if (ParamType->isPointerType() && !AddressTaken) |
| 4635 | ArgType = S.Context.getPointerType(Func->getType()); |
| 4636 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 4637 | // If we originally had an address-of operator, but the |
| 4638 | // parameter has reference type, complain and (if things look |
| 4639 | // like they will work) drop the address-of operator. |
| 4640 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 4641 | ParamType.getNonReferenceType())) { |
| 4642 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4643 | << ParamType; |
| 4644 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4645 | return true; |
| 4646 | } |
| 4647 | |
| 4648 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4649 | << ParamType |
| 4650 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4651 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4652 | |
| 4653 | ArgType = Func->getType(); |
| 4654 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4655 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4656 | // A value of reference type is not an object. |
| 4657 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4658 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4659 | diag::err_template_arg_reference_var) |
| 4660 | << Var->getType() << Arg->getSourceRange(); |
| 4661 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4662 | return true; |
| 4663 | } |
| 4664 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4665 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 4666 | if (Var->getTLSKind()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4667 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 4668 | << Arg->getSourceRange(); |
| 4669 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 4670 | return true; |
| 4671 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4672 | |
| 4673 | // If the template parameter has pointer type, we must have taken |
| 4674 | // the address of this object. |
| 4675 | if (ParamType->isReferenceType()) { |
| 4676 | if (AddressTaken) { |
| 4677 | // If we originally had an address-of operator, but the |
| 4678 | // parameter has reference type, complain and (if things look |
| 4679 | // like they will work) drop the address-of operator. |
| 4680 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 4681 | ParamType.getNonReferenceType())) { |
| 4682 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4683 | << ParamType; |
| 4684 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4685 | return true; |
| 4686 | } |
| 4687 | |
| 4688 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4689 | << ParamType |
| 4690 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4691 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4692 | |
| 4693 | ArgType = Var->getType(); |
| 4694 | } |
| 4695 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 4696 | if (Var->getType()->isArrayType()) { |
| 4697 | // Array-to-pointer decay. |
| 4698 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 4699 | } else { |
| 4700 | // If the template parameter has pointer type but the address of |
| 4701 | // this object was not taken, complain and (possibly) recover by |
| 4702 | // taking the address of the entity. |
| 4703 | ArgType = S.Context.getPointerType(Var->getType()); |
| 4704 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 4705 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4706 | << ParamType; |
| 4707 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4708 | return true; |
| 4709 | } |
| 4710 | |
| 4711 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4712 | << ParamType |
| 4713 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 4714 | |
| 4715 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4716 | } |
| 4717 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4718 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4719 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4720 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 4721 | Arg, ArgType)) |
| 4722 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4723 | |
| 4724 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4725 | Converted = |
| 4726 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 4727 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4728 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4729 | } |
| 4730 | |
| 4731 | /// \brief Checks whether the given template argument is a pointer to |
| 4732 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4733 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 4734 | NonTypeTemplateParmDecl *Param, |
| 4735 | QualType ParamType, |
| 4736 | Expr *&ResultArg, |
| 4737 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4738 | bool Invalid = false; |
| 4739 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4740 | // Check for a null pointer value. |
| 4741 | Expr *Arg = ResultArg; |
| 4742 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 4743 | case NPV_Error: |
| 4744 | return true; |
| 4745 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 4746 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4747 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4748 | /*isNullPtr*/true); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4749 | return false; |
| 4750 | case NPV_NotNullPointer: |
| 4751 | break; |
| 4752 | } |
| 4753 | |
| 4754 | bool ObjCLifetimeConversion; |
| 4755 | if (S.IsQualificationConversion(Arg->getType(), |
| 4756 | ParamType.getNonReferenceType(), |
| 4757 | false, ObjCLifetimeConversion)) { |
| 4758 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4759 | Arg->getValueKind()).get(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4760 | ResultArg = Arg; |
| 4761 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), |
| 4762 | ParamType.getNonReferenceType())) { |
| 4763 | // We can't perform this conversion. |
| 4764 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4765 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4766 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4767 | return true; |
| 4768 | } |
| 4769 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4770 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4771 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4772 | Arg = Cast->getSubExpr(); |
| 4773 | |
| 4774 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4775 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4776 | // A template-argument for a non-type, non-template |
| 4777 | // template-parameter shall be one of: [...] |
| 4778 | // |
| 4779 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4780 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4781 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4782 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4783 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4784 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4785 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4786 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4787 | S.Diag(Arg->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4788 | S.getLangOpts().CPlusPlus11 ? |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4789 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 4790 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4791 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4792 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4793 | } |
| 4794 | |
| 4795 | Arg = Parens->getSubExpr(); |
| 4796 | } |
| 4797 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4798 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4799 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4800 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4801 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4802 | // A pointer-to-member constant written &Class::member. |
| 4803 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4804 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4805 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 4806 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4807 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4808 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4809 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4810 | // A constant of pointer-to-member type. |
| 4811 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 4812 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 4813 | if (VD->getType()->isMemberPointerType()) { |
David Majnemer | cd053cd | 2013-12-10 00:40:58 +0000 | [diff] [blame] | 4814 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4815 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4816 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4817 | } else { |
| 4818 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4819 | Converted = TemplateArgument(VD, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4820 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4821 | return Invalid; |
| 4822 | } |
| 4823 | } |
| 4824 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4825 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4826 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4827 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4828 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4829 | if (!DRE) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4830 | return S.Diag(Arg->getLocStart(), |
| 4831 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4832 | << Arg->getSourceRange(); |
| 4833 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4834 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 4835 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 4836 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4837 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4838 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4839 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 4840 | "Only non-static member pointers can make it here"); |
| 4841 | |
| 4842 | // Okay: this is the address of a non-static member, and therefore |
| 4843 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4844 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4845 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4846 | } else { |
| 4847 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4848 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4849 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4850 | return Invalid; |
| 4851 | } |
| 4852 | |
| 4853 | // 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] | 4854 | S.Diag(Arg->getLocStart(), |
| 4855 | diag::err_template_arg_not_pointer_to_member_form) |
| 4856 | << Arg->getSourceRange(); |
| 4857 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4858 | return true; |
| 4859 | } |
| 4860 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4861 | /// \brief Check a template argument against its corresponding |
| 4862 | /// non-type template parameter. |
| 4863 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4864 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4865 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4866 | /// returns the converted template argument. \p ParamType is the |
| 4867 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4868 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4869 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4870 | TemplateArgument &Converted, |
| 4871 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4872 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4873 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4874 | // If either the parameter has a dependent type or the argument is |
| 4875 | // type-dependent, there's nothing we can check now. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4876 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4877 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 4878 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4879 | return Arg; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4880 | } |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4881 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4882 | // We should have already dropped all cv-qualifiers by now. |
| 4883 | assert(!ParamType.hasQualifiers() && |
| 4884 | "non-type template parameter type cannot be qualified"); |
| 4885 | |
| 4886 | if (CTAK == CTAK_Deduced && |
| 4887 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 4888 | // C++ [temp.deduct.type]p17: |
| 4889 | // If, in the declaration of a function template with a non-type |
| 4890 | // template-parameter, the non-type template-parameter is used |
| 4891 | // in an expression in the function parameter-list and, if the |
| 4892 | // corresponding template-argument is deduced, the |
| 4893 | // template-argument type shall match the type of the |
| 4894 | // template-parameter exactly, except that a template-argument |
| 4895 | // deduced from an array bound may be of any integral type. |
| 4896 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 4897 | << Arg->getType().getUnqualifiedType() |
| 4898 | << ParamType.getUnqualifiedType(); |
| 4899 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4900 | return ExprError(); |
| 4901 | } |
| 4902 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4903 | if (getLangOpts().CPlusPlus1z) { |
| 4904 | // FIXME: We can do some limited checking for a value-dependent but not |
| 4905 | // type-dependent argument. |
| 4906 | if (Arg->isValueDependent()) { |
| 4907 | Converted = TemplateArgument(Arg); |
| 4908 | return Arg; |
| 4909 | } |
| 4910 | |
| 4911 | // C++1z [temp.arg.nontype]p1: |
| 4912 | // A template-argument for a non-type template parameter shall be |
| 4913 | // a converted constant expression of the type of the template-parameter. |
| 4914 | APValue Value; |
| 4915 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 4916 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 4917 | if (ArgResult.isInvalid()) |
| 4918 | return ExprError(); |
| 4919 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4920 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 4921 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4922 | // Convert the APValue to a TemplateArgument. |
| 4923 | switch (Value.getKind()) { |
| 4924 | case APValue::Uninitialized: |
| 4925 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4926 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4927 | break; |
| 4928 | case APValue::Int: |
| 4929 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4930 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4931 | break; |
| 4932 | case APValue::MemberPointer: { |
| 4933 | assert(ParamType->isMemberPointerType()); |
| 4934 | |
| 4935 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 4936 | if (!Value.getMemberPointerPath().empty()) { |
| 4937 | Diag(Arg->getLocStart(), |
| 4938 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 4939 | << Value.getMemberPointerDecl() << ParamType |
| 4940 | << Arg->getSourceRange(); |
| 4941 | return ExprError(); |
| 4942 | } |
| 4943 | |
| 4944 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4945 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 4946 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4947 | break; |
| 4948 | } |
| 4949 | case APValue::LValue: { |
| 4950 | // For a non-type template-parameter of pointer or reference type, |
| 4951 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4952 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 4953 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4954 | // -- a temporary object |
| 4955 | // -- a string literal |
| 4956 | // -- the result of a typeid expression, or |
| 4957 | // -- a predefind __func__ variable |
| 4958 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 4959 | if (isa<CXXUuidofExpr>(E)) { |
| 4960 | Converted = TemplateArgument(const_cast<Expr*>(E)); |
| 4961 | break; |
| 4962 | } |
| 4963 | Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4964 | << Arg->getSourceRange(); |
| 4965 | return ExprError(); |
| 4966 | } |
| 4967 | auto *VD = const_cast<ValueDecl *>( |
| 4968 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 4969 | // -- a subobject |
| 4970 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 4971 | VD && VD->getType()->isArrayType() && |
| 4972 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 4973 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 4974 | // Per defect report (no number yet): |
| 4975 | // ... other than a pointer to the first element of a complete array |
| 4976 | // object. |
| 4977 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 4978 | Value.isLValueOnePastTheEnd()) { |
| 4979 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 4980 | << Value.getAsString(Context, ParamType); |
| 4981 | return ExprError(); |
| 4982 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4983 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4984 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4985 | assert((!VD || !ParamType->isNullPtrType()) && |
| 4986 | "non-null value of type nullptr_t?"); |
| 4987 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 4988 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4989 | break; |
| 4990 | } |
| 4991 | case APValue::AddrLabelDiff: |
| 4992 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
| 4993 | case APValue::Float: |
| 4994 | case APValue::ComplexInt: |
| 4995 | case APValue::ComplexFloat: |
| 4996 | case APValue::Vector: |
| 4997 | case APValue::Array: |
| 4998 | case APValue::Struct: |
| 4999 | case APValue::Union: |
| 5000 | llvm_unreachable("invalid kind for template argument"); |
| 5001 | } |
| 5002 | |
| 5003 | return ArgResult.get(); |
| 5004 | } |
| 5005 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5006 | // C++ [temp.arg.nontype]p5: |
| 5007 | // The following conversions are performed on each expression used |
| 5008 | // as a non-type template-argument. If a non-type |
| 5009 | // template-argument cannot be converted to the type of the |
| 5010 | // corresponding template-parameter then the program is |
| 5011 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5012 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5013 | // C++11: |
| 5014 | // -- for a non-type template-parameter of integral or |
| 5015 | // enumeration type, conversions permitted in a converted |
| 5016 | // constant expression are applied. |
| 5017 | // |
| 5018 | // C++98: |
| 5019 | // -- for a non-type template-parameter of integral or |
| 5020 | // enumeration type, integral promotions (4.5) and integral |
| 5021 | // conversions (4.7) are applied. |
| 5022 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5023 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5024 | // We can't check arbitrary value-dependent arguments. |
| 5025 | // FIXME: If there's no viable conversion to the template parameter type, |
| 5026 | // we should be able to diagnose that prior to instantiation. |
| 5027 | if (Arg->isValueDependent()) { |
| 5028 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5029 | return Arg; |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5030 | } |
| 5031 | |
| 5032 | // C++ [temp.arg.nontype]p1: |
| 5033 | // A template-argument for a non-type, non-template template-parameter |
| 5034 | // shall be one of: |
| 5035 | // |
| 5036 | // -- for a non-type template-parameter of integral or enumeration |
| 5037 | // type, a converted constant expression of the type of the |
| 5038 | // template-parameter; or |
| 5039 | llvm::APSInt Value; |
| 5040 | ExprResult ArgResult = |
| 5041 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 5042 | CCEK_TemplateArg); |
| 5043 | if (ArgResult.isInvalid()) |
| 5044 | return ExprError(); |
| 5045 | |
| 5046 | // Widen the argument value to sizeof(parameter type). This is almost |
| 5047 | // always a no-op, except when the parameter type is bool. In |
| 5048 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 5049 | QualType IntegerType = ParamType; |
| 5050 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 5051 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 5052 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 5053 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5054 | Converted = TemplateArgument(Context, Value, |
| 5055 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5056 | return ArgResult; |
| 5057 | } |
| 5058 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5059 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 5060 | if (ArgResult.isInvalid()) |
| 5061 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5062 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5063 | |
| 5064 | QualType ArgType = Arg->getType(); |
| 5065 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5066 | // C++ [temp.arg.nontype]p1: |
| 5067 | // A template-argument for a non-type, non-template |
| 5068 | // template-parameter shall be one of: |
| 5069 | // |
| 5070 | // -- an integral constant-expression of integral or enumeration |
| 5071 | // type; or |
| 5072 | // -- the name of a non-type template-parameter; or |
| 5073 | SourceLocation NonConstantLoc; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5074 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5075 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5076 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5077 | diag::err_template_arg_not_integral_or_enumeral) |
| 5078 | << ArgType << Arg->getSourceRange(); |
| 5079 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5080 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5081 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5082 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 5083 | QualType T; |
| 5084 | |
| 5085 | public: |
| 5086 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 5087 | |
| 5088 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 5089 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5090 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 5091 | } |
| 5092 | } Diagnoser(ArgType); |
| 5093 | |
| 5094 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5095 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5096 | if (!Arg) |
| 5097 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5098 | } |
| 5099 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5100 | // From here on out, all we care about is the unqualified form |
| 5101 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5102 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5103 | |
| 5104 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 5105 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5106 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5107 | } else if (ParamType->isBooleanType()) { |
| 5108 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5109 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5110 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 5111 | !ParamType->isEnumeralType()) { |
| 5112 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5113 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5114 | } else { |
| 5115 | // We can't perform this conversion. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5116 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5117 | diag::err_template_arg_not_convertible) |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5118 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5119 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5120 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5121 | } |
| 5122 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5123 | // Add the value of this argument to the list of converted |
| 5124 | // arguments. We use the bitwidth and signedness of the template |
| 5125 | // parameter. |
| 5126 | if (Arg->isValueDependent()) { |
| 5127 | // The argument is value-dependent. Create a new |
| 5128 | // TemplateArgument with the converted expression. |
| 5129 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5130 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5131 | } |
| 5132 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5133 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5134 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 5135 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5136 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5137 | if (ParamType->isBooleanType()) { |
| 5138 | // Value must be zero or one. |
| 5139 | Value = Value != 0; |
| 5140 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 5141 | if (Value.getBitWidth() != AllowedBits) |
| 5142 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5143 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5144 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5145 | llvm::APSInt OldValue = Value; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5146 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5147 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5148 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5149 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5150 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5151 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5152 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5153 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5154 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5155 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5156 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5157 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5158 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5159 | << Arg->getSourceRange(); |
| 5160 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5161 | } |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5162 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5163 | // Complain if we overflowed the template parameter's type. |
| 5164 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5165 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5166 | RequiredBits = OldValue.getActiveBits(); |
| 5167 | else if (OldValue.isUnsigned()) |
| 5168 | RequiredBits = OldValue.getActiveBits() + 1; |
| 5169 | else |
| 5170 | RequiredBits = OldValue.getMinSignedBits(); |
| 5171 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5172 | Diag(Arg->getLocStart(), |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5173 | diag::warn_template_arg_too_large) |
| 5174 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5175 | << Arg->getSourceRange(); |
| 5176 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5177 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5178 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5179 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5180 | Converted = TemplateArgument(Context, Value, |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 5181 | ParamType->isEnumeralType() |
| 5182 | ? Context.getCanonicalType(ParamType) |
| 5183 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5184 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5185 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5186 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5187 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5188 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 5189 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5190 | // Handle pointer-to-function, reference-to-function, and |
| 5191 | // pointer-to-member-function all in (roughly) the same way. |
| 5192 | if (// -- For a non-type template-parameter of type pointer to |
| 5193 | // function, only the function-to-pointer conversion (4.3) is |
| 5194 | // applied. If the template-argument represents a set of |
| 5195 | // overloaded functions (or a pointer to such), the matching |
| 5196 | // function is selected from the set (13.4). |
| 5197 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5198 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5199 | // -- For a non-type template-parameter of type reference to |
| 5200 | // function, no conversions apply. If the template-argument |
| 5201 | // represents a set of overloaded functions, the matching |
| 5202 | // function is selected from the set (13.4). |
| 5203 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5204 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5205 | // -- For a non-type template-parameter of type pointer to |
| 5206 | // member function, no conversions apply. If the |
| 5207 | // template-argument represents a set of overloaded member |
| 5208 | // functions, the matching member function is selected from |
| 5209 | // the set (13.4). |
| 5210 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5211 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5212 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5213 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5214 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5215 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5216 | true, |
| 5217 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5218 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5219 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5220 | |
| 5221 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5222 | ArgType = Arg->getType(); |
| 5223 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5224 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5225 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5226 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5227 | if (!ParamType->isMemberPointerType()) { |
| 5228 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5229 | ParamType, |
| 5230 | Arg, Converted)) |
| 5231 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5232 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5233 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5234 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5235 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5236 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5237 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5238 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5239 | } |
| 5240 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 5241 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5242 | // -- for a non-type template-parameter of type pointer to |
| 5243 | // object, qualification conversions (4.4) and the |
| 5244 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5245 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5246 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5247 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5248 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5249 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5250 | ParamType, |
| 5251 | Arg, Converted)) |
| 5252 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5253 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5254 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5255 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5256 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5257 | // -- For a non-type template-parameter of type reference to |
| 5258 | // object, no conversions apply. The type referred to by the |
| 5259 | // reference may be more cv-qualified than the (otherwise |
| 5260 | // identical) type of the template-argument. The |
| 5261 | // template-parameter is bound directly to the |
| 5262 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5263 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5264 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5265 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5266 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5267 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 5268 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5269 | true, |
| 5270 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5271 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5272 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5273 | |
| 5274 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5275 | ArgType = Arg->getType(); |
| 5276 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5277 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5278 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5279 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5280 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5281 | ParamType, |
| 5282 | Arg, Converted)) |
| 5283 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5284 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5285 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5286 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5287 | // Deal with parameters of type std::nullptr_t. |
| 5288 | if (ParamType->isNullPtrType()) { |
| 5289 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 5290 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5291 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5292 | } |
| 5293 | |
| 5294 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 5295 | case NPV_NotNullPointer: |
| 5296 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 5297 | << Arg->getType() << ParamType; |
| 5298 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5299 | return ExprError(); |
| 5300 | |
| 5301 | case NPV_Error: |
| 5302 | return ExprError(); |
| 5303 | |
| 5304 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 5305 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5306 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 5307 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5308 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5309 | } |
| 5310 | } |
| 5311 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5312 | // -- For a non-type template-parameter of type pointer to data |
| 5313 | // member, qualification conversions (4.4) are applied. |
| 5314 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 5315 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5316 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5317 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5318 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5319 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5320 | } |
| 5321 | |
| 5322 | /// \brief Check a template argument against its corresponding |
| 5323 | /// template template parameter. |
| 5324 | /// |
| 5325 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 5326 | /// It returns true if an error occurred, and false otherwise. |
| 5327 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 5328 | TemplateArgumentLoc &Arg, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5329 | unsigned ArgumentPackIndex) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5330 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5331 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 5332 | if (!Template) { |
| 5333 | // Any dependent template name is fine. |
| 5334 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 5335 | return false; |
| 5336 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5337 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5338 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5339 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5340 | // the name of a class template or an alias template, expressed as an |
| 5341 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5342 | // primary class templates are considered when matching the |
| 5343 | // template template argument with the corresponding parameter; |
| 5344 | // partial specializations are not considered even if their |
| 5345 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5346 | // |
| 5347 | // Note that we also allow template template parameters here, which |
| 5348 | // will happen when we are dealing with, e.g., class template |
| 5349 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5350 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5351 | !isa<TemplateTemplateParmDecl>(Template) && |
| 5352 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5353 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5354 | "Only function templates are possible here"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5355 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5356 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5357 | << Template; |
| 5358 | } |
| 5359 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5360 | TemplateParameterList *Params = Param->getTemplateParameters(); |
| 5361 | if (Param->isExpandedParameterPack()) |
| 5362 | Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); |
| 5363 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5364 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5365 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5366 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5367 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5368 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5369 | } |
| 5370 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5371 | /// \brief Given a non-type template argument that refers to a |
| 5372 | /// declaration and the type of its corresponding non-type template |
| 5373 | /// parameter, produce an expression that properly refers to that |
| 5374 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5375 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5376 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 5377 | QualType ParamType, |
| 5378 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 5379 | // C++ [temp.param]p8: |
| 5380 | // |
| 5381 | // A non-type template-parameter of type "array of T" or |
| 5382 | // "function returning T" is adjusted to be of type "pointer to |
| 5383 | // T" or "pointer to function returning T", respectively. |
| 5384 | if (ParamType->isArrayType()) |
| 5385 | ParamType = Context.getArrayDecayedType(ParamType); |
| 5386 | else if (ParamType->isFunctionType()) |
| 5387 | ParamType = Context.getPointerType(ParamType); |
| 5388 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5389 | // For a NULL non-type template argument, return nullptr casted to the |
| 5390 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5391 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5392 | return ImpCastExprToType( |
| 5393 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 5394 | ParamType, |
| 5395 | ParamType->getAs<MemberPointerType>() |
| 5396 | ? CK_NullToMemberPointer |
| 5397 | : CK_NullToPointer); |
| 5398 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5399 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 5400 | "Only declaration template arguments permitted here"); |
| 5401 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5402 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 5403 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5404 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 5405 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 5406 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5407 | // If the value is a class member, we might have a pointer-to-member. |
| 5408 | // Determine whether the non-type template template parameter is of |
| 5409 | // pointer-to-member type. If so, we need to build an appropriate |
| 5410 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 5411 | // would refer to the member itself. |
| 5412 | if (ParamType->isMemberPointerType()) { |
| 5413 | QualType ClassType |
| 5414 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 5415 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5416 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5417 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5418 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 5419 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5420 | |
| 5421 | // The actual value-ness of this is unimportant, but for |
| 5422 | // internal consistency's sake, references to instance methods |
| 5423 | // are r-values. |
| 5424 | ExprValueKind VK = VK_LValue; |
| 5425 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 5426 | VK = VK_RValue; |
| 5427 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5428 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5429 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5430 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5431 | Loc, |
| 5432 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5433 | if (RefExpr.isInvalid()) |
| 5434 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5435 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5436 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5437 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5438 | // We might need to perform a trailing qualification conversion, since |
| 5439 | // the element type on the parameter could be more qualified than the |
| 5440 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5441 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5442 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5443 | ParamType.getUnqualifiedType(), false, |
| 5444 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5445 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5446 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5447 | assert(!RefExpr.isInvalid() && |
| 5448 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5449 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5450 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5451 | } |
| 5452 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5453 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5454 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5455 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5456 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5457 | // When the non-type template parameter is a pointer, take the |
| 5458 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5459 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5460 | if (RefExpr.isInvalid()) |
| 5461 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5462 | |
| 5463 | if (T->isFunctionType() || T->isArrayType()) { |
| 5464 | // Decay functions and arrays. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5465 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5466 | if (RefExpr.isInvalid()) |
| 5467 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5468 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5469 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5470 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5471 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5472 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5473 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5474 | } |
| 5475 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5476 | ExprValueKind VK = VK_RValue; |
| 5477 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5478 | // If the non-type template parameter has reference type, qualify the |
| 5479 | // resulting declaration reference with the extra qualifiers on the |
| 5480 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5481 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 5482 | VK = VK_LValue; |
| 5483 | T = Context.getQualifiedType(T, |
| 5484 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5485 | } else if (isa<FunctionDecl>(VD)) { |
| 5486 | // References to functions are always lvalues. |
| 5487 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5488 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5489 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5490 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5491 | } |
| 5492 | |
| 5493 | /// \brief Construct a new expression that refers to the given |
| 5494 | /// integral template argument with the given source-location |
| 5495 | /// information. |
| 5496 | /// |
| 5497 | /// This routine takes care of the mapping from an integral template |
| 5498 | /// argument (which may have any integral type) to the appropriate |
| 5499 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5500 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5501 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 5502 | SourceLocation Loc) { |
| 5503 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 5504 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5505 | QualType OrigT = Arg.getIntegralType(); |
| 5506 | |
| 5507 | // If this is an enum type that we're instantiating, we need to use an integer |
| 5508 | // type the same size as the enumerator. We don't want to build an |
| 5509 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 5510 | // any integral type with C++11 enum classes, make sure we create the right |
| 5511 | // type of literal for it. |
| 5512 | QualType T = OrigT; |
| 5513 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 5514 | T = ET->getDecl()->getIntegerType(); |
| 5515 | |
| 5516 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5517 | if (T->isAnyCharacterType()) { |
Aaron Ballman | 9a17c85 | 2016-01-07 20:59:26 +0000 | [diff] [blame] | 5518 | // This does not need to handle u8 character literals because those are |
| 5519 | // 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] | 5520 | CharacterLiteral::CharacterKind Kind; |
| 5521 | if (T->isWideCharType()) |
| 5522 | Kind = CharacterLiteral::Wide; |
| 5523 | else if (T->isChar16Type()) |
| 5524 | Kind = CharacterLiteral::UTF16; |
| 5525 | else if (T->isChar32Type()) |
| 5526 | Kind = CharacterLiteral::UTF32; |
| 5527 | else |
| 5528 | Kind = CharacterLiteral::Ascii; |
| 5529 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5530 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 5531 | Kind, T, Loc); |
| 5532 | } else if (T->isBooleanType()) { |
| 5533 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 5534 | T, Loc); |
| 5535 | } else if (T->isNullPtrType()) { |
| 5536 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 5537 | } else { |
| 5538 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5539 | } |
| 5540 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5541 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5542 | // FIXME: This is a hack. We need a better way to handle substituted |
| 5543 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5544 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 5545 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5546 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5547 | Loc, Loc); |
| 5548 | } |
| 5549 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5550 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5551 | } |
| 5552 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5553 | /// \brief Match two template parameters within template parameter lists. |
| 5554 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 5555 | bool Complain, |
| 5556 | Sema::TemplateParameterListEqualKind Kind, |
| 5557 | SourceLocation TemplateArgLoc) { |
| 5558 | // Check the actual kind (type, non-type, template). |
| 5559 | if (Old->getKind() != New->getKind()) { |
| 5560 | if (Complain) { |
| 5561 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 5562 | if (TemplateArgLoc.isValid()) { |
| 5563 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5564 | NextDiag = diag::note_template_param_different_kind; |
| 5565 | } |
| 5566 | S.Diag(New->getLocation(), NextDiag) |
| 5567 | << (Kind != Sema::TPL_TemplateMatch); |
| 5568 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 5569 | << (Kind != Sema::TPL_TemplateMatch); |
| 5570 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5571 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5572 | return false; |
| 5573 | } |
| 5574 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5575 | // Check that both are parameter packs are neither are parameter packs. |
| 5576 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5577 | // template template parameter, the template template parameter can have |
| 5578 | // a parameter pack where the template template argument does not. |
| 5579 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 5580 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5581 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5582 | if (Complain) { |
| 5583 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 5584 | if (TemplateArgLoc.isValid()) { |
| 5585 | S.Diag(TemplateArgLoc, |
| 5586 | diag::err_template_arg_template_params_mismatch); |
| 5587 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 5588 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5589 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5590 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 5591 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 5592 | : 2; |
| 5593 | S.Diag(New->getLocation(), NextDiag) |
| 5594 | << ParamKind << New->isParameterPack(); |
| 5595 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 5596 | << ParamKind << Old->isParameterPack(); |
| 5597 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5598 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5599 | return false; |
| 5600 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5601 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5602 | // For non-type template parameters, check the type of the parameter. |
| 5603 | if (NonTypeTemplateParmDecl *OldNTTP |
| 5604 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 5605 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
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 | // If we are matching a template template argument to a template |
| 5608 | // template parameter and one of the non-type template parameter types |
| 5609 | // is dependent, then we must wait until template instantiation time |
| 5610 | // to actually compare the arguments. |
| 5611 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5612 | (OldNTTP->getType()->isDependentType() || |
| 5613 | NewNTTP->getType()->isDependentType())) |
| 5614 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5615 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5616 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 5617 | if (Complain) { |
| 5618 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 5619 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5620 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5621 | diag::err_template_arg_template_params_mismatch); |
| 5622 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 5623 | } |
| 5624 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 5625 | << NewNTTP->getType() |
| 5626 | << (Kind != Sema::TPL_TemplateMatch); |
| 5627 | S.Diag(OldNTTP->getLocation(), |
| 5628 | diag::note_template_nontype_parm_prev_declaration) |
| 5629 | << OldNTTP->getType(); |
| 5630 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5631 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5632 | return false; |
| 5633 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5634 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5635 | return true; |
| 5636 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5637 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5638 | // For template template parameters, check the template parameter types. |
| 5639 | // The template parameter lists of template template |
| 5640 | // parameters must agree. |
| 5641 | if (TemplateTemplateParmDecl *OldTTP |
| 5642 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5643 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5644 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 5645 | OldTTP->getTemplateParameters(), |
| 5646 | Complain, |
| 5647 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5648 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5649 | : Kind), |
| 5650 | TemplateArgLoc); |
| 5651 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5652 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5653 | return true; |
| 5654 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5655 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5656 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 5657 | /// lists. |
| 5658 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5659 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5660 | TemplateParameterList *New, |
| 5661 | TemplateParameterList *Old, |
| 5662 | Sema::TemplateParameterListEqualKind Kind, |
| 5663 | SourceLocation TemplateArgLoc) { |
| 5664 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 5665 | if (TemplateArgLoc.isValid()) { |
| 5666 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5667 | NextDiag = diag::note_template_param_list_different_arity; |
| 5668 | } |
| 5669 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 5670 | << (New->size() > Old->size()) |
| 5671 | << (Kind != Sema::TPL_TemplateMatch) |
| 5672 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 5673 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 5674 | << (Kind != Sema::TPL_TemplateMatch) |
| 5675 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 5676 | } |
| 5677 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5678 | /// \brief Determine whether the given template parameter lists are |
| 5679 | /// equivalent. |
| 5680 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5681 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5682 | /// source code as part of a new template declaration. |
| 5683 | /// |
| 5684 | /// \param Old The old template parameter list, typically found via |
| 5685 | /// name lookup of the template declared with this template parameter |
| 5686 | /// list. |
| 5687 | /// |
| 5688 | /// \param Complain If true, this routine will produce a diagnostic if |
| 5689 | /// the template parameter lists are not equivalent. |
| 5690 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5691 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5692 | /// |
| 5693 | /// \param TemplateArgLoc If this source location is valid, then we |
| 5694 | /// are actually checking the template parameter list of a template |
| 5695 | /// argument (New) against the template parameter list of its |
| 5696 | /// corresponding template template parameter (Old). We produce |
| 5697 | /// slightly different diagnostics in this scenario. |
| 5698 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5699 | /// \returns True if the template parameter lists are equal, false |
| 5700 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5701 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5702 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 5703 | TemplateParameterList *Old, |
| 5704 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5705 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5706 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5707 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 5708 | if (Complain) |
| 5709 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5710 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5711 | |
| 5712 | return false; |
| 5713 | } |
| 5714 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5715 | // C++0x [temp.arg.template]p3: |
| 5716 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5717 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5718 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5719 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5720 | // template-parameter-list of P. [...] |
| 5721 | TemplateParameterList::iterator NewParm = New->begin(); |
| 5722 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5723 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5724 | OldParmEnd = Old->end(); |
| 5725 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 5726 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 5727 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5728 | if (NewParm == NewParmEnd) { |
| 5729 | if (Complain) |
| 5730 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5731 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5732 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5733 | return false; |
| 5734 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5735 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5736 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5737 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5738 | return false; |
| 5739 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5740 | ++NewParm; |
| 5741 | continue; |
| 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 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5745 | // [...] When P's template- parameter-list contains a template parameter |
| 5746 | // pack (14.5.3), the template parameter pack will match zero or more |
| 5747 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5748 | // template-parameter-list of A with the same type and form as the |
| 5749 | // template parameter pack in P (ignoring whether those template |
| 5750 | // parameters are template parameter packs). |
| 5751 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 5752 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5753 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5754 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5755 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5756 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5757 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5758 | // Make sure we exhausted all of the arguments. |
| 5759 | if (NewParm != NewParmEnd) { |
| 5760 | if (Complain) |
| 5761 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5762 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5763 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5764 | return false; |
| 5765 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5766 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5767 | return true; |
| 5768 | } |
| 5769 | |
| 5770 | /// \brief Check whether a template can be declared within this scope. |
| 5771 | /// |
| 5772 | /// If the template declaration is valid in this scope, returns |
| 5773 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5774 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5775 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 5776 | if (!S) |
| 5777 | return false; |
| 5778 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5779 | // Find the nearest enclosing declaration scope. |
| 5780 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5781 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5782 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5783 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5784 | // C++ [temp]p4: |
| 5785 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 5786 | DeclContext *Ctx = S->getEntity(); |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5787 | if (Ctx && Ctx->isExternCContext()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5788 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5789 | << TemplateParams->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5790 | |
Eli Friedman | dfbd0c4 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 5791 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5792 | Ctx = Ctx->getParent(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5793 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5794 | // C++ [temp]p2: |
| 5795 | // A template-declaration can appear only as a namespace scope or |
| 5796 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 5797 | if (Ctx) { |
| 5798 | if (Ctx->isFileContext()) |
| 5799 | return false; |
| 5800 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 5801 | // C++ [temp.mem]p2: |
| 5802 | // A local class shall not have member templates. |
| 5803 | if (RD->isLocalClass()) |
| 5804 | return Diag(TemplateParams->getTemplateLoc(), |
| 5805 | diag::err_template_inside_local_class) |
| 5806 | << TemplateParams->getSourceRange(); |
| 5807 | else |
| 5808 | return false; |
| 5809 | } |
| 5810 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5811 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5812 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5813 | diag::err_template_outside_namespace_or_class_scope) |
| 5814 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5815 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5816 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5817 | /// \brief Determine what kind of template specialization the given declaration |
| 5818 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5819 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5820 | if (!D) |
| 5821 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5822 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5823 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 5824 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5825 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 5826 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5827 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 5828 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5829 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5830 | return TSK_Undeclared; |
| 5831 | } |
| 5832 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5833 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5834 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5835 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5836 | /// This routine determines whether a template specialization can be declared |
| 5837 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5838 | /// |
| 5839 | /// \param S the semantic analysis object for which this check is being |
| 5840 | /// performed. |
| 5841 | /// |
| 5842 | /// \param Specialized the entity being specialized or instantiated, which |
| 5843 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5844 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5845 | /// member class). |
| 5846 | /// |
| 5847 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 5848 | /// |
| 5849 | /// \param Loc the location of the explicit specialization or instantiation of |
| 5850 | /// this entity. |
| 5851 | /// |
| 5852 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 5853 | /// a class template. |
| 5854 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5855 | /// \returns true if there was an error that we cannot recover from, false |
| 5856 | /// otherwise. |
| 5857 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 5858 | NamedDecl *Specialized, |
| 5859 | NamedDecl *PrevDecl, |
| 5860 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5861 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5862 | // Keep these "kind" numbers in sync with the %select statements in the |
| 5863 | // various diagnostics emitted by this routine. |
| 5864 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5865 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5866 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5867 | else if (isa<VarTemplateDecl>(Specialized)) |
| 5868 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5869 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5870 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5871 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5872 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5873 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5874 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5875 | else if (isa<RecordDecl>(Specialized)) |
| 5876 | EntityKind = 7; |
| 5877 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 5878 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5879 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5880 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5881 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5882 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5883 | return true; |
| 5884 | } |
| 5885 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5886 | // C++ [temp.expl.spec]p2: |
| 5887 | // An explicit specialization shall be declared in the namespace |
| 5888 | // of which the template is a member, or, for member templates, in |
| 5889 | // the namespace of which the enclosing class or enclosing class |
| 5890 | // template is a member. An explicit specialization of a member |
| 5891 | // function, member class or static data member of a class |
| 5892 | // template shall be declared in the namespace of which the class |
| 5893 | // template is a member. Such a declaration may also be a |
| 5894 | // definition. If the declaration is not a definition, the |
| 5895 | // specialization may be defined later in the name- space in which |
| 5896 | // the explicit specialization was declared, or in a namespace |
| 5897 | // that encloses the one in which the explicit specialization was |
| 5898 | // declared. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5899 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5900 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5901 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5902 | return true; |
| 5903 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5904 | |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5905 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5906 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 5907 | // Do not warn for class scope explicit specialization during |
| 5908 | // instantiation, warning was already emitted during pattern |
| 5909 | // semantic analysis. |
| 5910 | if (!S.ActiveTemplateInstantiations.size()) |
| 5911 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 5912 | << Specialized; |
| 5913 | } else { |
| 5914 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5915 | << Specialized; |
| 5916 | return true; |
| 5917 | } |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5918 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5919 | |
Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 5920 | if (S.CurContext->isRecord() && |
| 5921 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 5922 | // Make sure that we're specializing in the right record context. |
| 5923 | // Otherwise, things can go horribly wrong. |
| 5924 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5925 | << Specialized; |
| 5926 | return true; |
| 5927 | } |
| 5928 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5929 | // C++ [temp.class.spec]p6: |
| 5930 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5931 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 5932 | // and 14.5.2). |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5933 | DeclContext *SpecializedContext |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5934 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5935 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5936 | |
| 5937 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
| 5938 | // namespace. |
| 5939 | // Note that HandleDeclarator() performs this check for explicit |
| 5940 | // specializations of function templates, static data members, and member |
| 5941 | // functions, so we skip the check here for those kinds of entities. |
| 5942 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
| 5943 | // Should we refactor that check, so that it occurs later? |
| 5944 | if (!DC->Encloses(SpecializedContext) && |
| 5945 | !(isa<FunctionTemplateDecl>(Specialized) || |
| 5946 | isa<FunctionDecl>(Specialized) || |
| 5947 | isa<VarTemplateDecl>(Specialized) || |
| 5948 | isa<VarDecl>(Specialized))) { |
| 5949 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 5950 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 5951 | << EntityKind << Specialized; |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 5952 | else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 5953 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
| 5954 | if (S.getLangOpts().MicrosoftExt) |
| 5955 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 5956 | S.Diag(Loc, Diag) << EntityKind << Specialized |
| 5957 | << cast<NamedDecl>(SpecializedContext); |
| 5958 | } else |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5959 | llvm_unreachable("unexpected namespace context for specialization"); |
| 5960 | |
| 5961 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 5962 | } else if ((!PrevDecl || |
| 5963 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 5964 | getTemplateSpecializationKind(PrevDecl) == |
| 5965 | TSK_ImplicitInstantiation)) { |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5966 | // C++ [temp.exp.spec]p2: |
| 5967 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5968 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5969 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5970 | // An explicit specialization of a member function, member class or |
| 5971 | // static data member of a class template shall be declared in the |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5972 | // namespace of which the class template is a member. |
| 5973 | // |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5974 | // C++11 [temp.expl.spec]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5975 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5976 | // the specialized template. |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5977 | // C++11 [temp.explicit]p3: |
| 5978 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 5979 | // template. |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5980 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5981 | bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext); |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5982 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5983 | assert(!IsCPlusPlus11Extension && |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5984 | "DC encloses TU but isn't in enclosing namespace set"); |
| 5985 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | 8ce6315 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 5986 | << EntityKind << Specialized; |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5987 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 5988 | int Diag; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5989 | if (!IsCPlusPlus11Extension) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5990 | Diag = diag::err_template_spec_decl_out_of_scope; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5991 | else if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5992 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 5993 | else |
| 5994 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 5995 | S.Diag(Loc, Diag) |
| 5996 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 5997 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5998 | |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5999 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6000 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6001 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6002 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6003 | return false; |
| 6004 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6005 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6006 | static SourceRange findTemplateParameter(unsigned Depth, Expr *E) { |
| 6007 | if (!E->isInstantiationDependent()) |
| 6008 | return SourceLocation(); |
| 6009 | DependencyChecker Checker(Depth); |
| 6010 | Checker.TraverseStmt(E); |
| 6011 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 6012 | return E->getSourceRange(); |
| 6013 | return Checker.MatchLoc; |
| 6014 | } |
| 6015 | |
| 6016 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 6017 | if (!TL.getType()->isDependentType()) |
| 6018 | return SourceLocation(); |
| 6019 | DependencyChecker Checker(Depth); |
| 6020 | Checker.TraverseTypeLoc(TL); |
| 6021 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 6022 | return TL.getSourceRange(); |
| 6023 | return Checker.MatchLoc; |
| 6024 | } |
| 6025 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6026 | /// \brief Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6027 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6028 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6029 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 6030 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6031 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 6032 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6033 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6034 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 6035 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6036 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6037 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6038 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6039 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6040 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6041 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6042 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6043 | |
| 6044 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6045 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 6046 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6047 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 6048 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6049 | |
| 6050 | // Strip off any implicit casts we added as part of type checking. |
| 6051 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 6052 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6053 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6054 | // C++ [temp.class.spec]p8: |
| 6055 | // A non-type argument is non-specialized if it is the name of a |
| 6056 | // non-type parameter. All other non-type arguments are |
| 6057 | // specialized. |
| 6058 | // |
| 6059 | // Below, we check the two conditions that only apply to |
| 6060 | // specialized non-type arguments, so skip any non-specialized |
| 6061 | // arguments. |
| 6062 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6063 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6064 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6065 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6066 | // C++ [temp.class.spec]p9: |
| 6067 | // Within the argument list of a class template partial |
| 6068 | // specialization, the following restrictions apply: |
| 6069 | // -- A partially specialized non-type argument expression |
| 6070 | // shall not involve a template parameter of the partial |
| 6071 | // specialization except when the argument expression is a |
| 6072 | // simple identifier. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6073 | SourceRange ParamUseRange = |
| 6074 | findTemplateParameter(Param->getDepth(), ArgExpr); |
| 6075 | if (ParamUseRange.isValid()) { |
| 6076 | if (IsDefaultArgument) { |
| 6077 | S.Diag(TemplateNameLoc, |
| 6078 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 6079 | S.Diag(ParamUseRange.getBegin(), |
| 6080 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 6081 | << ParamUseRange; |
| 6082 | } else { |
| 6083 | S.Diag(ParamUseRange.getBegin(), |
| 6084 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 6085 | << ParamUseRange; |
| 6086 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6087 | return true; |
| 6088 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6089 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6090 | // -- The type of a template parameter corresponding to a |
| 6091 | // specialized non-type argument shall not be dependent on a |
| 6092 | // parameter of the specialization. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6093 | // |
| 6094 | // FIXME: We need to delay this check until instantiation in some cases: |
| 6095 | // |
| 6096 | // template<template<typename> class X> struct A { |
| 6097 | // template<typename T, X<T> N> struct B; |
| 6098 | // template<typename T> struct B<T, 0>; |
| 6099 | // }; |
| 6100 | // template<typename> using X = int; |
| 6101 | // A<X>::B<int, 0> b; |
| 6102 | ParamUseRange = findTemplateParameter( |
| 6103 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
| 6104 | if (ParamUseRange.isValid()) { |
| 6105 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getLocStart(), |
| 6106 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 6107 | << Param->getType() << ParamUseRange; |
| 6108 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
| 6109 | << (IsDefaultArgument ? ParamUseRange : SourceRange()); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6110 | return true; |
| 6111 | } |
| 6112 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6113 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6114 | return false; |
| 6115 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6116 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6117 | /// \brief Check the non-type template arguments of a class template |
| 6118 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 6119 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6120 | /// \param TemplateNameLoc the location of the template name. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6121 | /// \param TemplateParams the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6122 | /// template. |
| 6123 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6124 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6125 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6126 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6127 | /// \returns \c true if there was an error, \c false otherwise. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6128 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6129 | Sema &S, SourceLocation TemplateNameLoc, |
| 6130 | TemplateParameterList *TemplateParams, unsigned NumExplicit, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6131 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6132 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 6133 | |
| 6134 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6135 | NonTypeTemplateParmDecl *Param |
| 6136 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 6137 | if (!Param) |
| 6138 | continue; |
| 6139 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6140 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
| 6141 | S, TemplateNameLoc, Param, &ArgList[I], 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6142 | return true; |
| 6143 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6144 | |
| 6145 | return false; |
| 6146 | } |
| 6147 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6148 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6149 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 6150 | TagUseKind TUK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6151 | SourceLocation KWLoc, |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6152 | SourceLocation ModulePrivateLoc, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6153 | TemplateIdAnnotation &TemplateId, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6154 | AttributeList *Attr, |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6155 | MultiTemplateParamsArg |
| 6156 | TemplateParameterLists, |
| 6157 | SkipBodyInfo *SkipBody) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6158 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 6159 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6160 | CXXScopeSpec &SS = TemplateId.SS; |
| 6161 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6162 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 6163 | // store the location of the outermost template keyword in the declaration. |
| 6164 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6165 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 6166 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 6167 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 6168 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6169 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6170 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6171 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6172 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6173 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 6174 | |
| 6175 | if (!ClassTemplate) { |
| 6176 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6177 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6178 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 6179 | return true; |
| 6180 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6181 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6182 | bool isExplicitSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6183 | bool isPartialSpecialization = false; |
| 6184 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6185 | // Check the validity of the template headers that introduce this |
| 6186 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6187 | // FIXME: We probably shouldn't complain about these headers for |
| 6188 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6189 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 6190 | TemplateParameterList *TemplateParams = |
| 6191 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6192 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
| 6193 | TemplateParameterLists, TUK == TUK_Friend, isExplicitSpecialization, |
| 6194 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6195 | if (Invalid) |
| 6196 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6197 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6198 | if (TemplateParams && TemplateParams->size() > 0) { |
| 6199 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6200 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 6201 | if (TUK == TUK_Friend) { |
| 6202 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 6203 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6204 | return true; |
| 6205 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6206 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6207 | // C++ [temp.class.spec]p10: |
| 6208 | // The template parameter list of a specialization shall not |
| 6209 | // contain default template argument values. |
| 6210 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6211 | Decl *Param = TemplateParams->getParam(I); |
| 6212 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 6213 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6214 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6215 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 6216 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6217 | } |
| 6218 | } else if (NonTypeTemplateParmDecl *NTTP |
| 6219 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 6220 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6221 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6222 | diag::err_default_arg_in_partial_spec) |
| 6223 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6224 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6225 | } |
| 6226 | } else { |
| 6227 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6228 | if (TTP->hasDefaultArgument()) { |
| 6229 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6230 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6231 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6232 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6233 | } |
| 6234 | } |
| 6235 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6236 | } else if (TemplateParams) { |
| 6237 | if (TUK == TUK_Friend) |
| 6238 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6239 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6240 | SourceRange(TemplateParams->getTemplateLoc(), |
| 6241 | TemplateParams->getRAngleLoc())) |
| 6242 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6243 | else |
| 6244 | isExplicitSpecialization = true; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6245 | } else { |
| 6246 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6247 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6248 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6249 | // Check that the specialization uses the same tag kind as the |
| 6250 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6251 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6252 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6253 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6254 | Kind, TUK == TUK_Definition, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 6255 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6256 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6257 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6258 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6259 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6260 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6261 | diag::note_previous_use); |
| 6262 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6263 | } |
| 6264 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6265 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6266 | TemplateArgumentListInfo TemplateArgs = |
| 6267 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6268 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6269 | // Check for unexpanded parameter packs in any of the template arguments. |
| 6270 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6271 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6272 | UPPC_PartialSpecialization)) |
| 6273 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6274 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6275 | // Check that the template argument list is well-formed for this |
| 6276 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6277 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6278 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6279 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6280 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6281 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6282 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6283 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6284 | if (isPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6285 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6286 | *this, TemplateNameLoc, ClassTemplate->getTemplateParameters(), |
| 6287 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6288 | return true; |
| 6289 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6290 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6291 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6292 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6293 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6294 | TemplateArgs.size(), |
| 6295 | InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6296 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 6297 | << ClassTemplate->getDeclName(); |
| 6298 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6299 | } |
| 6300 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6301 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6302 | void *InsertPos = nullptr; |
| 6303 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6304 | |
| 6305 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6306 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6307 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6308 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6309 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6310 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6311 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6312 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6313 | // Check whether we can declare a class template specialization in |
| 6314 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6315 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6316 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 6317 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6318 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6319 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6320 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6321 | // The canonical type |
| 6322 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 6323 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6324 | // Build the canonical type that describes the converted template |
| 6325 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6326 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6327 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6328 | Converted.data(), |
| 6329 | Converted.size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6330 | |
| 6331 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6332 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 6333 | // C++ [temp.class.spec]p9b3: |
| 6334 | // |
| 6335 | // -- The argument list of the specialization shall not be identical |
| 6336 | // to the implicit argument list of the primary template. |
| 6337 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6338 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6339 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6340 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 6341 | ClassTemplate->getIdentifier(), |
| 6342 | TemplateNameLoc, |
| 6343 | Attr, |
| 6344 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6345 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 6346 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6347 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6348 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6349 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6350 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6351 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6352 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 6353 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6354 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6355 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6356 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6357 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 6358 | TemplateParams, |
| 6359 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6360 | Converted.data(), |
| 6361 | Converted.size(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6362 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6363 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 6364 | PrevPartial); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6365 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6366 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6367 | Partial->setTemplateParameterListsInfo( |
| 6368 | Context, TemplateParameterLists.drop_back(1)); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6369 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6370 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6371 | if (!PrevPartial) |
| 6372 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6373 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6374 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6375 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6376 | // template specialization, make a note of that. |
| 6377 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 6378 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6379 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6380 | // Check that all of the template parameters of the class template |
| 6381 | // partial specialization are deducible from the template |
| 6382 | // arguments. If not, this class template partial specialization |
| 6383 | // will never be used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6384 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6385 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6386 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 6387 | DeducibleParams); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6388 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6389 | if (!DeducibleParams.all()) { |
| 6390 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6391 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6392 | << /*class template*/0 << (NumNonDeducible > 1) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6393 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 6394 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 6395 | if (!DeducibleParams[I]) { |
| 6396 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 6397 | if (Param->getDeclName()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6398 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6399 | diag::note_partial_spec_unused_parameter) |
| 6400 | << Param->getDeclName(); |
| 6401 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6402 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6403 | diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 6404 | << "(anonymous)"; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6405 | } |
| 6406 | } |
| 6407 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6408 | } else { |
| 6409 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6410 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6411 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6412 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6413 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6414 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6415 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6416 | Converted.data(), |
| 6417 | Converted.size(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6418 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6419 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6420 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6421 | Specialization->setTemplateParameterListsInfo(Context, |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6422 | TemplateParameterLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6423 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6424 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6425 | if (!PrevDecl) |
| 6426 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6427 | |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame] | 6428 | if (CurContext->isDependentContext()) { |
| 6429 | // -fms-extensions permits specialization of nested classes without |
| 6430 | // fully specializing the outer class(es). |
| 6431 | assert(getLangOpts().MicrosoftExt && |
| 6432 | "Only possible with -fms-extensions!"); |
| 6433 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6434 | CanonType = Context.getTemplateSpecializationType( |
| 6435 | CanonTemplate, Converted.data(), Converted.size()); |
| 6436 | } else { |
| 6437 | CanonType = Context.getTypeDeclType(Specialization); |
| 6438 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6439 | } |
| 6440 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6441 | // C++ [temp.expl.spec]p6: |
| 6442 | // 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] | 6443 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6444 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6445 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6446 | // use occurs; no diagnostic is required. |
| 6447 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6448 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6449 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6450 | // Is there any previous explicit specialization declaration? |
| 6451 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6452 | Okay = true; |
| 6453 | break; |
| 6454 | } |
| 6455 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6456 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6457 | if (!Okay) { |
| 6458 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 6459 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 6460 | << Context.getTypeDeclType(Specialization) << Range; |
| 6461 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6462 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6463 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6464 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6465 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6466 | return true; |
| 6467 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6468 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6469 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6470 | // If this is not a friend, note that this is an explicit specialization. |
| 6471 | if (TUK != TUK_Friend) |
| 6472 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6473 | |
| 6474 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6475 | if (TUK == TUK_Definition) { |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6476 | RecordDecl *Def = Specialization->getDefinition(); |
| 6477 | NamedDecl *Hidden = nullptr; |
| 6478 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 6479 | SkipBody->ShouldSkip = true; |
| 6480 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 6481 | // From here on out, treat this as just a redeclaration. |
| 6482 | TUK = TUK_Declaration; |
| 6483 | } else if (Def) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6484 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6485 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6486 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6487 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 6488 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6489 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6490 | } |
| 6491 | } |
| 6492 | |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 6493 | if (Attr) |
| 6494 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6495 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 6496 | // Add alignment attributes if necessary; these attributes are checked when |
| 6497 | // the ASTContext lays out the structure. |
| 6498 | if (TUK == TUK_Definition) { |
| 6499 | AddAlignmentAttributesForRecord(Specialization); |
| 6500 | AddMsStructLayoutForRecord(Specialization); |
| 6501 | } |
| 6502 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6503 | if (ModulePrivateLoc.isValid()) |
| 6504 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 6505 | << (isPartialSpecialization? 1 : 0) |
| 6506 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 6507 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 6508 | // Build the fully-sugared type for this class template |
| 6509 | // specialization as the user wrote in the specialization |
| 6510 | // itself. This means that we'll pretty-print the type retrieved |
| 6511 | // from the specialization's declaration the way that the user |
| 6512 | // actually wrote the specialization, rather than formatting the |
| 6513 | // name based on the "canonical" representation used to store the |
| 6514 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6515 | TypeSourceInfo *WrittenTy |
| 6516 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6517 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6518 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6519 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6520 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6521 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6522 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 6523 | // C++ [temp.expl.spec]p9: |
| 6524 | // A template explicit specialization is in the scope of the |
| 6525 | // namespace in which the template was defined. |
| 6526 | // |
| 6527 | // We actually implement this paragraph where we set the semantic |
| 6528 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 6529 | // but we also maintain the lexical context where the actual |
| 6530 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6531 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6532 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6533 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6534 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6535 | Specialization->startDefinition(); |
| 6536 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6537 | if (TUK == TUK_Friend) { |
| 6538 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 6539 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6540 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6541 | /*FIXME:*/KWLoc); |
| 6542 | Friend->setAccess(AS_public); |
| 6543 | CurContext->addDecl(Friend); |
| 6544 | } else { |
| 6545 | // Add the specialization into its lexical context, so that it can |
| 6546 | // be seen when iterating through the list of declarations in that |
| 6547 | // context. However, specializations are not found by name lookup. |
| 6548 | CurContext->addDecl(Specialization); |
| 6549 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6550 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6551 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6552 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6553 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6554 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6555 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6556 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 6557 | ActOnDocumentableDecl(NewDecl); |
| 6558 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6559 | } |
| 6560 | |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6561 | /// \brief Strips various properties off an implicit instantiation |
| 6562 | /// that has just been explicitly specialized. |
| 6563 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6564 | D->dropAttr<DLLImportAttr>(); |
| 6565 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6566 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6567 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6568 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6569 | } |
| 6570 | |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6571 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 6572 | // declaration or definition. |
| 6573 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6574 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6575 | // Explicit instantiations following a specialization have no effect and |
| 6576 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 6577 | // until a valid name loc is found. |
| 6578 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6579 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 6580 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6581 | PrevDiagLoc = Prev->getLocation(); |
| 6582 | } |
| 6583 | assert(PrevDiagLoc.isValid() && |
| 6584 | "Explicit instantiation without point of instantiation?"); |
| 6585 | return PrevDiagLoc; |
| 6586 | } |
| 6587 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6588 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6589 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6590 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6591 | /// new specialization/instantiation will have any effect. |
| 6592 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6593 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6594 | /// instantiation. |
| 6595 | /// |
| 6596 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 6597 | /// |
| 6598 | /// \param PrevDecl the previous declaration of the entity. |
| 6599 | /// |
| 6600 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 6601 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6602 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6603 | /// declaration was instantiated (either implicitly or explicitly). |
| 6604 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6605 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6606 | /// specialization or instantiation has no effect and should be ignored. |
| 6607 | /// |
| 6608 | /// \returns true if there was an error that should prevent the introduction of |
| 6609 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6610 | bool |
| 6611 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 6612 | TemplateSpecializationKind NewTSK, |
| 6613 | NamedDecl *PrevDecl, |
| 6614 | TemplateSpecializationKind PrevTSK, |
| 6615 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6616 | bool &HasNoEffect) { |
| 6617 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6618 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6619 | switch (NewTSK) { |
| 6620 | case TSK_Undeclared: |
| 6621 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 6622 | assert( |
| 6623 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 6624 | "previous declaration must be implicit!"); |
| 6625 | return 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 | case TSK_ExplicitSpecialization: |
| 6628 | switch (PrevTSK) { |
| 6629 | case TSK_Undeclared: |
| 6630 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6631 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6632 | // explicitly specialized or has merely been mentioned without any |
| 6633 | // instantiation. |
| 6634 | return false; |
| 6635 | |
| 6636 | case TSK_ImplicitInstantiation: |
| 6637 | if (PrevPointOfInstantiation.isInvalid()) { |
| 6638 | // The declaration itself has not actually been instantiated, so it is |
| 6639 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6640 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6641 | return false; |
| 6642 | } |
| 6643 | // Fall through |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6644 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6645 | case TSK_ExplicitInstantiationDeclaration: |
| 6646 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6647 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 6648 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6649 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6650 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6651 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6652 | // 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] | 6653 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6654 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6655 | // implicit instantiation to take place, in every translation unit in |
| 6656 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6657 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6658 | // Is there any previous explicit specialization declaration? |
| 6659 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 6660 | return false; |
| 6661 | } |
| 6662 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6663 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6664 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6665 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6666 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6667 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6668 | return true; |
| 6669 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6670 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6671 | case TSK_ExplicitInstantiationDeclaration: |
| 6672 | switch (PrevTSK) { |
| 6673 | case TSK_ExplicitInstantiationDeclaration: |
| 6674 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6675 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6676 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6677 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6678 | case TSK_Undeclared: |
| 6679 | case TSK_ImplicitInstantiation: |
| 6680 | // We're explicitly instantiating something that may have already been |
| 6681 | // implicitly instantiated; that's fine. |
| 6682 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6683 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6684 | case TSK_ExplicitSpecialization: |
| 6685 | // C++0x [temp.explicit]p4: |
| 6686 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6687 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6688 | // specialization for that template, the explicit instantiation has no |
| 6689 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6690 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6691 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6692 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6693 | case TSK_ExplicitInstantiationDefinition: |
| 6694 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6695 | // If an entity is the subject of both an explicit instantiation |
| 6696 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6697 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6698 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6699 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6700 | |
| 6701 | // Explicit instantiations following a specialization have no effect and |
| 6702 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 6703 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6704 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 6705 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6706 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6707 | return false; |
| 6708 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6709 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6710 | case TSK_ExplicitInstantiationDefinition: |
| 6711 | switch (PrevTSK) { |
| 6712 | case TSK_Undeclared: |
| 6713 | case TSK_ImplicitInstantiation: |
| 6714 | // We're explicitly instantiating something that may have already been |
| 6715 | // implicitly instantiated; that's fine. |
| 6716 | return false; |
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_ExplicitSpecialization: |
| 6719 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 6720 | // For a given set of template parameters, if an explicit |
| 6721 | // instantiation of a template appears after a declaration of |
| 6722 | // an explicit specialization for that template, the explicit |
| 6723 | // instantiation has no effect. |
| 6724 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6725 | // 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] | 6726 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6727 | // has been explicitly specialized. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6728 | Diag(NewLoc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6729 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 6730 | diag::ext_explicit_instantiation_after_specialization) |
| 6731 | << PrevDecl; |
| 6732 | Diag(PrevDecl->getLocation(), |
| 6733 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6734 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6735 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6736 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6737 | case TSK_ExplicitInstantiationDeclaration: |
| 6738 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6739 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6740 | |
| 6741 | // C++0x [temp.explicit]p4: |
| 6742 | // For a given set of template parameters, if an explicit instantiation |
| 6743 | // of a template appears after a declaration of an explicit |
| 6744 | // specialization for that template, the explicit instantiation has no |
| 6745 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6746 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6747 | // Is there any previous explicit specialization declaration? |
| 6748 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6749 | HasNoEffect = true; |
| 6750 | break; |
| 6751 | } |
| 6752 | } |
| 6753 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6754 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6755 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6756 | case TSK_ExplicitInstantiationDefinition: |
| 6757 | // C++0x [temp.spec]p5: |
| 6758 | // For a given template and a given set of template-arguments, |
| 6759 | // - an explicit instantiation definition shall appear at most once |
| 6760 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6761 | |
| 6762 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 6763 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 6764 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6765 | : diag::err_explicit_instantiation_duplicate) |
| 6766 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6767 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6768 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6769 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6770 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6771 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6772 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6773 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6774 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6775 | } |
| 6776 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6777 | /// \brief Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6778 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6779 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6780 | /// The only possible way to get a dependent function template specialization |
| 6781 | /// is with a friend declaration, like so: |
| 6782 | /// |
| 6783 | /// \code |
| 6784 | /// template \<class T> void foo(T); |
| 6785 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6786 | /// friend void foo<>(T); |
| 6787 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6788 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6789 | /// |
| 6790 | /// There really isn't any useful analysis we can do here, so we |
| 6791 | /// just store the information. |
| 6792 | bool |
| 6793 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 6794 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 6795 | LookupResult &Previous) { |
| 6796 | // Remove anything from Previous that isn't a function template in |
| 6797 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6798 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6799 | LookupResult::Filter F = Previous.makeFilter(); |
| 6800 | while (F.hasNext()) { |
| 6801 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 6802 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6803 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 6804 | D->getDeclContext()->getRedeclContext())) |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6805 | F.erase(); |
| 6806 | } |
| 6807 | F.done(); |
| 6808 | |
| 6809 | // Should this be diagnosed here? |
| 6810 | if (Previous.empty()) return true; |
| 6811 | |
| 6812 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 6813 | ExplicitTemplateArgs); |
| 6814 | return false; |
| 6815 | } |
| 6816 | |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6817 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6818 | /// specialization. |
| 6819 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6820 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6821 | /// explicit function template specialization. On successful completion, |
| 6822 | /// the function declaration \p FD will become a function template |
| 6823 | /// specialization. |
| 6824 | /// |
| 6825 | /// \param FD the function declaration, which will be updated to become a |
| 6826 | /// function template specialization. |
| 6827 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6828 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 6829 | /// if any. Note that this may be valid info even when 0 arguments are |
| 6830 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 6831 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6832 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6833 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6834 | /// this function specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6835 | bool Sema::CheckFunctionTemplateSpecialization( |
| 6836 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 6837 | LookupResult &Previous) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6838 | // The set of function template specializations that could match this |
| 6839 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6840 | UnresolvedSet<8> Candidates; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 6841 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), |
| 6842 | /*ForTakingAddress=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6843 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6844 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
| 6845 | ConvertedTemplateArgs; |
| 6846 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6847 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6848 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6849 | I != E; ++I) { |
| 6850 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 6851 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6852 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6853 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6854 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 6855 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6856 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6857 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6858 | // When matching a constexpr member function template specialization |
| 6859 | // against the primary template, we don't yet know whether the |
| 6860 | // specialization has an implicit 'const' (because we don't know whether |
| 6861 | // it will be a static member function until we know which template it |
| 6862 | // specializes), so adjust it now assuming it specializes this template. |
| 6863 | QualType FT = FD->getType(); |
| 6864 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6865 | CXXMethodDecl *OldMD = |
| 6866 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6867 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6868 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6869 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6870 | EPI.TypeQuals |= Qualifiers::Const; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6871 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6872 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6873 | } |
| 6874 | } |
| 6875 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6876 | TemplateArgumentListInfo Args; |
| 6877 | if (ExplicitTemplateArgs) |
| 6878 | Args = *ExplicitTemplateArgs; |
| 6879 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6880 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6881 | // A trailing template-argument can be left unspecified in the |
| 6882 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6883 | // provided it can be deduced from the function argument type. |
| 6884 | // Perform template argument deduction to determine whether we may be |
| 6885 | // specializing this template. |
| 6886 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6887 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6888 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 6889 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 6890 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6891 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6892 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6893 | // that we can provide nifty diagnostics. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6894 | FailedCandidates.addCandidate() |
| 6895 | .set(FunTmpl->getTemplatedDecl(), |
| 6896 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6897 | (void)TDK; |
| 6898 | continue; |
| 6899 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6900 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6901 | // Record this candidate. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6902 | if (ExplicitTemplateArgs) |
| 6903 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6904 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6905 | } |
| 6906 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6907 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 6908 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6909 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 6910 | Candidates.begin(), Candidates.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6911 | FD->getLocation(), |
| 6912 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 6913 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6914 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6915 | PDiag(diag::note_function_template_spec_matched)); |
| 6916 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6917 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6918 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6919 | |
| 6920 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 6921 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 6922 | |
| 6923 | FunctionTemplateSpecializationInfo *SpecInfo |
| 6924 | = Specialization->getTemplateSpecializationInfo(); |
| 6925 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6926 | |
| 6927 | // Note: do not overwrite location info if previous template |
| 6928 | // specialization kind was explicit. |
| 6929 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6930 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6931 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6932 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 6933 | // function can differ from the template declaration with respect to |
| 6934 | // the constexpr specifier. |
| 6935 | Specialization->setConstexpr(FD->isConstexpr()); |
| 6936 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6937 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6938 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6939 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6940 | |
| 6941 | // If this is a friend declaration, then we're not really declaring |
| 6942 | // an explicit specialization. |
| 6943 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6944 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6945 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6946 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6947 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6948 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6949 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6950 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6951 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6952 | |
| 6953 | // C++ [temp.expl.spec]p6: |
| 6954 | // 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] | 6955 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6956 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6957 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6958 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6959 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6960 | if (!isFriend && |
| 6961 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6962 | TSK_ExplicitSpecialization, |
| 6963 | Specialization, |
| 6964 | SpecInfo->getTemplateSpecializationKind(), |
| 6965 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6966 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6967 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6968 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6969 | // Mark the prior declaration as an explicit specialization, so that later |
| 6970 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6971 | if (!isFriend) { |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6972 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6973 | MarkUnusedFileScopedDecl(Specialization); |
| 6974 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6975 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6976 | // Turn the given function declaration into a function template |
| 6977 | // specialization, with the template arguments from the previous |
| 6978 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6979 | // Take copies of (semantic and syntactic) template argument lists. |
| 6980 | const TemplateArgumentList* TemplArgs = new (Context) |
| 6981 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6982 | FD->setFunctionTemplateSpecialization( |
| 6983 | Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr, |
| 6984 | SpecInfo->getTemplateSpecializationKind(), |
| 6985 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6986 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6987 | // The "previous declaration" for this function template specialization is |
| 6988 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6989 | Previous.clear(); |
| 6990 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6991 | return false; |
| 6992 | } |
| 6993 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6994 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6995 | /// specialization. |
| 6996 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6997 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6998 | /// explicit member function specialization. On successful completion, |
| 6999 | /// the function declaration \p FD will become a member function |
| 7000 | /// specialization. |
| 7001 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7002 | /// \param Member the member declaration, which will be updated to become a |
| 7003 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7004 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7005 | /// \param Previous the set of declarations, one of which may be specialized |
| 7006 | /// by this function specialization; the set will be modified to contain the |
| 7007 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7008 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7009 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7010 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7011 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7012 | // Try to find the member we are instantiating. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7013 | NamedDecl *Instantiation = nullptr; |
| 7014 | NamedDecl *InstantiatedFrom = nullptr; |
| 7015 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7016 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7017 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7018 | // Nowhere to look anyway. |
| 7019 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7020 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 7021 | I != E; ++I) { |
| 7022 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 7023 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 7024 | QualType Adjusted = Function->getType(); |
| 7025 | if (!hasExplicitCallingConv(Adjusted)) |
| 7026 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
| 7027 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7028 | Instantiation = Method; |
| 7029 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7030 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7031 | break; |
| 7032 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7033 | } |
| 7034 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7035 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7036 | VarDecl *PrevVar; |
| 7037 | if (Previous.isSingleResult() && |
| 7038 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7039 | if (PrevVar->isStaticDataMember()) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7040 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7041 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7042 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7043 | } |
| 7044 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7045 | CXXRecordDecl *PrevRecord; |
| 7046 | if (Previous.isSingleResult() && |
| 7047 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 7048 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7049 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7050 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7051 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7052 | } else if (isa<EnumDecl>(Member)) { |
| 7053 | EnumDecl *PrevEnum; |
| 7054 | if (Previous.isSingleResult() && |
| 7055 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
| 7056 | Instantiation = PrevEnum; |
| 7057 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 7058 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 7059 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7060 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7061 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7062 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7063 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7064 | // specializations are always out-of-line, the caller will complain about |
| 7065 | // this mismatch later. |
| 7066 | return false; |
| 7067 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7068 | |
| 7069 | // If this is a friend, just bail out here before we start turning |
| 7070 | // things into explicit specializations. |
| 7071 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 7072 | // Preserve instantiation information. |
| 7073 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 7074 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 7075 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7076 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7077 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 7078 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 7079 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7080 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7081 | } |
| 7082 | |
| 7083 | Previous.clear(); |
| 7084 | Previous.addDecl(Instantiation); |
| 7085 | return false; |
| 7086 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7087 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7088 | // Make sure that this is a specialization of a member. |
| 7089 | if (!InstantiatedFrom) { |
| 7090 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 7091 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7092 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 7093 | return true; |
| 7094 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7095 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7096 | // C++ [temp.expl.spec]p6: |
| 7097 | // 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] | 7098 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7099 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7100 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7101 | // use occurs; no diagnostic is required. |
| 7102 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7103 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7104 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7105 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 7106 | TSK_ExplicitSpecialization, |
| 7107 | Instantiation, |
| 7108 | MSInfo->getTemplateSpecializationKind(), |
| 7109 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7110 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7111 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7112 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7113 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7114 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7115 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7116 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7117 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7118 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 7119 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7120 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7121 | // the original declaration to note that it is an explicit specialization |
| 7122 | // (if it was previously an implicit instantiation). This latter step |
| 7123 | // makes bookkeeping easier. |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7124 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7125 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 7126 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 7127 | TSK_ImplicitInstantiation) { |
| 7128 | InstantiationFunction->setTemplateSpecializationKind( |
| 7129 | TSK_ExplicitSpecialization); |
| 7130 | InstantiationFunction->setLocation(Member->getLocation()); |
| 7131 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7132 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7133 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 7134 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7135 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7136 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7137 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7138 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 7139 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 7140 | TSK_ImplicitInstantiation) { |
| 7141 | InstantiationVar->setTemplateSpecializationKind( |
| 7142 | TSK_ExplicitSpecialization); |
| 7143 | InstantiationVar->setLocation(Member->getLocation()); |
| 7144 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7145 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7146 | cast<VarDecl>(Member)->setInstantiationOfStaticDataMember( |
| 7147 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7148 | MarkUnusedFileScopedDecl(InstantiationVar); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7149 | } else if (isa<CXXRecordDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7150 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 7151 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 7152 | TSK_ImplicitInstantiation) { |
| 7153 | InstantiationClass->setTemplateSpecializationKind( |
| 7154 | TSK_ExplicitSpecialization); |
| 7155 | InstantiationClass->setLocation(Member->getLocation()); |
| 7156 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7157 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7158 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7159 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7160 | TSK_ExplicitSpecialization); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7161 | } else { |
| 7162 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); |
| 7163 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); |
| 7164 | if (InstantiationEnum->getTemplateSpecializationKind() == |
| 7165 | TSK_ImplicitInstantiation) { |
| 7166 | InstantiationEnum->setTemplateSpecializationKind( |
| 7167 | TSK_ExplicitSpecialization); |
| 7168 | InstantiationEnum->setLocation(Member->getLocation()); |
| 7169 | } |
| 7170 | |
| 7171 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( |
| 7172 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7173 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7174 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7175 | // Save the caller the trouble of having to figure out which declaration |
| 7176 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7177 | Previous.clear(); |
| 7178 | Previous.addDecl(Instantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7179 | return false; |
| 7180 | } |
| 7181 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7182 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7183 | /// |
| 7184 | /// \returns true if a serious error occurs, false otherwise. |
| 7185 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7186 | SourceLocation InstLoc, |
| 7187 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7188 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 7189 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7190 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7191 | if (CurContext->isRecord()) { |
| 7192 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 7193 | << D; |
| 7194 | return true; |
| 7195 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7196 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7197 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7198 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7199 | // template. If the name declared in the explicit instantiation is an |
| 7200 | // unqualified name, the explicit instantiation shall appear in the |
| 7201 | // namespace where its template is declared or, if that namespace is inline |
| 7202 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7203 | // |
| 7204 | // 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] | 7205 | if (WasQualifiedName) { |
| 7206 | if (CurContext->Encloses(OrigContext)) |
| 7207 | return false; |
| 7208 | } else { |
| 7209 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 7210 | return false; |
| 7211 | } |
| 7212 | |
| 7213 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 7214 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7215 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7216 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7217 | diag::err_explicit_instantiation_out_of_scope : |
| 7218 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7219 | << D << NS; |
| 7220 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7221 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7222 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7223 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 7224 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 7225 | << D << NS; |
| 7226 | } else |
| 7227 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7228 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7229 | diag::err_explicit_instantiation_must_be_global : |
| 7230 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 7231 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7232 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7233 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7234 | } |
| 7235 | |
| 7236 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 7237 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 7238 | if (!SS.isSet()) |
| 7239 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7240 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7241 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7242 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7243 | // or a static data member of a class template specialization, the name of |
| 7244 | // the class template specialization in the qualified-id for the member |
| 7245 | // name shall be a simple-template-id. |
| 7246 | // |
| 7247 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7248 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 7249 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7250 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7251 | if (isa<TemplateSpecializationType>(T)) |
| 7252 | return true; |
| 7253 | |
| 7254 | return false; |
| 7255 | } |
| 7256 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7257 | // Explicit instantiation of a class template specialization |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7258 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7259 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7260 | SourceLocation ExternLoc, |
| 7261 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7262 | unsigned TagSpec, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7263 | SourceLocation KWLoc, |
| 7264 | const CXXScopeSpec &SS, |
| 7265 | TemplateTy TemplateD, |
| 7266 | SourceLocation TemplateNameLoc, |
| 7267 | SourceLocation LAngleLoc, |
| 7268 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7269 | SourceLocation RAngleLoc, |
| 7270 | AttributeList *Attr) { |
| 7271 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 7272 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7273 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7274 | // Check that the specialization uses the same tag kind as the |
| 7275 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7276 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7277 | assert(Kind != TTK_Enum && |
| 7278 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7279 | |
| 7280 | if (isa<TypeAliasTemplateDecl>(TD)) { |
| 7281 | Diag(KWLoc, diag::err_tag_reference_non_tag) << Kind; |
| 7282 | Diag(TD->getTemplatedDecl()->getLocation(), |
| 7283 | diag::note_previous_use); |
| 7284 | return true; |
| 7285 | } |
| 7286 | |
| 7287 | ClassTemplateDecl *ClassTemplate = cast<ClassTemplateDecl>(TD); |
| 7288 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7289 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7290 | Kind, /*isDefinition*/false, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 7291 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7292 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7293 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7294 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7295 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7296 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7297 | diag::note_previous_use); |
| 7298 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7299 | } |
| 7300 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7301 | // C++0x [temp.explicit]p2: |
| 7302 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7303 | // definition and an explicit instantiation declaration. An explicit |
| 7304 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 7305 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 7306 | ? TSK_ExplicitInstantiationDefinition |
| 7307 | : TSK_ExplicitInstantiationDeclaration; |
| 7308 | |
| 7309 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 7310 | // Check for dllexport class template instantiation declarations. |
| 7311 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7312 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7313 | Diag(ExternLoc, |
| 7314 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7315 | Diag(A->getLoc(), diag::note_attribute); |
| 7316 | break; |
| 7317 | } |
| 7318 | } |
| 7319 | |
| 7320 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 7321 | Diag(ExternLoc, |
| 7322 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7323 | Diag(A->getLocation(), diag::note_attribute); |
| 7324 | } |
| 7325 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7326 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7327 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7328 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 7329 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7330 | |
| 7331 | // Check that the template argument list is well-formed for this |
| 7332 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7333 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7334 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7335 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7336 | return true; |
| 7337 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7338 | // Find the class template specialization declaration that |
| 7339 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7340 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7341 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7342 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7343 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7344 | TemplateSpecializationKind PrevDecl_TSK |
| 7345 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 7346 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7347 | // C++0x [temp.explicit]p2: |
| 7348 | // [...] An explicit instantiation shall appear in an enclosing |
| 7349 | // namespace of its template. [...] |
| 7350 | // |
| 7351 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7352 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 7353 | SS.isSet())) |
| 7354 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7355 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7356 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7357 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7358 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7359 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7360 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7361 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7362 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7363 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7364 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7365 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7366 | // Even though HasNoEffect == true means that this explicit instantiation |
| 7367 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 7368 | |
| 7369 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 7370 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7371 | // Since the only prior class template specialization with these |
| 7372 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7373 | // declaration node as our own, updating the source location |
| 7374 | // for the template name to reflect our new declaration. |
| 7375 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7376 | Specialization = PrevDecl; |
| 7377 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7378 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7379 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7380 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7381 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7382 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7383 | // Create a new class template specialization declaration node for |
| 7384 | // this explicit specialization. |
| 7385 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7386 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7387 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7388 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7389 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7390 | Converted.data(), |
| 7391 | Converted.size(), |
| 7392 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7393 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7394 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7395 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7396 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7397 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7398 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7399 | } |
| 7400 | |
| 7401 | // Build the fully-sugared type for this explicit instantiation as |
| 7402 | // the user wrote in the explicit instantiation itself. This means |
| 7403 | // that we'll pretty-print the type retrieved from the |
| 7404 | // specialization's declaration the way that the user actually wrote |
| 7405 | // the explicit instantiation, rather than formatting the name based |
| 7406 | // on the "canonical" representation used to store the template |
| 7407 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7408 | TypeSourceInfo *WrittenTy |
| 7409 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7410 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7411 | Context.getTypeDeclType(Specialization)); |
| 7412 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7413 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7414 | // Set source locations for keywords. |
| 7415 | Specialization->setExternLoc(ExternLoc); |
| 7416 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | 40bcfd7 | 2013-04-22 23:23:42 +0000 | [diff] [blame] | 7417 | Specialization->setRBraceLoc(SourceLocation()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7418 | |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 7419 | if (Attr) |
| 7420 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 7421 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7422 | // Add the explicit instantiation into its lexical context. However, |
| 7423 | // since explicit instantiations are never found by name lookup, we |
| 7424 | // just put it into the declaration context directly. |
| 7425 | Specialization->setLexicalDeclContext(CurContext); |
| 7426 | CurContext->addDecl(Specialization); |
| 7427 | |
| 7428 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 7429 | if (HasNoEffect) { |
| 7430 | // Set the template specialization kind. |
| 7431 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7432 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 7433 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7434 | |
| 7435 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7436 | // A definition of a class template or class member template |
| 7437 | // shall be in scope at the point of the explicit instantiation of |
| 7438 | // the class template or class member template. |
| 7439 | // |
| 7440 | // This check comes when we actually try to perform the |
| 7441 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7442 | ClassTemplateSpecializationDecl *Def |
| 7443 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7444 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7445 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7446 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7447 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7448 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7449 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 7450 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7451 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7452 | // Instantiate the members of this class template specialization. |
| 7453 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7454 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7455 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7456 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 7457 | |
| 7458 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 7459 | // TSK_ExplicitInstantiationDefinition |
| 7460 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7461 | TSK == TSK_ExplicitInstantiationDefinition) { |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7462 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7463 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7464 | |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 7465 | if (!getDLLAttr(Def) && getDLLAttr(Specialization) && |
| 7466 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 7467 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 7468 | // attribute to a template with a previous instantiation declaration. |
| 7469 | // MinGW doesn't allow this. |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7470 | auto *A = cast<InheritableAttr>( |
| 7471 | getDLLAttr(Specialization)->clone(getASTContext())); |
| 7472 | A->setInherited(true); |
| 7473 | Def->addAttr(A); |
Reid Kleckner | 5b64034 | 2016-02-26 19:51:02 +0000 | [diff] [blame] | 7474 | |
| 7475 | // We reject explicit instantiations in class scope, so there should |
| 7476 | // never be any delayed exported classes to worry about. |
| 7477 | assert(DelayedDllExportClasses.empty() && |
| 7478 | "delayed exports present at explicit instantiation"); |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7479 | checkClassLevelDLLAttribute(Def); |
Reid Kleckner | 5b64034 | 2016-02-26 19:51:02 +0000 | [diff] [blame] | 7480 | referenceDLLExportedClassMethods(); |
Hans Wennborg | fce87ca | 2015-06-09 00:39:09 +0000 | [diff] [blame] | 7481 | |
| 7482 | // Propagate attribute to base class templates. |
| 7483 | for (auto &B : Def->bases()) { |
| 7484 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 7485 | B.getType()->getAsCXXRecordDecl())) |
| 7486 | propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getLocStart()); |
| 7487 | } |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7488 | } |
| 7489 | } |
| 7490 | |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 7491 | // Set the template specialization kind. Make sure it is set before |
| 7492 | // instantiating the members which will trigger ASTConsumer callbacks. |
| 7493 | Specialization->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7494 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 7495 | } else { |
| 7496 | |
| 7497 | // Set the template specialization kind. |
| 7498 | Specialization->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7499 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7500 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7501 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7502 | } |
| 7503 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7504 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7505 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7506 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7507 | SourceLocation ExternLoc, |
| 7508 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7509 | unsigned TagSpec, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7510 | SourceLocation KWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7511 | CXXScopeSpec &SS, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7512 | IdentifierInfo *Name, |
| 7513 | SourceLocation NameLoc, |
| 7514 | AttributeList *Attr) { |
| 7515 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 7516 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7517 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7518 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7519 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7520 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7521 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 7522 | SourceLocation(), false, TypeResult(), |
| 7523 | /*IsTypeSpecifier*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7524 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 7525 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7526 | if (!TagD) |
| 7527 | return true; |
| 7528 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7529 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7530 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7531 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 7532 | if (Tag->isInvalidDecl()) |
| 7533 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7534 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7535 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 7536 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 7537 | if (!Pattern) { |
| 7538 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 7539 | << Context.getTypeDeclType(Record); |
| 7540 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 7541 | return true; |
| 7542 | } |
| 7543 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7544 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7545 | // If the explicit instantiation is for a class or member class, the |
| 7546 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7547 | // simple-template-id. |
| 7548 | // |
| 7549 | // C++98 has the same restriction, just worded differently. |
| 7550 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7551 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7552 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7553 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7554 | // C++0x [temp.explicit]p2: |
| 7555 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7556 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7557 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 7558 | TemplateSpecializationKind TSK |
| 7559 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7560 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7561 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7562 | // C++0x [temp.explicit]p2: |
| 7563 | // [...] An explicit instantiation shall appear in an enclosing |
| 7564 | // namespace of its template. [...] |
| 7565 | // |
| 7566 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7567 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7568 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7569 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7570 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7571 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7572 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7573 | PrevDecl = Record; |
| 7574 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7575 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7576 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7577 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7578 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7579 | PrevDecl, |
| 7580 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7581 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7582 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7583 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7584 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7585 | return TagD; |
| 7586 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7587 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7588 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7589 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7590 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7591 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7592 | // 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] | 7593 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7594 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7595 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7596 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 7597 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 7598 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7599 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 7600 | << Pattern; |
| 7601 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7602 | } else { |
| 7603 | if (InstantiateClass(NameLoc, Record, Def, |
| 7604 | getTemplateInstantiationArgs(Record), |
| 7605 | TSK)) |
| 7606 | return true; |
| 7607 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7608 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7609 | if (!RecordDef) |
| 7610 | return true; |
| 7611 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7612 | } |
| 7613 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7614 | // Instantiate all of the members of the class. |
| 7615 | InstantiateClassMembers(NameLoc, RecordDef, |
| 7616 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7617 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7618 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7619 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 7620 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7621 | // FIXME: We don't have any representation for explicit instantiations of |
| 7622 | // member classes. Such a representation is not needed for compilation, but it |
| 7623 | // should be available for clients that want to see all of the declarations in |
| 7624 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7625 | return TagD; |
| 7626 | } |
| 7627 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7628 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 7629 | SourceLocation ExternLoc, |
| 7630 | SourceLocation TemplateLoc, |
| 7631 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7632 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7633 | // TODO: check if/when DNInfo should replace Name. |
| 7634 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 7635 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7636 | if (!Name) { |
| 7637 | if (!D.isInvalidType()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7638 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7639 | diag::err_explicit_instantiation_requires_name) |
| 7640 | << D.getDeclSpec().getSourceRange() |
| 7641 | << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7642 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7643 | return true; |
| 7644 | } |
| 7645 | |
| 7646 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 7647 | // we find one that is. |
| 7648 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 7649 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 7650 | S = S->getParent(); |
| 7651 | |
| 7652 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7653 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 7654 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7655 | if (R.isNull()) |
| 7656 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7657 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7658 | // C++ [dcl.stc]p1: |
| 7659 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 7660 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7661 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7662 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 7663 | << Name; |
| 7664 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7665 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 7666 | != DeclSpec::SCS_unspecified) { |
| 7667 | // Complain about then remove the storage class specifier. |
| 7668 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 7669 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 7670 | |
| 7671 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7672 | } |
| 7673 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 7674 | // C++0x [temp.explicit]p1: |
| 7675 | // [...] An explicit instantiation of a function template shall not use the |
| 7676 | // inline or constexpr specifiers. |
| 7677 | // Presumably, this also applies to member functions of class templates as |
| 7678 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7679 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7680 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7681 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7682 | diag::err_explicit_instantiation_inline : |
| 7683 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7684 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7685 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7686 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 7687 | // not already specified. |
| 7688 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 7689 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7690 | |
Nathan Wilson | de49845 | 2016-02-08 05:34:00 +0000 | [diff] [blame] | 7691 | // C++ Concepts TS [dcl.spec.concept]p1: The concept specifier shall be |
| 7692 | // applied only to the definition of a function template or variable template, |
| 7693 | // declared in namespace scope. |
| 7694 | if (D.getDeclSpec().isConceptSpecified()) { |
| 7695 | Diag(D.getDeclSpec().getConceptSpecLoc(), |
| 7696 | diag::err_concept_specified_specialization) << 0; |
| 7697 | return true; |
| 7698 | } |
| 7699 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7700 | // C++0x [temp.explicit]p2: |
| 7701 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7702 | // definition and an explicit instantiation declaration. An explicit |
| 7703 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7704 | TemplateSpecializationKind TSK |
| 7705 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7706 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7707 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7708 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7709 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7710 | |
| 7711 | if (!R->isFunctionType()) { |
| 7712 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7713 | // A [...] static data member of a class template can be explicitly |
| 7714 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7715 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7716 | // C++1y [temp.explicit]p1: |
| 7717 | // A [...] variable [...] template specialization can be explicitly |
| 7718 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7719 | if (Previous.isAmbiguous()) |
| 7720 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7721 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 7722 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7723 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7724 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7725 | if (!PrevTemplate) { |
| 7726 | if (!Prev || !Prev->isStaticDataMember()) { |
| 7727 | // We expect to see a data data member here. |
| 7728 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 7729 | << Name; |
| 7730 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7731 | P != PEnd; ++P) |
| 7732 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 7733 | return true; |
| 7734 | } |
| 7735 | |
| 7736 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 7737 | // FIXME: Check for explicit specialization? |
| 7738 | Diag(D.getIdentifierLoc(), |
| 7739 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 7740 | << Prev; |
| 7741 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 7742 | // FIXME: Can we provide a note showing where this was declared? |
| 7743 | return true; |
| 7744 | } |
| 7745 | } else { |
| 7746 | // Explicitly instantiate a variable template. |
| 7747 | |
| 7748 | // C++1y [dcl.spec.auto]p6: |
| 7749 | // ... A program that uses auto or decltype(auto) in a context not |
| 7750 | // explicitly allowed in this section is ill-formed. |
| 7751 | // |
| 7752 | // This includes auto-typed variable template instantiations. |
| 7753 | if (R->isUndeducedType()) { |
| 7754 | Diag(T->getTypeLoc().getLocStart(), |
| 7755 | diag::err_auto_not_allowed_var_inst); |
| 7756 | return true; |
| 7757 | } |
| 7758 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7759 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 7760 | // C++1y [temp.explicit]p3: |
| 7761 | // If the explicit instantiation is for a variable, the unqualified-id |
| 7762 | // in the declaration shall be a template-id. |
| 7763 | Diag(D.getIdentifierLoc(), |
| 7764 | diag::err_explicit_instantiation_without_template_id) |
| 7765 | << PrevTemplate; |
| 7766 | Diag(PrevTemplate->getLocation(), |
| 7767 | diag::note_explicit_instantiation_here); |
| 7768 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7769 | } |
| 7770 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7771 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7772 | TemplateArgumentListInfo TemplateArgs = |
| 7773 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7774 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7775 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 7776 | D.getIdentifierLoc(), TemplateArgs); |
| 7777 | if (Res.isInvalid()) |
| 7778 | return true; |
| 7779 | |
| 7780 | // Ignore access control bits, we don't need them for redeclaration |
| 7781 | // checking. |
| 7782 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7783 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7784 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7785 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7786 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7787 | // or a static data member of a class template specialization, the name of |
| 7788 | // the class template specialization in the qualified-id for the member |
| 7789 | // name shall be a simple-template-id. |
| 7790 | // |
| 7791 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7792 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 7793 | // This does not apply to variable template specializations, where the |
| 7794 | // template-id is in the unqualified-id instead. |
| 7795 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7796 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7797 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7798 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7799 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7800 | // Check the scope of this explicit instantiation. |
| 7801 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7802 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7803 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 7804 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 7805 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7806 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7807 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7808 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7809 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7810 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7811 | if (!HasNoEffect) { |
| 7812 | // Instantiate static data member or variable template. |
| 7813 | |
| 7814 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 7815 | if (PrevTemplate) { |
| 7816 | // Merge attributes. |
| 7817 | if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList()) |
| 7818 | ProcessDeclAttributeList(S, Prev, Attr); |
| 7819 | } |
| 7820 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7821 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 7822 | } |
| 7823 | |
| 7824 | // Check the new variable specialization against the parsed input. |
| 7825 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
| 7826 | Diag(T->getTypeLoc().getLocStart(), |
| 7827 | diag::err_invalid_var_template_spec_type) |
| 7828 | << 0 << PrevTemplate << R << Prev->getType(); |
| 7829 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 7830 | << 2 << PrevTemplate->getDeclName(); |
| 7831 | return true; |
| 7832 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7833 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7834 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7835 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7836 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7837 | |
| 7838 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 7839 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7840 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7841 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7842 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7843 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7844 | HasExplicitTemplateArgs = true; |
| 7845 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7846 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7847 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7848 | // A [...] function [...] can be explicitly instantiated from its template. |
| 7849 | // A member function [...] of a class template can be explicitly |
| 7850 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7851 | // template. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7852 | UnresolvedSet<8> Matches; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7853 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7854 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7855 | P != PEnd; ++P) { |
| 7856 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7857 | if (!HasExplicitTemplateArgs) { |
| 7858 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 7859 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType()); |
| 7860 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7861 | Matches.clear(); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7862 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7863 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7864 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 7865 | break; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7866 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7867 | } |
| 7868 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7869 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7870 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 7871 | if (!FunTmpl) |
| 7872 | continue; |
| 7873 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7874 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7875 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7876 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7877 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7878 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 7879 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7880 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7881 | // Keep track of almost-matches. |
| 7882 | FailedCandidates.addCandidate() |
| 7883 | .set(FunTmpl->getTemplatedDecl(), |
| 7884 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7885 | (void)TDK; |
| 7886 | continue; |
| 7887 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7888 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7889 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7890 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7891 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7892 | // Find the most specialized function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7893 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 7894 | Matches.begin(), Matches.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7895 | D.getIdentifierLoc(), |
| 7896 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 7897 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 7898 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7899 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7900 | if (Result == Matches.end()) |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7901 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7902 | |
| 7903 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 7904 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7905 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 7906 | // C++11 [except.spec]p4 |
| 7907 | // In an explicit instantiation an exception-specification may be specified, |
| 7908 | // but is not required. |
| 7909 | // If an exception-specification is specified in an explicit instantiation |
| 7910 | // directive, it shall be compatible with the exception-specifications of |
| 7911 | // other declarations of that function. |
| 7912 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 7913 | if (FPT->hasExceptionSpec()) { |
| 7914 | unsigned DiagID = |
| 7915 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 7916 | if (getLangOpts().MicrosoftExt) |
| 7917 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 7918 | bool Result = CheckEquivalentExceptionSpec( |
| 7919 | PDiag(DiagID) << Specialization->getType(), |
| 7920 | PDiag(diag::note_explicit_instantiation_here), |
| 7921 | Specialization->getType()->getAs<FunctionProtoType>(), |
| 7922 | Specialization->getLocation(), FPT, D.getLocStart()); |
| 7923 | // In Microsoft mode, mismatching exception specifications just cause a |
| 7924 | // warning. |
| 7925 | if (!getLangOpts().MicrosoftExt && Result) |
| 7926 | return true; |
| 7927 | } |
| 7928 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7929 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7930 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7931 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 7932 | << Specialization |
| 7933 | << (Specialization->getTemplateSpecializationKind() == |
| 7934 | TSK_ExplicitSpecialization); |
| 7935 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 7936 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7937 | } |
| 7938 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7939 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7940 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 7941 | PrevDecl = Specialization; |
| 7942 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7943 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7944 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7945 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7946 | PrevDecl, |
| 7947 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7948 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7949 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7950 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7951 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7952 | // FIXME: We may still want to build some representation of this |
| 7953 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7954 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7955 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7956 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 7957 | |
| 7958 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 2aa7acf | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 7959 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 7960 | if (Attr) |
| 7961 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7962 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7963 | if (Specialization->isDefined()) { |
| 7964 | // Let the ASTConsumer know that this function has been explicitly |
| 7965 | // instantiated now, and its linkage might have changed. |
| 7966 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 7967 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 7968 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7969 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7970 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7971 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7972 | // or a static data member of a class template specialization, the name of |
| 7973 | // the class template specialization in the qualified-id for the member |
| 7974 | // name shall be a simple-template-id. |
| 7975 | // |
| 7976 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7977 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7978 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7979 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7980 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7981 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7982 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7983 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7984 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7985 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7986 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7987 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7988 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7989 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7990 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7991 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7992 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7993 | } |
| 7994 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7995 | TypeResult |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7996 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 7997 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 7998 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 7999 | // This has to hold, because SS is expected to be defined. |
| 8000 | assert(Name && "Expected a name in a dependent tag"); |
| 8001 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8002 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8003 | if (!NNS) |
| 8004 | return true; |
| 8005 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8006 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 8007 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 8008 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 8009 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8010 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 8011 | return true; |
| 8012 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8013 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8014 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8015 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8016 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 8017 | |
| 8018 | // Create type-source location information for this type. |
| 8019 | TypeLocBuilder TLB; |
| 8020 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8021 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 8022 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 8023 | TL.setNameLoc(NameLoc); |
| 8024 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 8025 | } |
| 8026 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8027 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8028 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 8029 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 8030 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8031 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8032 | return true; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8033 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8034 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8035 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8036 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8037 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8038 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8039 | << FixItHint::CreateRemoval(TypenameLoc); |
| 8040 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8041 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8042 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 8043 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 8044 | if (T.isNull()) |
| 8045 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8046 | |
| 8047 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 8048 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8049 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8050 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8051 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8052 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8053 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8054 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8055 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8056 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8057 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8058 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8059 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8060 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8061 | } |
| 8062 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8063 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8064 | Sema::ActOnTypenameType(Scope *S, |
| 8065 | SourceLocation TypenameLoc, |
| 8066 | const CXXScopeSpec &SS, |
| 8067 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8068 | TemplateTy TemplateIn, |
| 8069 | SourceLocation TemplateNameLoc, |
| 8070 | SourceLocation LAngleLoc, |
| 8071 | ASTTemplateArgsPtr TemplateArgsIn, |
| 8072 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8073 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8074 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8075 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8076 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8077 | diag::ext_typename_outside_of_template) |
| 8078 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8079 | |
| 8080 | // Translate the parser's template argument list in our AST format. |
| 8081 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 8082 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 8083 | |
| 8084 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8085 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 8086 | // Construct a dependent template specialization type. |
| 8087 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8088 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8089 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 8090 | DTN->getQualifier(), |
| 8091 | DTN->getIdentifier(), |
| 8092 | TemplateArgs); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8093 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8094 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8095 | TypeLocBuilder Builder; |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8096 | DependentTemplateSpecializationTypeLoc SpecTL |
| 8097 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8098 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 8099 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 8100 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8101 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8102 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8103 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8104 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8105 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8106 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 8107 | } |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8108 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8109 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 8110 | if (T.isNull()) |
| 8111 | return true; |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8112 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8113 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8114 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8115 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8116 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8117 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 8118 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8119 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8120 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8121 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8122 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 8123 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8124 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 8125 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8126 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8127 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 8128 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8129 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 8130 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 8131 | } |
| 8132 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8133 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8134 | /// Determine whether this failed name lookup should be treated as being |
| 8135 | /// disabled by a usage of std::enable_if. |
| 8136 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
| 8137 | SourceRange &CondRange) { |
| 8138 | // We must be looking for a ::type... |
| 8139 | if (!II.isStr("type")) |
| 8140 | return false; |
| 8141 | |
| 8142 | // ... within an explicitly-written template specialization... |
| 8143 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 8144 | return false; |
| 8145 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8146 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 8147 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 8148 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8149 | return false; |
| 8150 | const TemplateSpecializationType *EnableIfTST = |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8151 | cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr()); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8152 | |
| 8153 | // ... which names a complete class template declaration... |
| 8154 | const TemplateDecl *EnableIfDecl = |
| 8155 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 8156 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 8157 | return false; |
| 8158 | |
| 8159 | // ... called "enable_if". |
| 8160 | const IdentifierInfo *EnableIfII = |
| 8161 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 8162 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 8163 | return false; |
| 8164 | |
| 8165 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8166 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8167 | return true; |
| 8168 | } |
| 8169 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8170 | /// \brief Build the type that describes a C++ typename specifier, |
| 8171 | /// e.g., "typename T::type". |
| 8172 | QualType |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8173 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 8174 | SourceLocation KeywordLoc, |
| 8175 | NestedNameSpecifierLoc QualifierLoc, |
| 8176 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8177 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8178 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8179 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8180 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8181 | DeclContext *Ctx = computeDeclContext(SS); |
| 8182 | if (!Ctx) { |
| 8183 | // If the nested-name-specifier is dependent and couldn't be |
| 8184 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8185 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 8186 | return Context.getDependentNameType(Keyword, |
| 8187 | QualifierLoc.getNestedNameSpecifier(), |
| 8188 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 8189 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8190 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8191 | // If the nested-name-specifier refers to the current instantiation, |
| 8192 | // the "typename" keyword itself is superfluous. In C++03, the |
| 8193 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 8194 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 8195 | // 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] | 8196 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8197 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 8198 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8199 | |
| 8200 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8201 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 8202 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8203 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8204 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8205 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8206 | case LookupResult::NotFound: { |
| 8207 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 8208 | // a more specific diagnostic. |
| 8209 | SourceRange CondRange; |
| 8210 | if (isEnableIf(QualifierLoc, II, CondRange)) { |
| 8211 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
| 8212 | << Ctx << CondRange; |
| 8213 | return QualType(); |
| 8214 | } |
| 8215 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 8216 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8217 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8218 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8219 | |
| 8220 | case LookupResult::FoundUnresolvedValue: { |
| 8221 | // We found a using declaration that is a value. Most likely, the using |
| 8222 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8223 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8224 | IILoc); |
| 8225 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 8226 | << Name << Ctx << FullRange; |
| 8227 | if (UnresolvedUsingValueDecl *Using |
| 8228 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8229 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8230 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 8231 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 8232 | } |
| 8233 | } |
| 8234 | // Fall through to create a dependent typename type, from which we can recover |
| 8235 | // better. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8236 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 8237 | case LookupResult::NotFoundInCurrentInstantiation: |
| 8238 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8239 | return Context.getDependentNameType(Keyword, |
| 8240 | QualifierLoc.getNestedNameSpecifier(), |
| 8241 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8242 | |
| 8243 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8244 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8245 | // We found a type. Build an ElaboratedType, since the |
| 8246 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 8247 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8248 | return Context.getElaboratedType(ETK_Typename, |
| 8249 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8250 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8251 | } |
| 8252 | |
| 8253 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 8254 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8255 | break; |
| 8256 | |
| 8257 | case LookupResult::FoundOverloaded: |
| 8258 | DiagID = diag::err_typename_nested_not_type; |
| 8259 | Referenced = *Result.begin(); |
| 8260 | break; |
| 8261 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 8262 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8263 | return QualType(); |
| 8264 | } |
| 8265 | |
| 8266 | // If we get here, it's because name lookup did not find a |
| 8267 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8268 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8269 | IILoc); |
| 8270 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8271 | if (Referenced) |
| 8272 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 8273 | << Name; |
| 8274 | return QualType(); |
| 8275 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8276 | |
| 8277 | namespace { |
| 8278 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 8279 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8280 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8281 | SourceLocation Loc; |
| 8282 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8283 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8284 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 8285 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8286 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8287 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8288 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8289 | DeclarationName Entity) |
| 8290 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8291 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8292 | |
| 8293 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8294 | /// transformed. |
| 8295 | /// |
| 8296 | /// For the purposes of type reconstruction, a type has already been |
| 8297 | /// transformed if it is NULL or if it is not dependent. |
| 8298 | bool AlreadyTransformed(QualType T) { |
| 8299 | return T.isNull() || !T->isDependentType(); |
| 8300 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8301 | |
| 8302 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8303 | /// rebuilt. |
| 8304 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8305 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8306 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 8307 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8308 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 8309 | /// \brief Sets the "base" location and entity when that |
| 8310 | /// information is known based on another transformation. |
| 8311 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 8312 | this->Loc = Loc; |
| 8313 | this->Entity = Entity; |
| 8314 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 8315 | |
| 8316 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 8317 | // Lambdas never need to be transformed. |
| 8318 | return E; |
| 8319 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8320 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 8321 | } // end anonymous namespace |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8322 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8323 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 8324 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8325 | /// 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] | 8326 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8327 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8328 | /// partial specialization thereof). This routine will rebuild that type now |
| 8329 | /// that we have entered the declarator's scope, which may produce different |
| 8330 | /// canonical types, e.g., |
| 8331 | /// |
| 8332 | /// \code |
| 8333 | /// template<typename T> |
| 8334 | /// struct X { |
| 8335 | /// typedef T* pointer; |
| 8336 | /// pointer data(); |
| 8337 | /// }; |
| 8338 | /// |
| 8339 | /// template<typename T> |
| 8340 | /// typename X<T>::pointer X<T>::data() { ... } |
| 8341 | /// \endcode |
| 8342 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 8343 | /// 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] | 8344 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 8345 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8346 | /// 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] | 8347 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 8348 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8349 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 8350 | SourceLocation Loc, |
| 8351 | DeclarationName Name) { |
| 8352 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8353 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8354 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8355 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 8356 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 8357 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8358 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8359 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8360 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 8361 | DeclarationName()); |
| 8362 | return Rebuilder.TransformExpr(E); |
| 8363 | } |
| 8364 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8365 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8366 | if (SS.isInvalid()) |
| 8367 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8368 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8369 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8370 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 8371 | DeclarationName()); |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8372 | NestedNameSpecifierLoc Rebuilt |
| 8373 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 8374 | if (!Rebuilt) |
| 8375 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8376 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8377 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8378 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8379 | } |
| 8380 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8381 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 8382 | /// instantiation. |
| 8383 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 8384 | TemplateParameterList *Params) { |
| 8385 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 8386 | Decl *Param = Params->getParam(I); |
| 8387 | |
| 8388 | // There is nothing to rebuild in a type parameter. |
| 8389 | if (isa<TemplateTypeParmDecl>(Param)) |
| 8390 | continue; |
| 8391 | |
| 8392 | // Rebuild the template parameter list of a template template parameter. |
| 8393 | if (TemplateTemplateParmDecl *TTP |
| 8394 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 8395 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 8396 | TTP->getTemplateParameters())) |
| 8397 | return true; |
| 8398 | |
| 8399 | continue; |
| 8400 | } |
| 8401 | |
| 8402 | // Rebuild the type of a non-type template parameter. |
| 8403 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 8404 | TypeSourceInfo *NewTSI |
| 8405 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 8406 | NTTP->getLocation(), |
| 8407 | NTTP->getDeclName()); |
| 8408 | if (!NewTSI) |
| 8409 | return true; |
| 8410 | |
| 8411 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 8412 | NTTP->setTypeSourceInfo(NewTSI); |
| 8413 | NTTP->setType(NewTSI->getType()); |
| 8414 | } |
| 8415 | } |
| 8416 | |
| 8417 | return false; |
| 8418 | } |
| 8419 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8420 | /// \brief Produces a formatted string that describes the binding of |
| 8421 | /// template parameters to template arguments. |
| 8422 | std::string |
| 8423 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8424 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8425 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8426 | } |
| 8427 | |
| 8428 | std::string |
| 8429 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8430 | const TemplateArgument *Args, |
| 8431 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8432 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8433 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8434 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8435 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8436 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8437 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8438 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8439 | if (I >= NumArgs) |
| 8440 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8441 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8442 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8443 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8444 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8445 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8446 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8447 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8448 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8449 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8450 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8451 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8452 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8453 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 8454 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8455 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8456 | |
| 8457 | Out << ']'; |
| 8458 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8459 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8460 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8461 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 8462 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8463 | if (!FD) |
| 8464 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8465 | |
| 8466 | LateParsedTemplate *LPT = new LateParsedTemplate; |
| 8467 | |
| 8468 | // Take tokens to avoid allocations |
| 8469 | LPT->Toks.swap(Toks); |
| 8470 | LPT->D = FnD; |
Chandler Carruth | 52cee4d | 2015-03-26 09:08:15 +0000 | [diff] [blame] | 8471 | LateParsedTemplateMap.insert(std::make_pair(FD, LPT)); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8472 | |
| 8473 | FD->setLateTemplateParsed(true); |
| 8474 | } |
| 8475 | |
| 8476 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 8477 | if (!FD) |
| 8478 | return; |
| 8479 | FD->setLateTemplateParsed(false); |
| 8480 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8481 | |
| 8482 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 8483 | DeclContext *DC = CurContext; |
| 8484 | |
| 8485 | while (DC) { |
| 8486 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 8487 | const FunctionDecl *FD = RD->isLocalClass(); |
| 8488 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 8489 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 8490 | return false; |
| 8491 | |
| 8492 | DC = DC->getParent(); |
| 8493 | } |
| 8494 | return false; |
| 8495 | } |