Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +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. |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +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. |
Douglas Gregor | fe1e110 | 2009-02-27 19:31:52 +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" |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 35 | using namespace clang; |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 36 | using namespace sema; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 37 | |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 38 | // Exported for use by Parser. |
| 39 | SourceRange |
| 40 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
| 41 | unsigned N) { |
| 42 | if (!N) return SourceRange(); |
| 43 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
| 44 | } |
| 45 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 46 | /// \brief Determine whether the declaration found is acceptable as the name |
| 47 | /// of a template and, if so, return that template declaration. Otherwise, |
| 48 | /// returns NULL. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 49 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 50 | NamedDecl *Orig, |
| 51 | bool AllowFunctionTemplates) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 52 | NamedDecl *D = Orig->getUnderlyingDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 54 | if (isa<TemplateDecl>(D)) { |
| 55 | if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 56 | return nullptr; |
| 57 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 58 | return Orig; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 59 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 61 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 62 | // C++ [temp.local]p1: |
| 63 | // Like normal (non-template) classes, class templates have an |
| 64 | // injected-class-name (Clause 9). The injected-class-name |
| 65 | // can be used with or without a template-argument-list. When |
| 66 | // it is used without a template-argument-list, it is |
| 67 | // equivalent to the injected-class-name followed by the |
| 68 | // template-parameters of the class template enclosed in |
| 69 | // <>. When it is used with a template-argument-list, it |
| 70 | // refers to the specified class template specialization, |
| 71 | // which could be the current specialization or another |
| 72 | // specialization. |
| 73 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 568a071 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 74 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 75 | if (Record->getDescribedClassTemplate()) |
| 76 | return Record->getDescribedClassTemplate(); |
| 77 | |
| 78 | if (ClassTemplateSpecializationDecl *Spec |
| 79 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 80 | return Spec->getSpecializedTemplate(); |
| 81 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 83 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 84 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 86 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 89 | void Sema::FilterAcceptableTemplateNames(LookupResult &R, |
| 90 | bool AllowFunctionTemplates) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 91 | // The set of class templates we've already seen. |
| 92 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 93 | LookupResult::Filter filter = R.makeFilter(); |
| 94 | while (filter.hasNext()) { |
| 95 | NamedDecl *Orig = filter.next(); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 96 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig, |
| 97 | AllowFunctionTemplates); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 98 | if (!Repl) |
| 99 | filter.erase(); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 100 | else if (Repl != Orig) { |
| 101 | |
| 102 | // C++ [temp.local]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 103 | // 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] | 104 | // 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] | 105 | // one base class). If all of the injected-class-names that are found |
| 106 | // refer to specializations of the same class template, and if the name |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 107 | // is used as a template-name, the reference refers to the class |
| 108 | // template itself and not a specialization thereof, and is not |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 109 | // ambiguous. |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 110 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 111 | if (!ClassTemplates.insert(ClassTmpl).second) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 112 | filter.erase(); |
| 113 | continue; |
| 114 | } |
John McCall | bd8062d | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 115 | |
| 116 | // FIXME: we promote access to public here as a workaround to |
| 117 | // the fact that LookupResult doesn't let us remember that we |
| 118 | // found this template through a particular injected class name, |
| 119 | // which means we end up doing nasty things to the invariants. |
| 120 | // Pretending that access is public is *much* safer. |
| 121 | filter.replace(Repl, AS_public); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 122 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 123 | } |
| 124 | filter.done(); |
| 125 | } |
| 126 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 127 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R, |
| 128 | bool AllowFunctionTemplates) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 129 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 130 | if (isAcceptableTemplateName(Context, *I, AllowFunctionTemplates)) |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 131 | return true; |
| 132 | |
Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 133 | return false; |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 136 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 137 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 138 | bool hasTemplateKeyword, |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 139 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 140 | ParsedType ObjectTypePtr, |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 141 | bool EnteringContext, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 142 | TemplateTy &TemplateResult, |
| 143 | bool &MemberOfUnknownSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 144 | assert(getLangOpts().CPlusPlus && "No template names in C!"); |
Douglas Gregor | 411e5ac | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 145 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 146 | DeclarationName TName; |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 147 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 148 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 149 | switch (Name.getKind()) { |
| 150 | case UnqualifiedId::IK_Identifier: |
| 151 | TName = DeclarationName(Name.Identifier); |
| 152 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 153 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 154 | case UnqualifiedId::IK_OperatorFunctionId: |
| 155 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 156 | Name.OperatorFunctionId.Operator); |
| 157 | break; |
| 158 | |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 159 | case UnqualifiedId::IK_LiteralOperatorId: |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 160 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 161 | break; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 162 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 163 | default: |
| 164 | return TNK_Non_template; |
| 165 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 167 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 169 | LookupResult R(*this, TName, Name.getLocStart(), LookupOrdinaryName); |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 170 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 171 | MemberOfUnknownSpecialization); |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 172 | if (R.empty()) return TNK_Non_template; |
| 173 | if (R.isAmbiguous()) { |
| 174 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 175 | R.suppressDiagnostics(); |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 176 | |
| 177 | // FIXME: we might have ambiguous templates, in which case we |
| 178 | // should at least parse them properly! |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 179 | return TNK_Non_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 180 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 181 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 182 | TemplateName Template; |
| 183 | TemplateNameKind TemplateKind; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 185 | unsigned ResultCount = R.end() - R.begin(); |
| 186 | if (ResultCount > 1) { |
| 187 | // We assume that we'll preserve the qualifier from a function |
| 188 | // template name in other ways. |
| 189 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 190 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 191 | |
| 192 | // We'll do this lookup again later. |
| 193 | R.suppressDiagnostics(); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 194 | } else { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 195 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 196 | |
| 197 | if (SS.isSet() && !SS.isInvalid()) { |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 198 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 199 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 200 | hasTemplateKeyword, TD); |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 201 | } else { |
| 202 | Template = TemplateName(TD); |
| 203 | } |
| 204 | |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 205 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 206 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 207 | |
| 208 | // We'll do this lookup again later. |
| 209 | R.suppressDiagnostics(); |
| 210 | } else { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 211 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 212 | isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD) || |
| 213 | isa<BuiltinTemplateDecl>(TD)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 214 | TemplateKind = |
| 215 | isa<VarTemplateDecl>(TD) ? TNK_Var_template : TNK_Type_template; |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 216 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 217 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 219 | TemplateResult = TemplateTy::make(Template); |
| 220 | return TemplateKind; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 221 | } |
| 222 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 223 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 224 | SourceLocation IILoc, |
| 225 | Scope *S, |
| 226 | const CXXScopeSpec *SS, |
| 227 | TemplateTy &SuggestedTemplate, |
| 228 | TemplateNameKind &SuggestedKind) { |
| 229 | // We can't recover unless there's a dependent scope specifier preceding the |
| 230 | // template name. |
Douglas Gregor | 20c38a7 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 231 | // FIXME: Typo correction? |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 232 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 233 | computeDeclContext(*SS)) |
| 234 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 235 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 236 | // The code is missing a 'template' keyword prior to the dependent template |
| 237 | // name. |
| 238 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 239 | Diag(IILoc, diag::err_template_kw_missing) |
| 240 | << Qualifier << II.getName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 241 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 242 | SuggestedTemplate |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 243 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 244 | SuggestedKind = TNK_Dependent_template_name; |
| 245 | return true; |
| 246 | } |
| 247 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 248 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 249 | Scope *S, CXXScopeSpec &SS, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 250 | QualType ObjectType, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 251 | bool EnteringContext, |
| 252 | bool &MemberOfUnknownSpecialization) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 253 | // Determine where to perform name lookup |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 254 | MemberOfUnknownSpecialization = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 255 | DeclContext *LookupCtx = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 256 | bool isDependent = false; |
| 257 | if (!ObjectType.isNull()) { |
| 258 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 259 | // x->B::f, and we are looking into the type of the object. |
| 260 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 261 | LookupCtx = computeDeclContext(ObjectType); |
| 262 | isDependent = ObjectType->isDependentType(); |
Richard Smith | 5ed7956 | 2013-06-07 20:03:01 +0000 | [diff] [blame] | 263 | assert((isDependent || !ObjectType->isIncompleteType() || |
| 264 | ObjectType->castAs<TagType>()->isBeingDefined()) && |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 265 | "Caller should have completed object type"); |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 266 | |
| 267 | // Template names cannot appear inside an Objective-C class or object type. |
| 268 | if (ObjectType->isObjCObjectOrInterfaceType()) { |
| 269 | Found.clear(); |
| 270 | return; |
| 271 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 272 | } else if (SS.isSet()) { |
| 273 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 274 | // so long into the context associated with the prior nested-name-specifier. |
| 275 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 276 | isDependent = isDependentScopeSpecifier(SS); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 277 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 278 | // The declaration context must be complete. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 279 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 280 | return; |
| 281 | } |
| 282 | |
| 283 | bool ObjectTypeSearchedInScope = false; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 284 | bool AllowFunctionTemplatesInLookup = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 285 | if (LookupCtx) { |
| 286 | // Perform "qualified" name lookup into the declaration context we |
| 287 | // computed, which is either the type of the base of a member access |
| 288 | // expression or the declaration context associated with a prior |
| 289 | // nested-name-specifier. |
| 290 | LookupQualifiedName(Found, LookupCtx); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 291 | if (!ObjectType.isNull() && Found.empty()) { |
| 292 | // C++ [basic.lookup.classref]p1: |
| 293 | // In a class member access expression (5.2.5), if the . or -> token is |
| 294 | // immediately followed by an identifier followed by a <, the |
| 295 | // identifier must be looked up to determine whether the < is the |
| 296 | // beginning of a template argument list (14.2) or a less-than operator. |
| 297 | // The identifier is first looked up in the class of the object |
| 298 | // expression. If the identifier is not found, it is then looked up in |
| 299 | // the context of the entire postfix-expression and shall name a class |
| 300 | // or function template. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 301 | if (S) LookupName(Found, S); |
| 302 | ObjectTypeSearchedInScope = true; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 303 | AllowFunctionTemplatesInLookup = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 304 | } |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 305 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 306 | // We cannot look into a dependent object type or nested nme |
| 307 | // specifier. |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 308 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 309 | return; |
| 310 | } else { |
| 311 | // Perform unqualified name lookup in the current scope. |
| 312 | LookupName(Found, S); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 313 | |
| 314 | if (!ObjectType.isNull()) |
| 315 | AllowFunctionTemplatesInLookup = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 318 | if (Found.empty() && !isDependent) { |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 319 | // If we did not find any names, attempt to correct any typos. |
| 320 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 321 | Found.clear(); |
Kaelyn Uhrain | 637b5b3 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 322 | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 323 | auto FilterCCC = llvm::make_unique<CorrectionCandidateCallback>(); |
| 324 | FilterCCC->WantTypeSpecifiers = false; |
| 325 | FilterCCC->WantExpressionKeywords = false; |
| 326 | FilterCCC->WantRemainingKeywords = false; |
| 327 | FilterCCC->WantCXXNamedCasts = true; |
| 328 | if (TypoCorrection Corrected = CorrectTypo( |
| 329 | Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS, |
| 330 | std::move(FilterCCC), CTK_ErrorRecovery, LookupCtx)) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 331 | Found.setLookupName(Corrected.getCorrection()); |
| 332 | if (Corrected.getCorrectionDecl()) |
| 333 | Found.addDecl(Corrected.getCorrectionDecl()); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 334 | FilterAcceptableTemplateNames(Found); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 335 | if (!Found.empty()) { |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 336 | if (LookupCtx) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 337 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 338 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 339 | Name.getAsString() == CorrectedStr; |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 340 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
| 341 | << Name << LookupCtx << DroppedSpecifier |
| 342 | << SS.getRange()); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 343 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 344 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 345 | } |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 346 | } |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 347 | } else { |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 348 | Found.setLookupName(Name); |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 352 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 353 | if (Found.empty()) { |
| 354 | if (isDependent) |
| 355 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 356 | return; |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 357 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 358 | |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 359 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && |
Richard Smith | e7d67f2 | 2013-09-03 21:22:41 +0000 | [diff] [blame] | 360 | !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 361 | // C++03 [basic.lookup.classref]p1: |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 362 | // [...] If the lookup in the class of the object expression finds a |
| 363 | // template, the name is also looked up in the context of the entire |
| 364 | // postfix-expression and [...] |
| 365 | // |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 366 | // Note: C++11 does not perform this second lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 367 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 368 | LookupOrdinaryName); |
| 369 | LookupName(FoundOuter, S); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 370 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 371 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 372 | if (FoundOuter.empty()) { |
| 373 | // - if the name is not found, the name found in the class of the |
| 374 | // object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 375 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 376 | FoundOuter.isAmbiguous()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 377 | // - if the name is found in the context of the entire |
| 378 | // postfix-expression and does not name a class template, the name |
| 379 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 380 | FoundOuter.clear(); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 381 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 382 | // - if the name found is a class template, it must refer to the same |
| 383 | // entity as the one found in the class of the object expression, |
| 384 | // otherwise the program is ill-formed. |
| 385 | if (!Found.isSingleResult() || |
| 386 | Found.getFoundDecl()->getCanonicalDecl() |
| 387 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 388 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 2f96e9f | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 389 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 390 | << Found.getLookupName() |
| 391 | << ObjectType; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 392 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 393 | diag::note_ambig_member_ref_object_type) |
| 394 | << ObjectType; |
| 395 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 396 | diag::note_ambig_member_ref_scope); |
| 397 | |
| 398 | // Recover by taking the template that we found in the object |
| 399 | // expression's type. |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 405 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 406 | /// was just parsed. This is only possible with an explicit scope |
| 407 | /// specifier naming a dependent type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 408 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 409 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 410 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 411 | const DeclarationNameInfo &NameInfo, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 412 | bool isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 413 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 414 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 415 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 416 | if (!isAddressOfOperand && |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 417 | isa<CXXMethodDecl>(DC) && |
| 418 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 419 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 420 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 421 | // Since the 'this' expression is synthesized, we don't need to |
| 422 | // perform the double-lookup check. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 423 | NamedDecl *FirstQualifierInScope = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 424 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 425 | return CXXDependentScopeMemberExpr::Create( |
| 426 | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
| 427 | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
| 428 | FirstQualifierInScope, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 431 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 432 | } |
| 433 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 434 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 435 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 436 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 437 | const DeclarationNameInfo &NameInfo, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 438 | const TemplateArgumentListInfo *TemplateArgs) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 439 | return DependentScopeDeclRefExpr::Create( |
| 440 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 441 | TemplateArgs); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 444 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 445 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 446 | /// declaration at location Loc. Returns true to indicate that this is |
| 447 | /// an error, and false otherwise. |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 448 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 449 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 450 | |
| 451 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 452 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 453 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 454 | |
| 455 | // C++ [temp.local]p4: |
| 456 | // A template-parameter shall not be redeclared within its |
| 457 | // scope (including nested scopes). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 459 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 460 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 461 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 464 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 465 | /// the parameter D to reference the templated declaration and return a pointer |
| 466 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 467 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 468 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 469 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 470 | return Temp; |
| 471 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 472 | return nullptr; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 475 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 476 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 477 | assert(Kind == Template && |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 478 | "Only template template arguments can be pack expansions here"); |
| 479 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 480 | "Template template argument pack expansion without packs"); |
| 481 | ParsedTemplateArgument Result(*this); |
| 482 | Result.EllipsisLoc = EllipsisLoc; |
| 483 | return Result; |
| 484 | } |
| 485 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 486 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 487 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 488 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 489 | switch (Arg.getKind()) { |
| 490 | case ParsedTemplateArgument::Type: { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 491 | TypeSourceInfo *DI; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 492 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 493 | if (!DI) |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 494 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 495 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 496 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 497 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 498 | case ParsedTemplateArgument::NonType: { |
| 499 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 500 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 501 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 502 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 503 | case ParsedTemplateArgument::Template: { |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 504 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 505 | TemplateArgument TArg; |
| 506 | if (Arg.getEllipsisLoc().isValid()) |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 507 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 508 | else |
| 509 | TArg = Template; |
| 510 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 511 | Arg.getScopeSpec().getWithLocInContext( |
| 512 | SemaRef.Context), |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 513 | Arg.getLocation(), |
| 514 | Arg.getEllipsisLoc()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 515 | } |
| 516 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 517 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 518 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 519 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 520 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 521 | /// \brief Translates template arguments as provided by the parser |
| 522 | /// into template arguments used by semantic analysis. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 523 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 524 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 525 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 526 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 527 | TemplateArgsIn[I])); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 528 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 529 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 530 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 531 | SourceLocation Loc, |
| 532 | IdentifierInfo *Name) { |
| 533 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
| 534 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 535 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 536 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 537 | } |
| 538 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 539 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 540 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 541 | /// the keyword "typename" was used to declare the type parameter |
| 542 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 543 | /// "class" or "typename" keyword. ParamName is the name of the |
| 544 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 545 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 546 | /// If the type parameter has a default argument, it will be added |
| 547 | /// later via ActOnTypeParameterDefault. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 548 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 549 | SourceLocation EllipsisLoc, |
| 550 | SourceLocation KeyLoc, |
| 551 | IdentifierInfo *ParamName, |
| 552 | SourceLocation ParamNameLoc, |
| 553 | unsigned Depth, unsigned Position, |
| 554 | SourceLocation EqualLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 555 | ParsedType DefaultArg) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 556 | assert(S->isTemplateParamScope() && |
| 557 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 558 | bool Invalid = false; |
| 559 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 560 | SourceLocation Loc = ParamNameLoc; |
| 561 | if (!ParamName) |
| 562 | Loc = KeyLoc; |
| 563 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 564 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 565 | TemplateTypeParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 566 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 567 | KeyLoc, Loc, Depth, Position, ParamName, |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 568 | Typename, IsParameterPack); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 569 | Param->setAccess(AS_public); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 570 | if (Invalid) |
| 571 | Param->setInvalidDecl(); |
| 572 | |
| 573 | if (ParamName) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 574 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 575 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 576 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 577 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 578 | IdResolver.AddDecl(Param); |
| 579 | } |
| 580 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 581 | // C++0x [temp.param]p9: |
| 582 | // A default template-argument may be specified for any kind of |
| 583 | // template-parameter that is not a template parameter pack. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 584 | if (DefaultArg && IsParameterPack) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 585 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 586 | DefaultArg = ParsedType(); |
| 587 | } |
| 588 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 589 | // Handle the default argument, if provided. |
| 590 | if (DefaultArg) { |
| 591 | TypeSourceInfo *DefaultTInfo; |
| 592 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 593 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 594 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 595 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 596 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 597 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 598 | UPPC_DefaultArgument)) |
| 599 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 600 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 601 | // Check the template argument itself. |
| 602 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 603 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 604 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 605 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 606 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 607 | Param->setDefaultArgument(DefaultTInfo); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 608 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 609 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 610 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 613 | /// \brief Check that the type of a non-type template parameter is |
| 614 | /// well-formed. |
| 615 | /// |
| 616 | /// \returns the (possibly-promoted) parameter type if valid; |
| 617 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 618 | QualType |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 619 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 620 | // We don't allow variably-modified types as the type of non-type template |
| 621 | // parameters. |
| 622 | if (T->isVariablyModifiedType()) { |
| 623 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 624 | << T; |
| 625 | return QualType(); |
| 626 | } |
| 627 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 628 | // C++ [temp.param]p4: |
| 629 | // |
| 630 | // A non-type template-parameter shall have one of the following |
| 631 | // (optionally cv-qualified) types: |
| 632 | // |
| 633 | // -- integral or enumeration type, |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 634 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | // -- pointer to object or pointer to function, |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 636 | T->isPointerType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 637 | // -- reference to object or reference to function, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 638 | T->isReferenceType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 639 | // -- pointer to member, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 640 | T->isMemberPointerType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 641 | // -- std::nullptr_t. |
| 642 | T->isNullPtrType() || |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 643 | // If T is a dependent type, we can't do the check now, so we |
| 644 | // assume that it is well-formed. |
Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 645 | T->isDependentType()) { |
| 646 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 647 | // are ignored when determining its type. |
| 648 | return T.getUnqualifiedType(); |
| 649 | } |
| 650 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 651 | // C++ [temp.param]p8: |
| 652 | // |
| 653 | // A non-type template-parameter of type "array of T" or |
| 654 | // "function returning T" is adjusted to be of type "pointer to |
| 655 | // T" or "pointer to function returning T", respectively. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 656 | else if (T->isArrayType() || T->isFunctionType()) |
| 657 | return Context.getDecayedType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 658 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 659 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 660 | << T; |
| 661 | |
| 662 | return QualType(); |
| 663 | } |
| 664 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 665 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 666 | unsigned Depth, |
| 667 | unsigned Position, |
| 668 | SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 669 | Expr *Default) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 670 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 671 | QualType T = TInfo->getType(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 672 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 673 | assert(S->isTemplateParamScope() && |
| 674 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 675 | bool Invalid = false; |
| 676 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 677 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 678 | if (T.isNull()) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 679 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 680 | Invalid = true; |
| 681 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 682 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 683 | IdentifierInfo *ParamName = D.getIdentifier(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 684 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 685 | NonTypeTemplateParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 686 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 687 | D.getLocStart(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 688 | D.getIdentifierLoc(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 689 | Depth, Position, ParamName, T, |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 690 | IsParameterPack, TInfo); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 691 | Param->setAccess(AS_public); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 692 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 693 | if (Invalid) |
| 694 | Param->setInvalidDecl(); |
| 695 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 696 | if (ParamName) { |
| 697 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 698 | ParamName); |
| 699 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 700 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 701 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 702 | IdResolver.AddDecl(Param); |
| 703 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 704 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 705 | // C++0x [temp.param]p9: |
| 706 | // A default template-argument may be specified for any kind of |
| 707 | // template-parameter that is not a template parameter pack. |
| 708 | if (Default && IsParameterPack) { |
| 709 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 710 | Default = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 713 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 714 | if (Default) { |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 715 | // Check for unexpanded parameter packs. |
| 716 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 717 | return Param; |
| 718 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 719 | TemplateArgument Converted; |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 720 | ExprResult DefaultRes = |
| 721 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 722 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 723 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 724 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 725 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 726 | Default = DefaultRes.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 727 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 728 | Param->setDefaultArgument(Default); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 729 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 730 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 731 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 732 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 733 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 734 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 735 | /// parameter (e.g. T in template <template \<typename> class T> class array) |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 736 | /// has been parsed. S is the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 737 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 738 | SourceLocation TmpLoc, |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 739 | TemplateParameterList *Params, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 740 | SourceLocation EllipsisLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 741 | IdentifierInfo *Name, |
| 742 | SourceLocation NameLoc, |
| 743 | unsigned Depth, |
| 744 | unsigned Position, |
| 745 | SourceLocation EqualLoc, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 746 | ParsedTemplateArgument Default) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 747 | assert(S->isTemplateParamScope() && |
| 748 | "Template template parameter not in template parameter scope!"); |
| 749 | |
| 750 | // Construct the parameter object. |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 751 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 752 | TemplateTemplateParmDecl *Param = |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 753 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 754 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 755 | Depth, Position, IsParameterPack, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 756 | Name, Params); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 757 | Param->setAccess(AS_public); |
| 758 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 759 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 760 | // into the scope and lookup mechanisms. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 761 | if (Name) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 762 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 763 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 764 | S->AddDecl(Param); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 765 | IdResolver.AddDecl(Param); |
| 766 | } |
| 767 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 768 | if (Params->size() == 0) { |
| 769 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 770 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 771 | Param->setInvalidDecl(); |
| 772 | } |
| 773 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 774 | // C++0x [temp.param]p9: |
| 775 | // A default template-argument may be specified for any kind of |
| 776 | // template-parameter that is not a template parameter pack. |
| 777 | if (IsParameterPack && !Default.isInvalid()) { |
| 778 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 779 | Default = ParsedTemplateArgument(); |
| 780 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 781 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 782 | if (!Default.isInvalid()) { |
| 783 | // Check only that we have a template template argument. We don't want to |
| 784 | // try to check well-formedness now, because our template template parameter |
| 785 | // might have dependent types in its template parameters, which we wouldn't |
| 786 | // be able to match now. |
| 787 | // |
| 788 | // If none of the template template parameter's template arguments mention |
| 789 | // other template parameters, we could actually perform more checking here. |
| 790 | // However, it isn't worth doing. |
| 791 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 792 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 793 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 794 | << DefaultArg.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 795 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 796 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 797 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 798 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 799 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 800 | DefaultArg.getArgument().getAsTemplate(), |
| 801 | UPPC_DefaultArgument)) |
| 802 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 803 | |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 804 | Param->setDefaultArgument(Context, DefaultArg); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 805 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 806 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 807 | return Param; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 810 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 811 | /// contains the template parameters in Params/NumParams. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 812 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 813 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 814 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 815 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 816 | SourceLocation LAngleLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 817 | Decl **Params, unsigned NumParams, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 818 | SourceLocation RAngleLoc) { |
| 819 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 820 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 821 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 822 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 823 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 824 | RAngleLoc); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 825 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 826 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 827 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 828 | if (SS.isSet()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 829 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 830 | } |
| 831 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 832 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 833 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 834 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 835 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 836 | AttributeList *Attr, |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 837 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 838 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 839 | SourceLocation FriendLoc, |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 840 | unsigned NumOuterTemplateParamLists, |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 841 | TemplateParameterList** OuterTemplateParamLists, |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 842 | SkipBodyInfo *SkipBody) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 844 | "No template parameters"); |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 845 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 846 | bool Invalid = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 847 | |
| 848 | // Check that we can declare a template here. |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 849 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 850 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 851 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 852 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 853 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 854 | |
| 855 | // There is no such thing as an unnamed class template. |
| 856 | if (!Name) { |
| 857 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 858 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 861 | // Find any previous declaration with this name. For a friend with no |
| 862 | // scope explicitly specified, we only look for tag declarations (per |
| 863 | // C++11 [basic.lookup.elab]p2). |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 864 | DeclContext *SemanticContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 865 | LookupResult Previous(*this, Name, NameLoc, |
| 866 | (SS.isEmpty() && TUK == TUK_Friend) |
| 867 | ? LookupTagName : LookupOrdinaryName, |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 868 | ForRedeclaration); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 869 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 870 | SemanticContext = computeDeclContext(SS, true); |
| 871 | if (!SemanticContext) { |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 872 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 873 | // in the AST, and historically we have just ignored such friend |
| 874 | // class templates, so don't complain here. |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 875 | Diag(NameLoc, TUK == TUK_Friend |
| 876 | ? diag::warn_template_qualified_friend_ignored |
| 877 | : diag::err_template_qualified_declarator_no_match) |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 878 | << SS.getScopeRep() << SS.getRange(); |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 879 | return TUK != TUK_Friend; |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 880 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 881 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 882 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 883 | return true; |
| 884 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 885 | // If we're adding a template to a dependent context, we may need to |
| 886 | // rebuilding some of the types used within the template parameter list, |
| 887 | // now that we know what the current instantiation is. |
| 888 | if (SemanticContext->isDependentContext()) { |
| 889 | ContextRAII SavedContext(*this, SemanticContext); |
| 890 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 891 | Invalid = true; |
Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 892 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
| 893 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 894 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 895 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 896 | } else { |
| 897 | SemanticContext = CurContext; |
Richard Smith | 88fe69c | 2015-07-06 01:45:27 +0000 | [diff] [blame] | 898 | |
| 899 | // C++14 [class.mem]p14: |
| 900 | // If T is the name of a class, then each of the following shall have a |
| 901 | // name different from T: |
| 902 | // -- every member template of class T |
| 903 | if (TUK != TUK_Friend && |
| 904 | DiagnoseClassNameShadow(SemanticContext, |
| 905 | DeclarationNameInfo(Name, NameLoc))) |
| 906 | return true; |
| 907 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 908 | LookupName(Previous, S); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 909 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 910 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 911 | if (Previous.isAmbiguous()) |
| 912 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 913 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 914 | NamedDecl *PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 915 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 916 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 917 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 918 | // If there is a previous declaration with the same name, check |
| 919 | // whether this is a valid redeclaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 920 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 921 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 922 | |
| 923 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 924 | // class template partial specialization, or class template specialization. |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 925 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 926 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 927 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 928 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 929 | PrevClassTemplate |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 930 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 931 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 932 | PrevClassTemplate |
| 933 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 934 | ->getSpecializedTemplate(); |
| 935 | } |
| 936 | } |
| 937 | |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 938 | if (TUK == TUK_Friend) { |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 939 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 940 | // [...] When looking for a prior declaration of a class or a function |
| 941 | // 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] | 942 | // function is neither a qualified name nor a template-id, scopes outside |
| 943 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 944 | if (!SS.isSet()) { |
| 945 | DeclContext *OutermostContext = CurContext; |
| 946 | while (!OutermostContext->isFileContext()) |
| 947 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 948 | |
Richard Smith | 61e582f | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 949 | if (PrevDecl && |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 950 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 951 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 952 | SemanticContext = PrevDecl->getDeclContext(); |
| 953 | } else { |
| 954 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 955 | // context we computed is the semantic context for our new |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 956 | // declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 957 | PrevDecl = PrevClassTemplate = nullptr; |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 958 | SemanticContext = OutermostContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 959 | |
| 960 | // Check that the chosen semantic context doesn't already contain a |
| 961 | // declaration of this name as a non-tag type. |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 962 | Previous.clear(LookupOrdinaryName); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 963 | DeclContext *LookupContext = SemanticContext; |
| 964 | while (LookupContext->isTransparentContext()) |
| 965 | LookupContext = LookupContext->getLookupParent(); |
| 966 | LookupQualifiedName(Previous, LookupContext); |
| 967 | |
| 968 | if (Previous.isAmbiguous()) |
| 969 | return true; |
| 970 | |
| 971 | if (Previous.begin() != Previous.end()) |
| 972 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 973 | } |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 974 | } |
Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 975 | } else if (PrevDecl && |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 976 | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, |
| 977 | S, SS.isValid())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 978 | PrevDecl = PrevClassTemplate = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 979 | |
Richard Smith | fc805ca | 2015-07-06 04:43:58 +0000 | [diff] [blame] | 980 | if (auto *Shadow = dyn_cast_or_null<UsingShadowDecl>( |
| 981 | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { |
| 982 | if (SS.isEmpty() && |
| 983 | !(PrevClassTemplate && |
| 984 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( |
| 985 | SemanticContext->getRedeclContext()))) { |
| 986 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
| 987 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 988 | diag::note_using_decl_target); |
| 989 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
| 990 | // Recover by ignoring the old declaration. |
| 991 | PrevDecl = PrevClassTemplate = nullptr; |
| 992 | } |
| 993 | } |
| 994 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 995 | if (PrevClassTemplate) { |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 996 | // Ensure that the template parameter lists are compatible. Skip this check |
| 997 | // for a friend in a dependent context: the template parameter list itself |
| 998 | // could be dependent. |
| 999 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 1000 | !TemplateParameterListsAreEqual(TemplateParams, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1001 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 1002 | /*Complain=*/true, |
| 1003 | TPL_TemplateMatch)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1004 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1005 | |
| 1006 | // C++ [temp.class]p4: |
| 1007 | // In a redeclaration, partial specialization, explicit |
| 1008 | // specialization or explicit instantiation of a class template, |
| 1009 | // the class-key shall agree in kind with the original class |
| 1010 | // template declaration (7.1.5.3). |
| 1011 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 1012 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 1013 | TUK == TUK_Definition, KWLoc, Name)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1015 | << Name |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1016 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1017 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1018 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1021 | // Check for redefinition of this class template. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1022 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1023 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1024 | // If we have a prior definition that is not visible, treat this as |
| 1025 | // simply making that previous definition visible. |
| 1026 | NamedDecl *Hidden = nullptr; |
| 1027 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1028 | SkipBody->ShouldSkip = true; |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1029 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
| 1030 | assert(Tmpl && "original definition of a class template is not a " |
| 1031 | "class template?"); |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1032 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 1033 | makeMergedDefinitionVisible(Tmpl, KWLoc); |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1034 | return Def; |
| 1035 | } |
| 1036 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1037 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1038 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1039 | // FIXME: Would it make sense to try to "forget" the previous |
| 1040 | // definition, as part of error recovery? |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1041 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1042 | } |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1043 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1044 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 1045 | // Maybe we will complain about the shadowed template parameter. |
| 1046 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 1047 | // Just pretend that we didn't see the previous declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1048 | PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1049 | } else if (PrevDecl) { |
| 1050 | // C++ [temp]p5: |
| 1051 | // A class template shall not have the same name as any other |
| 1052 | // template, class, function, object, enumeration, enumerator, |
| 1053 | // namespace, or type in the same scope (3.3), except as specified |
| 1054 | // in (14.5.4). |
| 1055 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1056 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1057 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1060 | // Check the template parameter list of this declaration, possibly |
| 1061 | // merging in the template parameter list from the previous class |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1062 | // template declaration. Skip this check for a friend in a dependent |
| 1063 | // context, because the template parameter list might be dependent. |
| 1064 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1065 | CheckTemplateParameterList( |
| 1066 | TemplateParams, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1067 | PrevClassTemplate ? PrevClassTemplate->getTemplateParameters() |
| 1068 | : nullptr, |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1069 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1070 | SemanticContext->isDependentContext()) |
| 1071 | ? TPC_ClassTemplateMember |
| 1072 | : TUK == TUK_Friend ? TPC_FriendClassTemplate |
| 1073 | : TPC_ClassTemplate)) |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1074 | Invalid = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1075 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1076 | if (SS.isSet()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1077 | // 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] | 1078 | // template out-of-line. |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1079 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1080 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1081 | : diag::err_member_decl_does_not_match) |
| 1082 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1083 | Invalid = true; |
| 1084 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1087 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1088 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | PrevClassTemplate? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1090 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1091 | /*DelayTypeCreation=*/true); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1092 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1093 | if (NumOuterTemplateParamLists > 0) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1094 | NewClass->setTemplateParameterListsInfo( |
| 1095 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
| 1096 | NumOuterTemplateParamLists)); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1097 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1098 | // Add alignment attributes if necessary; these attributes are checked when |
| 1099 | // the ASTContext lays out the structure. |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1100 | if (TUK == TUK_Definition) { |
| 1101 | AddAlignmentAttributesForRecord(NewClass); |
| 1102 | AddMsStructLayoutForRecord(NewClass); |
| 1103 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1104 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1105 | ClassTemplateDecl *NewTemplate |
| 1106 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1107 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1108 | NewClass, PrevClassTemplate); |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1109 | NewClass->setDescribedClassTemplate(NewTemplate); |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1110 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1111 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1112 | NewTemplate->setModulePrivate(); |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1113 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1114 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1115 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1116 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1117 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1118 | (void)T; |
| 1119 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1120 | // 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] | 1121 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1122 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1123 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1124 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1125 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1126 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1127 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1128 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1130 | // Set the lexical context of these templates |
| 1131 | NewClass->setLexicalDeclContext(CurContext); |
| 1132 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1133 | |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1134 | if (TUK == TUK_Definition) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1135 | NewClass->startDefinition(); |
| 1136 | |
| 1137 | if (Attr) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1138 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1139 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1140 | if (PrevClassTemplate) |
| 1141 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1142 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1143 | AddPushedVisibilityAttribute(NewClass); |
| 1144 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1145 | if (TUK != TUK_Friend) { |
| 1146 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1147 | Scope *Outer = S; |
| 1148 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1149 | Outer = Outer->getParent(); |
| 1150 | PushOnScopeChains(NewTemplate, Outer); |
| 1151 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1152 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1153 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1154 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1155 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1156 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1157 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1158 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1159 | // Friend templates are visible in fairly strange ways. |
| 1160 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1161 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1162 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1163 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1164 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1165 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1166 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1167 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1168 | FriendDecl *Friend = FriendDecl::Create( |
| 1169 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1170 | Friend->setAccess(AS_public); |
| 1171 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1172 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1173 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1174 | if (Invalid) { |
| 1175 | NewTemplate->setInvalidDecl(); |
| 1176 | NewClass->setInvalidDecl(); |
| 1177 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1178 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1179 | ActOnDocumentableDecl(NewTemplate); |
| 1180 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1181 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1184 | /// \brief Diagnose the presence of a default template argument on a |
| 1185 | /// template parameter, which is ill-formed in certain contexts. |
| 1186 | /// |
| 1187 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1188 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1189 | Sema::TemplateParamListContext TPC, |
| 1190 | SourceLocation ParamLoc, |
| 1191 | SourceRange DefArgRange) { |
| 1192 | switch (TPC) { |
| 1193 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1194 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1195 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1196 | return false; |
| 1197 | |
| 1198 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1199 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1200 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1201 | // A default template-argument shall not be specified in a |
| 1202 | // function template declaration or a function template |
| 1203 | // definition [...] |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1204 | // If a friend function template declaration specifies a default |
| 1205 | // template-argument, that declaration shall be a definition and shall be |
| 1206 | // the only declaration of the function template in the translation unit. |
| 1207 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1208 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1209 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1210 | : diag::ext_template_parameter_default_in_function_template) |
| 1211 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1212 | return false; |
| 1213 | |
| 1214 | case Sema::TPC_ClassTemplateMember: |
| 1215 | // C++0x [temp.param]p9: |
| 1216 | // A default template-argument shall not be specified in the |
| 1217 | // template-parameter-lists of the definition of a member of a |
| 1218 | // class template that appears outside of the member's class. |
| 1219 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1220 | << DefArgRange; |
| 1221 | return true; |
| 1222 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1223 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1224 | case Sema::TPC_FriendFunctionTemplate: |
| 1225 | // C++ [temp.param]p9: |
| 1226 | // A default template-argument shall not be specified in a |
| 1227 | // friend template declaration. |
| 1228 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1229 | << DefArgRange; |
| 1230 | return true; |
| 1231 | |
| 1232 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1233 | // for friend function templates if there is only a single |
| 1234 | // declaration (and it is a definition). Strange! |
| 1235 | } |
| 1236 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1237 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1240 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1241 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1242 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1243 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1244 | // A template template parameter which is a parameter pack is also a pack |
| 1245 | // expansion. |
| 1246 | if (TTP->isParameterPack()) |
| 1247 | return false; |
| 1248 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1249 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1250 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1251 | NamedDecl *P = Params->getParam(I); |
| 1252 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1253 | if (!NTTP->isParameterPack() && |
| 1254 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1255 | NTTP->getTypeSourceInfo(), |
| 1256 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1257 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1258 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1259 | continue; |
| 1260 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1261 | |
| 1262 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1263 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1264 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1265 | return true; |
| 1266 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1267 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1268 | return false; |
| 1269 | } |
| 1270 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1271 | /// \brief Checks the validity of a template parameter list, possibly |
| 1272 | /// considering the template parameter list from a previous |
| 1273 | /// declaration. |
| 1274 | /// |
| 1275 | /// If an "old" template parameter list is provided, it must be |
| 1276 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1277 | /// template parameter list. |
| 1278 | /// |
| 1279 | /// \param NewParams Template parameter list for a new template |
| 1280 | /// declaration. This template parameter list will be updated with any |
| 1281 | /// default arguments that are carried through from the previous |
| 1282 | /// template parameter list. |
| 1283 | /// |
| 1284 | /// \param OldParams If provided, template parameter list from a |
| 1285 | /// previous declaration of the same template. Default template |
| 1286 | /// arguments will be merged from the old template parameter list to |
| 1287 | /// the new template parameter list. |
| 1288 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1289 | /// \param TPC Describes the context in which we are checking the given |
| 1290 | /// template parameter list. |
| 1291 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1292 | /// \returns true if an error occurred, false otherwise. |
| 1293 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1294 | TemplateParameterList *OldParams, |
| 1295 | TemplateParamListContext TPC) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1296 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1298 | // C++ [temp.param]p10: |
| 1299 | // The set of default template-arguments available for use with a |
| 1300 | // template declaration or definition is obtained by merging the |
| 1301 | // default arguments from the definition (if in scope) and all |
| 1302 | // declarations in scope in the same way default function |
| 1303 | // arguments are (8.3.6). |
| 1304 | bool SawDefaultArgument = false; |
| 1305 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1306 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1307 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1308 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1309 | if (OldParams) |
| 1310 | OldParam = OldParams->begin(); |
| 1311 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1312 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1313 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1314 | NewParamEnd = NewParams->end(); |
| 1315 | NewParam != NewParamEnd; ++NewParam) { |
| 1316 | // Variables used to diagnose redundant default arguments |
| 1317 | bool RedundantDefaultArg = false; |
| 1318 | SourceLocation OldDefaultLoc; |
| 1319 | SourceLocation NewDefaultLoc; |
| 1320 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1321 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1322 | bool MissingDefaultArg = false; |
| 1323 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1324 | // Variable used to diagnose non-final parameter packs |
| 1325 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1326 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1327 | if (TemplateTypeParmDecl *NewTypeParm |
| 1328 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1329 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1330 | if (NewTypeParm->hasDefaultArgument() && |
| 1331 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1332 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1333 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1334 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1335 | NewTypeParm->removeDefaultArgument(); |
| 1336 | |
| 1337 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1338 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1339 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1340 | if (NewTypeParm->isParameterPack()) { |
| 1341 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1342 | "Parameter packs can't have a default argument!"); |
| 1343 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1344 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1345 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1346 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1347 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1348 | SawDefaultArgument = true; |
| 1349 | RedundantDefaultArg = true; |
| 1350 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1351 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1352 | // Merge the default argument from the old declaration to the |
| 1353 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1354 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1355 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1356 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1357 | SawDefaultArgument = true; |
| 1358 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1359 | } else if (SawDefaultArgument) |
| 1360 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1361 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1362 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1363 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1364 | if (!NewNonTypeParm->isParameterPack() && |
| 1365 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1366 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1367 | UPPC_NonTypeTemplateParameterType)) { |
| 1368 | Invalid = true; |
| 1369 | continue; |
| 1370 | } |
| 1371 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1372 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1373 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1374 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1375 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1376 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1377 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1380 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1381 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1382 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1383 | if (NewNonTypeParm->isParameterPack()) { |
| 1384 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1385 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1386 | if (!NewNonTypeParm->isPackExpansion()) |
| 1387 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1388 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 1389 | NewNonTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1390 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1391 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1392 | SawDefaultArgument = true; |
| 1393 | RedundantDefaultArg = true; |
| 1394 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1395 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1396 | // Merge the default argument from the old declaration to the |
| 1397 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1398 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1399 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1400 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1401 | SawDefaultArgument = true; |
| 1402 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1403 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1405 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1406 | TemplateTemplateParmDecl *NewTemplateParm |
| 1407 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1408 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1409 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1410 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1411 | Invalid = true; |
| 1412 | continue; |
| 1413 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1414 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1415 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1416 | if (NewTemplateParm->hasDefaultArgument() && |
| 1417 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1418 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1419 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1420 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1421 | |
| 1422 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1423 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1424 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1425 | if (NewTemplateParm->isParameterPack()) { |
| 1426 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1427 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1428 | if (!NewTemplateParm->isPackExpansion()) |
| 1429 | SawParameterPack = true; |
Richard Smith | e7bd6de | 2015-06-10 20:30:23 +0000 | [diff] [blame] | 1430 | } else if (OldTemplateParm && |
| 1431 | hasVisibleDefaultArgument(OldTemplateParm) && |
| 1432 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1433 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1434 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1435 | SawDefaultArgument = true; |
| 1436 | RedundantDefaultArg = true; |
| 1437 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1438 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1439 | // Merge the default argument from the old declaration to the |
| 1440 | // new declaration. |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 1441 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1442 | PreviousDefaultArgLoc |
| 1443 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1444 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1445 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1446 | PreviousDefaultArgLoc |
| 1447 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1448 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1449 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1452 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1453 | // If a template parameter of a primary class template or alias template |
| 1454 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1455 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1456 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 1457 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1458 | Diag((*NewParam)->getLocation(), |
| 1459 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1460 | Invalid = true; |
| 1461 | } |
| 1462 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1463 | if (RedundantDefaultArg) { |
| 1464 | // C++ [temp.param]p12: |
| 1465 | // A template-parameter shall not be given default arguments |
| 1466 | // by two different declarations in the same scope. |
| 1467 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1468 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1469 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1470 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1471 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1472 | // If a template-parameter of a class template has a default |
| 1473 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1474 | // have a default template-argument supplied or be a template parameter |
| 1475 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1477 | diag::err_template_param_default_arg_missing); |
| 1478 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1479 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1480 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | // If we have an old template parameter list that we're merging |
| 1484 | // in, move on to the next parameter. |
| 1485 | if (OldParams) |
| 1486 | ++OldParam; |
| 1487 | } |
| 1488 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1489 | // We were missing some default arguments at the end of the list, so remove |
| 1490 | // all of the default arguments. |
| 1491 | if (RemoveDefaultArguments) { |
| 1492 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1493 | NewParamEnd = NewParams->end(); |
| 1494 | NewParam != NewParamEnd; ++NewParam) { |
| 1495 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1496 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1497 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1498 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1499 | NTTP->removeDefaultArgument(); |
| 1500 | else |
| 1501 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1502 | } |
| 1503 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1504 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1505 | return Invalid; |
| 1506 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1507 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1508 | namespace { |
| 1509 | |
| 1510 | /// A class which looks for a use of a certain level of template |
| 1511 | /// parameter. |
| 1512 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1513 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1514 | |
| 1515 | unsigned Depth; |
| 1516 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1517 | SourceLocation MatchLoc; |
| 1518 | |
| 1519 | DependencyChecker(unsigned Depth) : Depth(Depth), Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1520 | |
| 1521 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1522 | NamedDecl *ND = Params->getParam(0); |
| 1523 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1524 | Depth = PD->getDepth(); |
| 1525 | } else if (NonTypeTemplateParmDecl *PD = |
| 1526 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1527 | Depth = PD->getDepth(); |
| 1528 | } else { |
| 1529 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1530 | } |
| 1531 | } |
| 1532 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1533 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1534 | if (ParmDepth >= Depth) { |
| 1535 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1536 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1537 | return true; |
| 1538 | } |
| 1539 | return false; |
| 1540 | } |
| 1541 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1542 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1543 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 1544 | } |
| 1545 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1546 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1547 | return !Matches(T->getDepth()); |
| 1548 | } |
| 1549 | |
| 1550 | bool TraverseTemplateName(TemplateName N) { |
| 1551 | if (TemplateTemplateParmDecl *PD = |
| 1552 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1553 | if (Matches(PD->getDepth())) |
| 1554 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1555 | return super::TraverseTemplateName(N); |
| 1556 | } |
| 1557 | |
| 1558 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1559 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1560 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 1561 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1562 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1563 | return super::VisitDeclRefExpr(E); |
| 1564 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1565 | |
| 1566 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 1567 | return TraverseType(T->getReplacementType()); |
| 1568 | } |
| 1569 | |
| 1570 | bool |
| 1571 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 1572 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 1573 | } |
| 1574 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1575 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1576 | return TraverseType(T->getInjectedSpecializationType()); |
| 1577 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1578 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1579 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1580 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1581 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1582 | /// list. |
| 1583 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1584 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1585 | DependencyChecker Checker(Params); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1586 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1587 | return Checker.Match; |
| 1588 | } |
| 1589 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1590 | // Find the source range corresponding to the named type in the given |
| 1591 | // nested-name-specifier, if any. |
| 1592 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1593 | QualType T, |
| 1594 | const CXXScopeSpec &SS) { |
| 1595 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1596 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1597 | if (const Type *CurType = NNS->getAsType()) { |
| 1598 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1599 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1600 | } else |
| 1601 | break; |
| 1602 | |
| 1603 | NNSLoc = NNSLoc.getPrefix(); |
| 1604 | } |
| 1605 | |
| 1606 | return SourceRange(); |
| 1607 | } |
| 1608 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1609 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1610 | /// specifier, returning the template parameter list that applies to the |
| 1611 | /// name. |
| 1612 | /// |
| 1613 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1614 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1615 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1616 | /// \param DeclLoc The location of the declaration itself. |
| 1617 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1618 | /// \param SS the scope specifier that will be matched to the given template |
| 1619 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1620 | /// being declared. |
| 1621 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1622 | /// \param TemplateId The template-id following the scope specifier, if there |
| 1623 | /// is one. Used to check for a missing 'template<>'. |
| 1624 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1625 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1626 | /// innermost template parameter lists. |
| 1627 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1628 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1629 | /// matching template parameters to scope specifiers in friend |
| 1630 | /// declarations. |
| 1631 | /// |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1632 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1633 | /// declared is an explicit specialization, false otherwise. |
| 1634 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1635 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1636 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1637 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1638 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1639 | /// 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] | 1640 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1641 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 1642 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1643 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1644 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
| 1645 | bool &IsExplicitSpecialization, bool &Invalid) { |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1646 | IsExplicitSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1647 | Invalid = false; |
| 1648 | |
| 1649 | // The sequence of nested types to which we will match up the template |
| 1650 | // parameter lists. We first build this list by starting with the type named |
| 1651 | // 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] | 1652 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1653 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1654 | if (SS.getScopeRep()) { |
| 1655 | if (CXXRecordDecl *Record |
| 1656 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1657 | T = Context.getTypeDeclType(Record); |
| 1658 | else |
| 1659 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1660 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1661 | |
| 1662 | // If we found an explicit specialization that prevents us from needing |
| 1663 | // 'template<>' headers, this will be set to the location of that |
| 1664 | // explicit specialization. |
| 1665 | SourceLocation ExplicitSpecLoc; |
| 1666 | |
| 1667 | while (!T.isNull()) { |
| 1668 | NestedTypes.push_back(T); |
| 1669 | |
| 1670 | // Retrieve the parent of a record type. |
| 1671 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1672 | // If this type is an explicit specialization, we're done. |
| 1673 | if (ClassTemplateSpecializationDecl *Spec |
| 1674 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1675 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1676 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1677 | ExplicitSpecLoc = Spec->getLocation(); |
| 1678 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1679 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1680 | } else if (Record->getTemplateSpecializationKind() |
| 1681 | == TSK_ExplicitSpecialization) { |
| 1682 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1683 | break; |
| 1684 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1685 | |
| 1686 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1687 | T = Context.getTypeDeclType(Parent); |
| 1688 | else |
| 1689 | T = QualType(); |
| 1690 | continue; |
| 1691 | } |
| 1692 | |
| 1693 | if (const TemplateSpecializationType *TST |
| 1694 | = T->getAs<TemplateSpecializationType>()) { |
| 1695 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1696 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1697 | T = Context.getTypeDeclType(Parent); |
| 1698 | else |
| 1699 | T = QualType(); |
| 1700 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1701 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | // Look one step prior in a dependent template specialization type. |
| 1705 | if (const DependentTemplateSpecializationType *DependentTST |
| 1706 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1707 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1708 | T = QualType(NNS->getAsType(), 0); |
| 1709 | else |
| 1710 | T = QualType(); |
| 1711 | continue; |
| 1712 | } |
| 1713 | |
| 1714 | // Look one step prior in a dependent name type. |
| 1715 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1716 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1717 | T = QualType(NNS->getAsType(), 0); |
| 1718 | else |
| 1719 | T = QualType(); |
| 1720 | continue; |
| 1721 | } |
| 1722 | |
| 1723 | // Retrieve the parent of an enumeration type. |
| 1724 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1725 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1726 | // check here. |
| 1727 | EnumDecl *Enum = EnumT->getDecl(); |
| 1728 | |
| 1729 | // Get to the parent type. |
| 1730 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1731 | T = Context.getTypeDeclType(Parent); |
| 1732 | else |
| 1733 | T = QualType(); |
| 1734 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1735 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1736 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1737 | T = QualType(); |
| 1738 | } |
| 1739 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1740 | // to the innermost while checking template-parameter-lists. |
| 1741 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1742 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1743 | // C++0x [temp.expl.spec]p17: |
| 1744 | // A member or a member template may be nested within many |
| 1745 | // enclosing class templates. In an explicit specialization for |
| 1746 | // such a member, the member declaration shall be preceded by a |
| 1747 | // template<> for each enclosing class template that is |
| 1748 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1749 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1750 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1751 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1752 | if (SawNonEmptyTemplateParameterList) { |
| 1753 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1754 | << !Recovery << Range; |
| 1755 | Invalid = true; |
| 1756 | IsExplicitSpecialization = false; |
| 1757 | return true; |
| 1758 | } |
| 1759 | |
| 1760 | return false; |
| 1761 | }; |
| 1762 | |
| 1763 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 1764 | // Check that we can have an explicit specialization here. |
| 1765 | if (CheckExplicitSpecialization(Range, true)) |
| 1766 | return true; |
| 1767 | |
| 1768 | // We don't have a template header, but we should. |
| 1769 | SourceLocation ExpectedTemplateLoc; |
| 1770 | if (!ParamLists.empty()) |
| 1771 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1772 | else |
| 1773 | ExpectedTemplateLoc = DeclStartLoc; |
| 1774 | |
| 1775 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1776 | << Range |
| 1777 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1778 | return false; |
| 1779 | }; |
| 1780 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1781 | unsigned ParamIdx = 0; |
| 1782 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1783 | ++TypeIdx) { |
| 1784 | T = NestedTypes[TypeIdx]; |
| 1785 | |
| 1786 | // Whether we expect a 'template<>' header. |
| 1787 | bool NeedEmptyTemplateHeader = false; |
| 1788 | |
| 1789 | // Whether we expect a template header with parameters. |
| 1790 | bool NeedNonemptyTemplateHeader = false; |
| 1791 | |
| 1792 | // For a dependent type, the set of template parameters that we |
| 1793 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1794 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1795 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1796 | // C++0x [temp.expl.spec]p15: |
| 1797 | // A member or a member template may be nested within many enclosing |
| 1798 | // class templates. In an explicit specialization for such a member, the |
| 1799 | // member declaration shall be preceded by a template<> for each |
| 1800 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1801 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1802 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1803 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1804 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1805 | NeedNonemptyTemplateHeader = true; |
| 1806 | } else if (Record->isDependentType()) { |
| 1807 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1808 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1809 | ->getTemplateParameters(); |
| 1810 | NeedNonemptyTemplateHeader = true; |
| 1811 | } |
| 1812 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1813 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1814 | // C++0x [temp.expl.spec]p4: |
| 1815 | // Members of an explicitly specialized class template are defined |
| 1816 | // in the same manner as members of normal classes, and not using |
| 1817 | // the template<> syntax. |
| 1818 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1819 | NeedEmptyTemplateHeader = true; |
| 1820 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1821 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1822 | } else if (Record->getTemplateSpecializationKind()) { |
| 1823 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1824 | != TSK_ExplicitSpecialization && |
| 1825 | TypeIdx == NumTypes - 1) |
| 1826 | IsExplicitSpecialization = true; |
| 1827 | |
| 1828 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1829 | } |
| 1830 | } else if (const TemplateSpecializationType *TST |
| 1831 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 1832 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1833 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1834 | NeedNonemptyTemplateHeader = true; |
| 1835 | } |
| 1836 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1837 | // FIXME: We actually could/should check the template arguments here |
| 1838 | // against the corresponding template parameter list. |
| 1839 | NeedNonemptyTemplateHeader = false; |
| 1840 | } |
| 1841 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1842 | // C++ [temp.expl.spec]p16: |
| 1843 | // In an explicit specialization declaration for a member of a class |
| 1844 | // template or a member template that ap- pears in namespace scope, the |
| 1845 | // member template and some of its enclosing class templates may remain |
| 1846 | // unspecialized, except that the declaration shall not explicitly |
| 1847 | // specialize a class member template if its en- closing class templates |
| 1848 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1849 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1850 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1851 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 1852 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1853 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1854 | } else |
| 1855 | SawNonEmptyTemplateParameterList = true; |
| 1856 | } |
| 1857 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1858 | if (NeedEmptyTemplateHeader) { |
| 1859 | // If we're on the last of the types, and we need a 'template<>' header |
| 1860 | // here, then it's an explicit specialization. |
| 1861 | if (TypeIdx == NumTypes - 1) |
| 1862 | IsExplicitSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1863 | |
| 1864 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1865 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1866 | // The header has template parameters when it shouldn't. Complain. |
| 1867 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1868 | diag::err_template_param_list_matches_nontemplate) |
| 1869 | << T |
| 1870 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1871 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1872 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1873 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1874 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1875 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1876 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1877 | // Consume this template header. |
| 1878 | ++ParamIdx; |
| 1879 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1880 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1881 | |
| 1882 | if (!IsFriend) |
| 1883 | if (DiagnoseMissingExplicitSpecialization( |
| 1884 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1885 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1886 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1887 | continue; |
| 1888 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1889 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1890 | if (NeedNonemptyTemplateHeader) { |
| 1891 | // In friend declarations we can have template-ids which don't |
| 1892 | // depend on the corresponding template parameter lists. But |
| 1893 | // assume that empty parameter lists are supposed to match this |
| 1894 | // template-id. |
| 1895 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1896 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1897 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1898 | ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1899 | else |
| 1900 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1901 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1902 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1903 | if (ParamIdx < ParamLists.size()) { |
| 1904 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1905 | if (ExpectedTemplateParams && |
| 1906 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1907 | ExpectedTemplateParams, |
| 1908 | true, TPL_TemplateMatch)) |
| 1909 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1910 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1911 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1912 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1913 | TPC_ClassTemplateMember)) |
| 1914 | Invalid = true; |
| 1915 | |
| 1916 | ++ParamIdx; |
| 1917 | continue; |
| 1918 | } |
| 1919 | |
| 1920 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1921 | << T |
| 1922 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1923 | Invalid = true; |
| 1924 | continue; |
| 1925 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1926 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1927 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1928 | // If there were at least as many template-ids as there were template |
| 1929 | // parameter lists, then there are no template parameter lists remaining for |
| 1930 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1931 | if (ParamIdx >= ParamLists.size()) { |
| 1932 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1933 | // We don't have a template header for the declaration itself, but we |
| 1934 | // should. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1935 | IsExplicitSpecialization = true; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1936 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 1937 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1938 | |
| 1939 | // Fabricate an empty template parameter list for the invented header. |
| 1940 | return TemplateParameterList::Create(Context, SourceLocation(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1941 | SourceLocation(), nullptr, 0, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1942 | SourceLocation()); |
| 1943 | } |
| 1944 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1945 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1946 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1947 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1948 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1949 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1950 | bool HasAnyExplicitSpecHeader = false; |
| 1951 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1952 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1953 | if (ParamLists[I]->size() == 0) |
| 1954 | HasAnyExplicitSpecHeader = true; |
| 1955 | else |
| 1956 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1957 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1958 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1959 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1960 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 1961 | : diag::err_template_spec_extra_headers) |
| 1962 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1963 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1964 | |
| 1965 | // If there was a specialization somewhere, such that 'template<>' is |
| 1966 | // not required, and there were any 'template<>' headers, note where the |
| 1967 | // specialization occurred. |
| 1968 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1969 | Diag(ExplicitSpecLoc, |
| 1970 | diag::note_explicit_template_spec_does_not_need_header) |
| 1971 | << NestedTypes.back(); |
| 1972 | |
| 1973 | // We have a template parameter list with no corresponding scope, which |
| 1974 | // means that the resulting template declaration can't be instantiated |
| 1975 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1976 | if (!AllExplicitSpecHeaders) |
| 1977 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1978 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1979 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1980 | // C++ [temp.expl.spec]p16: |
| 1981 | // In an explicit specialization declaration for a member of a class |
| 1982 | // template or a member template that ap- pears in namespace scope, the |
| 1983 | // member template and some of its enclosing class templates may remain |
| 1984 | // unspecialized, except that the declaration shall not explicitly |
| 1985 | // specialize a class member template if its en- closing class templates |
| 1986 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1987 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1988 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 1989 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1990 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1991 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1992 | // Return the last template parameter list, which corresponds to the |
| 1993 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1994 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1997 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1998 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1999 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2000 | << (isa<FunctionTemplateDecl>(Template) |
| 2001 | ? 0 |
| 2002 | : isa<ClassTemplateDecl>(Template) |
| 2003 | ? 1 |
| 2004 | : isa<VarTemplateDecl>(Template) |
| 2005 | ? 2 |
| 2006 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 2007 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2008 | return; |
| 2009 | } |
| 2010 | |
| 2011 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 2012 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 2013 | IEnd = OST->end(); |
| 2014 | I != IEnd; ++I) |
| 2015 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 2016 | << 0 << (*I)->getDeclName(); |
| 2017 | |
| 2018 | return; |
| 2019 | } |
| 2020 | } |
| 2021 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2022 | static QualType |
| 2023 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
| 2024 | const SmallVectorImpl<TemplateArgument> &Converted, |
| 2025 | SourceLocation TemplateLoc, |
| 2026 | TemplateArgumentListInfo &TemplateArgs) { |
| 2027 | ASTContext &Context = SemaRef.getASTContext(); |
| 2028 | switch (BTD->getBuiltinTemplateKind()) { |
| 2029 | case BTK__make_integer_seq: |
| 2030 | // Specializations of __make_integer_seq<S, T, N> are treated like |
| 2031 | // S<T, 0, ..., N-1>. |
| 2032 | |
| 2033 | // C++14 [inteseq.intseq]p1: |
| 2034 | // T shall be an integer type. |
| 2035 | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
| 2036 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
| 2037 | diag::err_integer_sequence_integral_element_type); |
| 2038 | return QualType(); |
| 2039 | } |
| 2040 | |
| 2041 | // C++14 [inteseq.make]p1: |
| 2042 | // If N is negative the program is ill-formed. |
| 2043 | TemplateArgument NumArgsArg = Converted[2]; |
| 2044 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
| 2045 | if (NumArgs < 0) { |
| 2046 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
| 2047 | diag::err_integer_sequence_negative_length); |
| 2048 | return QualType(); |
| 2049 | } |
| 2050 | |
| 2051 | QualType ArgTy = NumArgsArg.getIntegralType(); |
| 2052 | TemplateArgumentListInfo SyntheticTemplateArgs; |
| 2053 | // The type argument gets reused as the first template argument in the |
| 2054 | // synthetic template argument list. |
| 2055 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
| 2056 | // Expand N into 0 ... N-1. |
| 2057 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
| 2058 | I < NumArgs; ++I) { |
| 2059 | TemplateArgument TA(Context, I, ArgTy); |
| 2060 | Expr *E = SemaRef.BuildExpressionFromIntegralTemplateArgument( |
| 2061 | TA, TemplateArgs[2].getLocation()) |
| 2062 | .getAs<Expr>(); |
| 2063 | SyntheticTemplateArgs.addArgument( |
| 2064 | TemplateArgumentLoc(TemplateArgument(E), E)); |
| 2065 | } |
| 2066 | // The first template argument will be reused as the template decl that |
| 2067 | // our synthetic template arguments will be applied to. |
| 2068 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
| 2069 | TemplateLoc, SyntheticTemplateArgs); |
| 2070 | } |
| 2071 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
| 2072 | } |
| 2073 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2074 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 2075 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2076 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 2077 | DependentTemplateName *DTN |
| 2078 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2079 | if (DTN && DTN->isIdentifier()) |
| 2080 | // When building a template-id where the template-name is dependent, |
| 2081 | // assume the template is a type template. Either our assumption is |
| 2082 | // correct, or the code is ill-formed and will be diagnosed when the |
| 2083 | // dependent name is substituted. |
| 2084 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 2085 | DTN->getQualifier(), |
| 2086 | DTN->getIdentifier(), |
| 2087 | TemplateArgs); |
| 2088 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2089 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 2090 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
| 2091 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2092 | // We might have a substituted template template parameter pack. If so, |
| 2093 | // build a template specialization type for it. |
| 2094 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 2095 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2096 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2097 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 2098 | << Name; |
| 2099 | NoteAllFoundTemplates(Name); |
| 2100 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2101 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2102 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2103 | // Check that the template argument list is well-formed for this |
| 2104 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2105 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2106 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2107 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2108 | return QualType(); |
| 2109 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2110 | QualType CanonType; |
| 2111 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2112 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2113 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 2114 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2115 | // Find the canonical type for this type alias template specialization. |
| 2116 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 2117 | if (Pattern->isInvalidDecl()) |
| 2118 | return QualType(); |
| 2119 | |
| 2120 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2121 | Converted.data(), Converted.size()); |
| 2122 | |
| 2123 | // Only substitute for the innermost template argument list. |
| 2124 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 0c4a34b | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 2125 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 2126 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 2127 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 2128 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2129 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2130 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2131 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2132 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2133 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2134 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2135 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 2136 | TemplateArgLists, AliasTemplate->getLocation(), |
| 2137 | AliasTemplate->getDeclName()); |
| 2138 | if (CanonType.isNull()) |
| 2139 | return QualType(); |
| 2140 | } else if (Name.isDependent() || |
| 2141 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2142 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2143 | // This class template specialization is a dependent |
| 2144 | // type. Therefore, its canonical type is another class template |
| 2145 | // specialization type that contains all of the converted |
| 2146 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 2147 | // A<T, T> have identical types when A is declared as: |
| 2148 | // |
| 2149 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2150 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2151 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2152 | Converted.data(), |
| 2153 | Converted.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2154 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2155 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2156 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2157 | // In the future, we need to teach getTemplateSpecializationType to only |
| 2158 | // build the canonical type and return that to us. |
| 2159 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2160 | |
| 2161 | // This might work out to be a current instantiation, in which |
| 2162 | // case the canonical type needs to be the InjectedClassNameType. |
| 2163 | // |
| 2164 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2165 | // changes to CurContext don't change the set of current |
| 2166 | // instantiations. |
| 2167 | if (isa<ClassTemplateDecl>(Template)) { |
| 2168 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2169 | // If we get out to a namespace, we're done. |
| 2170 | if (Ctx->isFileContext()) break; |
| 2171 | |
| 2172 | // If this isn't a record, keep looking. |
| 2173 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2174 | if (!Record) continue; |
| 2175 | |
| 2176 | // Look for one of the two cases with InjectedClassNameTypes |
| 2177 | // and check whether it's the same template. |
| 2178 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2179 | !Record->getDescribedClassTemplate()) |
| 2180 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2181 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2182 | // Fetch the injected class name type and check whether its |
| 2183 | // injected type is equal to the type we just built. |
| 2184 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2185 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2186 | ->getInjectedSpecializationType(); |
| 2187 | |
| 2188 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2189 | continue; |
| 2190 | |
| 2191 | // If so, the canonical type of this TST is the injected |
| 2192 | // class name type of the record we just found. |
| 2193 | assert(ICNT.isCanonical()); |
| 2194 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2195 | break; |
| 2196 | } |
| 2197 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2198 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2199 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2200 | // Find the class template specialization declaration that |
| 2201 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2202 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2203 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2204 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2205 | if (!Decl) { |
| 2206 | // This is the first time we have referenced this class template |
| 2207 | // specialization. Create the canonical declaration and add it to |
| 2208 | // the set of specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2209 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2210 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2211 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2212 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2213 | ClassTemplate->getLocation(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2214 | ClassTemplate, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2215 | Converted.data(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2216 | Converted.size(), nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2217 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 2218 | if (ClassTemplate->isOutOfLine()) |
| 2219 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 2222 | // Diagnose uses of this specialization. |
| 2223 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 2224 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2225 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2226 | assert(isa<RecordType>(CanonType) && |
| 2227 | "type of non-dependent specialization is not a RecordType"); |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 2228 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { |
| 2229 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
| 2230 | TemplateArgs); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2231 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2232 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2233 | // Build the fully-sugared type for this class template |
| 2234 | // specialization, which refers back to the class template |
| 2235 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2236 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2239 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2240 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2241 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2242 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2243 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2244 | SourceLocation RAngleLoc, |
| 2245 | bool IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2246 | if (SS.isInvalid()) |
| 2247 | return true; |
| 2248 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2249 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2250 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2251 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2252 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2253 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2254 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2255 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2256 | QualType T |
| 2257 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2258 | DTN->getQualifier(), |
| 2259 | DTN->getIdentifier(), |
| 2260 | TemplateArgs); |
| 2261 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2262 | TypeLocBuilder TLB; |
| 2263 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2264 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2265 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2266 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2267 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2268 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2269 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2270 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2271 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2272 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2273 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2274 | } |
| 2275 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2276 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2277 | |
| 2278 | if (Result.isNull()) |
| 2279 | return true; |
| 2280 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2281 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2282 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2283 | TemplateSpecializationTypeLoc SpecTL |
| 2284 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2285 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2286 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2287 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2288 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2289 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2290 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2291 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2292 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2293 | // constructor or destructor name (in such a case, the scope specifier |
| 2294 | // will be attached to the enclosing Decl or Expr node). |
| 2295 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2296 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2297 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2298 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2299 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2300 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2301 | } |
| 2302 | |
| 2303 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2304 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2305 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2306 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2307 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2308 | SourceLocation TagLoc, |
| 2309 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2310 | SourceLocation TemplateKWLoc, |
| 2311 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2312 | SourceLocation TemplateLoc, |
| 2313 | SourceLocation LAngleLoc, |
| 2314 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2315 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2316 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2317 | |
| 2318 | // Translate the parser's template argument list in our AST format. |
| 2319 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2320 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2321 | |
| 2322 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2323 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2324 | ElaboratedTypeKeyword Keyword |
| 2325 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2326 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2327 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2328 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2329 | DTN->getQualifier(), |
| 2330 | DTN->getIdentifier(), |
| 2331 | TemplateArgs); |
| 2332 | |
| 2333 | // Build type-source information. |
| 2334 | TypeLocBuilder TLB; |
| 2335 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2336 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2337 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2338 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2339 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2340 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2341 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2342 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2343 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2344 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2345 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2346 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2347 | |
| 2348 | if (TypeAliasTemplateDecl *TAT = |
| 2349 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2350 | // C++0x [dcl.type.elab]p2: |
| 2351 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2352 | // resolves to an alias template specialization, the |
| 2353 | // elaborated-type-specifier is ill-formed. |
| 2354 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2355 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2356 | } |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2357 | |
| 2358 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2359 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2360 | return TypeResult(true); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2361 | |
| 2362 | // Check the tag kind |
| 2363 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2364 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2365 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2366 | IdentifierInfo *Id = D->getIdentifier(); |
| 2367 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2368 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2369 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 2370 | TagLoc, Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2371 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2372 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2373 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2374 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2375 | } |
| 2376 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2377 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2378 | // Provide source-location information for the template specialization. |
| 2379 | TypeLocBuilder TLB; |
| 2380 | TemplateSpecializationTypeLoc SpecTL |
| 2381 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2382 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2383 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2384 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2385 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2386 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2387 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2388 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2389 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2390 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2391 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2392 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2393 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2394 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2395 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2398 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2399 | Sema &S, SourceLocation NameLoc, TemplateParameterList *TemplateParams, |
| 2400 | unsigned ExplicitArgs, SmallVectorImpl<TemplateArgument> &TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2401 | |
| 2402 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 2403 | NamedDecl *PrevDecl, |
| 2404 | SourceLocation Loc, |
| 2405 | bool IsPartialSpecialization); |
| 2406 | |
| 2407 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2408 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2409 | static bool isTemplateArgumentTemplateParameter( |
| 2410 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 2411 | switch (Arg.getKind()) { |
| 2412 | case TemplateArgument::Null: |
| 2413 | case TemplateArgument::NullPtr: |
| 2414 | case TemplateArgument::Integral: |
| 2415 | case TemplateArgument::Declaration: |
| 2416 | case TemplateArgument::Pack: |
| 2417 | case TemplateArgument::TemplateExpansion: |
| 2418 | return false; |
| 2419 | |
| 2420 | case TemplateArgument::Type: { |
| 2421 | QualType Type = Arg.getAsType(); |
| 2422 | const TemplateTypeParmType *TPT = |
| 2423 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 2424 | return TPT && !Type.hasQualifiers() && |
| 2425 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 2426 | } |
| 2427 | |
| 2428 | case TemplateArgument::Expression: { |
| 2429 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 2430 | if (!DRE || !DRE->getDecl()) |
| 2431 | return false; |
| 2432 | const NonTypeTemplateParmDecl *NTTP = |
| 2433 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 2434 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 2435 | } |
| 2436 | |
| 2437 | case TemplateArgument::Template: |
| 2438 | const TemplateTemplateParmDecl *TTP = |
| 2439 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 2440 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 2441 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 2442 | } |
| 2443 | llvm_unreachable("unexpected kind of template argument"); |
| 2444 | } |
| 2445 | |
| 2446 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 2447 | ArrayRef<TemplateArgument> Args) { |
| 2448 | if (Params->size() != Args.size()) |
| 2449 | return false; |
| 2450 | |
| 2451 | unsigned Depth = Params->getDepth(); |
| 2452 | |
| 2453 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 2454 | TemplateArgument Arg = Args[I]; |
| 2455 | |
| 2456 | // If the parameter is a pack expansion, the argument must be a pack |
| 2457 | // whose only element is a pack expansion. |
| 2458 | if (Params->getParam(I)->isParameterPack()) { |
| 2459 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 2460 | !Arg.pack_begin()->isPackExpansion()) |
| 2461 | return false; |
| 2462 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 2463 | } |
| 2464 | |
| 2465 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 2466 | return false; |
| 2467 | } |
| 2468 | |
| 2469 | return true; |
| 2470 | } |
| 2471 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2472 | /// Convert the parser's template argument list representation into our form. |
| 2473 | static TemplateArgumentListInfo |
| 2474 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 2475 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 2476 | TemplateId.RAngleLoc); |
| 2477 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 2478 | TemplateId.NumArgs); |
| 2479 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 2480 | return TemplateArgs; |
| 2481 | } |
| 2482 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2483 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2484 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 2485 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2486 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2487 | // D must be variable template id. |
| 2488 | assert(D.getName().getKind() == UnqualifiedId::IK_TemplateId && |
| 2489 | "Variable template specialization is declared with a template it."); |
| 2490 | |
| 2491 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2492 | TemplateArgumentListInfo TemplateArgs = |
| 2493 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2494 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 2495 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 2496 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2497 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2498 | TemplateName Name = TemplateId->Template.get(); |
| 2499 | |
| 2500 | // The template-id must name a variable template. |
| 2501 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2502 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 2503 | if (!VarTemplate) { |
| 2504 | NamedDecl *FnTemplate; |
| 2505 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 2506 | FnTemplate = *OTS->begin(); |
| 2507 | else |
| 2508 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 2509 | if (FnTemplate) |
| 2510 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 2511 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2512 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 2513 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2514 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2515 | |
| 2516 | // Check for unexpanded parameter packs in any of the template arguments. |
| 2517 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 2518 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 2519 | UPPC_PartialSpecialization)) |
| 2520 | return true; |
| 2521 | |
| 2522 | // Check that the template argument list is well-formed for this |
| 2523 | // template. |
| 2524 | SmallVector<TemplateArgument, 4> Converted; |
| 2525 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 2526 | false, Converted)) |
| 2527 | return true; |
| 2528 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2529 | // Find the variable template (partial) specialization declaration that |
| 2530 | // corresponds to these arguments. |
| 2531 | if (IsPartialSpecialization) { |
| 2532 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2533 | *this, TemplateNameLoc, VarTemplate->getTemplateParameters(), |
| 2534 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2535 | return true; |
| 2536 | |
| 2537 | bool InstantiationDependent; |
| 2538 | if (!Name.isDependent() && |
| 2539 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2540 | TemplateArgs.getArgumentArray(), TemplateArgs.size(), |
| 2541 | InstantiationDependent)) { |
| 2542 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 2543 | << VarTemplate->getDeclName(); |
| 2544 | IsPartialSpecialization = false; |
| 2545 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2546 | |
| 2547 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 2548 | Converted)) { |
| 2549 | // C++ [temp.class.spec]p9b3: |
| 2550 | // |
| 2551 | // -- The argument list of the specialization shall not be identical |
| 2552 | // to the implicit argument list of the primary template. |
| 2553 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 2554 | << /*variable template*/ 1 |
| 2555 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 2556 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 2557 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 2558 | // of the primary template. |
| 2559 | return true; |
| 2560 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2563 | void *InsertPos = nullptr; |
| 2564 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2565 | |
| 2566 | if (IsPartialSpecialization) |
| 2567 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2568 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2569 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2570 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2571 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2572 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2573 | |
| 2574 | // Check whether we can declare a variable template specialization in |
| 2575 | // the current scope. |
| 2576 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 2577 | TemplateNameLoc, |
| 2578 | IsPartialSpecialization)) |
| 2579 | return true; |
| 2580 | |
| 2581 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 2582 | // Since the only prior variable template specialization with these |
| 2583 | // arguments was referenced but not declared, reuse that |
| 2584 | // declaration node as our own, updating its source location and |
| 2585 | // the list of outer template parameters to reflect our new declaration. |
| 2586 | Specialization = PrevDecl; |
| 2587 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2588 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2589 | } else if (IsPartialSpecialization) { |
| 2590 | // Create a new class template partial specialization declaration node. |
| 2591 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 2592 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2593 | VarTemplatePartialSpecializationDecl *Partial = |
| 2594 | VarTemplatePartialSpecializationDecl::Create( |
| 2595 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 2596 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 2597 | Converted.data(), Converted.size(), TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2598 | |
| 2599 | if (!PrevPartial) |
| 2600 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 2601 | Specialization = Partial; |
| 2602 | |
| 2603 | // If we are providing an explicit specialization of a member variable |
| 2604 | // template specialization, make a note of that. |
| 2605 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2606 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2607 | |
| 2608 | // Check that all of the template parameters of the variable template |
| 2609 | // partial specialization are deducible from the template |
| 2610 | // arguments. If not, this variable template partial specialization |
| 2611 | // will never be used. |
| 2612 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 2613 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 2614 | TemplateParams->getDepth(), DeducibleParams); |
| 2615 | |
| 2616 | if (!DeducibleParams.all()) { |
| 2617 | unsigned NumNonDeducible = |
| 2618 | DeducibleParams.size() - DeducibleParams.count(); |
| 2619 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2620 | << /*variable template*/ 1 << (NumNonDeducible > 1) |
| 2621 | << SourceRange(TemplateNameLoc, RAngleLoc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2622 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 2623 | if (!DeducibleParams[I]) { |
| 2624 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 2625 | if (Param->getDeclName()) |
| 2626 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
| 2627 | << Param->getDeclName(); |
| 2628 | else |
| 2629 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 2630 | << "(anonymous)"; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2631 | } |
| 2632 | } |
| 2633 | } |
| 2634 | } else { |
| 2635 | // Create a new class template specialization declaration node for |
| 2636 | // this explicit specialization or friend declaration. |
| 2637 | Specialization = VarTemplateSpecializationDecl::Create( |
| 2638 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
| 2639 | VarTemplate, DI->getType(), DI, SC, Converted.data(), Converted.size()); |
| 2640 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 2641 | |
| 2642 | if (!PrevDecl) |
| 2643 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 2644 | } |
| 2645 | |
| 2646 | // C++ [temp.expl.spec]p6: |
| 2647 | // If a template, a member template or the member of a class template is |
| 2648 | // explicitly specialized then that specialization shall be declared |
| 2649 | // before the first use of that specialization that would cause an implicit |
| 2650 | // instantiation to take place, in every translation unit in which such a |
| 2651 | // use occurs; no diagnostic is required. |
| 2652 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 2653 | bool Okay = false; |
| 2654 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 2655 | // Is there any previous explicit specialization declaration? |
| 2656 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 2657 | Okay = true; |
| 2658 | break; |
| 2659 | } |
| 2660 | } |
| 2661 | |
| 2662 | if (!Okay) { |
| 2663 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 2664 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 2665 | << Name << Range; |
| 2666 | |
| 2667 | Diag(PrevDecl->getPointOfInstantiation(), |
| 2668 | diag::note_instantiation_required_here) |
| 2669 | << (PrevDecl->getTemplateSpecializationKind() != |
| 2670 | TSK_ImplicitInstantiation); |
| 2671 | return true; |
| 2672 | } |
| 2673 | } |
| 2674 | |
| 2675 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 2676 | Specialization->setLexicalDeclContext(CurContext); |
| 2677 | |
| 2678 | // Add the specialization into its lexical context, so that it can |
| 2679 | // be seen when iterating through the list of declarations in that |
| 2680 | // context. However, specializations are not found by name lookup. |
| 2681 | CurContext->addDecl(Specialization); |
| 2682 | |
| 2683 | // Note that this is an explicit specialization. |
| 2684 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 2685 | |
| 2686 | if (PrevDecl) { |
| 2687 | // Check that this isn't a redefinition of this specialization, |
| 2688 | // merging with previous declarations. |
| 2689 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
| 2690 | ForRedeclaration); |
| 2691 | PrevSpec.addDecl(PrevDecl); |
| 2692 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2693 | } else if (Specialization->isStaticDataMember() && |
| 2694 | Specialization->isOutOfLine()) { |
| 2695 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
| 2698 | // Link instantiations of static data members back to the template from |
| 2699 | // which they were instantiated. |
| 2700 | if (Specialization->isStaticDataMember()) |
| 2701 | Specialization->setInstantiationOfStaticDataMember( |
| 2702 | VarTemplate->getTemplatedDecl(), |
| 2703 | Specialization->getSpecializationKind()); |
| 2704 | |
| 2705 | return Specialization; |
| 2706 | } |
| 2707 | |
| 2708 | namespace { |
| 2709 | /// \brief A partial specialization whose template arguments have matched |
| 2710 | /// a given template-id. |
| 2711 | struct PartialSpecMatchResult { |
| 2712 | VarTemplatePartialSpecializationDecl *Partial; |
| 2713 | TemplateArgumentList *Args; |
| 2714 | }; |
| 2715 | } |
| 2716 | |
| 2717 | DeclResult |
| 2718 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2719 | SourceLocation TemplateNameLoc, |
| 2720 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2721 | assert(Template && "A variable template id without template?"); |
| 2722 | |
| 2723 | // Check that the template argument list is well-formed for this template. |
| 2724 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2725 | if (CheckTemplateArgumentList( |
| 2726 | Template, TemplateNameLoc, |
| 2727 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2728 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2729 | return true; |
| 2730 | |
| 2731 | // Find the variable template specialization declaration that |
| 2732 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2733 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2734 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2735 | Converted, InsertPos)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2736 | // If we already have a variable template specialization, return it. |
| 2737 | return Spec; |
| 2738 | |
| 2739 | // This is the first time we have referenced this variable template |
| 2740 | // specialization. Create the canonical declaration and add it to |
| 2741 | // the set of specializations, based on the closest partial specialization |
| 2742 | // that it represents. That is, |
| 2743 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 2744 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
| 2745 | Converted.data(), Converted.size()); |
| 2746 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 2747 | bool AmbiguousPartialSpec = false; |
| 2748 | typedef PartialSpecMatchResult MatchResult; |
| 2749 | SmallVector<MatchResult, 4> Matched; |
| 2750 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
| 2751 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation); |
| 2752 | |
| 2753 | // 1. Attempt to find the closest partial specialization that this |
| 2754 | // specializes, if any. |
| 2755 | // If any of the template arguments is dependent, then this is probably |
| 2756 | // a placeholder for an incomplete declarative context; which must be |
| 2757 | // complete by instantiation time. Thus, do not search through the partial |
| 2758 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2759 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 2760 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 2761 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2762 | bool InstantiationDependent = false; |
| 2763 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 2764 | TemplateArgs, InstantiationDependent)) { |
| 2765 | |
| 2766 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 2767 | Template->getPartialSpecializations(PartialSpecs); |
| 2768 | |
| 2769 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 2770 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 2771 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 2772 | |
| 2773 | if (TemplateDeductionResult Result = |
| 2774 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 2775 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2776 | // TODO: Actually use the failed-deduction info? |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2777 | FailedCandidates.addCandidate() |
| 2778 | .set(Partial, MakeDeductionFailureInfo(Context, Result, Info)); |
| 2779 | (void)Result; |
| 2780 | } else { |
| 2781 | Matched.push_back(PartialSpecMatchResult()); |
| 2782 | Matched.back().Partial = Partial; |
| 2783 | Matched.back().Args = Info.take(); |
| 2784 | } |
| 2785 | } |
| 2786 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2787 | if (Matched.size() >= 1) { |
| 2788 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 2789 | if (Matched.size() == 1) { |
| 2790 | // -- If exactly one matching specialization is found, the |
| 2791 | // instantiation is generated from that specialization. |
| 2792 | // We don't need to do anything for this. |
| 2793 | } else { |
| 2794 | // -- If more than one matching specialization is found, the |
| 2795 | // partial order rules (14.5.4.2) are used to determine |
| 2796 | // whether one of the specializations is more specialized |
| 2797 | // than the others. If none of the specializations is more |
| 2798 | // specialized than all of the other matching |
| 2799 | // specializations, then the use of the variable template is |
| 2800 | // ambiguous and the program is ill-formed. |
| 2801 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 2802 | PEnd = Matched.end(); |
| 2803 | P != PEnd; ++P) { |
| 2804 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 2805 | PointOfInstantiation) == |
| 2806 | P->Partial) |
| 2807 | Best = P; |
| 2808 | } |
| 2809 | |
| 2810 | // Determine if the best partial specialization is more specialized than |
| 2811 | // the others. |
| 2812 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2813 | PEnd = Matched.end(); |
| 2814 | P != PEnd; ++P) { |
| 2815 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 2816 | P->Partial, Best->Partial, |
| 2817 | PointOfInstantiation) != Best->Partial) { |
| 2818 | AmbiguousPartialSpec = true; |
| 2819 | break; |
| 2820 | } |
| 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | // Instantiate using the best variable template partial specialization. |
| 2825 | InstantiationPattern = Best->Partial; |
| 2826 | InstantiationArgs = Best->Args; |
| 2827 | } else { |
| 2828 | // -- If no match is found, the instantiation is generated |
| 2829 | // from the primary template. |
| 2830 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 2831 | } |
| 2832 | } |
| 2833 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2834 | // 2. Create the canonical declaration. |
| 2835 | // Note that we do not instantiate the variable just yet, since |
| 2836 | // instantiation is handled in DoMarkVarDeclReferenced(). |
| 2837 | // FIXME: LateAttrs et al.? |
| 2838 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 2839 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 2840 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 2841 | if (!Decl) |
| 2842 | return true; |
| 2843 | |
| 2844 | if (AmbiguousPartialSpec) { |
| 2845 | // Partial ordering did not produce a clear winner. Complain. |
| 2846 | Decl->setInvalidDecl(); |
| 2847 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 2848 | << Decl; |
| 2849 | |
| 2850 | // Print the matching partial specializations. |
| 2851 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2852 | PEnd = Matched.end(); |
| 2853 | P != PEnd; ++P) |
| 2854 | Diag(P->Partial->getLocation(), diag::note_partial_spec_match) |
| 2855 | << getTemplateArgumentBindingsText( |
| 2856 | P->Partial->getTemplateParameters(), *P->Args); |
| 2857 | return true; |
| 2858 | } |
| 2859 | |
| 2860 | if (VarTemplatePartialSpecializationDecl *D = |
| 2861 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 2862 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 2863 | |
| 2864 | assert(Decl && "No variable template specialization?"); |
| 2865 | return Decl; |
| 2866 | } |
| 2867 | |
| 2868 | ExprResult |
| 2869 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 2870 | const DeclarationNameInfo &NameInfo, |
| 2871 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2872 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2873 | |
| 2874 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 2875 | *TemplateArgs); |
| 2876 | if (Decl.isInvalid()) |
| 2877 | return ExprError(); |
| 2878 | |
| 2879 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 2880 | if (!Var->getTemplateSpecializationKind()) |
| 2881 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 2882 | NameInfo.getLoc()); |
| 2883 | |
| 2884 | // Build an ordinary singleton decl ref. |
| 2885 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2886 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2889 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2890 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2891 | LookupResult &R, |
| 2892 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2893 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2894 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2895 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2896 | // 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] | 2897 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2898 | // foo<int> could identify a single function unambiguously |
| 2899 | // This approach does NOT work, since f<int>(1); |
| 2900 | // gets resolved prior to resorting to overload resolution |
| 2901 | // i.e., template<class T> void f(double); |
| 2902 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2903 | |
| 2904 | // These should be filtered out by our callers. |
| 2905 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2906 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2907 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2908 | // In C++1y, check variable template ids. |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 2909 | bool InstantiationDependent; |
| 2910 | if (R.getAsSingle<VarTemplateDecl>() && |
| 2911 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2912 | *TemplateArgs, InstantiationDependent)) { |
| 2913 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 2914 | R.getAsSingle<VarTemplateDecl>(), |
| 2915 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2918 | // We don't want lookup warnings at this point. |
| 2919 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2920 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2921 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2922 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2923 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2924 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2925 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2926 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2927 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2928 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2929 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2932 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2933 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2934 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2935 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2936 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2937 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2938 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2939 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2940 | DeclContext *DC; |
| 2941 | if (!(DC = computeDeclContext(SS, false)) || |
| 2942 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2943 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 2944 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2945 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2946 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2947 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2948 | LookupTemplateName(R, (Scope*)nullptr, SS, QualType(), /*Entering*/ false, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2949 | MemberOfUnknownSpecialization); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2950 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2951 | if (R.isAmbiguous()) |
| 2952 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2953 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2954 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2955 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2956 | << NameInfo.getName() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2957 | return ExprError(); |
| 2958 | } |
| 2959 | |
| 2960 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2961 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 2962 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 2963 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2964 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2965 | return ExprError(); |
| 2966 | } |
| 2967 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2968 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2971 | /// \brief Form a dependent template name. |
| 2972 | /// |
| 2973 | /// This action forms a dependent template name given the template |
| 2974 | /// name and its (presumably dependent) scope specifier. For |
| 2975 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2976 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2977 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2978 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2979 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2980 | SourceLocation TemplateKWLoc, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2981 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2982 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2983 | bool EnteringContext, |
| 2984 | TemplateTy &Result) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2985 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 2986 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2987 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2988 | diag::warn_cxx98_compat_template_outside_of_template : |
| 2989 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2990 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2991 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2992 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2993 | if (SS.isSet()) |
| 2994 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2995 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2996 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2997 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2998 | // C++0x [temp.names]p5: |
| 2999 | // If a name prefixed by the keyword template is not the name of |
| 3000 | // a template, the program is ill-formed. [Note: the keyword |
| 3001 | // template may not be applied to non-template members of class |
| 3002 | // templates. -end note ] [ Note: as is the case with the |
| 3003 | // typename prefix, the template prefix is allowed in cases |
| 3004 | // where it is not strictly necessary; i.e., when the |
| 3005 | // nested-name-specifier or the expression on the left of the -> |
| 3006 | // or . is not dependent on a template-parameter, or the use |
| 3007 | // does not appear in the scope of a template. -end note] |
| 3008 | // |
| 3009 | // Note: C++03 was more strict here, because it banned the use of |
| 3010 | // the "template" keyword prior to a template-name that was not a |
| 3011 | // dependent name. C++ DR468 relaxed this requirement (the |
| 3012 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 3013 | // 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] | 3014 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 3015 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 3016 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 3017 | MemberOfUnknownSpecialization); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 3018 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 3019 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | 5ecbb1b | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 3020 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 3021 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3022 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3023 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3024 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3025 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3026 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3027 | << Name.getSourceRange() |
| 3028 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3029 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 3030 | } else { |
| 3031 | // We found something; return it. |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3032 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3033 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 3036 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3037 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3038 | switch (Name.getKind()) { |
| 3039 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3040 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3041 | Name.Identifier)); |
| 3042 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3043 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3044 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3045 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3046 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 3047 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3048 | |
| 3049 | case UnqualifiedId::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 3050 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3051 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3052 | default: |
| 3053 | break; |
| 3054 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3055 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3056 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3057 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3058 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3059 | << Name.getSourceRange() |
| 3060 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3061 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3064 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3065 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3066 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3067 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3068 | QualType ArgType; |
| 3069 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3070 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3071 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3072 | switch(Arg.getKind()) { |
| 3073 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3074 | // C++ [temp.arg.type]p1: |
| 3075 | // A template-argument for a template-parameter which is a |
| 3076 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3077 | ArgType = Arg.getAsType(); |
| 3078 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3079 | break; |
| 3080 | case TemplateArgument::Template: { |
| 3081 | // We have a template type parameter but the template argument |
| 3082 | // is a template without any arguments. |
| 3083 | SourceRange SR = AL.getSourceRange(); |
| 3084 | TemplateName Name = Arg.getAsTemplate(); |
| 3085 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 3086 | << Name << SR; |
| 3087 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 3088 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3089 | |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3090 | return true; |
| 3091 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3092 | case TemplateArgument::Expression: { |
| 3093 | // We have a template type parameter but the template argument is an |
| 3094 | // expression; see if maybe it is missing the "typename" keyword. |
| 3095 | CXXScopeSpec SS; |
| 3096 | DeclarationNameInfo NameInfo; |
| 3097 | |
| 3098 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 3099 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3100 | NameInfo = ArgExpr->getNameInfo(); |
| 3101 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 3102 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 3103 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3104 | NameInfo = ArgExpr->getNameInfo(); |
| 3105 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 3106 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3107 | if (ArgExpr->isImplicitAccess()) { |
| 3108 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3109 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 3110 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3113 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3114 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 3115 | LookupParsedName(Result, CurScope, &SS); |
| 3116 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3117 | if (Result.getAsSingle<TypeDecl>() || |
| 3118 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3119 | LookupResult::NotFoundInCurrentInstantiation) { |
| 3120 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3121 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3122 | Diag(Loc, getLangOpts().MSVCCompat |
| 3123 | ? diag::ext_ms_template_type_arg_missing_typename |
| 3124 | : diag::err_template_arg_must_be_type_suggest) |
| 3125 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3126 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3127 | |
| 3128 | // Recover by synthesizing a type using the location information that we |
| 3129 | // already have. |
| 3130 | ArgType = |
| 3131 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 3132 | TypeLocBuilder TLB; |
| 3133 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 3134 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 3135 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3136 | TL.setNameLoc(NameInfo.getLoc()); |
| 3137 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 3138 | |
| 3139 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 3140 | // properly. |
| 3141 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 3142 | TemplateArgumentLocInfo(TSI)); |
| 3143 | |
| 3144 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3145 | } |
| 3146 | } |
| 3147 | // fallthrough |
| 3148 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3149 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3150 | // We have a template type parameter but the template argument |
| 3151 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 3152 | SourceRange SR = AL.getSourceRange(); |
| 3153 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3154 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3155 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3156 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3157 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3158 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3159 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3160 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3161 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3162 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3163 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3164 | ArgType = Context.getCanonicalType(ArgType); |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3165 | |
| 3166 | // Objective-C ARC: |
| 3167 | // If an explicitly-specified template argument type is a lifetime type |
| 3168 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3169 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3170 | ArgType->isObjCLifetimeType() && |
| 3171 | !ArgType.getObjCLifetime()) { |
| 3172 | Qualifiers Qs; |
| 3173 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 3174 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 3175 | } |
| 3176 | |
| 3177 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3178 | return false; |
| 3179 | } |
| 3180 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3181 | /// \brief Substitute template arguments into the default template argument for |
| 3182 | /// the given template type parameter. |
| 3183 | /// |
| 3184 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3185 | /// the substitution. |
| 3186 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3187 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3188 | /// for. |
| 3189 | /// |
| 3190 | /// \param TemplateLoc the location of the template name that started the |
| 3191 | /// template-id we are checking. |
| 3192 | /// |
| 3193 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3194 | /// terminates the template-id. |
| 3195 | /// |
| 3196 | /// \param Param the template template parameter whose default we are |
| 3197 | /// substituting into. |
| 3198 | /// |
| 3199 | /// \param Converted the list of template arguments provided for template |
| 3200 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3201 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3202 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3203 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3204 | TemplateDecl *Template, |
| 3205 | SourceLocation TemplateLoc, |
| 3206 | SourceLocation RAngleLoc, |
| 3207 | TemplateTypeParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3208 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3209 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3210 | |
| 3211 | // If the argument type is dependent, instantiate it now based |
| 3212 | // on the previously-computed template arguments. |
| 3213 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3214 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3215 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3216 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3217 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3218 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3219 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3220 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3221 | Converted.data(), Converted.size()); |
| 3222 | |
| 3223 | // Only substitute for the innermost template argument list. |
| 3224 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3225 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3226 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3227 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3228 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3229 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3230 | ArgType = |
| 3231 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 3232 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
| 3235 | return ArgType; |
| 3236 | } |
| 3237 | |
| 3238 | /// \brief Substitute template arguments into the default template argument for |
| 3239 | /// the given non-type template parameter. |
| 3240 | /// |
| 3241 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3242 | /// the substitution. |
| 3243 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3244 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3245 | /// for. |
| 3246 | /// |
| 3247 | /// \param TemplateLoc the location of the template name that started the |
| 3248 | /// template-id we are checking. |
| 3249 | /// |
| 3250 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3251 | /// terminates the template-id. |
| 3252 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3253 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3254 | /// substituting into. |
| 3255 | /// |
| 3256 | /// \param Converted the list of template arguments provided for template |
| 3257 | /// parameters that precede \p Param in the template parameter list. |
| 3258 | /// |
| 3259 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3260 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3261 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3262 | TemplateDecl *Template, |
| 3263 | SourceLocation TemplateLoc, |
| 3264 | SourceLocation RAngleLoc, |
| 3265 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3266 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3267 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3268 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3269 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3270 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3271 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3272 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3273 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3274 | Converted.data(), Converted.size()); |
| 3275 | |
| 3276 | // Only substitute for the innermost template argument list. |
| 3277 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3278 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3279 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3280 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3281 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3282 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
Eli Friedman | c25372b | 2012-04-26 22:43:24 +0000 | [diff] [blame] | 3283 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3284 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3287 | /// \brief Substitute template arguments into the default template argument for |
| 3288 | /// the given template template parameter. |
| 3289 | /// |
| 3290 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3291 | /// the substitution. |
| 3292 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3293 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3294 | /// for. |
| 3295 | /// |
| 3296 | /// \param TemplateLoc the location of the template name that started the |
| 3297 | /// template-id we are checking. |
| 3298 | /// |
| 3299 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3300 | /// terminates the template-id. |
| 3301 | /// |
| 3302 | /// \param Param the template template parameter whose default we are |
| 3303 | /// substituting into. |
| 3304 | /// |
| 3305 | /// \param Converted the list of template arguments provided for template |
| 3306 | /// parameters that precede \p Param in the template parameter list. |
| 3307 | /// |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3308 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 3309 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3310 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3311 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 3312 | static TemplateName |
| 3313 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3314 | TemplateDecl *Template, |
| 3315 | SourceLocation TemplateLoc, |
| 3316 | SourceLocation RAngleLoc, |
| 3317 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3318 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3319 | NestedNameSpecifierLoc &QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3320 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, Template, Converted, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3321 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3322 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3323 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3324 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3325 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3326 | Converted.data(), Converted.size()); |
| 3327 | |
| 3328 | // Only substitute for the innermost template argument list. |
| 3329 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3330 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3331 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3332 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3333 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3334 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3335 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3336 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3337 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3338 | QualifierLoc = |
| 3339 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3340 | if (!QualifierLoc) |
| 3341 | return TemplateName(); |
| 3342 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3343 | |
| 3344 | return SemaRef.SubstTemplateName( |
| 3345 | QualifierLoc, |
| 3346 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 3347 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 3348 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3351 | /// \brief If the given template parameter has a default template |
| 3352 | /// argument, substitute into that default template argument and |
| 3353 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3354 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3355 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 3356 | SourceLocation TemplateLoc, |
| 3357 | SourceLocation RAngleLoc, |
| 3358 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3359 | SmallVectorImpl<TemplateArgument> |
| 3360 | &Converted, |
| 3361 | bool &HasDefaultArg) { |
| 3362 | HasDefaultArg = false; |
| 3363 | |
| 3364 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3365 | if (!hasVisibleDefaultArgument(TypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3366 | return TemplateArgumentLoc(); |
| 3367 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3368 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3369 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3370 | TemplateLoc, |
| 3371 | RAngleLoc, |
| 3372 | TypeParm, |
| 3373 | Converted); |
| 3374 | if (DI) |
| 3375 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 3376 | |
| 3377 | return TemplateArgumentLoc(); |
| 3378 | } |
| 3379 | |
| 3380 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 3381 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3382 | if (!hasVisibleDefaultArgument(NonTypeParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3383 | return TemplateArgumentLoc(); |
| 3384 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3385 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3386 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3387 | TemplateLoc, |
| 3388 | RAngleLoc, |
| 3389 | NonTypeParm, |
| 3390 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3391 | if (Arg.isInvalid()) |
| 3392 | return TemplateArgumentLoc(); |
| 3393 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3394 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3395 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 3396 | } |
| 3397 | |
| 3398 | TemplateTemplateParmDecl *TempTempParm |
| 3399 | = cast<TemplateTemplateParmDecl>(Param); |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3400 | if (!hasVisibleDefaultArgument(TempTempParm)) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3401 | return TemplateArgumentLoc(); |
| 3402 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3403 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3404 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3405 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3406 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3407 | RAngleLoc, |
| 3408 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3409 | Converted, |
| 3410 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3411 | if (TName.isNull()) |
| 3412 | return TemplateArgumentLoc(); |
| 3413 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3414 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3415 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3416 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 3417 | } |
| 3418 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3419 | /// \brief Check that the given template argument corresponds to the given |
| 3420 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3421 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3422 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3423 | /// checked. |
| 3424 | /// |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3425 | /// \param Arg The template argument, which may be updated due to conversions. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3426 | /// |
| 3427 | /// \param Template The template in which the template argument resides. |
| 3428 | /// |
| 3429 | /// \param TemplateLoc The location of the template name for the template |
| 3430 | /// whose argument list we're matching. |
| 3431 | /// |
| 3432 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 3433 | /// the template argument list. |
| 3434 | /// |
| 3435 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 3436 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 3437 | /// |
| 3438 | /// \param Converted The checked, converted argument will be added to the |
| 3439 | /// end of this small vector. |
| 3440 | /// |
| 3441 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 3442 | /// explicitly written, deduced, etc. |
| 3443 | /// |
| 3444 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3445 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3446 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 3447 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3448 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3449 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3450 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3451 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3452 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3453 | // Check template type parameters. |
| 3454 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3455 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3456 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3457 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3458 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3459 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3460 | // with the template arguments we've seen thus far. But if the |
| 3461 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3462 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3463 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 3464 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3465 | |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3466 | if (NTTPType->isDependentType() && |
| 3467 | !isa<TemplateTemplateParmDecl>(Template) && |
| 3468 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3469 | // Do substitution on the type of the non-type template parameter. |
| 3470 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3471 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3472 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3473 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3474 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3475 | |
| 3476 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3477 | Converted.data(), Converted.size()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3478 | NTTPType = SubstType(NTTPType, |
| 3479 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 3480 | NTTP->getLocation(), |
| 3481 | NTTP->getDeclName()); |
| 3482 | // If that worked, check the non-type template parameter type |
| 3483 | // for validity. |
| 3484 | if (!NTTPType.isNull()) |
| 3485 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 3486 | NTTP->getLocation()); |
| 3487 | if (NTTPType.isNull()) |
| 3488 | return true; |
| 3489 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3490 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3491 | switch (Arg.getArgument().getKind()) { |
| 3492 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3493 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3494 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3495 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3496 | TemplateArgument Result; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3497 | ExprResult Res = |
| 3498 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 3499 | Result, CTAK); |
| 3500 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3501 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3502 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3503 | // If the resulting expression is new, then use it in place of the |
| 3504 | // old expression in the template argument. |
| 3505 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 3506 | TemplateArgument TA(Res.get()); |
| 3507 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 3508 | } |
| 3509 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3510 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3511 | break; |
| 3512 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3513 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3514 | case TemplateArgument::Declaration: |
| 3515 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3516 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3517 | // We've already checked this template argument, so just copy |
| 3518 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3519 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3520 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3521 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3522 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3523 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3524 | // We were given a template template argument. It may not be ill-formed; |
| 3525 | // see below. |
| 3526 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3527 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 3528 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3529 | // We have a template argument such as \c T::template X, which we |
| 3530 | // parsed as a template template argument. However, since we now |
| 3531 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3532 | // template name into an expression. |
| 3533 | |
| 3534 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 3535 | Arg.getTemplateNameLoc()); |
| 3536 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 3537 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3538 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3539 | // FIXME: the template-template arg was a DependentTemplateName, |
| 3540 | // so it was provided with a template keyword. However, its source |
| 3541 | // location is not stored in the template argument structure. |
| 3542 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3543 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 3544 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 3545 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3546 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3547 | // If we parsed the template argument as a pack expansion, create a |
| 3548 | // pack expansion expression. |
| 3549 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3550 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3551 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3552 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3553 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3554 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3555 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3556 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3557 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3558 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3559 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3560 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3561 | break; |
| 3562 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3563 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3564 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3565 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3566 | // therefore cannot be a non-type template argument. |
| 3567 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 3568 | << Arg.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3569 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3570 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3571 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3572 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3573 | case TemplateArgument::Type: { |
| 3574 | // We have a non-type template parameter but the template |
| 3575 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3576 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3577 | // C++ [temp.arg]p2: |
| 3578 | // In a template-argument, an ambiguity between a type-id and |
| 3579 | // an expression is resolved to a type-id, regardless of the |
| 3580 | // form of the corresponding template-parameter. |
| 3581 | // |
| 3582 | // We warn specifically about this case, since it can be rather |
| 3583 | // confusing for users. |
| 3584 | QualType T = Arg.getArgument().getAsType(); |
| 3585 | SourceRange SR = Arg.getSourceRange(); |
| 3586 | if (T->isFunctionType()) |
| 3587 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 3588 | else |
| 3589 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 3590 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3591 | return true; |
| 3592 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3593 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3594 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3595 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3596 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3597 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3598 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3602 | // Check template template parameters. |
| 3603 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3604 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3605 | // Substitute into the template parameter list of the template |
| 3606 | // template parameter, since previously-supplied template arguments |
| 3607 | // may appear within the template template parameter. |
| 3608 | { |
| 3609 | // Set up a template instantiation context. |
| 3610 | LocalInstantiationScope Scope(*this); |
| 3611 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3612 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3613 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3614 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3615 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3616 | |
| 3617 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3618 | Converted.data(), Converted.size()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3619 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3620 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3621 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 3622 | if (!TempParm) |
| 3623 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3624 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3625 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3626 | switch (Arg.getArgument().getKind()) { |
| 3627 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3628 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3629 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3630 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3631 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3632 | if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3633 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3634 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3635 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3636 | break; |
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 | case TemplateArgument::Expression: |
| 3639 | case TemplateArgument::Type: |
| 3640 | // We have a template template parameter but the template |
| 3641 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3642 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3643 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3644 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3645 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3646 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3647 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3648 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3649 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3650 | case TemplateArgument::NullPtr: |
| 3651 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3652 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3653 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3654 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3655 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3656 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3657 | return false; |
| 3658 | } |
| 3659 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3660 | /// \brief Diagnose an arity mismatch in the |
| 3661 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 3662 | SourceLocation TemplateLoc, |
| 3663 | TemplateArgumentListInfo &TemplateArgs) { |
| 3664 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 3665 | unsigned NumParams = Params->size(); |
| 3666 | unsigned NumArgs = TemplateArgs.size(); |
| 3667 | |
| 3668 | SourceRange Range; |
| 3669 | if (NumArgs > NumParams) |
| 3670 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
| 3671 | TemplateArgs.getRAngleLoc()); |
| 3672 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3673 | << (NumArgs > NumParams) |
| 3674 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3675 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3676 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3677 | << Template << Range; |
| 3678 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3679 | << Params->getSourceRange(); |
| 3680 | return true; |
| 3681 | } |
| 3682 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3683 | /// \brief Check whether the template parameter is a pack expansion, and if so, |
| 3684 | /// determine the number of parameters produced by that expansion. For instance: |
| 3685 | /// |
| 3686 | /// \code |
| 3687 | /// template<typename ...Ts> struct A { |
| 3688 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 3689 | /// }; |
| 3690 | /// \endcode |
| 3691 | /// |
| 3692 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 3693 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3694 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3695 | if (NonTypeTemplateParmDecl *NTTP |
| 3696 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3697 | if (NTTP->isExpandedParameterPack()) |
| 3698 | return NTTP->getNumExpansionTypes(); |
| 3699 | } |
| 3700 | |
| 3701 | if (TemplateTemplateParmDecl *TTP |
| 3702 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 3703 | if (TTP->isExpandedParameterPack()) |
| 3704 | return TTP->getNumExpansionTemplateParameters(); |
| 3705 | } |
| 3706 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 3707 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3710 | /// Diagnose a missing template argument. |
| 3711 | template<typename TemplateParmDecl> |
| 3712 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
| 3713 | TemplateDecl *TD, |
| 3714 | const TemplateParmDecl *D, |
| 3715 | TemplateArgumentListInfo &Args) { |
| 3716 | // Dig out the most recent declaration of the template parameter; there may be |
| 3717 | // declarations of the template that are more recent than TD. |
| 3718 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
| 3719 | ->getTemplateParameters() |
| 3720 | ->getParam(D->getIndex())); |
| 3721 | |
| 3722 | // If there's a default argument that's not visible, diagnose that we're |
| 3723 | // missing a module import. |
| 3724 | llvm::SmallVector<Module*, 8> Modules; |
| 3725 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { |
| 3726 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
| 3727 | D->getDefaultArgumentLoc(), Modules, |
| 3728 | Sema::MissingImportKind::DefaultArgument, |
| 3729 | /*Recover*/ true); |
| 3730 | return true; |
| 3731 | } |
| 3732 | |
| 3733 | // FIXME: If there's a more recent default argument that *is* visible, |
| 3734 | // diagnose that it was declared too late. |
| 3735 | |
| 3736 | return diagnoseArityMismatch(S, TD, Loc, Args); |
| 3737 | } |
| 3738 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3739 | /// \brief Check that the given template argument list is well-formed |
| 3740 | /// for specializing the given template. |
| 3741 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 3742 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3743 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 3744 | bool PartialTemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3745 | SmallVectorImpl<TemplateArgument> &Converted) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3746 | // Make a copy of the template arguments for processing. Only make the |
| 3747 | // changes at the end when successful in matching the arguments to the |
| 3748 | // template. |
| 3749 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 3750 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3751 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3752 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3753 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3754 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3755 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3756 | // [...] The type and form of each template-argument specified in |
| 3757 | // a template-id shall match the type and form specified for the |
| 3758 | // corresponding parameter declared by the template in its |
| 3759 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3760 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3761 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3762 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3763 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3764 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 3765 | ParamEnd = Params->end(); |
| 3766 | Param != ParamEnd; /* increment in loop */) { |
| 3767 | // If we have an expanded parameter pack, make sure we don't have too |
| 3768 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3769 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3770 | if (*Expansions == ArgumentPack.size()) { |
| 3771 | // We're done with this parameter pack. Pack up its arguments and add |
| 3772 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3773 | Converted.push_back( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 3774 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3775 | ArgumentPack.clear(); |
| 3776 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3777 | // This argument is assigned to the next parameter. |
| 3778 | ++Param; |
| 3779 | continue; |
| 3780 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 3781 | // Not enough arguments for this parameter pack. |
| 3782 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3783 | << false |
| 3784 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3785 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3786 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3787 | << Template; |
| 3788 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3789 | << Params->getSourceRange(); |
| 3790 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3791 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3792 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3793 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3794 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3795 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3796 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3797 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3798 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3799 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3800 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3801 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3802 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3803 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 3804 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3805 | // 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] | 3806 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3807 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3808 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3809 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3810 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3811 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 3812 | return true; |
| 3813 | } |
| 3814 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3815 | // We're now done with this argument. |
| 3816 | ++ArgIdx; |
| 3817 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3818 | if ((*Param)->isTemplateParameterPack()) { |
| 3819 | // The template parameter was a template parameter pack, so take the |
| 3820 | // deduced argument and place it on the argument pack. Note that we |
| 3821 | // stay on the same template parameter so that we can deduce more |
| 3822 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 3823 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3824 | } else { |
| 3825 | // Move to the next template parameter. |
| 3826 | ++Param; |
| 3827 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3828 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3829 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 3830 | // the remaining arguments, because we don't know what parameters they'll |
| 3831 | // match up with. |
| 3832 | if (PackExpansionIntoNonPack) { |
| 3833 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3834 | // If we were part way through filling in an expanded parameter pack, |
| 3835 | // fall back to just producing individual arguments. |
| 3836 | Converted.insert(Converted.end(), |
| 3837 | ArgumentPack.begin(), ArgumentPack.end()); |
| 3838 | ArgumentPack.clear(); |
| 3839 | } |
| 3840 | |
| 3841 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3842 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3843 | ++ArgIdx; |
| 3844 | } |
| 3845 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3846 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3847 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3848 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3849 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3850 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3851 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3852 | // If we're checking a partial template argument list, we're done. |
| 3853 | if (PartialTemplateArgs) { |
| 3854 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 3855 | Converted.push_back( |
| 3856 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 3857 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3858 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3859 | } |
| 3860 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3861 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3862 | // 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] | 3863 | if ((*Param)->isTemplateParameterPack()) { |
| 3864 | assert(!getExpandedPackSize(*Param) && |
| 3865 | "Should have dealt with this already"); |
| 3866 | |
| 3867 | // A non-expanded parameter pack before the end of the parameter list |
| 3868 | // only occurs for an ill-formed template parameter list, unless we've |
| 3869 | // got a partial argument list for a function template, so just bail out. |
| 3870 | if (Param + 1 != ParamEnd) |
| 3871 | return true; |
| 3872 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 3873 | Converted.push_back( |
| 3874 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3875 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3876 | |
| 3877 | ++Param; |
| 3878 | continue; |
| 3879 | } |
| 3880 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3881 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3882 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3883 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3884 | // Retrieve the default template argument from the template |
| 3885 | // parameter. For each kind of template parameter, we substitute the |
| 3886 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3887 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3888 | // the default argument. |
| 3889 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3890 | if (!hasVisibleDefaultArgument(TTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3891 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
| 3892 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3893 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3894 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3895 | Template, |
| 3896 | TemplateLoc, |
| 3897 | RAngleLoc, |
| 3898 | TTP, |
| 3899 | Converted); |
| 3900 | if (!ArgType) |
| 3901 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3902 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3903 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 3904 | ArgType); |
| 3905 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3906 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3907 | if (!hasVisibleDefaultArgument(NTTP)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3908 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
| 3909 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3910 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3911 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3912 | TemplateLoc, |
| 3913 | RAngleLoc, |
| 3914 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3915 | Converted); |
| 3916 | if (E.isInvalid()) |
| 3917 | return true; |
| 3918 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3919 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3920 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 3921 | } else { |
| 3922 | TemplateTemplateParmDecl *TempParm |
| 3923 | = cast<TemplateTemplateParmDecl>(*Param); |
| 3924 | |
Richard Smith | 95d8395 | 2015-06-10 20:36:34 +0000 | [diff] [blame] | 3925 | if (!hasVisibleDefaultArgument(TempParm)) |
Richard Smith | 35c1df5 | 2015-06-17 20:16:32 +0000 | [diff] [blame] | 3926 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
| 3927 | NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3928 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3929 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3930 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3931 | TemplateLoc, |
| 3932 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3933 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3934 | Converted, |
| 3935 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3936 | if (Name.isNull()) |
| 3937 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3938 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3939 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3940 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3941 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3942 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3943 | // Introduce an instantiation record that describes where we are using |
| 3944 | // the default template argument. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3945 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 3946 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3947 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3948 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3949 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3950 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3951 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3952 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3953 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3954 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3955 | // Core issue 150 (assumed resolution): if this is a template template |
| 3956 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3957 | // template definition. |
| 3958 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3959 | NewArgs.addArgument(Arg); |
| 3960 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3961 | // Move to the next template parameter and argument. |
| 3962 | ++Param; |
| 3963 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3964 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3965 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 3966 | // If we're performing a partial argument substitution, allow any trailing |
| 3967 | // pack expansions; they might be empty. This can happen even if |
| 3968 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 3969 | // still dependent). |
| 3970 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 3971 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3972 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 3973 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3976 | // If we have any leftover arguments, then there were too many arguments. |
| 3977 | // Complain and fail. |
| 3978 | if (ArgIdx < NumArgs) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3979 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
| 3980 | |
| 3981 | // No problems found with the new argument list, propagate changes back |
| 3982 | // to caller. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 3983 | TemplateArgs = std::move(NewArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3984 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3985 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3986 | } |
| 3987 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3988 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3989 | class UnnamedLocalNoLinkageFinder |
| 3990 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3991 | { |
| 3992 | Sema &S; |
| 3993 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3994 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3995 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3996 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3997 | public: |
| 3998 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3999 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4000 | bool Visit(QualType T) { |
| 4001 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4002 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4003 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4004 | #define TYPE(Class, Parent) \ |
| 4005 | bool Visit##Class##Type(const Class##Type *); |
| 4006 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 4007 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4008 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 4009 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 4010 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4011 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4012 | bool VisitTagDecl(const TagDecl *Tag); |
| 4013 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 4014 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4015 | } |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4016 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4017 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4018 | return false; |
| 4019 | } |
| 4020 | |
| 4021 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 4022 | return Visit(T->getElementType()); |
| 4023 | } |
| 4024 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4025 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4026 | return Visit(T->getPointeeType()); |
| 4027 | } |
| 4028 | |
| 4029 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4030 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4031 | return Visit(T->getPointeeType()); |
| 4032 | } |
| 4033 | |
| 4034 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4035 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4036 | return Visit(T->getPointeeType()); |
| 4037 | } |
| 4038 | |
| 4039 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4040 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4041 | return Visit(T->getPointeeType()); |
| 4042 | } |
| 4043 | |
| 4044 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4045 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4046 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 4047 | } |
| 4048 | |
| 4049 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4050 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4051 | return Visit(T->getElementType()); |
| 4052 | } |
| 4053 | |
| 4054 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4055 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4056 | return Visit(T->getElementType()); |
| 4057 | } |
| 4058 | |
| 4059 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4060 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4061 | return Visit(T->getElementType()); |
| 4062 | } |
| 4063 | |
| 4064 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4065 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4066 | return Visit(T->getElementType()); |
| 4067 | } |
| 4068 | |
| 4069 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4070 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4071 | return Visit(T->getElementType()); |
| 4072 | } |
| 4073 | |
| 4074 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 4075 | return Visit(T->getElementType()); |
| 4076 | } |
| 4077 | |
| 4078 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 4079 | return Visit(T->getElementType()); |
| 4080 | } |
| 4081 | |
| 4082 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 4083 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4084 | for (const auto &A : T->param_types()) { |
| 4085 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4086 | return true; |
| 4087 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4088 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4089 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4090 | } |
| 4091 | |
| 4092 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 4093 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4094 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 4098 | const UnresolvedUsingType*) { |
| 4099 | return false; |
| 4100 | } |
| 4101 | |
| 4102 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 4103 | return false; |
| 4104 | } |
| 4105 | |
| 4106 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 4107 | return Visit(T->getUnderlyingType()); |
| 4108 | } |
| 4109 | |
| 4110 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 4111 | return false; |
| 4112 | } |
| 4113 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4114 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 4115 | const UnaryTransformType*) { |
| 4116 | return false; |
| 4117 | } |
| 4118 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4119 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 4120 | return Visit(T->getDeducedType()); |
| 4121 | } |
| 4122 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4123 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 4124 | return VisitTagDecl(T->getDecl()); |
| 4125 | } |
| 4126 | |
| 4127 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 4128 | return VisitTagDecl(T->getDecl()); |
| 4129 | } |
| 4130 | |
| 4131 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 4132 | const TemplateTypeParmType*) { |
| 4133 | return false; |
| 4134 | } |
| 4135 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4136 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 4137 | const SubstTemplateTypeParmPackType *) { |
| 4138 | return false; |
| 4139 | } |
| 4140 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4141 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 4142 | const TemplateSpecializationType*) { |
| 4143 | return false; |
| 4144 | } |
| 4145 | |
| 4146 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 4147 | const InjectedClassNameType* T) { |
| 4148 | return VisitTagDecl(T->getDecl()); |
| 4149 | } |
| 4150 | |
| 4151 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 4152 | const DependentNameType* T) { |
| 4153 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4154 | } |
| 4155 | |
| 4156 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 4157 | const DependentTemplateSpecializationType* T) { |
| 4158 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4159 | } |
| 4160 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4161 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 4162 | const PackExpansionType* T) { |
| 4163 | return Visit(T->getPattern()); |
| 4164 | } |
| 4165 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4166 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 4167 | return false; |
| 4168 | } |
| 4169 | |
| 4170 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 4171 | const ObjCInterfaceType *) { |
| 4172 | return false; |
| 4173 | } |
| 4174 | |
| 4175 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 4176 | const ObjCObjectPointerType *) { |
| 4177 | return false; |
| 4178 | } |
| 4179 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4180 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 4181 | return Visit(T->getValueType()); |
| 4182 | } |
| 4183 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4184 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 4185 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4186 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4187 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4188 | diag::warn_cxx98_compat_template_arg_local_type : |
| 4189 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4190 | << S.Context.getTypeDeclType(Tag) << SR; |
| 4191 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 4194 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4195 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4196 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4197 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 4198 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4199 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 4200 | return true; |
| 4201 | } |
| 4202 | |
| 4203 | return false; |
| 4204 | } |
| 4205 | |
| 4206 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 4207 | NestedNameSpecifier *NNS) { |
| 4208 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 4209 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4210 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4211 | switch (NNS->getKind()) { |
| 4212 | case NestedNameSpecifier::Identifier: |
| 4213 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4214 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4215 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 4216 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4217 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4218 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4219 | case NestedNameSpecifier::TypeSpec: |
| 4220 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 4221 | return Visit(QualType(NNS->getAsType(), 0)); |
| 4222 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4223 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
| 4226 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4227 | /// \brief Check a template argument against its corresponding |
| 4228 | /// template type parameter. |
| 4229 | /// |
| 4230 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 4231 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4232 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4233 | TypeSourceInfo *ArgInfo) { |
| 4234 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4235 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 4236 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 4237 | |
| 4238 | if (Arg->isVariablyModifiedType()) { |
| 4239 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4240 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4241 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4244 | // C++03 [temp.arg.type]p2: |
| 4245 | // A local type, a type with no linkage, an unnamed type or a type |
| 4246 | // compounded from any of these types shall not be used as a |
| 4247 | // template-argument for a template type-parameter. |
| 4248 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4249 | // 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] | 4250 | // a warning. |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 4251 | bool NeedsCheck; |
| 4252 | if (LangOpts.CPlusPlus11) |
| 4253 | NeedsCheck = |
| 4254 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 4255 | SR.getBegin()) || |
| 4256 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_local_type, |
| 4257 | SR.getBegin()); |
| 4258 | else |
| 4259 | NeedsCheck = Arg->hasUnnamedOrLocalType(); |
| 4260 | |
| 4261 | if (NeedsCheck) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4262 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 4263 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 4264 | } |
| 4265 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4266 | return false; |
| 4267 | } |
| 4268 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4269 | enum NullPointerValueKind { |
| 4270 | NPV_NotNullPointer, |
| 4271 | NPV_NullPointer, |
| 4272 | NPV_Error |
| 4273 | }; |
| 4274 | |
| 4275 | /// \brief Determine whether the given template argument is a null pointer |
| 4276 | /// value of the appropriate type. |
| 4277 | static NullPointerValueKind |
| 4278 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 4279 | QualType ParamType, Expr *Arg) { |
| 4280 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 4281 | return NPV_NotNullPointer; |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4282 | |
David Majnemer | b54368c | 2015-09-11 20:55:29 +0000 | [diff] [blame] | 4283 | if (S.RequireCompleteType(Arg->getExprLoc(), ParamType, 0)) |
| 4284 | llvm_unreachable( |
| 4285 | "Incomplete parameter type in isNullPointerValueTemplateArgument!"); |
David Majnemer | 69c3ddc | 2015-09-11 20:18:09 +0000 | [diff] [blame] | 4286 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 4287 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4288 | return NPV_NotNullPointer; |
| 4289 | |
| 4290 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4291 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 4292 | if (ArgRV.isInvalid()) |
| 4293 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4294 | Arg = ArgRV.get(); |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4295 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4296 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4297 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4298 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4299 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4300 | EvalResult.HasSideEffects) { |
| 4301 | SourceLocation DiagLoc = Arg->getExprLoc(); |
| 4302 | |
| 4303 | // If our only note is the usual "invalid subexpression" note, just point |
| 4304 | // the caret at its location rather than producing an essentially |
| 4305 | // redundant note. |
| 4306 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 4307 | diag::note_invalid_subexpr_in_const_expr) { |
| 4308 | DiagLoc = Notes[0].first; |
| 4309 | Notes.clear(); |
| 4310 | } |
| 4311 | |
| 4312 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 4313 | << Arg->getType() << Arg->getSourceRange(); |
| 4314 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 4315 | S.Diag(Notes[I].first, Notes[I].second); |
| 4316 | |
| 4317 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4318 | return NPV_Error; |
| 4319 | } |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4320 | |
| 4321 | // C++11 [temp.arg.nontype]p1: |
| 4322 | // - an address constant expression of type std::nullptr_t |
| 4323 | if (Arg->getType()->isNullPtrType()) |
| 4324 | return NPV_NullPointer; |
| 4325 | |
| 4326 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 4327 | // - a constant expression that evaluates to a null member pointer value |
| 4328 | // (4.11); or |
| 4329 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 4330 | (EvalResult.Val.isMemberPointer() && |
| 4331 | !EvalResult.Val.getMemberPointerDecl())) { |
| 4332 | // If our expression has an appropriate type, we've succeeded. |
| 4333 | bool ObjCLifetimeConversion; |
| 4334 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 4335 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 4336 | ObjCLifetimeConversion)) |
| 4337 | return NPV_NullPointer; |
| 4338 | |
| 4339 | // The types didn't match, but we know we got a null pointer; complain, |
| 4340 | // then recover as if the types were correct. |
| 4341 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 4342 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4343 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4344 | return NPV_NullPointer; |
| 4345 | } |
| 4346 | |
| 4347 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 4348 | // constant, suggest a cast to the appropriate type. |
| 4349 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 4350 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 4351 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4352 | << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 4353 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()), |
| 4354 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4355 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4356 | return NPV_NullPointer; |
| 4357 | } |
| 4358 | |
| 4359 | // FIXME: If we ever want to support general, address-constant expressions |
| 4360 | // as non-type template arguments, we should return the ExprResult here to |
| 4361 | // be interpreted by the caller. |
| 4362 | return NPV_NotNullPointer; |
| 4363 | } |
| 4364 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4365 | /// \brief Checks whether the given template argument is compatible with its |
| 4366 | /// template parameter. |
| 4367 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 4368 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 4369 | Expr *Arg, QualType ArgType) { |
| 4370 | bool ObjCLifetimeConversion; |
| 4371 | if (ParamType->isPointerType() && |
| 4372 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 4373 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 4374 | ObjCLifetimeConversion)) { |
| 4375 | // For pointer-to-object types, qualification conversions are |
| 4376 | // permitted. |
| 4377 | } else { |
| 4378 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 4379 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 4380 | // C++ [temp.arg.nontype]p5b3: |
| 4381 | // For a non-type template-parameter of type reference to |
| 4382 | // object, no conversions apply. The type referred to by the |
| 4383 | // reference may be more cv-qualified than the (otherwise |
| 4384 | // identical) type of the template- argument. The |
| 4385 | // template-parameter is bound directly to the |
| 4386 | // template-argument, which shall be an lvalue. |
| 4387 | |
| 4388 | // FIXME: Other qualifiers? |
| 4389 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 4390 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 4391 | |
| 4392 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 4393 | S.Diag(Arg->getLocStart(), |
| 4394 | diag::err_template_arg_ref_bind_ignores_quals) |
| 4395 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 4396 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4397 | return true; |
| 4398 | } |
| 4399 | } |
| 4400 | } |
| 4401 | |
| 4402 | // At this point, the template argument refers to an object or |
| 4403 | // function with external linkage. We now need to check whether the |
| 4404 | // argument and parameter types are compatible. |
| 4405 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 4406 | ParamType.getNonReferenceType())) { |
| 4407 | // We can't perform this conversion or binding. |
| 4408 | if (ParamType->isReferenceType()) |
| 4409 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 4410 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
| 4411 | else |
| 4412 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4413 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
| 4414 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4415 | return true; |
| 4416 | } |
| 4417 | } |
| 4418 | |
| 4419 | return false; |
| 4420 | } |
| 4421 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4422 | /// \brief Checks whether the given template argument is the address |
| 4423 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4424 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4425 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 4426 | NonTypeTemplateParmDecl *Param, |
| 4427 | QualType ParamType, |
| 4428 | Expr *ArgIn, |
| 4429 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4430 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4431 | Expr *Arg = ArgIn; |
| 4432 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4433 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4434 | bool AddressTaken = false; |
| 4435 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4436 | if (S.getLangOpts().MicrosoftExt) { |
| 4437 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 4438 | // dereference and address-of operators. |
| 4439 | Arg = Arg->IgnoreParenCasts(); |
| 4440 | |
| 4441 | bool ExtWarnMSTemplateArg = false; |
| 4442 | UnaryOperatorKind FirstOpKind; |
| 4443 | SourceLocation FirstOpLoc; |
| 4444 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4445 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 4446 | if (UnOpKind == UO_Deref) |
| 4447 | ExtWarnMSTemplateArg = true; |
| 4448 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 4449 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 4450 | if (!AddrOpLoc.isValid()) { |
| 4451 | FirstOpKind = UnOpKind; |
| 4452 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 4453 | } |
| 4454 | } else |
| 4455 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4456 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4457 | if (FirstOpLoc.isValid()) { |
| 4458 | if (ExtWarnMSTemplateArg) |
| 4459 | S.Diag(ArgIn->getLocStart(), diag::ext_ms_deref_template_argument) |
| 4460 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4461 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4462 | if (FirstOpKind == UO_AddrOf) |
| 4463 | AddressTaken = true; |
| 4464 | else if (Arg->getType()->isPointerType()) { |
| 4465 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 4466 | // constant expression. |
| 4467 | assert(FirstOpKind == UO_Deref); |
| 4468 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4469 | << Arg->getSourceRange(); |
| 4470 | } |
| 4471 | } |
| 4472 | } else { |
| 4473 | // See through any implicit casts we added to fix the type. |
| 4474 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4475 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4476 | // C++ [temp.arg.nontype]p1: |
| 4477 | // |
| 4478 | // A template-argument for a non-type, non-template |
| 4479 | // template-parameter shall be one of: [...] |
| 4480 | // |
| 4481 | // -- the address of an object or function with external |
| 4482 | // linkage, including function templates and function |
| 4483 | // template-ids but excluding non-static class members, |
| 4484 | // expressed as & id-expression where the & is optional if |
| 4485 | // the name refers to a function or array, or if the |
| 4486 | // corresponding template-parameter is a reference; or |
| 4487 | |
| 4488 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4489 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4490 | bool ExtraParens = false; |
| 4491 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 4492 | if (!Invalid && !ExtraParens) { |
| 4493 | S.Diag(Arg->getLocStart(), |
| 4494 | S.getLangOpts().CPlusPlus11 |
| 4495 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 4496 | : diag::ext_template_arg_extra_parens) |
| 4497 | << Arg->getSourceRange(); |
| 4498 | ExtraParens = true; |
| 4499 | } |
| 4500 | |
| 4501 | Arg = Parens->getSubExpr(); |
| 4502 | } |
| 4503 | |
| 4504 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4505 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4506 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4507 | |
| 4508 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4509 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 4510 | Arg = UnOp->getSubExpr(); |
| 4511 | AddressTaken = true; |
| 4512 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 4513 | } |
| 4514 | } |
| 4515 | |
| 4516 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4517 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4518 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4519 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4520 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4521 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 4522 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 4523 | |
| 4524 | // If our parameter has pointer type, check for a null template value. |
| 4525 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 4526 | NullPointerValueKind NPV; |
| 4527 | // dllimport'd entities aren't constant but are available inside of template |
| 4528 | // arguments. |
| 4529 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 4530 | NPV = NPV_NotNullPointer; |
| 4531 | else |
| 4532 | NPV = isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn); |
| 4533 | switch (NPV) { |
| 4534 | case NPV_NullPointer: |
| 4535 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4536 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4537 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4538 | return false; |
| 4539 | |
| 4540 | case NPV_Error: |
| 4541 | return true; |
| 4542 | |
| 4543 | case NPV_NotNullPointer: |
| 4544 | break; |
| 4545 | } |
| 4546 | } |
| 4547 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4548 | // Stop checking the precise nature of the argument if it is value dependent, |
| 4549 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4550 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4551 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4552 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4553 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4554 | |
| 4555 | if (isa<CXXUuidofExpr>(Arg)) { |
| 4556 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 4557 | ArgIn, Arg, ArgType)) |
| 4558 | return true; |
| 4559 | |
| 4560 | Converted = TemplateArgument(ArgIn); |
| 4561 | return false; |
| 4562 | } |
| 4563 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4564 | if (!DRE) { |
| 4565 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4566 | << Arg->getSourceRange(); |
| 4567 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4568 | return true; |
| 4569 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4570 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4571 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4572 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4573 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4574 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4575 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4576 | return true; |
| 4577 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4578 | |
| 4579 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4580 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4581 | if (!Method->isStatic()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4582 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4583 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4584 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4585 | return true; |
| 4586 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4587 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4588 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4589 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 4590 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4591 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4592 | // A non-type template argument must refer to an object or function. |
| 4593 | if (!Func && !Var) { |
| 4594 | // We found something, but we don't know specifically what it is. |
| 4595 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 4596 | << Arg->getSourceRange(); |
| 4597 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 4598 | return true; |
| 4599 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4600 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4601 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4602 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4603 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4604 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 4605 | diag::ext_template_arg_object_internal) |
| 4606 | << !Func << Entity << Arg->getSourceRange(); |
| 4607 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4608 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4609 | } else if (!Entity->hasLinkage()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4610 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 4611 | << !Func << Entity << Arg->getSourceRange(); |
| 4612 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4613 | << !Func; |
| 4614 | return true; |
| 4615 | } |
| 4616 | |
| 4617 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4618 | // If the template parameter has pointer type, the function decays. |
| 4619 | if (ParamType->isPointerType() && !AddressTaken) |
| 4620 | ArgType = S.Context.getPointerType(Func->getType()); |
| 4621 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 4622 | // If we originally had an address-of operator, but the |
| 4623 | // parameter has reference type, complain and (if things look |
| 4624 | // like they will work) drop the address-of operator. |
| 4625 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 4626 | ParamType.getNonReferenceType())) { |
| 4627 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4628 | << ParamType; |
| 4629 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4630 | return true; |
| 4631 | } |
| 4632 | |
| 4633 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4634 | << ParamType |
| 4635 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4636 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4637 | |
| 4638 | ArgType = Func->getType(); |
| 4639 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4640 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4641 | // A value of reference type is not an object. |
| 4642 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4643 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4644 | diag::err_template_arg_reference_var) |
| 4645 | << Var->getType() << Arg->getSourceRange(); |
| 4646 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4647 | return true; |
| 4648 | } |
| 4649 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4650 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 4651 | if (Var->getTLSKind()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4652 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 4653 | << Arg->getSourceRange(); |
| 4654 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 4655 | return true; |
| 4656 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4657 | |
| 4658 | // If the template parameter has pointer type, we must have taken |
| 4659 | // the address of this object. |
| 4660 | if (ParamType->isReferenceType()) { |
| 4661 | if (AddressTaken) { |
| 4662 | // If we originally had an address-of operator, but the |
| 4663 | // parameter has reference type, complain and (if things look |
| 4664 | // like they will work) drop the address-of operator. |
| 4665 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 4666 | ParamType.getNonReferenceType())) { |
| 4667 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4668 | << ParamType; |
| 4669 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4670 | return true; |
| 4671 | } |
| 4672 | |
| 4673 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4674 | << ParamType |
| 4675 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4676 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4677 | |
| 4678 | ArgType = Var->getType(); |
| 4679 | } |
| 4680 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 4681 | if (Var->getType()->isArrayType()) { |
| 4682 | // Array-to-pointer decay. |
| 4683 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 4684 | } else { |
| 4685 | // If the template parameter has pointer type but the address of |
| 4686 | // this object was not taken, complain and (possibly) recover by |
| 4687 | // taking the address of the entity. |
| 4688 | ArgType = S.Context.getPointerType(Var->getType()); |
| 4689 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 4690 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4691 | << ParamType; |
| 4692 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4693 | return true; |
| 4694 | } |
| 4695 | |
| 4696 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4697 | << ParamType |
| 4698 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 4699 | |
| 4700 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4701 | } |
| 4702 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4703 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4704 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4705 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 4706 | Arg, ArgType)) |
| 4707 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4708 | |
| 4709 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4710 | Converted = |
| 4711 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 4712 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4713 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4714 | } |
| 4715 | |
| 4716 | /// \brief Checks whether the given template argument is a pointer to |
| 4717 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4718 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 4719 | NonTypeTemplateParmDecl *Param, |
| 4720 | QualType ParamType, |
| 4721 | Expr *&ResultArg, |
| 4722 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4723 | bool Invalid = false; |
| 4724 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4725 | // Check for a null pointer value. |
| 4726 | Expr *Arg = ResultArg; |
| 4727 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 4728 | case NPV_Error: |
| 4729 | return true; |
| 4730 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 4731 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4732 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4733 | /*isNullPtr*/true); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4734 | return false; |
| 4735 | case NPV_NotNullPointer: |
| 4736 | break; |
| 4737 | } |
| 4738 | |
| 4739 | bool ObjCLifetimeConversion; |
| 4740 | if (S.IsQualificationConversion(Arg->getType(), |
| 4741 | ParamType.getNonReferenceType(), |
| 4742 | false, ObjCLifetimeConversion)) { |
| 4743 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4744 | Arg->getValueKind()).get(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4745 | ResultArg = Arg; |
| 4746 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), |
| 4747 | ParamType.getNonReferenceType())) { |
| 4748 | // We can't perform this conversion. |
| 4749 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4750 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4751 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4752 | return true; |
| 4753 | } |
| 4754 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4755 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4756 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4757 | Arg = Cast->getSubExpr(); |
| 4758 | |
| 4759 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4760 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4761 | // A template-argument for a non-type, non-template |
| 4762 | // template-parameter shall be one of: [...] |
| 4763 | // |
| 4764 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4765 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4766 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4767 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4768 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4769 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4770 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4771 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4772 | S.Diag(Arg->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4773 | S.getLangOpts().CPlusPlus11 ? |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4774 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 4775 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4776 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4777 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4778 | } |
| 4779 | |
| 4780 | Arg = Parens->getSubExpr(); |
| 4781 | } |
| 4782 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4783 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4784 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4785 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4786 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4787 | // A pointer-to-member constant written &Class::member. |
| 4788 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4789 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4790 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 4791 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4792 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4793 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4794 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4795 | // A constant of pointer-to-member type. |
| 4796 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 4797 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 4798 | if (VD->getType()->isMemberPointerType()) { |
David Majnemer | cd053cd | 2013-12-10 00:40:58 +0000 | [diff] [blame] | 4799 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4800 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4801 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4802 | } else { |
| 4803 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4804 | Converted = TemplateArgument(VD, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4805 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4806 | return Invalid; |
| 4807 | } |
| 4808 | } |
| 4809 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4810 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4811 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4812 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4813 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4814 | if (!DRE) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4815 | return S.Diag(Arg->getLocStart(), |
| 4816 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4817 | << Arg->getSourceRange(); |
| 4818 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4819 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 4820 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 4821 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4822 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4823 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4824 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 4825 | "Only non-static member pointers can make it here"); |
| 4826 | |
| 4827 | // Okay: this is the address of a non-static member, and therefore |
| 4828 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4829 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4830 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4831 | } else { |
| 4832 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4833 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4834 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4835 | return Invalid; |
| 4836 | } |
| 4837 | |
| 4838 | // 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] | 4839 | S.Diag(Arg->getLocStart(), |
| 4840 | diag::err_template_arg_not_pointer_to_member_form) |
| 4841 | << Arg->getSourceRange(); |
| 4842 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4843 | return true; |
| 4844 | } |
| 4845 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4846 | /// \brief Check a template argument against its corresponding |
| 4847 | /// non-type template parameter. |
| 4848 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4849 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4850 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4851 | /// returns the converted template argument. \p ParamType is the |
| 4852 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4853 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4854 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4855 | TemplateArgument &Converted, |
| 4856 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4857 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4858 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4859 | // If either the parameter has a dependent type or the argument is |
| 4860 | // type-dependent, there's nothing we can check now. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4861 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4862 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 4863 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4864 | return Arg; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4865 | } |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4866 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4867 | // We should have already dropped all cv-qualifiers by now. |
| 4868 | assert(!ParamType.hasQualifiers() && |
| 4869 | "non-type template parameter type cannot be qualified"); |
| 4870 | |
| 4871 | if (CTAK == CTAK_Deduced && |
| 4872 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 4873 | // C++ [temp.deduct.type]p17: |
| 4874 | // If, in the declaration of a function template with a non-type |
| 4875 | // template-parameter, the non-type template-parameter is used |
| 4876 | // in an expression in the function parameter-list and, if the |
| 4877 | // corresponding template-argument is deduced, the |
| 4878 | // template-argument type shall match the type of the |
| 4879 | // template-parameter exactly, except that a template-argument |
| 4880 | // deduced from an array bound may be of any integral type. |
| 4881 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 4882 | << Arg->getType().getUnqualifiedType() |
| 4883 | << ParamType.getUnqualifiedType(); |
| 4884 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4885 | return ExprError(); |
| 4886 | } |
| 4887 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4888 | if (getLangOpts().CPlusPlus1z) { |
| 4889 | // FIXME: We can do some limited checking for a value-dependent but not |
| 4890 | // type-dependent argument. |
| 4891 | if (Arg->isValueDependent()) { |
| 4892 | Converted = TemplateArgument(Arg); |
| 4893 | return Arg; |
| 4894 | } |
| 4895 | |
| 4896 | // C++1z [temp.arg.nontype]p1: |
| 4897 | // A template-argument for a non-type template parameter shall be |
| 4898 | // a converted constant expression of the type of the template-parameter. |
| 4899 | APValue Value; |
| 4900 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 4901 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 4902 | if (ArgResult.isInvalid()) |
| 4903 | return ExprError(); |
| 4904 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4905 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 4906 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4907 | // Convert the APValue to a TemplateArgument. |
| 4908 | switch (Value.getKind()) { |
| 4909 | case APValue::Uninitialized: |
| 4910 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4911 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4912 | break; |
| 4913 | case APValue::Int: |
| 4914 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4915 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4916 | break; |
| 4917 | case APValue::MemberPointer: { |
| 4918 | assert(ParamType->isMemberPointerType()); |
| 4919 | |
| 4920 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 4921 | if (!Value.getMemberPointerPath().empty()) { |
| 4922 | Diag(Arg->getLocStart(), |
| 4923 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 4924 | << Value.getMemberPointerDecl() << ParamType |
| 4925 | << Arg->getSourceRange(); |
| 4926 | return ExprError(); |
| 4927 | } |
| 4928 | |
| 4929 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4930 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 4931 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4932 | break; |
| 4933 | } |
| 4934 | case APValue::LValue: { |
| 4935 | // For a non-type template-parameter of pointer or reference type, |
| 4936 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4937 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 4938 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4939 | // -- a temporary object |
| 4940 | // -- a string literal |
| 4941 | // -- the result of a typeid expression, or |
| 4942 | // -- a predefind __func__ variable |
| 4943 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 4944 | if (isa<CXXUuidofExpr>(E)) { |
| 4945 | Converted = TemplateArgument(const_cast<Expr*>(E)); |
| 4946 | break; |
| 4947 | } |
| 4948 | Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4949 | << Arg->getSourceRange(); |
| 4950 | return ExprError(); |
| 4951 | } |
| 4952 | auto *VD = const_cast<ValueDecl *>( |
| 4953 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 4954 | // -- a subobject |
| 4955 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 4956 | VD && VD->getType()->isArrayType() && |
| 4957 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 4958 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 4959 | // Per defect report (no number yet): |
| 4960 | // ... other than a pointer to the first element of a complete array |
| 4961 | // object. |
| 4962 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 4963 | Value.isLValueOnePastTheEnd()) { |
| 4964 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 4965 | << Value.getAsString(Context, ParamType); |
| 4966 | return ExprError(); |
| 4967 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4968 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4969 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4970 | assert((!VD || !ParamType->isNullPtrType()) && |
| 4971 | "non-null value of type nullptr_t?"); |
| 4972 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 4973 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4974 | break; |
| 4975 | } |
| 4976 | case APValue::AddrLabelDiff: |
| 4977 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
| 4978 | case APValue::Float: |
| 4979 | case APValue::ComplexInt: |
| 4980 | case APValue::ComplexFloat: |
| 4981 | case APValue::Vector: |
| 4982 | case APValue::Array: |
| 4983 | case APValue::Struct: |
| 4984 | case APValue::Union: |
| 4985 | llvm_unreachable("invalid kind for template argument"); |
| 4986 | } |
| 4987 | |
| 4988 | return ArgResult.get(); |
| 4989 | } |
| 4990 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4991 | // C++ [temp.arg.nontype]p5: |
| 4992 | // The following conversions are performed on each expression used |
| 4993 | // as a non-type template-argument. If a non-type |
| 4994 | // template-argument cannot be converted to the type of the |
| 4995 | // corresponding template-parameter then the program is |
| 4996 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4997 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4998 | // C++11: |
| 4999 | // -- for a non-type template-parameter of integral or |
| 5000 | // enumeration type, conversions permitted in a converted |
| 5001 | // constant expression are applied. |
| 5002 | // |
| 5003 | // C++98: |
| 5004 | // -- for a non-type template-parameter of integral or |
| 5005 | // enumeration type, integral promotions (4.5) and integral |
| 5006 | // conversions (4.7) are applied. |
| 5007 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5008 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5009 | // We can't check arbitrary value-dependent arguments. |
| 5010 | // FIXME: If there's no viable conversion to the template parameter type, |
| 5011 | // we should be able to diagnose that prior to instantiation. |
| 5012 | if (Arg->isValueDependent()) { |
| 5013 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5014 | return Arg; |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5015 | } |
| 5016 | |
| 5017 | // C++ [temp.arg.nontype]p1: |
| 5018 | // A template-argument for a non-type, non-template template-parameter |
| 5019 | // shall be one of: |
| 5020 | // |
| 5021 | // -- for a non-type template-parameter of integral or enumeration |
| 5022 | // type, a converted constant expression of the type of the |
| 5023 | // template-parameter; or |
| 5024 | llvm::APSInt Value; |
| 5025 | ExprResult ArgResult = |
| 5026 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 5027 | CCEK_TemplateArg); |
| 5028 | if (ArgResult.isInvalid()) |
| 5029 | return ExprError(); |
| 5030 | |
| 5031 | // Widen the argument value to sizeof(parameter type). This is almost |
| 5032 | // always a no-op, except when the parameter type is bool. In |
| 5033 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 5034 | QualType IntegerType = ParamType; |
| 5035 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 5036 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 5037 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 5038 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5039 | Converted = TemplateArgument(Context, Value, |
| 5040 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 5041 | return ArgResult; |
| 5042 | } |
| 5043 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5044 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 5045 | if (ArgResult.isInvalid()) |
| 5046 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5047 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5048 | |
| 5049 | QualType ArgType = Arg->getType(); |
| 5050 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5051 | // C++ [temp.arg.nontype]p1: |
| 5052 | // A template-argument for a non-type, non-template |
| 5053 | // template-parameter shall be one of: |
| 5054 | // |
| 5055 | // -- an integral constant-expression of integral or enumeration |
| 5056 | // type; or |
| 5057 | // -- the name of a non-type template-parameter; or |
| 5058 | SourceLocation NonConstantLoc; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5059 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5060 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5061 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5062 | diag::err_template_arg_not_integral_or_enumeral) |
| 5063 | << ArgType << Arg->getSourceRange(); |
| 5064 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5065 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5066 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5067 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 5068 | QualType T; |
| 5069 | |
| 5070 | public: |
| 5071 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 5072 | |
| 5073 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 5074 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 5075 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 5076 | } |
| 5077 | } Diagnoser(ArgType); |
| 5078 | |
| 5079 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5080 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5081 | if (!Arg) |
| 5082 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5085 | // From here on out, all we care about is the unqualified form |
| 5086 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5087 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5088 | |
| 5089 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 5090 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5091 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5092 | } else if (ParamType->isBooleanType()) { |
| 5093 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5094 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5095 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 5096 | !ParamType->isEnumeralType()) { |
| 5097 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5098 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5099 | } else { |
| 5100 | // We can't perform this conversion. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5101 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5102 | diag::err_template_arg_not_convertible) |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5103 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5104 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5105 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5106 | } |
| 5107 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5108 | // Add the value of this argument to the list of converted |
| 5109 | // arguments. We use the bitwidth and signedness of the template |
| 5110 | // parameter. |
| 5111 | if (Arg->isValueDependent()) { |
| 5112 | // The argument is value-dependent. Create a new |
| 5113 | // TemplateArgument with the converted expression. |
| 5114 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5115 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5116 | } |
| 5117 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5118 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5119 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 5120 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5121 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5122 | if (ParamType->isBooleanType()) { |
| 5123 | // Value must be zero or one. |
| 5124 | Value = Value != 0; |
| 5125 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 5126 | if (Value.getBitWidth() != AllowedBits) |
| 5127 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5128 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5129 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5130 | llvm::APSInt OldValue = Value; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5131 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5132 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5133 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5134 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5135 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5136 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5137 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5138 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5139 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5140 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5141 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5142 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5143 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5144 | << Arg->getSourceRange(); |
| 5145 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5146 | } |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5147 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5148 | // Complain if we overflowed the template parameter's type. |
| 5149 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5150 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5151 | RequiredBits = OldValue.getActiveBits(); |
| 5152 | else if (OldValue.isUnsigned()) |
| 5153 | RequiredBits = OldValue.getActiveBits() + 1; |
| 5154 | else |
| 5155 | RequiredBits = OldValue.getMinSignedBits(); |
| 5156 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5157 | Diag(Arg->getLocStart(), |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5158 | diag::warn_template_arg_too_large) |
| 5159 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5160 | << Arg->getSourceRange(); |
| 5161 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5162 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5163 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5164 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5165 | Converted = TemplateArgument(Context, Value, |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 5166 | ParamType->isEnumeralType() |
| 5167 | ? Context.getCanonicalType(ParamType) |
| 5168 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5169 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5170 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5171 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5172 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5173 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 5174 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5175 | // Handle pointer-to-function, reference-to-function, and |
| 5176 | // pointer-to-member-function all in (roughly) the same way. |
| 5177 | if (// -- For a non-type template-parameter of type pointer to |
| 5178 | // function, only the function-to-pointer conversion (4.3) is |
| 5179 | // applied. If the template-argument represents a set of |
| 5180 | // overloaded functions (or a pointer to such), the matching |
| 5181 | // function is selected from the set (13.4). |
| 5182 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5183 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5184 | // -- For a non-type template-parameter of type reference to |
| 5185 | // function, no conversions apply. If the template-argument |
| 5186 | // represents a set of overloaded functions, the matching |
| 5187 | // function is selected from the set (13.4). |
| 5188 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5189 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5190 | // -- For a non-type template-parameter of type pointer to |
| 5191 | // member function, no conversions apply. If the |
| 5192 | // template-argument represents a set of overloaded member |
| 5193 | // functions, the matching member function is selected from |
| 5194 | // the set (13.4). |
| 5195 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5196 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5197 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5198 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5199 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5200 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5201 | true, |
| 5202 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5203 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5204 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5205 | |
| 5206 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5207 | ArgType = Arg->getType(); |
| 5208 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5209 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5210 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5211 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5212 | if (!ParamType->isMemberPointerType()) { |
| 5213 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5214 | ParamType, |
| 5215 | Arg, Converted)) |
| 5216 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5217 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5218 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5219 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5220 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5221 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5222 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5223 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5224 | } |
| 5225 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 5226 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5227 | // -- for a non-type template-parameter of type pointer to |
| 5228 | // object, qualification conversions (4.4) and the |
| 5229 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5230 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5231 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5232 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5233 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5234 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5235 | ParamType, |
| 5236 | Arg, Converted)) |
| 5237 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5238 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5239 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5240 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5241 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5242 | // -- For a non-type template-parameter of type reference to |
| 5243 | // object, no conversions apply. The type referred to by the |
| 5244 | // reference may be more cv-qualified than the (otherwise |
| 5245 | // identical) type of the template-argument. The |
| 5246 | // template-parameter is bound directly to the |
| 5247 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5248 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5249 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5250 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5251 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5252 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 5253 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5254 | true, |
| 5255 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5256 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5257 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5258 | |
| 5259 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5260 | ArgType = Arg->getType(); |
| 5261 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5262 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5263 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5264 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5265 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5266 | ParamType, |
| 5267 | Arg, Converted)) |
| 5268 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5269 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5270 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5271 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5272 | // Deal with parameters of type std::nullptr_t. |
| 5273 | if (ParamType->isNullPtrType()) { |
| 5274 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 5275 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5276 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5277 | } |
| 5278 | |
| 5279 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 5280 | case NPV_NotNullPointer: |
| 5281 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 5282 | << Arg->getType() << ParamType; |
| 5283 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5284 | return ExprError(); |
| 5285 | |
| 5286 | case NPV_Error: |
| 5287 | return ExprError(); |
| 5288 | |
| 5289 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 5290 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5291 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 5292 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5293 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5294 | } |
| 5295 | } |
| 5296 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5297 | // -- For a non-type template-parameter of type pointer to data |
| 5298 | // member, qualification conversions (4.4) are applied. |
| 5299 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 5300 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5301 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5302 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5303 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5304 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5305 | } |
| 5306 | |
| 5307 | /// \brief Check a template argument against its corresponding |
| 5308 | /// template template parameter. |
| 5309 | /// |
| 5310 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 5311 | /// It returns true if an error occurred, and false otherwise. |
| 5312 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 5313 | TemplateArgumentLoc &Arg, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5314 | unsigned ArgumentPackIndex) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5315 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5316 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 5317 | if (!Template) { |
| 5318 | // Any dependent template name is fine. |
| 5319 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 5320 | return false; |
| 5321 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5322 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5323 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5324 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5325 | // the name of a class template or an alias template, expressed as an |
| 5326 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5327 | // primary class templates are considered when matching the |
| 5328 | // template template argument with the corresponding parameter; |
| 5329 | // partial specializations are not considered even if their |
| 5330 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5331 | // |
| 5332 | // Note that we also allow template template parameters here, which |
| 5333 | // will happen when we are dealing with, e.g., class template |
| 5334 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5335 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5336 | !isa<TemplateTemplateParmDecl>(Template) && |
| 5337 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5338 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5339 | "Only function templates are possible here"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5340 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5341 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5342 | << Template; |
| 5343 | } |
| 5344 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5345 | TemplateParameterList *Params = Param->getTemplateParameters(); |
| 5346 | if (Param->isExpandedParameterPack()) |
| 5347 | Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); |
| 5348 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5349 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5350 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5351 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5352 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5353 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5354 | } |
| 5355 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5356 | /// \brief Given a non-type template argument that refers to a |
| 5357 | /// declaration and the type of its corresponding non-type template |
| 5358 | /// parameter, produce an expression that properly refers to that |
| 5359 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5360 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5361 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 5362 | QualType ParamType, |
| 5363 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 5364 | // C++ [temp.param]p8: |
| 5365 | // |
| 5366 | // A non-type template-parameter of type "array of T" or |
| 5367 | // "function returning T" is adjusted to be of type "pointer to |
| 5368 | // T" or "pointer to function returning T", respectively. |
| 5369 | if (ParamType->isArrayType()) |
| 5370 | ParamType = Context.getArrayDecayedType(ParamType); |
| 5371 | else if (ParamType->isFunctionType()) |
| 5372 | ParamType = Context.getPointerType(ParamType); |
| 5373 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5374 | // For a NULL non-type template argument, return nullptr casted to the |
| 5375 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5376 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5377 | return ImpCastExprToType( |
| 5378 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 5379 | ParamType, |
| 5380 | ParamType->getAs<MemberPointerType>() |
| 5381 | ? CK_NullToMemberPointer |
| 5382 | : CK_NullToPointer); |
| 5383 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5384 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 5385 | "Only declaration template arguments permitted here"); |
| 5386 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5387 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 5388 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5389 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 5390 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 5391 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5392 | // If the value is a class member, we might have a pointer-to-member. |
| 5393 | // Determine whether the non-type template template parameter is of |
| 5394 | // pointer-to-member type. If so, we need to build an appropriate |
| 5395 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 5396 | // would refer to the member itself. |
| 5397 | if (ParamType->isMemberPointerType()) { |
| 5398 | QualType ClassType |
| 5399 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 5400 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5401 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5402 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5403 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 5404 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5405 | |
| 5406 | // The actual value-ness of this is unimportant, but for |
| 5407 | // internal consistency's sake, references to instance methods |
| 5408 | // are r-values. |
| 5409 | ExprValueKind VK = VK_LValue; |
| 5410 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 5411 | VK = VK_RValue; |
| 5412 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5413 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5414 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5415 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5416 | Loc, |
| 5417 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5418 | if (RefExpr.isInvalid()) |
| 5419 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5420 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5421 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5422 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5423 | // We might need to perform a trailing qualification conversion, since |
| 5424 | // the element type on the parameter could be more qualified than the |
| 5425 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5426 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5427 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5428 | ParamType.getUnqualifiedType(), false, |
| 5429 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5430 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5431 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5432 | assert(!RefExpr.isInvalid() && |
| 5433 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5434 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5435 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5436 | } |
| 5437 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5438 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5439 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5440 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5441 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5442 | // When the non-type template parameter is a pointer, take the |
| 5443 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5444 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5445 | if (RefExpr.isInvalid()) |
| 5446 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5447 | |
| 5448 | if (T->isFunctionType() || T->isArrayType()) { |
| 5449 | // Decay functions and arrays. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5450 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5451 | if (RefExpr.isInvalid()) |
| 5452 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5453 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5454 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5455 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5456 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5457 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5458 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5459 | } |
| 5460 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5461 | ExprValueKind VK = VK_RValue; |
| 5462 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5463 | // If the non-type template parameter has reference type, qualify the |
| 5464 | // resulting declaration reference with the extra qualifiers on the |
| 5465 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5466 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 5467 | VK = VK_LValue; |
| 5468 | T = Context.getQualifiedType(T, |
| 5469 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5470 | } else if (isa<FunctionDecl>(VD)) { |
| 5471 | // References to functions are always lvalues. |
| 5472 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5473 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5474 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5475 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5476 | } |
| 5477 | |
| 5478 | /// \brief Construct a new expression that refers to the given |
| 5479 | /// integral template argument with the given source-location |
| 5480 | /// information. |
| 5481 | /// |
| 5482 | /// This routine takes care of the mapping from an integral template |
| 5483 | /// argument (which may have any integral type) to the appropriate |
| 5484 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5485 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5486 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 5487 | SourceLocation Loc) { |
| 5488 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 5489 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5490 | QualType OrigT = Arg.getIntegralType(); |
| 5491 | |
| 5492 | // If this is an enum type that we're instantiating, we need to use an integer |
| 5493 | // type the same size as the enumerator. We don't want to build an |
| 5494 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 5495 | // any integral type with C++11 enum classes, make sure we create the right |
| 5496 | // type of literal for it. |
| 5497 | QualType T = OrigT; |
| 5498 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 5499 | T = ET->getDecl()->getIntegerType(); |
| 5500 | |
| 5501 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5502 | if (T->isAnyCharacterType()) { |
| 5503 | CharacterLiteral::CharacterKind Kind; |
| 5504 | if (T->isWideCharType()) |
| 5505 | Kind = CharacterLiteral::Wide; |
| 5506 | else if (T->isChar16Type()) |
| 5507 | Kind = CharacterLiteral::UTF16; |
| 5508 | else if (T->isChar32Type()) |
| 5509 | Kind = CharacterLiteral::UTF32; |
| 5510 | else |
| 5511 | Kind = CharacterLiteral::Ascii; |
| 5512 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5513 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 5514 | Kind, T, Loc); |
| 5515 | } else if (T->isBooleanType()) { |
| 5516 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 5517 | T, Loc); |
| 5518 | } else if (T->isNullPtrType()) { |
| 5519 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 5520 | } else { |
| 5521 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5522 | } |
| 5523 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5524 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5525 | // FIXME: This is a hack. We need a better way to handle substituted |
| 5526 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5527 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 5528 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5529 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5530 | Loc, Loc); |
| 5531 | } |
| 5532 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5533 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5534 | } |
| 5535 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5536 | /// \brief Match two template parameters within template parameter lists. |
| 5537 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 5538 | bool Complain, |
| 5539 | Sema::TemplateParameterListEqualKind Kind, |
| 5540 | SourceLocation TemplateArgLoc) { |
| 5541 | // Check the actual kind (type, non-type, template). |
| 5542 | if (Old->getKind() != New->getKind()) { |
| 5543 | if (Complain) { |
| 5544 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 5545 | if (TemplateArgLoc.isValid()) { |
| 5546 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5547 | NextDiag = diag::note_template_param_different_kind; |
| 5548 | } |
| 5549 | S.Diag(New->getLocation(), NextDiag) |
| 5550 | << (Kind != Sema::TPL_TemplateMatch); |
| 5551 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 5552 | << (Kind != Sema::TPL_TemplateMatch); |
| 5553 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5554 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5555 | return false; |
| 5556 | } |
| 5557 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5558 | // Check that both are parameter packs are neither are parameter packs. |
| 5559 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5560 | // template template parameter, the template template parameter can have |
| 5561 | // a parameter pack where the template template argument does not. |
| 5562 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 5563 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5564 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5565 | if (Complain) { |
| 5566 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 5567 | if (TemplateArgLoc.isValid()) { |
| 5568 | S.Diag(TemplateArgLoc, |
| 5569 | diag::err_template_arg_template_params_mismatch); |
| 5570 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 5571 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5572 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5573 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 5574 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 5575 | : 2; |
| 5576 | S.Diag(New->getLocation(), NextDiag) |
| 5577 | << ParamKind << New->isParameterPack(); |
| 5578 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 5579 | << ParamKind << Old->isParameterPack(); |
| 5580 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5581 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5582 | return false; |
| 5583 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5584 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5585 | // For non-type template parameters, check the type of the parameter. |
| 5586 | if (NonTypeTemplateParmDecl *OldNTTP |
| 5587 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 5588 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
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 | // If we are matching a template template argument to a template |
| 5591 | // template parameter and one of the non-type template parameter types |
| 5592 | // is dependent, then we must wait until template instantiation time |
| 5593 | // to actually compare the arguments. |
| 5594 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5595 | (OldNTTP->getType()->isDependentType() || |
| 5596 | NewNTTP->getType()->isDependentType())) |
| 5597 | return true; |
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 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 5600 | if (Complain) { |
| 5601 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 5602 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5603 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5604 | diag::err_template_arg_template_params_mismatch); |
| 5605 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 5606 | } |
| 5607 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 5608 | << NewNTTP->getType() |
| 5609 | << (Kind != Sema::TPL_TemplateMatch); |
| 5610 | S.Diag(OldNTTP->getLocation(), |
| 5611 | diag::note_template_nontype_parm_prev_declaration) |
| 5612 | << OldNTTP->getType(); |
| 5613 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5614 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5615 | return false; |
| 5616 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5617 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5618 | return true; |
| 5619 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5620 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5621 | // For template template parameters, check the template parameter types. |
| 5622 | // The template parameter lists of template template |
| 5623 | // parameters must agree. |
| 5624 | if (TemplateTemplateParmDecl *OldTTP |
| 5625 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5626 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5627 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 5628 | OldTTP->getTemplateParameters(), |
| 5629 | Complain, |
| 5630 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5631 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5632 | : Kind), |
| 5633 | TemplateArgLoc); |
| 5634 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5635 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5636 | return true; |
| 5637 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5638 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5639 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 5640 | /// lists. |
| 5641 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5642 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5643 | TemplateParameterList *New, |
| 5644 | TemplateParameterList *Old, |
| 5645 | Sema::TemplateParameterListEqualKind Kind, |
| 5646 | SourceLocation TemplateArgLoc) { |
| 5647 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 5648 | if (TemplateArgLoc.isValid()) { |
| 5649 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5650 | NextDiag = diag::note_template_param_list_different_arity; |
| 5651 | } |
| 5652 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 5653 | << (New->size() > Old->size()) |
| 5654 | << (Kind != Sema::TPL_TemplateMatch) |
| 5655 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 5656 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 5657 | << (Kind != Sema::TPL_TemplateMatch) |
| 5658 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 5659 | } |
| 5660 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5661 | /// \brief Determine whether the given template parameter lists are |
| 5662 | /// equivalent. |
| 5663 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5664 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5665 | /// source code as part of a new template declaration. |
| 5666 | /// |
| 5667 | /// \param Old The old template parameter list, typically found via |
| 5668 | /// name lookup of the template declared with this template parameter |
| 5669 | /// list. |
| 5670 | /// |
| 5671 | /// \param Complain If true, this routine will produce a diagnostic if |
| 5672 | /// the template parameter lists are not equivalent. |
| 5673 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5674 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5675 | /// |
| 5676 | /// \param TemplateArgLoc If this source location is valid, then we |
| 5677 | /// are actually checking the template parameter list of a template |
| 5678 | /// argument (New) against the template parameter list of its |
| 5679 | /// corresponding template template parameter (Old). We produce |
| 5680 | /// slightly different diagnostics in this scenario. |
| 5681 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5682 | /// \returns True if the template parameter lists are equal, false |
| 5683 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5684 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5685 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 5686 | TemplateParameterList *Old, |
| 5687 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5688 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5689 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5690 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 5691 | if (Complain) |
| 5692 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5693 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5694 | |
| 5695 | return false; |
| 5696 | } |
| 5697 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5698 | // C++0x [temp.arg.template]p3: |
| 5699 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5700 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5701 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5702 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5703 | // template-parameter-list of P. [...] |
| 5704 | TemplateParameterList::iterator NewParm = New->begin(); |
| 5705 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5706 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5707 | OldParmEnd = Old->end(); |
| 5708 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 5709 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 5710 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5711 | if (NewParm == NewParmEnd) { |
| 5712 | if (Complain) |
| 5713 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5714 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5715 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5716 | return false; |
| 5717 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5718 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5719 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5720 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5721 | return false; |
| 5722 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5723 | ++NewParm; |
| 5724 | continue; |
| 5725 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5726 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5727 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5728 | // [...] When P's template- parameter-list contains a template parameter |
| 5729 | // pack (14.5.3), the template parameter pack will match zero or more |
| 5730 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5731 | // template-parameter-list of A with the same type and form as the |
| 5732 | // template parameter pack in P (ignoring whether those template |
| 5733 | // parameters are template parameter packs). |
| 5734 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 5735 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5736 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5737 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5738 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5739 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5740 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5741 | // Make sure we exhausted all of the arguments. |
| 5742 | if (NewParm != NewParmEnd) { |
| 5743 | if (Complain) |
| 5744 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5745 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5746 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5747 | return false; |
| 5748 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5749 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5750 | return true; |
| 5751 | } |
| 5752 | |
| 5753 | /// \brief Check whether a template can be declared within this scope. |
| 5754 | /// |
| 5755 | /// If the template declaration is valid in this scope, returns |
| 5756 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5757 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5758 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 5759 | if (!S) |
| 5760 | return false; |
| 5761 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5762 | // Find the nearest enclosing declaration scope. |
| 5763 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5764 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5765 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5766 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5767 | // C++ [temp]p4: |
| 5768 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 5769 | DeclContext *Ctx = S->getEntity(); |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5770 | if (Ctx && Ctx->isExternCContext()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5771 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5772 | << TemplateParams->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5773 | |
Eli Friedman | dfbd0c4 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 5774 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5775 | Ctx = Ctx->getParent(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5776 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5777 | // C++ [temp]p2: |
| 5778 | // A template-declaration can appear only as a namespace scope or |
| 5779 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 5780 | if (Ctx) { |
| 5781 | if (Ctx->isFileContext()) |
| 5782 | return false; |
| 5783 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 5784 | // C++ [temp.mem]p2: |
| 5785 | // A local class shall not have member templates. |
| 5786 | if (RD->isLocalClass()) |
| 5787 | return Diag(TemplateParams->getTemplateLoc(), |
| 5788 | diag::err_template_inside_local_class) |
| 5789 | << TemplateParams->getSourceRange(); |
| 5790 | else |
| 5791 | return false; |
| 5792 | } |
| 5793 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5794 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5795 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5796 | diag::err_template_outside_namespace_or_class_scope) |
| 5797 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5798 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5799 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5800 | /// \brief Determine what kind of template specialization the given declaration |
| 5801 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5802 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5803 | if (!D) |
| 5804 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5805 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5806 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 5807 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5808 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 5809 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5810 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 5811 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5812 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5813 | return TSK_Undeclared; |
| 5814 | } |
| 5815 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5816 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5817 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5818 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5819 | /// This routine determines whether a template specialization can be declared |
| 5820 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5821 | /// |
| 5822 | /// \param S the semantic analysis object for which this check is being |
| 5823 | /// performed. |
| 5824 | /// |
| 5825 | /// \param Specialized the entity being specialized or instantiated, which |
| 5826 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5827 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5828 | /// member class). |
| 5829 | /// |
| 5830 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 5831 | /// |
| 5832 | /// \param Loc the location of the explicit specialization or instantiation of |
| 5833 | /// this entity. |
| 5834 | /// |
| 5835 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 5836 | /// a class template. |
| 5837 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5838 | /// \returns true if there was an error that we cannot recover from, false |
| 5839 | /// otherwise. |
| 5840 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 5841 | NamedDecl *Specialized, |
| 5842 | NamedDecl *PrevDecl, |
| 5843 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5844 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5845 | // Keep these "kind" numbers in sync with the %select statements in the |
| 5846 | // various diagnostics emitted by this routine. |
| 5847 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5848 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5849 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5850 | else if (isa<VarTemplateDecl>(Specialized)) |
| 5851 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5852 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5853 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5854 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5855 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5856 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5857 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5858 | else if (isa<RecordDecl>(Specialized)) |
| 5859 | EntityKind = 7; |
| 5860 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 5861 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5862 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5863 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5864 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5865 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5866 | return true; |
| 5867 | } |
| 5868 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5869 | // C++ [temp.expl.spec]p2: |
| 5870 | // An explicit specialization shall be declared in the namespace |
| 5871 | // of which the template is a member, or, for member templates, in |
| 5872 | // the namespace of which the enclosing class or enclosing class |
| 5873 | // template is a member. An explicit specialization of a member |
| 5874 | // function, member class or static data member of a class |
| 5875 | // template shall be declared in the namespace of which the class |
| 5876 | // template is a member. Such a declaration may also be a |
| 5877 | // definition. If the declaration is not a definition, the |
| 5878 | // specialization may be defined later in the name- space in which |
| 5879 | // the explicit specialization was declared, or in a namespace |
| 5880 | // that encloses the one in which the explicit specialization was |
| 5881 | // declared. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5882 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5883 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5884 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5885 | return true; |
| 5886 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5887 | |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5888 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5889 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 5890 | // Do not warn for class scope explicit specialization during |
| 5891 | // instantiation, warning was already emitted during pattern |
| 5892 | // semantic analysis. |
| 5893 | if (!S.ActiveTemplateInstantiations.size()) |
| 5894 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 5895 | << Specialized; |
| 5896 | } else { |
| 5897 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5898 | << Specialized; |
| 5899 | return true; |
| 5900 | } |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5901 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5902 | |
Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 5903 | if (S.CurContext->isRecord() && |
| 5904 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 5905 | // Make sure that we're specializing in the right record context. |
| 5906 | // Otherwise, things can go horribly wrong. |
| 5907 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5908 | << Specialized; |
| 5909 | return true; |
| 5910 | } |
| 5911 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5912 | // C++ [temp.class.spec]p6: |
| 5913 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5914 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 5915 | // and 14.5.2). |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5916 | DeclContext *SpecializedContext |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5917 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5918 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5919 | |
| 5920 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
| 5921 | // namespace. |
| 5922 | // Note that HandleDeclarator() performs this check for explicit |
| 5923 | // specializations of function templates, static data members, and member |
| 5924 | // functions, so we skip the check here for those kinds of entities. |
| 5925 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
| 5926 | // Should we refactor that check, so that it occurs later? |
| 5927 | if (!DC->Encloses(SpecializedContext) && |
| 5928 | !(isa<FunctionTemplateDecl>(Specialized) || |
| 5929 | isa<FunctionDecl>(Specialized) || |
| 5930 | isa<VarTemplateDecl>(Specialized) || |
| 5931 | isa<VarDecl>(Specialized))) { |
| 5932 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 5933 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 5934 | << EntityKind << Specialized; |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 5935 | else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 5936 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
| 5937 | if (S.getLangOpts().MicrosoftExt) |
| 5938 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 5939 | S.Diag(Loc, Diag) << EntityKind << Specialized |
| 5940 | << cast<NamedDecl>(SpecializedContext); |
| 5941 | } else |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5942 | llvm_unreachable("unexpected namespace context for specialization"); |
| 5943 | |
| 5944 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 5945 | } else if ((!PrevDecl || |
| 5946 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 5947 | getTemplateSpecializationKind(PrevDecl) == |
| 5948 | TSK_ImplicitInstantiation)) { |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5949 | // C++ [temp.exp.spec]p2: |
| 5950 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5951 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5952 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5953 | // An explicit specialization of a member function, member class or |
| 5954 | // static data member of a class template shall be declared in the |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5955 | // namespace of which the class template is a member. |
| 5956 | // |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5957 | // C++11 [temp.expl.spec]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5958 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5959 | // the specialized template. |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5960 | // C++11 [temp.explicit]p3: |
| 5961 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 5962 | // template. |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5963 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5964 | bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext); |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5965 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5966 | assert(!IsCPlusPlus11Extension && |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5967 | "DC encloses TU but isn't in enclosing namespace set"); |
| 5968 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | 8ce6315 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 5969 | << EntityKind << Specialized; |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5970 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 5971 | int Diag; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5972 | if (!IsCPlusPlus11Extension) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5973 | Diag = diag::err_template_spec_decl_out_of_scope; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5974 | else if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5975 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 5976 | else |
| 5977 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 5978 | S.Diag(Loc, Diag) |
| 5979 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 5980 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5981 | |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5982 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5983 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5984 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5985 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5986 | return false; |
| 5987 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5988 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5989 | static SourceRange findTemplateParameter(unsigned Depth, Expr *E) { |
| 5990 | if (!E->isInstantiationDependent()) |
| 5991 | return SourceLocation(); |
| 5992 | DependencyChecker Checker(Depth); |
| 5993 | Checker.TraverseStmt(E); |
| 5994 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 5995 | return E->getSourceRange(); |
| 5996 | return Checker.MatchLoc; |
| 5997 | } |
| 5998 | |
| 5999 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 6000 | if (!TL.getType()->isDependentType()) |
| 6001 | return SourceLocation(); |
| 6002 | DependencyChecker Checker(Depth); |
| 6003 | Checker.TraverseTypeLoc(TL); |
| 6004 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 6005 | return TL.getSourceRange(); |
| 6006 | return Checker.MatchLoc; |
| 6007 | } |
| 6008 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6009 | /// \brief Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6010 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6011 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6012 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 6013 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6014 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 6015 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6016 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6017 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 6018 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6019 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6020 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6021 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6022 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6023 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6024 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6025 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 6026 | |
| 6027 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6028 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 6029 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6030 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 6031 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6032 | |
| 6033 | // Strip off any implicit casts we added as part of type checking. |
| 6034 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 6035 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6036 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6037 | // C++ [temp.class.spec]p8: |
| 6038 | // A non-type argument is non-specialized if it is the name of a |
| 6039 | // non-type parameter. All other non-type arguments are |
| 6040 | // specialized. |
| 6041 | // |
| 6042 | // Below, we check the two conditions that only apply to |
| 6043 | // specialized non-type arguments, so skip any non-specialized |
| 6044 | // arguments. |
| 6045 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 6046 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6047 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6048 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6049 | // C++ [temp.class.spec]p9: |
| 6050 | // Within the argument list of a class template partial |
| 6051 | // specialization, the following restrictions apply: |
| 6052 | // -- A partially specialized non-type argument expression |
| 6053 | // shall not involve a template parameter of the partial |
| 6054 | // specialization except when the argument expression is a |
| 6055 | // simple identifier. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6056 | SourceRange ParamUseRange = |
| 6057 | findTemplateParameter(Param->getDepth(), ArgExpr); |
| 6058 | if (ParamUseRange.isValid()) { |
| 6059 | if (IsDefaultArgument) { |
| 6060 | S.Diag(TemplateNameLoc, |
| 6061 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 6062 | S.Diag(ParamUseRange.getBegin(), |
| 6063 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 6064 | << ParamUseRange; |
| 6065 | } else { |
| 6066 | S.Diag(ParamUseRange.getBegin(), |
| 6067 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 6068 | << ParamUseRange; |
| 6069 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6070 | return true; |
| 6071 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6072 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6073 | // -- The type of a template parameter corresponding to a |
| 6074 | // specialized non-type argument shall not be dependent on a |
| 6075 | // parameter of the specialization. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6076 | // |
| 6077 | // FIXME: We need to delay this check until instantiation in some cases: |
| 6078 | // |
| 6079 | // template<template<typename> class X> struct A { |
| 6080 | // template<typename T, X<T> N> struct B; |
| 6081 | // template<typename T> struct B<T, 0>; |
| 6082 | // }; |
| 6083 | // template<typename> using X = int; |
| 6084 | // A<X>::B<int, 0> b; |
| 6085 | ParamUseRange = findTemplateParameter( |
| 6086 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
| 6087 | if (ParamUseRange.isValid()) { |
| 6088 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getLocStart(), |
| 6089 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 6090 | << Param->getType() << ParamUseRange; |
| 6091 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
| 6092 | << (IsDefaultArgument ? ParamUseRange : SourceRange()); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6093 | return true; |
| 6094 | } |
| 6095 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6096 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6097 | return false; |
| 6098 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6099 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6100 | /// \brief Check the non-type template arguments of a class template |
| 6101 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 6102 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6103 | /// \param TemplateNameLoc the location of the template name. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6104 | /// \param TemplateParams the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6105 | /// template. |
| 6106 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6107 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6108 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6109 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6110 | /// \returns \c true if there was an error, \c false otherwise. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6111 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6112 | Sema &S, SourceLocation TemplateNameLoc, |
| 6113 | TemplateParameterList *TemplateParams, unsigned NumExplicit, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6114 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6115 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 6116 | |
| 6117 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6118 | NonTypeTemplateParmDecl *Param |
| 6119 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 6120 | if (!Param) |
| 6121 | continue; |
| 6122 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6123 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
| 6124 | S, TemplateNameLoc, Param, &ArgList[I], 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6125 | return true; |
| 6126 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6127 | |
| 6128 | return false; |
| 6129 | } |
| 6130 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6131 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6132 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 6133 | TagUseKind TUK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6134 | SourceLocation KWLoc, |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6135 | SourceLocation ModulePrivateLoc, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6136 | TemplateIdAnnotation &TemplateId, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6137 | AttributeList *Attr, |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6138 | MultiTemplateParamsArg |
| 6139 | TemplateParameterLists, |
| 6140 | SkipBodyInfo *SkipBody) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6141 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 6142 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6143 | CXXScopeSpec &SS = TemplateId.SS; |
| 6144 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6145 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 6146 | // store the location of the outermost template keyword in the declaration. |
| 6147 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6148 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 6149 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 6150 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 6151 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6152 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6153 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6154 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6155 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6156 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 6157 | |
| 6158 | if (!ClassTemplate) { |
| 6159 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6160 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6161 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 6162 | return true; |
| 6163 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6164 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6165 | bool isExplicitSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6166 | bool isPartialSpecialization = false; |
| 6167 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6168 | // Check the validity of the template headers that introduce this |
| 6169 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6170 | // FIXME: We probably shouldn't complain about these headers for |
| 6171 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6172 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 6173 | TemplateParameterList *TemplateParams = |
| 6174 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6175 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
| 6176 | TemplateParameterLists, TUK == TUK_Friend, isExplicitSpecialization, |
| 6177 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6178 | if (Invalid) |
| 6179 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6180 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6181 | if (TemplateParams && TemplateParams->size() > 0) { |
| 6182 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6183 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 6184 | if (TUK == TUK_Friend) { |
| 6185 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 6186 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6187 | return true; |
| 6188 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6189 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6190 | // C++ [temp.class.spec]p10: |
| 6191 | // The template parameter list of a specialization shall not |
| 6192 | // contain default template argument values. |
| 6193 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6194 | Decl *Param = TemplateParams->getParam(I); |
| 6195 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 6196 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6197 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6198 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 6199 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6200 | } |
| 6201 | } else if (NonTypeTemplateParmDecl *NTTP |
| 6202 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 6203 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6204 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6205 | diag::err_default_arg_in_partial_spec) |
| 6206 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6207 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6208 | } |
| 6209 | } else { |
| 6210 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6211 | if (TTP->hasDefaultArgument()) { |
| 6212 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6213 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6214 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6215 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6216 | } |
| 6217 | } |
| 6218 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6219 | } else if (TemplateParams) { |
| 6220 | if (TUK == TUK_Friend) |
| 6221 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6222 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6223 | SourceRange(TemplateParams->getTemplateLoc(), |
| 6224 | TemplateParams->getRAngleLoc())) |
| 6225 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6226 | else |
| 6227 | isExplicitSpecialization = true; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6228 | } else { |
| 6229 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6230 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6231 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6232 | // Check that the specialization uses the same tag kind as the |
| 6233 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6234 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6235 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6236 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6237 | Kind, TUK == TUK_Definition, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 6238 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6239 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6240 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6241 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6242 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6243 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6244 | diag::note_previous_use); |
| 6245 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6246 | } |
| 6247 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6248 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6249 | TemplateArgumentListInfo TemplateArgs = |
| 6250 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6251 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6252 | // Check for unexpanded parameter packs in any of the template arguments. |
| 6253 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6254 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6255 | UPPC_PartialSpecialization)) |
| 6256 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6257 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6258 | // Check that the template argument list is well-formed for this |
| 6259 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6260 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6261 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6262 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6263 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6264 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6265 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6266 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6267 | if (isPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6268 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6269 | *this, TemplateNameLoc, ClassTemplate->getTemplateParameters(), |
| 6270 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6271 | return true; |
| 6272 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6273 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6274 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6275 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6276 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6277 | TemplateArgs.size(), |
| 6278 | InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6279 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 6280 | << ClassTemplate->getDeclName(); |
| 6281 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6282 | } |
| 6283 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6284 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6285 | void *InsertPos = nullptr; |
| 6286 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6287 | |
| 6288 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6289 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6290 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6291 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6292 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6293 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6294 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6295 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6296 | // Check whether we can declare a class template specialization in |
| 6297 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6298 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6299 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 6300 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6301 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6302 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6303 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6304 | // The canonical type |
| 6305 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 6306 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6307 | // Build the canonical type that describes the converted template |
| 6308 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6309 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6310 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6311 | Converted.data(), |
| 6312 | Converted.size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6313 | |
| 6314 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6315 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 6316 | // C++ [temp.class.spec]p9b3: |
| 6317 | // |
| 6318 | // -- The argument list of the specialization shall not be identical |
| 6319 | // to the implicit argument list of the primary template. |
| 6320 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6321 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6322 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6323 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 6324 | ClassTemplate->getIdentifier(), |
| 6325 | TemplateNameLoc, |
| 6326 | Attr, |
| 6327 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6328 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 6329 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6330 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6331 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6332 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6333 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6334 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6335 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 6336 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6337 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6338 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6339 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6340 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 6341 | TemplateParams, |
| 6342 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6343 | Converted.data(), |
| 6344 | Converted.size(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6345 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6346 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 6347 | PrevPartial); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6348 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6349 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6350 | Partial->setTemplateParameterListsInfo( |
| 6351 | Context, TemplateParameterLists.drop_back(1)); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6352 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6353 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6354 | if (!PrevPartial) |
| 6355 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6356 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6357 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6358 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6359 | // template specialization, make a note of that. |
| 6360 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 6361 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6362 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6363 | // Check that all of the template parameters of the class template |
| 6364 | // partial specialization are deducible from the template |
| 6365 | // arguments. If not, this class template partial specialization |
| 6366 | // will never be used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6367 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6368 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6369 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 6370 | DeducibleParams); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6371 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6372 | if (!DeducibleParams.all()) { |
| 6373 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6374 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6375 | << /*class template*/0 << (NumNonDeducible > 1) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6376 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 6377 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 6378 | if (!DeducibleParams[I]) { |
| 6379 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 6380 | if (Param->getDeclName()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6381 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6382 | diag::note_partial_spec_unused_parameter) |
| 6383 | << Param->getDeclName(); |
| 6384 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6385 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6386 | diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 6387 | << "(anonymous)"; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6388 | } |
| 6389 | } |
| 6390 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6391 | } else { |
| 6392 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6393 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6394 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6395 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6396 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6397 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6398 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6399 | Converted.data(), |
| 6400 | Converted.size(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6401 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6402 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6403 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6404 | Specialization->setTemplateParameterListsInfo(Context, |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 6405 | TemplateParameterLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6406 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6407 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6408 | if (!PrevDecl) |
| 6409 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6410 | |
David Majnemer | 678f50b | 2015-11-18 19:49:19 +0000 | [diff] [blame^] | 6411 | if (CurContext->isDependentContext()) { |
| 6412 | // -fms-extensions permits specialization of nested classes without |
| 6413 | // fully specializing the outer class(es). |
| 6414 | assert(getLangOpts().MicrosoftExt && |
| 6415 | "Only possible with -fms-extensions!"); |
| 6416 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6417 | CanonType = Context.getTemplateSpecializationType( |
| 6418 | CanonTemplate, Converted.data(), Converted.size()); |
| 6419 | } else { |
| 6420 | CanonType = Context.getTypeDeclType(Specialization); |
| 6421 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6422 | } |
| 6423 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6424 | // C++ [temp.expl.spec]p6: |
| 6425 | // 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] | 6426 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6427 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6428 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6429 | // use occurs; no diagnostic is required. |
| 6430 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6431 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6432 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6433 | // Is there any previous explicit specialization declaration? |
| 6434 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6435 | Okay = true; |
| 6436 | break; |
| 6437 | } |
| 6438 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6439 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6440 | if (!Okay) { |
| 6441 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 6442 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 6443 | << Context.getTypeDeclType(Specialization) << Range; |
| 6444 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6445 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6446 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6447 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6448 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6449 | return true; |
| 6450 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6451 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6452 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6453 | // If this is not a friend, note that this is an explicit specialization. |
| 6454 | if (TUK != TUK_Friend) |
| 6455 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6456 | |
| 6457 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6458 | if (TUK == TUK_Definition) { |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6459 | RecordDecl *Def = Specialization->getDefinition(); |
| 6460 | NamedDecl *Hidden = nullptr; |
| 6461 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 6462 | SkipBody->ShouldSkip = true; |
| 6463 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 6464 | // From here on out, treat this as just a redeclaration. |
| 6465 | TUK = TUK_Declaration; |
| 6466 | } else if (Def) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6467 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6468 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6469 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6470 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 6471 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6472 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6473 | } |
| 6474 | } |
| 6475 | |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 6476 | if (Attr) |
| 6477 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6478 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 6479 | // Add alignment attributes if necessary; these attributes are checked when |
| 6480 | // the ASTContext lays out the structure. |
| 6481 | if (TUK == TUK_Definition) { |
| 6482 | AddAlignmentAttributesForRecord(Specialization); |
| 6483 | AddMsStructLayoutForRecord(Specialization); |
| 6484 | } |
| 6485 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6486 | if (ModulePrivateLoc.isValid()) |
| 6487 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 6488 | << (isPartialSpecialization? 1 : 0) |
| 6489 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 6490 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 6491 | // Build the fully-sugared type for this class template |
| 6492 | // specialization as the user wrote in the specialization |
| 6493 | // itself. This means that we'll pretty-print the type retrieved |
| 6494 | // from the specialization's declaration the way that the user |
| 6495 | // actually wrote the specialization, rather than formatting the |
| 6496 | // name based on the "canonical" representation used to store the |
| 6497 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6498 | TypeSourceInfo *WrittenTy |
| 6499 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6500 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6501 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6502 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6503 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6504 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6505 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 6506 | // C++ [temp.expl.spec]p9: |
| 6507 | // A template explicit specialization is in the scope of the |
| 6508 | // namespace in which the template was defined. |
| 6509 | // |
| 6510 | // We actually implement this paragraph where we set the semantic |
| 6511 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 6512 | // but we also maintain the lexical context where the actual |
| 6513 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6514 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6515 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6516 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6517 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6518 | Specialization->startDefinition(); |
| 6519 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6520 | if (TUK == TUK_Friend) { |
| 6521 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 6522 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6523 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6524 | /*FIXME:*/KWLoc); |
| 6525 | Friend->setAccess(AS_public); |
| 6526 | CurContext->addDecl(Friend); |
| 6527 | } else { |
| 6528 | // Add the specialization into its lexical context, so that it can |
| 6529 | // be seen when iterating through the list of declarations in that |
| 6530 | // context. However, specializations are not found by name lookup. |
| 6531 | CurContext->addDecl(Specialization); |
| 6532 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6533 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6534 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6535 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6536 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6537 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6538 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6539 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 6540 | ActOnDocumentableDecl(NewDecl); |
| 6541 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6542 | } |
| 6543 | |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6544 | /// \brief Strips various properties off an implicit instantiation |
| 6545 | /// that has just been explicitly specialized. |
| 6546 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6547 | D->dropAttr<DLLImportAttr>(); |
| 6548 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6549 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6550 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6551 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6552 | } |
| 6553 | |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6554 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 6555 | // declaration or definition. |
| 6556 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6557 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6558 | // Explicit instantiations following a specialization have no effect and |
| 6559 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 6560 | // until a valid name loc is found. |
| 6561 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6562 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 6563 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6564 | PrevDiagLoc = Prev->getLocation(); |
| 6565 | } |
| 6566 | assert(PrevDiagLoc.isValid() && |
| 6567 | "Explicit instantiation without point of instantiation?"); |
| 6568 | return PrevDiagLoc; |
| 6569 | } |
| 6570 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6571 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6572 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6573 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6574 | /// new specialization/instantiation will have any effect. |
| 6575 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6576 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6577 | /// instantiation. |
| 6578 | /// |
| 6579 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 6580 | /// |
| 6581 | /// \param PrevDecl the previous declaration of the entity. |
| 6582 | /// |
| 6583 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 6584 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6585 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6586 | /// declaration was instantiated (either implicitly or explicitly). |
| 6587 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6588 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6589 | /// specialization or instantiation has no effect and should be ignored. |
| 6590 | /// |
| 6591 | /// \returns true if there was an error that should prevent the introduction of |
| 6592 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6593 | bool |
| 6594 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 6595 | TemplateSpecializationKind NewTSK, |
| 6596 | NamedDecl *PrevDecl, |
| 6597 | TemplateSpecializationKind PrevTSK, |
| 6598 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6599 | bool &HasNoEffect) { |
| 6600 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6601 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6602 | switch (NewTSK) { |
| 6603 | case TSK_Undeclared: |
| 6604 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 6605 | assert( |
| 6606 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 6607 | "previous declaration must be implicit!"); |
| 6608 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6609 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6610 | case TSK_ExplicitSpecialization: |
| 6611 | switch (PrevTSK) { |
| 6612 | case TSK_Undeclared: |
| 6613 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6614 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6615 | // explicitly specialized or has merely been mentioned without any |
| 6616 | // instantiation. |
| 6617 | return false; |
| 6618 | |
| 6619 | case TSK_ImplicitInstantiation: |
| 6620 | if (PrevPointOfInstantiation.isInvalid()) { |
| 6621 | // The declaration itself has not actually been instantiated, so it is |
| 6622 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6623 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6624 | return false; |
| 6625 | } |
| 6626 | // Fall through |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6627 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6628 | case TSK_ExplicitInstantiationDeclaration: |
| 6629 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6630 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 6631 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6632 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6633 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6634 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6635 | // 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] | 6636 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6637 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6638 | // implicit instantiation to take place, in every translation unit in |
| 6639 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6640 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6641 | // Is there any previous explicit specialization declaration? |
| 6642 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 6643 | return false; |
| 6644 | } |
| 6645 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6646 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6647 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6648 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6649 | << (PrevTSK != TSK_ImplicitInstantiation); |
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 | return true; |
| 6652 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6653 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6654 | case TSK_ExplicitInstantiationDeclaration: |
| 6655 | switch (PrevTSK) { |
| 6656 | case TSK_ExplicitInstantiationDeclaration: |
| 6657 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6658 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6659 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6660 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6661 | case TSK_Undeclared: |
| 6662 | case TSK_ImplicitInstantiation: |
| 6663 | // We're explicitly instantiating something that may have already been |
| 6664 | // implicitly instantiated; that's fine. |
| 6665 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6666 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6667 | case TSK_ExplicitSpecialization: |
| 6668 | // C++0x [temp.explicit]p4: |
| 6669 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6670 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6671 | // specialization for that template, the explicit instantiation has no |
| 6672 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6673 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6674 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6675 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6676 | case TSK_ExplicitInstantiationDefinition: |
| 6677 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6678 | // If an entity is the subject of both an explicit instantiation |
| 6679 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6680 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6681 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6682 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6683 | |
| 6684 | // Explicit instantiations following a specialization have no effect and |
| 6685 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 6686 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6687 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 6688 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6689 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6690 | return false; |
| 6691 | } |
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 | switch (PrevTSK) { |
| 6695 | case TSK_Undeclared: |
| 6696 | case TSK_ImplicitInstantiation: |
| 6697 | // We're explicitly instantiating something that may have already been |
| 6698 | // implicitly instantiated; that's fine. |
| 6699 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6700 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6701 | case TSK_ExplicitSpecialization: |
| 6702 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 6703 | // For a given set of template parameters, if an explicit |
| 6704 | // instantiation of a template appears after a declaration of |
| 6705 | // an explicit specialization for that template, the explicit |
| 6706 | // instantiation has no effect. |
| 6707 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6708 | // 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] | 6709 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6710 | // has been explicitly specialized. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6711 | Diag(NewLoc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6712 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 6713 | diag::ext_explicit_instantiation_after_specialization) |
| 6714 | << PrevDecl; |
| 6715 | Diag(PrevDecl->getLocation(), |
| 6716 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6717 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6718 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6719 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6720 | case TSK_ExplicitInstantiationDeclaration: |
| 6721 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6722 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6723 | |
| 6724 | // C++0x [temp.explicit]p4: |
| 6725 | // For a given set of template parameters, if an explicit instantiation |
| 6726 | // of a template appears after a declaration of an explicit |
| 6727 | // specialization for that template, the explicit instantiation has no |
| 6728 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6729 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6730 | // Is there any previous explicit specialization declaration? |
| 6731 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6732 | HasNoEffect = true; |
| 6733 | break; |
| 6734 | } |
| 6735 | } |
| 6736 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6737 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6738 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6739 | case TSK_ExplicitInstantiationDefinition: |
| 6740 | // C++0x [temp.spec]p5: |
| 6741 | // For a given template and a given set of template-arguments, |
| 6742 | // - an explicit instantiation definition shall appear at most once |
| 6743 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6744 | |
| 6745 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 6746 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 6747 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6748 | : diag::err_explicit_instantiation_duplicate) |
| 6749 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6750 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6751 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6752 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6753 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6754 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6755 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6756 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6757 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6758 | } |
| 6759 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6760 | /// \brief Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6761 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6762 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6763 | /// The only possible way to get a dependent function template specialization |
| 6764 | /// is with a friend declaration, like so: |
| 6765 | /// |
| 6766 | /// \code |
| 6767 | /// template \<class T> void foo(T); |
| 6768 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6769 | /// friend void foo<>(T); |
| 6770 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6771 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6772 | /// |
| 6773 | /// There really isn't any useful analysis we can do here, so we |
| 6774 | /// just store the information. |
| 6775 | bool |
| 6776 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 6777 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 6778 | LookupResult &Previous) { |
| 6779 | // Remove anything from Previous that isn't a function template in |
| 6780 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6781 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6782 | LookupResult::Filter F = Previous.makeFilter(); |
| 6783 | while (F.hasNext()) { |
| 6784 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 6785 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6786 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 6787 | D->getDeclContext()->getRedeclContext())) |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6788 | F.erase(); |
| 6789 | } |
| 6790 | F.done(); |
| 6791 | |
| 6792 | // Should this be diagnosed here? |
| 6793 | if (Previous.empty()) return true; |
| 6794 | |
| 6795 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 6796 | ExplicitTemplateArgs); |
| 6797 | return false; |
| 6798 | } |
| 6799 | |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6800 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6801 | /// specialization. |
| 6802 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6803 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6804 | /// explicit function template specialization. On successful completion, |
| 6805 | /// the function declaration \p FD will become a function template |
| 6806 | /// specialization. |
| 6807 | /// |
| 6808 | /// \param FD the function declaration, which will be updated to become a |
| 6809 | /// function template specialization. |
| 6810 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6811 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 6812 | /// if any. Note that this may be valid info even when 0 arguments are |
| 6813 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 6814 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6815 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6816 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6817 | /// this function specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6818 | bool Sema::CheckFunctionTemplateSpecialization( |
| 6819 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 6820 | LookupResult &Previous) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6821 | // The set of function template specializations that could match this |
| 6822 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6823 | UnresolvedSet<8> Candidates; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6824 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6825 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6826 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
| 6827 | ConvertedTemplateArgs; |
| 6828 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6829 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6830 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6831 | I != E; ++I) { |
| 6832 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 6833 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6834 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6835 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6836 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 6837 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6838 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6839 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6840 | // When matching a constexpr member function template specialization |
| 6841 | // against the primary template, we don't yet know whether the |
| 6842 | // specialization has an implicit 'const' (because we don't know whether |
| 6843 | // it will be a static member function until we know which template it |
| 6844 | // specializes), so adjust it now assuming it specializes this template. |
| 6845 | QualType FT = FD->getType(); |
| 6846 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6847 | CXXMethodDecl *OldMD = |
| 6848 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6849 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6850 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6851 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6852 | EPI.TypeQuals |= Qualifiers::Const; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6853 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6854 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6855 | } |
| 6856 | } |
| 6857 | |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6858 | TemplateArgumentListInfo Args; |
| 6859 | if (ExplicitTemplateArgs) |
| 6860 | Args = *ExplicitTemplateArgs; |
| 6861 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6862 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6863 | // A trailing template-argument can be left unspecified in the |
| 6864 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6865 | // provided it can be deduced from the function argument type. |
| 6866 | // Perform template argument deduction to determine whether we may be |
| 6867 | // specializing this template. |
| 6868 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6869 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6870 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 6871 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 6872 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6873 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6874 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6875 | // that we can provide nifty diagnostics. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6876 | FailedCandidates.addCandidate() |
| 6877 | .set(FunTmpl->getTemplatedDecl(), |
| 6878 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6879 | (void)TDK; |
| 6880 | continue; |
| 6881 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6882 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6883 | // Record this candidate. |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6884 | if (ExplicitTemplateArgs) |
| 6885 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6886 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6887 | } |
| 6888 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6889 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 6890 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6891 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 6892 | Candidates.begin(), Candidates.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6893 | FD->getLocation(), |
| 6894 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 6895 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6896 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6897 | PDiag(diag::note_function_template_spec_matched)); |
| 6898 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6899 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6900 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6901 | |
| 6902 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 6903 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 6904 | |
| 6905 | FunctionTemplateSpecializationInfo *SpecInfo |
| 6906 | = Specialization->getTemplateSpecializationInfo(); |
| 6907 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6908 | |
| 6909 | // Note: do not overwrite location info if previous template |
| 6910 | // specialization kind was explicit. |
| 6911 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6912 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6913 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6914 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 6915 | // function can differ from the template declaration with respect to |
| 6916 | // the constexpr specifier. |
| 6917 | Specialization->setConstexpr(FD->isConstexpr()); |
| 6918 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6919 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6920 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6921 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6922 | |
| 6923 | // If this is a friend declaration, then we're not really declaring |
| 6924 | // an explicit specialization. |
| 6925 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6926 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6927 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6928 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6929 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6930 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6931 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6932 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6933 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6934 | |
| 6935 | // C++ [temp.expl.spec]p6: |
| 6936 | // 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] | 6937 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6938 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6939 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6940 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6941 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6942 | if (!isFriend && |
| 6943 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6944 | TSK_ExplicitSpecialization, |
| 6945 | Specialization, |
| 6946 | SpecInfo->getTemplateSpecializationKind(), |
| 6947 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6948 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6949 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6950 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6951 | // Mark the prior declaration as an explicit specialization, so that later |
| 6952 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6953 | if (!isFriend) { |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6954 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6955 | MarkUnusedFileScopedDecl(Specialization); |
| 6956 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6957 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6958 | // Turn the given function declaration into a function template |
| 6959 | // specialization, with the template arguments from the previous |
| 6960 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6961 | // Take copies of (semantic and syntactic) template argument lists. |
| 6962 | const TemplateArgumentList* TemplArgs = new (Context) |
| 6963 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Richard Smith | 7d3c3ef | 2015-10-02 00:49:37 +0000 | [diff] [blame] | 6964 | FD->setFunctionTemplateSpecialization( |
| 6965 | Specialization->getPrimaryTemplate(), TemplArgs, /*InsertPos=*/nullptr, |
| 6966 | SpecInfo->getTemplateSpecializationKind(), |
| 6967 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6968 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6969 | // The "previous declaration" for this function template specialization is |
| 6970 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6971 | Previous.clear(); |
| 6972 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6973 | return false; |
| 6974 | } |
| 6975 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6976 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6977 | /// specialization. |
| 6978 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6979 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6980 | /// explicit member function specialization. On successful completion, |
| 6981 | /// the function declaration \p FD will become a member function |
| 6982 | /// specialization. |
| 6983 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6984 | /// \param Member the member declaration, which will be updated to become a |
| 6985 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6986 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6987 | /// \param Previous the set of declarations, one of which may be specialized |
| 6988 | /// by this function specialization; the set will be modified to contain the |
| 6989 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6990 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6991 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6992 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 6993 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6994 | // Try to find the member we are instantiating. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6995 | NamedDecl *Instantiation = nullptr; |
| 6996 | NamedDecl *InstantiatedFrom = nullptr; |
| 6997 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6998 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6999 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7000 | // Nowhere to look anyway. |
| 7001 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7002 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 7003 | I != E; ++I) { |
| 7004 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 7005 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 7006 | QualType Adjusted = Function->getType(); |
| 7007 | if (!hasExplicitCallingConv(Adjusted)) |
| 7008 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
| 7009 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7010 | Instantiation = Method; |
| 7011 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7012 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7013 | break; |
| 7014 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7015 | } |
| 7016 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7017 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7018 | VarDecl *PrevVar; |
| 7019 | if (Previous.isSingleResult() && |
| 7020 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7021 | if (PrevVar->isStaticDataMember()) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7022 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7023 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7024 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7025 | } |
| 7026 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7027 | CXXRecordDecl *PrevRecord; |
| 7028 | if (Previous.isSingleResult() && |
| 7029 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 7030 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7031 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7032 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7033 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7034 | } else if (isa<EnumDecl>(Member)) { |
| 7035 | EnumDecl *PrevEnum; |
| 7036 | if (Previous.isSingleResult() && |
| 7037 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
| 7038 | Instantiation = PrevEnum; |
| 7039 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 7040 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 7041 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7042 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7043 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7044 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7045 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7046 | // specializations are always out-of-line, the caller will complain about |
| 7047 | // this mismatch later. |
| 7048 | return false; |
| 7049 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 7050 | |
| 7051 | // If this is a friend, just bail out here before we start turning |
| 7052 | // things into explicit specializations. |
| 7053 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 7054 | // Preserve instantiation information. |
| 7055 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 7056 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 7057 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7058 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7059 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 7060 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 7061 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7062 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 7063 | } |
| 7064 | |
| 7065 | Previous.clear(); |
| 7066 | Previous.addDecl(Instantiation); |
| 7067 | return false; |
| 7068 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7069 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7070 | // Make sure that this is a specialization of a member. |
| 7071 | if (!InstantiatedFrom) { |
| 7072 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 7073 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7074 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 7075 | return true; |
| 7076 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7077 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7078 | // C++ [temp.expl.spec]p6: |
| 7079 | // 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] | 7080 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7081 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7082 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7083 | // use occurs; no diagnostic is required. |
| 7084 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7085 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7086 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7087 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 7088 | TSK_ExplicitSpecialization, |
| 7089 | Instantiation, |
| 7090 | MSInfo->getTemplateSpecializationKind(), |
| 7091 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7092 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7093 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7094 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7095 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7096 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7097 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7098 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7099 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7100 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 7101 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7102 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7103 | // the original declaration to note that it is an explicit specialization |
| 7104 | // (if it was previously an implicit instantiation). This latter step |
| 7105 | // makes bookkeeping easier. |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7106 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7107 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 7108 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 7109 | TSK_ImplicitInstantiation) { |
| 7110 | InstantiationFunction->setTemplateSpecializationKind( |
| 7111 | TSK_ExplicitSpecialization); |
| 7112 | InstantiationFunction->setLocation(Member->getLocation()); |
| 7113 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7114 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7115 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 7116 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7117 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7118 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7119 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7120 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 7121 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 7122 | TSK_ImplicitInstantiation) { |
| 7123 | InstantiationVar->setTemplateSpecializationKind( |
| 7124 | TSK_ExplicitSpecialization); |
| 7125 | InstantiationVar->setLocation(Member->getLocation()); |
| 7126 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7127 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7128 | cast<VarDecl>(Member)->setInstantiationOfStaticDataMember( |
| 7129 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7130 | MarkUnusedFileScopedDecl(InstantiationVar); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7131 | } else if (isa<CXXRecordDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7132 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 7133 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 7134 | TSK_ImplicitInstantiation) { |
| 7135 | InstantiationClass->setTemplateSpecializationKind( |
| 7136 | TSK_ExplicitSpecialization); |
| 7137 | InstantiationClass->setLocation(Member->getLocation()); |
| 7138 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7139 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7140 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7141 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7142 | TSK_ExplicitSpecialization); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7143 | } else { |
| 7144 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); |
| 7145 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); |
| 7146 | if (InstantiationEnum->getTemplateSpecializationKind() == |
| 7147 | TSK_ImplicitInstantiation) { |
| 7148 | InstantiationEnum->setTemplateSpecializationKind( |
| 7149 | TSK_ExplicitSpecialization); |
| 7150 | InstantiationEnum->setLocation(Member->getLocation()); |
| 7151 | } |
| 7152 | |
| 7153 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( |
| 7154 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7155 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7156 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7157 | // Save the caller the trouble of having to figure out which declaration |
| 7158 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7159 | Previous.clear(); |
| 7160 | Previous.addDecl(Instantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7161 | return false; |
| 7162 | } |
| 7163 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7164 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7165 | /// |
| 7166 | /// \returns true if a serious error occurs, false otherwise. |
| 7167 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7168 | SourceLocation InstLoc, |
| 7169 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7170 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 7171 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7172 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7173 | if (CurContext->isRecord()) { |
| 7174 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 7175 | << D; |
| 7176 | return true; |
| 7177 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7178 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7179 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7180 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7181 | // template. If the name declared in the explicit instantiation is an |
| 7182 | // unqualified name, the explicit instantiation shall appear in the |
| 7183 | // namespace where its template is declared or, if that namespace is inline |
| 7184 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7185 | // |
| 7186 | // 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] | 7187 | if (WasQualifiedName) { |
| 7188 | if (CurContext->Encloses(OrigContext)) |
| 7189 | return false; |
| 7190 | } else { |
| 7191 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 7192 | return false; |
| 7193 | } |
| 7194 | |
| 7195 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 7196 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7197 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7198 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7199 | diag::err_explicit_instantiation_out_of_scope : |
| 7200 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7201 | << D << NS; |
| 7202 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7203 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7204 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7205 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 7206 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 7207 | << D << NS; |
| 7208 | } else |
| 7209 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7210 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7211 | diag::err_explicit_instantiation_must_be_global : |
| 7212 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 7213 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7214 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7215 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7216 | } |
| 7217 | |
| 7218 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 7219 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 7220 | if (!SS.isSet()) |
| 7221 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7222 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7223 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7224 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7225 | // or a static data member of a class template specialization, the name of |
| 7226 | // the class template specialization in the qualified-id for the member |
| 7227 | // name shall be a simple-template-id. |
| 7228 | // |
| 7229 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7230 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 7231 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7232 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7233 | if (isa<TemplateSpecializationType>(T)) |
| 7234 | return true; |
| 7235 | |
| 7236 | return false; |
| 7237 | } |
| 7238 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7239 | // Explicit instantiation of a class template specialization |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7240 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7241 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7242 | SourceLocation ExternLoc, |
| 7243 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7244 | unsigned TagSpec, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7245 | SourceLocation KWLoc, |
| 7246 | const CXXScopeSpec &SS, |
| 7247 | TemplateTy TemplateD, |
| 7248 | SourceLocation TemplateNameLoc, |
| 7249 | SourceLocation LAngleLoc, |
| 7250 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7251 | SourceLocation RAngleLoc, |
| 7252 | AttributeList *Attr) { |
| 7253 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 7254 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7255 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7256 | // Check that the specialization uses the same tag kind as the |
| 7257 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7258 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7259 | assert(Kind != TTK_Enum && |
| 7260 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7261 | |
| 7262 | if (isa<TypeAliasTemplateDecl>(TD)) { |
| 7263 | Diag(KWLoc, diag::err_tag_reference_non_tag) << Kind; |
| 7264 | Diag(TD->getTemplatedDecl()->getLocation(), |
| 7265 | diag::note_previous_use); |
| 7266 | return true; |
| 7267 | } |
| 7268 | |
| 7269 | ClassTemplateDecl *ClassTemplate = cast<ClassTemplateDecl>(TD); |
| 7270 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7271 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7272 | Kind, /*isDefinition*/false, KWLoc, |
Justin Bogner | c6ecb7c | 2015-07-10 23:05:47 +0000 | [diff] [blame] | 7273 | ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7274 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7275 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7276 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7277 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7278 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7279 | diag::note_previous_use); |
| 7280 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7281 | } |
| 7282 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7283 | // C++0x [temp.explicit]p2: |
| 7284 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7285 | // definition and an explicit instantiation declaration. An explicit |
| 7286 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 7287 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 7288 | ? TSK_ExplicitInstantiationDefinition |
| 7289 | : TSK_ExplicitInstantiationDeclaration; |
| 7290 | |
| 7291 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 7292 | // Check for dllexport class template instantiation declarations. |
| 7293 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7294 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7295 | Diag(ExternLoc, |
| 7296 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7297 | Diag(A->getLoc(), diag::note_attribute); |
| 7298 | break; |
| 7299 | } |
| 7300 | } |
| 7301 | |
| 7302 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 7303 | Diag(ExternLoc, |
| 7304 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7305 | Diag(A->getLocation(), diag::note_attribute); |
| 7306 | } |
| 7307 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7308 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7309 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7310 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 7311 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7312 | |
| 7313 | // Check that the template argument list is well-formed for this |
| 7314 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7315 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7316 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7317 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7318 | return true; |
| 7319 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7320 | // Find the class template specialization declaration that |
| 7321 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7322 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7323 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7324 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7325 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7326 | TemplateSpecializationKind PrevDecl_TSK |
| 7327 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 7328 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7329 | // C++0x [temp.explicit]p2: |
| 7330 | // [...] An explicit instantiation shall appear in an enclosing |
| 7331 | // namespace of its template. [...] |
| 7332 | // |
| 7333 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7334 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 7335 | SS.isSet())) |
| 7336 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7337 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7338 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7339 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7340 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7341 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7342 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7343 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7344 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7345 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7346 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7347 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7348 | // Even though HasNoEffect == true means that this explicit instantiation |
| 7349 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 7350 | |
| 7351 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 7352 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7353 | // Since the only prior class template specialization with these |
| 7354 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7355 | // declaration node as our own, updating the source location |
| 7356 | // for the template name to reflect our new declaration. |
| 7357 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7358 | Specialization = PrevDecl; |
| 7359 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7360 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7361 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7362 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7363 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7364 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7365 | // Create a new class template specialization declaration node for |
| 7366 | // this explicit specialization. |
| 7367 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7368 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7369 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7370 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7371 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7372 | Converted.data(), |
| 7373 | Converted.size(), |
| 7374 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7375 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7376 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7377 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7378 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7379 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7380 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7381 | } |
| 7382 | |
| 7383 | // Build the fully-sugared type for this explicit instantiation as |
| 7384 | // the user wrote in the explicit instantiation itself. This means |
| 7385 | // that we'll pretty-print the type retrieved from the |
| 7386 | // specialization's declaration the way that the user actually wrote |
| 7387 | // the explicit instantiation, rather than formatting the name based |
| 7388 | // on the "canonical" representation used to store the template |
| 7389 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7390 | TypeSourceInfo *WrittenTy |
| 7391 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7392 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7393 | Context.getTypeDeclType(Specialization)); |
| 7394 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7395 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7396 | // Set source locations for keywords. |
| 7397 | Specialization->setExternLoc(ExternLoc); |
| 7398 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | 40bcfd7 | 2013-04-22 23:23:42 +0000 | [diff] [blame] | 7399 | Specialization->setRBraceLoc(SourceLocation()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7400 | |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 7401 | if (Attr) |
| 7402 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 7403 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7404 | // Add the explicit instantiation into its lexical context. However, |
| 7405 | // since explicit instantiations are never found by name lookup, we |
| 7406 | // just put it into the declaration context directly. |
| 7407 | Specialization->setLexicalDeclContext(CurContext); |
| 7408 | CurContext->addDecl(Specialization); |
| 7409 | |
| 7410 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 7411 | if (HasNoEffect) { |
| 7412 | // Set the template specialization kind. |
| 7413 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7414 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 7415 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7416 | |
| 7417 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7418 | // A definition of a class template or class member template |
| 7419 | // shall be in scope at the point of the explicit instantiation of |
| 7420 | // the class template or class member template. |
| 7421 | // |
| 7422 | // This check comes when we actually try to perform the |
| 7423 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7424 | ClassTemplateSpecializationDecl *Def |
| 7425 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7426 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7427 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7428 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7429 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7430 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7431 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 7432 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7433 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7434 | // Instantiate the members of this class template specialization. |
| 7435 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7436 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7437 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7438 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 7439 | |
| 7440 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 7441 | // TSK_ExplicitInstantiationDefinition |
| 7442 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7443 | TSK == TSK_ExplicitInstantiationDefinition) { |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7444 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7445 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7446 | |
Hans Wennborg | c087550 | 2015-06-09 00:39:05 +0000 | [diff] [blame] | 7447 | if (!getDLLAttr(Def) && getDLLAttr(Specialization) && |
| 7448 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 7449 | // In the MS ABI, an explicit instantiation definition can add a dll |
| 7450 | // attribute to a template with a previous instantiation declaration. |
| 7451 | // MinGW doesn't allow this. |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7452 | auto *A = cast<InheritableAttr>( |
| 7453 | getDLLAttr(Specialization)->clone(getASTContext())); |
| 7454 | A->setInherited(true); |
| 7455 | Def->addAttr(A); |
| 7456 | checkClassLevelDLLAttribute(Def); |
Hans Wennborg | fce87ca | 2015-06-09 00:39:09 +0000 | [diff] [blame] | 7457 | |
| 7458 | // Propagate attribute to base class templates. |
| 7459 | for (auto &B : Def->bases()) { |
| 7460 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 7461 | B.getType()->getAsCXXRecordDecl())) |
| 7462 | propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getLocStart()); |
| 7463 | } |
Hans Wennborg | 17f9b44 | 2015-05-27 00:06:45 +0000 | [diff] [blame] | 7464 | } |
| 7465 | } |
| 7466 | |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 7467 | // Set the template specialization kind. Make sure it is set before |
| 7468 | // instantiating the members which will trigger ASTConsumer callbacks. |
| 7469 | Specialization->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7470 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Argyrios Kyrtzidis | 322d853 | 2015-09-11 01:44:56 +0000 | [diff] [blame] | 7471 | } else { |
| 7472 | |
| 7473 | // Set the template specialization kind. |
| 7474 | Specialization->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7475 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7476 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7477 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7478 | } |
| 7479 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7480 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7481 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7482 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7483 | SourceLocation ExternLoc, |
| 7484 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7485 | unsigned TagSpec, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7486 | SourceLocation KWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7487 | CXXScopeSpec &SS, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7488 | IdentifierInfo *Name, |
| 7489 | SourceLocation NameLoc, |
| 7490 | AttributeList *Attr) { |
| 7491 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 7492 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7493 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7494 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7495 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7496 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7497 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 7498 | SourceLocation(), false, TypeResult(), |
| 7499 | /*IsTypeSpecifier*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7500 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 7501 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7502 | if (!TagD) |
| 7503 | return true; |
| 7504 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7505 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7506 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7507 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 7508 | if (Tag->isInvalidDecl()) |
| 7509 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7510 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7511 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 7512 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 7513 | if (!Pattern) { |
| 7514 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 7515 | << Context.getTypeDeclType(Record); |
| 7516 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 7517 | return true; |
| 7518 | } |
| 7519 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7520 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7521 | // If the explicit instantiation is for a class or member class, the |
| 7522 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7523 | // simple-template-id. |
| 7524 | // |
| 7525 | // C++98 has the same restriction, just worded differently. |
| 7526 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7527 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7528 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7529 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7530 | // C++0x [temp.explicit]p2: |
| 7531 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7532 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7533 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 7534 | TemplateSpecializationKind TSK |
| 7535 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7536 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7537 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7538 | // C++0x [temp.explicit]p2: |
| 7539 | // [...] An explicit instantiation shall appear in an enclosing |
| 7540 | // namespace of its template. [...] |
| 7541 | // |
| 7542 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7543 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7544 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7545 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7546 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7547 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7548 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7549 | PrevDecl = Record; |
| 7550 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7551 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7552 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7553 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7554 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7555 | PrevDecl, |
| 7556 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7557 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7558 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7559 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7560 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7561 | return TagD; |
| 7562 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7563 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7564 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7565 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7566 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7567 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7568 | // 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] | 7569 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7570 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7571 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7572 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 7573 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 7574 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7575 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 7576 | << Pattern; |
| 7577 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7578 | } else { |
| 7579 | if (InstantiateClass(NameLoc, Record, Def, |
| 7580 | getTemplateInstantiationArgs(Record), |
| 7581 | TSK)) |
| 7582 | return true; |
| 7583 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7584 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7585 | if (!RecordDef) |
| 7586 | return true; |
| 7587 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7588 | } |
| 7589 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7590 | // Instantiate all of the members of the class. |
| 7591 | InstantiateClassMembers(NameLoc, RecordDef, |
| 7592 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7593 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7594 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7595 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 7596 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7597 | // FIXME: We don't have any representation for explicit instantiations of |
| 7598 | // member classes. Such a representation is not needed for compilation, but it |
| 7599 | // should be available for clients that want to see all of the declarations in |
| 7600 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7601 | return TagD; |
| 7602 | } |
| 7603 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7604 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 7605 | SourceLocation ExternLoc, |
| 7606 | SourceLocation TemplateLoc, |
| 7607 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7608 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7609 | // TODO: check if/when DNInfo should replace Name. |
| 7610 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 7611 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7612 | if (!Name) { |
| 7613 | if (!D.isInvalidType()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7614 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7615 | diag::err_explicit_instantiation_requires_name) |
| 7616 | << D.getDeclSpec().getSourceRange() |
| 7617 | << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7618 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7619 | return true; |
| 7620 | } |
| 7621 | |
| 7622 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 7623 | // we find one that is. |
| 7624 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 7625 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 7626 | S = S->getParent(); |
| 7627 | |
| 7628 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7629 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 7630 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7631 | if (R.isNull()) |
| 7632 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7633 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7634 | // C++ [dcl.stc]p1: |
| 7635 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 7636 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7637 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7638 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 7639 | << Name; |
| 7640 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7641 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 7642 | != DeclSpec::SCS_unspecified) { |
| 7643 | // Complain about then remove the storage class specifier. |
| 7644 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 7645 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 7646 | |
| 7647 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7648 | } |
| 7649 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 7650 | // C++0x [temp.explicit]p1: |
| 7651 | // [...] An explicit instantiation of a function template shall not use the |
| 7652 | // inline or constexpr specifiers. |
| 7653 | // Presumably, this also applies to member functions of class templates as |
| 7654 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7655 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7656 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7657 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7658 | diag::err_explicit_instantiation_inline : |
| 7659 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7660 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7661 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7662 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 7663 | // not already specified. |
| 7664 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 7665 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7666 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7667 | // C++0x [temp.explicit]p2: |
| 7668 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7669 | // definition and an explicit instantiation declaration. An explicit |
| 7670 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7671 | TemplateSpecializationKind TSK |
| 7672 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7673 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7674 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7675 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7676 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7677 | |
| 7678 | if (!R->isFunctionType()) { |
| 7679 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7680 | // A [...] static data member of a class template can be explicitly |
| 7681 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7682 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7683 | // C++1y [temp.explicit]p1: |
| 7684 | // A [...] variable [...] template specialization can be explicitly |
| 7685 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7686 | if (Previous.isAmbiguous()) |
| 7687 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7688 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 7689 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7690 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7691 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7692 | if (!PrevTemplate) { |
| 7693 | if (!Prev || !Prev->isStaticDataMember()) { |
| 7694 | // We expect to see a data data member here. |
| 7695 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 7696 | << Name; |
| 7697 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7698 | P != PEnd; ++P) |
| 7699 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 7700 | return true; |
| 7701 | } |
| 7702 | |
| 7703 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 7704 | // FIXME: Check for explicit specialization? |
| 7705 | Diag(D.getIdentifierLoc(), |
| 7706 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 7707 | << Prev; |
| 7708 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 7709 | // FIXME: Can we provide a note showing where this was declared? |
| 7710 | return true; |
| 7711 | } |
| 7712 | } else { |
| 7713 | // Explicitly instantiate a variable template. |
| 7714 | |
| 7715 | // C++1y [dcl.spec.auto]p6: |
| 7716 | // ... A program that uses auto or decltype(auto) in a context not |
| 7717 | // explicitly allowed in this section is ill-formed. |
| 7718 | // |
| 7719 | // This includes auto-typed variable template instantiations. |
| 7720 | if (R->isUndeducedType()) { |
| 7721 | Diag(T->getTypeLoc().getLocStart(), |
| 7722 | diag::err_auto_not_allowed_var_inst); |
| 7723 | return true; |
| 7724 | } |
| 7725 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7726 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 7727 | // C++1y [temp.explicit]p3: |
| 7728 | // If the explicit instantiation is for a variable, the unqualified-id |
| 7729 | // in the declaration shall be a template-id. |
| 7730 | Diag(D.getIdentifierLoc(), |
| 7731 | diag::err_explicit_instantiation_without_template_id) |
| 7732 | << PrevTemplate; |
| 7733 | Diag(PrevTemplate->getLocation(), |
| 7734 | diag::note_explicit_instantiation_here); |
| 7735 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7736 | } |
| 7737 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7738 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7739 | TemplateArgumentListInfo TemplateArgs = |
| 7740 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7741 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7742 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 7743 | D.getIdentifierLoc(), TemplateArgs); |
| 7744 | if (Res.isInvalid()) |
| 7745 | return true; |
| 7746 | |
| 7747 | // Ignore access control bits, we don't need them for redeclaration |
| 7748 | // checking. |
| 7749 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7750 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7751 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7752 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7753 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7754 | // or a static data member of a class template specialization, the name of |
| 7755 | // the class template specialization in the qualified-id for the member |
| 7756 | // name shall be a simple-template-id. |
| 7757 | // |
| 7758 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7759 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 7760 | // This does not apply to variable template specializations, where the |
| 7761 | // template-id is in the unqualified-id instead. |
| 7762 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7763 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7764 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7765 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7766 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7767 | // Check the scope of this explicit instantiation. |
| 7768 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7769 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7770 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 7771 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 7772 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7773 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7774 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7775 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7776 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7777 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7778 | if (!HasNoEffect) { |
| 7779 | // Instantiate static data member or variable template. |
| 7780 | |
| 7781 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 7782 | if (PrevTemplate) { |
| 7783 | // Merge attributes. |
| 7784 | if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList()) |
| 7785 | ProcessDeclAttributeList(S, Prev, Attr); |
| 7786 | } |
| 7787 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7788 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 7789 | } |
| 7790 | |
| 7791 | // Check the new variable specialization against the parsed input. |
| 7792 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
| 7793 | Diag(T->getTypeLoc().getLocStart(), |
| 7794 | diag::err_invalid_var_template_spec_type) |
| 7795 | << 0 << PrevTemplate << R << Prev->getType(); |
| 7796 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 7797 | << 2 << PrevTemplate->getDeclName(); |
| 7798 | return true; |
| 7799 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7800 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7801 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7802 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7803 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7804 | |
| 7805 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 7806 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7807 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7808 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7809 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7810 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7811 | HasExplicitTemplateArgs = true; |
| 7812 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7813 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7814 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7815 | // A [...] function [...] can be explicitly instantiated from its template. |
| 7816 | // A member function [...] of a class template can be explicitly |
| 7817 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7818 | // template. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7819 | UnresolvedSet<8> Matches; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7820 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7821 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7822 | P != PEnd; ++P) { |
| 7823 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7824 | if (!HasExplicitTemplateArgs) { |
| 7825 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 7826 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType()); |
| 7827 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7828 | Matches.clear(); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7829 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7830 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7831 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 7832 | break; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7833 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7834 | } |
| 7835 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7836 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7837 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 7838 | if (!FunTmpl) |
| 7839 | continue; |
| 7840 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7841 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7842 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7843 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7844 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7845 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 7846 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7847 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7848 | // Keep track of almost-matches. |
| 7849 | FailedCandidates.addCandidate() |
| 7850 | .set(FunTmpl->getTemplatedDecl(), |
| 7851 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7852 | (void)TDK; |
| 7853 | continue; |
| 7854 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7855 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7856 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7857 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7858 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7859 | // Find the most specialized function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7860 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 7861 | Matches.begin(), Matches.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7862 | D.getIdentifierLoc(), |
| 7863 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 7864 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 7865 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7866 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7867 | if (Result == Matches.end()) |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7868 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7869 | |
| 7870 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 7871 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7872 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 7873 | // C++11 [except.spec]p4 |
| 7874 | // In an explicit instantiation an exception-specification may be specified, |
| 7875 | // but is not required. |
| 7876 | // If an exception-specification is specified in an explicit instantiation |
| 7877 | // directive, it shall be compatible with the exception-specifications of |
| 7878 | // other declarations of that function. |
| 7879 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 7880 | if (FPT->hasExceptionSpec()) { |
| 7881 | unsigned DiagID = |
| 7882 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 7883 | if (getLangOpts().MicrosoftExt) |
| 7884 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 7885 | bool Result = CheckEquivalentExceptionSpec( |
| 7886 | PDiag(DiagID) << Specialization->getType(), |
| 7887 | PDiag(diag::note_explicit_instantiation_here), |
| 7888 | Specialization->getType()->getAs<FunctionProtoType>(), |
| 7889 | Specialization->getLocation(), FPT, D.getLocStart()); |
| 7890 | // In Microsoft mode, mismatching exception specifications just cause a |
| 7891 | // warning. |
| 7892 | if (!getLangOpts().MicrosoftExt && Result) |
| 7893 | return true; |
| 7894 | } |
| 7895 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7896 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7897 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7898 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 7899 | << Specialization |
| 7900 | << (Specialization->getTemplateSpecializationKind() == |
| 7901 | TSK_ExplicitSpecialization); |
| 7902 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 7903 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7904 | } |
| 7905 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7906 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7907 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 7908 | PrevDecl = Specialization; |
| 7909 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7910 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7911 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7912 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7913 | PrevDecl, |
| 7914 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7915 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7916 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7917 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7918 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7919 | // FIXME: We may still want to build some representation of this |
| 7920 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7921 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7922 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7923 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 7924 | |
| 7925 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 2aa7acf | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 7926 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 7927 | if (Attr) |
| 7928 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7929 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7930 | if (Specialization->isDefined()) { |
| 7931 | // Let the ASTConsumer know that this function has been explicitly |
| 7932 | // instantiated now, and its linkage might have changed. |
| 7933 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 7934 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 7935 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7936 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7937 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7938 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7939 | // or a static data member of a class template specialization, the name of |
| 7940 | // the class template specialization in the qualified-id for the member |
| 7941 | // name shall be a simple-template-id. |
| 7942 | // |
| 7943 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7944 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7945 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7946 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7947 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7948 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7949 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7950 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7951 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7952 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7953 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7954 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7955 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7956 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7957 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7958 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7959 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7960 | } |
| 7961 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7962 | TypeResult |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7963 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 7964 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 7965 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 7966 | // This has to hold, because SS is expected to be defined. |
| 7967 | assert(Name && "Expected a name in a dependent tag"); |
| 7968 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7969 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7970 | if (!NNS) |
| 7971 | return true; |
| 7972 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7973 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 7974 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 7975 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 7976 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7977 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 7978 | return true; |
| 7979 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7980 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7981 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7982 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7983 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 7984 | |
| 7985 | // Create type-source location information for this type. |
| 7986 | TypeLocBuilder TLB; |
| 7987 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7988 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7989 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 7990 | TL.setNameLoc(NameLoc); |
| 7991 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7992 | } |
| 7993 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7994 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7995 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 7996 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 7997 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7998 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7999 | return true; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8000 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8001 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8002 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8003 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8004 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8005 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8006 | << FixItHint::CreateRemoval(TypenameLoc); |
| 8007 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8008 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8009 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 8010 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 8011 | if (T.isNull()) |
| 8012 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8013 | |
| 8014 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 8015 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8016 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8017 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8018 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8019 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8020 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8021 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8022 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8023 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8024 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8025 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8026 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8027 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8028 | } |
| 8029 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8030 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8031 | Sema::ActOnTypenameType(Scope *S, |
| 8032 | SourceLocation TypenameLoc, |
| 8033 | const CXXScopeSpec &SS, |
| 8034 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8035 | TemplateTy TemplateIn, |
| 8036 | SourceLocation TemplateNameLoc, |
| 8037 | SourceLocation LAngleLoc, |
| 8038 | ASTTemplateArgsPtr TemplateArgsIn, |
| 8039 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8040 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 8041 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8042 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 8043 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 8044 | diag::ext_typename_outside_of_template) |
| 8045 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8046 | |
| 8047 | // Translate the parser's template argument list in our AST format. |
| 8048 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 8049 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 8050 | |
| 8051 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8052 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 8053 | // Construct a dependent template specialization type. |
| 8054 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 8055 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8056 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 8057 | DTN->getQualifier(), |
| 8058 | DTN->getIdentifier(), |
| 8059 | TemplateArgs); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8060 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8061 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 8062 | TypeLocBuilder Builder; |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8063 | DependentTemplateSpecializationTypeLoc SpecTL |
| 8064 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8065 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 8066 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 8067 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8068 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8069 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8070 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8071 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8072 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8073 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 8074 | } |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8075 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8076 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 8077 | if (T.isNull()) |
| 8078 | return true; |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8079 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8080 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8081 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8082 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8083 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 8084 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 8085 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8086 | SpecTL.setLAngleLoc(LAngleLoc); |
| 8087 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8088 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 8089 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 8090 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8091 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 8092 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 8093 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8094 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 8095 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 8096 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 8097 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 8098 | } |
| 8099 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 8100 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8101 | /// Determine whether this failed name lookup should be treated as being |
| 8102 | /// disabled by a usage of std::enable_if. |
| 8103 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
| 8104 | SourceRange &CondRange) { |
| 8105 | // We must be looking for a ::type... |
| 8106 | if (!II.isStr("type")) |
| 8107 | return false; |
| 8108 | |
| 8109 | // ... within an explicitly-written template specialization... |
| 8110 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 8111 | return false; |
| 8112 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8113 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 8114 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 8115 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8116 | return false; |
| 8117 | const TemplateSpecializationType *EnableIfTST = |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8118 | cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr()); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8119 | |
| 8120 | // ... which names a complete class template declaration... |
| 8121 | const TemplateDecl *EnableIfDecl = |
| 8122 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 8123 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 8124 | return false; |
| 8125 | |
| 8126 | // ... called "enable_if". |
| 8127 | const IdentifierInfo *EnableIfII = |
| 8128 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 8129 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 8130 | return false; |
| 8131 | |
| 8132 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8133 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8134 | return true; |
| 8135 | } |
| 8136 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8137 | /// \brief Build the type that describes a C++ typename specifier, |
| 8138 | /// e.g., "typename T::type". |
| 8139 | QualType |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8140 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 8141 | SourceLocation KeywordLoc, |
| 8142 | NestedNameSpecifierLoc QualifierLoc, |
| 8143 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8144 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8145 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8146 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8147 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8148 | DeclContext *Ctx = computeDeclContext(SS); |
| 8149 | if (!Ctx) { |
| 8150 | // If the nested-name-specifier is dependent and couldn't be |
| 8151 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8152 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 8153 | return Context.getDependentNameType(Keyword, |
| 8154 | QualifierLoc.getNestedNameSpecifier(), |
| 8155 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 8156 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8157 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8158 | // If the nested-name-specifier refers to the current instantiation, |
| 8159 | // the "typename" keyword itself is superfluous. In C++03, the |
| 8160 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 8161 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 8162 | // 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] | 8163 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8164 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 8165 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8166 | |
| 8167 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8168 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 8169 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8170 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8171 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8172 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8173 | case LookupResult::NotFound: { |
| 8174 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 8175 | // a more specific diagnostic. |
| 8176 | SourceRange CondRange; |
| 8177 | if (isEnableIf(QualifierLoc, II, CondRange)) { |
| 8178 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
| 8179 | << Ctx << CondRange; |
| 8180 | return QualType(); |
| 8181 | } |
| 8182 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 8183 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8184 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8185 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8186 | |
| 8187 | case LookupResult::FoundUnresolvedValue: { |
| 8188 | // We found a using declaration that is a value. Most likely, the using |
| 8189 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8190 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8191 | IILoc); |
| 8192 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 8193 | << Name << Ctx << FullRange; |
| 8194 | if (UnresolvedUsingValueDecl *Using |
| 8195 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8196 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8197 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 8198 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 8199 | } |
| 8200 | } |
| 8201 | // Fall through to create a dependent typename type, from which we can recover |
| 8202 | // better. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8203 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 8204 | case LookupResult::NotFoundInCurrentInstantiation: |
| 8205 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8206 | return Context.getDependentNameType(Keyword, |
| 8207 | QualifierLoc.getNestedNameSpecifier(), |
| 8208 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8209 | |
| 8210 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8211 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8212 | // We found a type. Build an ElaboratedType, since the |
| 8213 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 8214 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8215 | return Context.getElaboratedType(ETK_Typename, |
| 8216 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8217 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8218 | } |
| 8219 | |
| 8220 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 8221 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8222 | break; |
| 8223 | |
| 8224 | case LookupResult::FoundOverloaded: |
| 8225 | DiagID = diag::err_typename_nested_not_type; |
| 8226 | Referenced = *Result.begin(); |
| 8227 | break; |
| 8228 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 8229 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8230 | return QualType(); |
| 8231 | } |
| 8232 | |
| 8233 | // If we get here, it's because name lookup did not find a |
| 8234 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8235 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8236 | IILoc); |
| 8237 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8238 | if (Referenced) |
| 8239 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 8240 | << Name; |
| 8241 | return QualType(); |
| 8242 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8243 | |
| 8244 | namespace { |
| 8245 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 8246 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8247 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8248 | SourceLocation Loc; |
| 8249 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8250 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8251 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 8252 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8253 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8254 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8255 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8256 | DeclarationName Entity) |
| 8257 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8258 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8259 | |
| 8260 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8261 | /// transformed. |
| 8262 | /// |
| 8263 | /// For the purposes of type reconstruction, a type has already been |
| 8264 | /// transformed if it is NULL or if it is not dependent. |
| 8265 | bool AlreadyTransformed(QualType T) { |
| 8266 | return T.isNull() || !T->isDependentType(); |
| 8267 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8268 | |
| 8269 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8270 | /// rebuilt. |
| 8271 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8272 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8273 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 8274 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8275 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 8276 | /// \brief Sets the "base" location and entity when that |
| 8277 | /// information is known based on another transformation. |
| 8278 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 8279 | this->Loc = Loc; |
| 8280 | this->Entity = Entity; |
| 8281 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 8282 | |
| 8283 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 8284 | // Lambdas never need to be transformed. |
| 8285 | return E; |
| 8286 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8287 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 8288 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8289 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8290 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 8291 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8292 | /// 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] | 8293 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8294 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8295 | /// partial specialization thereof). This routine will rebuild that type now |
| 8296 | /// that we have entered the declarator's scope, which may produce different |
| 8297 | /// canonical types, e.g., |
| 8298 | /// |
| 8299 | /// \code |
| 8300 | /// template<typename T> |
| 8301 | /// struct X { |
| 8302 | /// typedef T* pointer; |
| 8303 | /// pointer data(); |
| 8304 | /// }; |
| 8305 | /// |
| 8306 | /// template<typename T> |
| 8307 | /// typename X<T>::pointer X<T>::data() { ... } |
| 8308 | /// \endcode |
| 8309 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 8310 | /// 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] | 8311 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 8312 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8313 | /// 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] | 8314 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 8315 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8316 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 8317 | SourceLocation Loc, |
| 8318 | DeclarationName Name) { |
| 8319 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8320 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8321 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8322 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 8323 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 8324 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8325 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8326 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8327 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 8328 | DeclarationName()); |
| 8329 | return Rebuilder.TransformExpr(E); |
| 8330 | } |
| 8331 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8332 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8333 | if (SS.isInvalid()) |
| 8334 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8335 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8336 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8337 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 8338 | DeclarationName()); |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8339 | NestedNameSpecifierLoc Rebuilt |
| 8340 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 8341 | if (!Rebuilt) |
| 8342 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8343 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8344 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8345 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8346 | } |
| 8347 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8348 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 8349 | /// instantiation. |
| 8350 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 8351 | TemplateParameterList *Params) { |
| 8352 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 8353 | Decl *Param = Params->getParam(I); |
| 8354 | |
| 8355 | // There is nothing to rebuild in a type parameter. |
| 8356 | if (isa<TemplateTypeParmDecl>(Param)) |
| 8357 | continue; |
| 8358 | |
| 8359 | // Rebuild the template parameter list of a template template parameter. |
| 8360 | if (TemplateTemplateParmDecl *TTP |
| 8361 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 8362 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 8363 | TTP->getTemplateParameters())) |
| 8364 | return true; |
| 8365 | |
| 8366 | continue; |
| 8367 | } |
| 8368 | |
| 8369 | // Rebuild the type of a non-type template parameter. |
| 8370 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 8371 | TypeSourceInfo *NewTSI |
| 8372 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 8373 | NTTP->getLocation(), |
| 8374 | NTTP->getDeclName()); |
| 8375 | if (!NewTSI) |
| 8376 | return true; |
| 8377 | |
| 8378 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 8379 | NTTP->setTypeSourceInfo(NewTSI); |
| 8380 | NTTP->setType(NewTSI->getType()); |
| 8381 | } |
| 8382 | } |
| 8383 | |
| 8384 | return false; |
| 8385 | } |
| 8386 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8387 | /// \brief Produces a formatted string that describes the binding of |
| 8388 | /// template parameters to template arguments. |
| 8389 | std::string |
| 8390 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8391 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8392 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8393 | } |
| 8394 | |
| 8395 | std::string |
| 8396 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8397 | const TemplateArgument *Args, |
| 8398 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8399 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8400 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8401 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8402 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8403 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8404 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8405 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8406 | if (I >= NumArgs) |
| 8407 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8408 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8409 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8410 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8411 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8412 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8413 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8414 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8415 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8416 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8417 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8418 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8419 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8420 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 8421 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8422 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8423 | |
| 8424 | Out << ']'; |
| 8425 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8426 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8427 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8428 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 8429 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8430 | if (!FD) |
| 8431 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8432 | |
| 8433 | LateParsedTemplate *LPT = new LateParsedTemplate; |
| 8434 | |
| 8435 | // Take tokens to avoid allocations |
| 8436 | LPT->Toks.swap(Toks); |
| 8437 | LPT->D = FnD; |
Chandler Carruth | 52cee4d | 2015-03-26 09:08:15 +0000 | [diff] [blame] | 8438 | LateParsedTemplateMap.insert(std::make_pair(FD, LPT)); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8439 | |
| 8440 | FD->setLateTemplateParsed(true); |
| 8441 | } |
| 8442 | |
| 8443 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 8444 | if (!FD) |
| 8445 | return; |
| 8446 | FD->setLateTemplateParsed(false); |
| 8447 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8448 | |
| 8449 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 8450 | DeclContext *DC = CurContext; |
| 8451 | |
| 8452 | while (DC) { |
| 8453 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 8454 | const FunctionDecl *FD = RD->isLocalClass(); |
| 8455 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 8456 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 8457 | return false; |
| 8458 | |
| 8459 | DC = DC->getParent(); |
| 8460 | } |
| 8461 | return false; |
| 8462 | } |