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" |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 21 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 22 | #include "clang/Basic/PartialDiagnostic.h" |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Sema/DeclSpec.h" |
| 25 | #include "clang/Sema/Lookup.h" |
| 26 | #include "clang/Sema/ParsedTemplate.h" |
| 27 | #include "clang/Sema/Scope.h" |
| 28 | #include "clang/Sema/SemaInternal.h" |
| 29 | #include "clang/Sema/Template.h" |
| 30 | #include "clang/Sema/TemplateDeduction.h" |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallBitVector.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 34 | using namespace clang; |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 35 | using namespace sema; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 36 | |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 37 | // Exported for use by Parser. |
| 38 | SourceRange |
| 39 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
| 40 | unsigned N) { |
| 41 | if (!N) return SourceRange(); |
| 42 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
| 43 | } |
| 44 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 45 | /// \brief Determine whether the declaration found is acceptable as the name |
| 46 | /// of a template and, if so, return that template declaration. Otherwise, |
| 47 | /// returns NULL. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 48 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 49 | NamedDecl *Orig, |
| 50 | bool AllowFunctionTemplates) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 51 | NamedDecl *D = Orig->getUnderlyingDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 53 | if (isa<TemplateDecl>(D)) { |
| 54 | if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 55 | return nullptr; |
| 56 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 57 | return Orig; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 58 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 60 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 61 | // C++ [temp.local]p1: |
| 62 | // Like normal (non-template) classes, class templates have an |
| 63 | // injected-class-name (Clause 9). The injected-class-name |
| 64 | // can be used with or without a template-argument-list. When |
| 65 | // it is used without a template-argument-list, it is |
| 66 | // equivalent to the injected-class-name followed by the |
| 67 | // template-parameters of the class template enclosed in |
| 68 | // <>. When it is used with a template-argument-list, it |
| 69 | // refers to the specified class template specialization, |
| 70 | // which could be the current specialization or another |
| 71 | // specialization. |
| 72 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 568a071 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 73 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 74 | if (Record->getDescribedClassTemplate()) |
| 75 | return Record->getDescribedClassTemplate(); |
| 76 | |
| 77 | if (ClassTemplateSpecializationDecl *Spec |
| 78 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 79 | return Spec->getSpecializedTemplate(); |
| 80 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 82 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 83 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 85 | return nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 88 | void Sema::FilterAcceptableTemplateNames(LookupResult &R, |
| 89 | bool AllowFunctionTemplates) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 90 | // The set of class templates we've already seen. |
| 91 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 92 | LookupResult::Filter filter = R.makeFilter(); |
| 93 | while (filter.hasNext()) { |
| 94 | NamedDecl *Orig = filter.next(); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 95 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig, |
| 96 | AllowFunctionTemplates); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 97 | if (!Repl) |
| 98 | filter.erase(); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 99 | else if (Repl != Orig) { |
| 100 | |
| 101 | // C++ [temp.local]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 102 | // 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] | 103 | // 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] | 104 | // one base class). If all of the injected-class-names that are found |
| 105 | // refer to specializations of the same class template, and if the name |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 106 | // is used as a template-name, the reference refers to the class |
| 107 | // template itself and not a specialization thereof, and is not |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 108 | // ambiguous. |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 109 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 110 | if (!ClassTemplates.insert(ClassTmpl).second) { |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 111 | filter.erase(); |
| 112 | continue; |
| 113 | } |
John McCall | bd8062d | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 114 | |
| 115 | // FIXME: we promote access to public here as a workaround to |
| 116 | // the fact that LookupResult doesn't let us remember that we |
| 117 | // found this template through a particular injected class name, |
| 118 | // which means we end up doing nasty things to the invariants. |
| 119 | // Pretending that access is public is *much* safer. |
| 120 | filter.replace(Repl, AS_public); |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 121 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 122 | } |
| 123 | filter.done(); |
| 124 | } |
| 125 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 126 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R, |
| 127 | bool AllowFunctionTemplates) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 128 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 129 | if (isAcceptableTemplateName(Context, *I, AllowFunctionTemplates)) |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 130 | return true; |
| 131 | |
Douglas Gregor | 8b02cd0 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 132 | return false; |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 135 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 136 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 137 | bool hasTemplateKeyword, |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 138 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 139 | ParsedType ObjectTypePtr, |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 140 | bool EnteringContext, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 141 | TemplateTy &TemplateResult, |
| 142 | bool &MemberOfUnknownSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 143 | assert(getLangOpts().CPlusPlus && "No template names in C!"); |
Douglas Gregor | 411e5ac | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 144 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 145 | DeclarationName TName; |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 146 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 147 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 148 | switch (Name.getKind()) { |
| 149 | case UnqualifiedId::IK_Identifier: |
| 150 | TName = DeclarationName(Name.Identifier); |
| 151 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 152 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 153 | case UnqualifiedId::IK_OperatorFunctionId: |
| 154 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 155 | Name.OperatorFunctionId.Operator); |
| 156 | break; |
| 157 | |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 158 | case UnqualifiedId::IK_LiteralOperatorId: |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 159 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 160 | break; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 161 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 162 | default: |
| 163 | return TNK_Non_template; |
| 164 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 166 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 168 | LookupResult R(*this, TName, Name.getLocStart(), LookupOrdinaryName); |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 169 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 170 | MemberOfUnknownSpecialization); |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 171 | if (R.empty()) return TNK_Non_template; |
| 172 | if (R.isAmbiguous()) { |
| 173 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 174 | R.suppressDiagnostics(); |
John McCall | fb3f9ba | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 175 | |
| 176 | // FIXME: we might have ambiguous templates, in which case we |
| 177 | // should at least parse them properly! |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 178 | return TNK_Non_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 179 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 180 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 181 | TemplateName Template; |
| 182 | TemplateNameKind TemplateKind; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 184 | unsigned ResultCount = R.end() - R.begin(); |
| 185 | if (ResultCount > 1) { |
| 186 | // We assume that we'll preserve the qualifier from a function |
| 187 | // template name in other ways. |
| 188 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 189 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 190 | |
| 191 | // We'll do this lookup again later. |
| 192 | R.suppressDiagnostics(); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 193 | } else { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 194 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 195 | |
| 196 | if (SS.isSet() && !SS.isInvalid()) { |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 197 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 198 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 199 | hasTemplateKeyword, TD); |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 200 | } else { |
| 201 | Template = TemplateName(TD); |
| 202 | } |
| 203 | |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 204 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 205 | TemplateKind = TNK_Function_template; |
John McCall | dcc7140 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 206 | |
| 207 | // We'll do this lookup again later. |
| 208 | R.suppressDiagnostics(); |
| 209 | } else { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 210 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 211 | isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD)); |
| 212 | TemplateKind = |
| 213 | isa<VarTemplateDecl>(TD) ? TNK_Var_template : TNK_Type_template; |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 214 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 215 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 217 | TemplateResult = TemplateTy::make(Template); |
| 218 | return TemplateKind; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 219 | } |
| 220 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 221 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 222 | SourceLocation IILoc, |
| 223 | Scope *S, |
| 224 | const CXXScopeSpec *SS, |
| 225 | TemplateTy &SuggestedTemplate, |
| 226 | TemplateNameKind &SuggestedKind) { |
| 227 | // We can't recover unless there's a dependent scope specifier preceding the |
| 228 | // template name. |
Douglas Gregor | 20c38a7 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 229 | // FIXME: Typo correction? |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 230 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 231 | computeDeclContext(*SS)) |
| 232 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 233 | |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 234 | // The code is missing a 'template' keyword prior to the dependent template |
| 235 | // name. |
| 236 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 237 | Diag(IILoc, diag::err_template_kw_missing) |
| 238 | << Qualifier << II.getName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 239 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 240 | SuggestedTemplate |
Douglas Gregor | 18473f3 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 241 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 242 | SuggestedKind = TNK_Dependent_template_name; |
| 243 | return true; |
| 244 | } |
| 245 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 246 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 247 | Scope *S, CXXScopeSpec &SS, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 248 | QualType ObjectType, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 249 | bool EnteringContext, |
| 250 | bool &MemberOfUnknownSpecialization) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 251 | // Determine where to perform name lookup |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 252 | MemberOfUnknownSpecialization = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 253 | DeclContext *LookupCtx = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 254 | bool isDependent = false; |
| 255 | if (!ObjectType.isNull()) { |
| 256 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 257 | // x->B::f, and we are looking into the type of the object. |
| 258 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 259 | LookupCtx = computeDeclContext(ObjectType); |
| 260 | isDependent = ObjectType->isDependentType(); |
Richard Smith | 5ed7956 | 2013-06-07 20:03:01 +0000 | [diff] [blame] | 261 | assert((isDependent || !ObjectType->isIncompleteType() || |
| 262 | ObjectType->castAs<TagType>()->isBeingDefined()) && |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 263 | "Caller should have completed object type"); |
Douglas Gregor | bf3a826 | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 264 | |
| 265 | // Template names cannot appear inside an Objective-C class or object type. |
| 266 | if (ObjectType->isObjCObjectOrInterfaceType()) { |
| 267 | Found.clear(); |
| 268 | return; |
| 269 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 270 | } else if (SS.isSet()) { |
| 271 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 272 | // so long into the context associated with the prior nested-name-specifier. |
| 273 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 274 | isDependent = isDependentScopeSpecifier(SS); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 275 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 276 | // The declaration context must be complete. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 277 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 278 | return; |
| 279 | } |
| 280 | |
| 281 | bool ObjectTypeSearchedInScope = false; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 282 | bool AllowFunctionTemplatesInLookup = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 283 | if (LookupCtx) { |
| 284 | // Perform "qualified" name lookup into the declaration context we |
| 285 | // computed, which is either the type of the base of a member access |
| 286 | // expression or the declaration context associated with a prior |
| 287 | // nested-name-specifier. |
| 288 | LookupQualifiedName(Found, LookupCtx); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 289 | if (!ObjectType.isNull() && Found.empty()) { |
| 290 | // C++ [basic.lookup.classref]p1: |
| 291 | // In a class member access expression (5.2.5), if the . or -> token is |
| 292 | // immediately followed by an identifier followed by a <, the |
| 293 | // identifier must be looked up to determine whether the < is the |
| 294 | // beginning of a template argument list (14.2) or a less-than operator. |
| 295 | // The identifier is first looked up in the class of the object |
| 296 | // expression. If the identifier is not found, it is then looked up in |
| 297 | // the context of the entire postfix-expression and shall name a class |
| 298 | // or function template. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 299 | if (S) LookupName(Found, S); |
| 300 | ObjectTypeSearchedInScope = true; |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 301 | AllowFunctionTemplatesInLookup = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 302 | } |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 303 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 304 | // We cannot look into a dependent object type or nested nme |
| 305 | // specifier. |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 306 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 307 | return; |
| 308 | } else { |
| 309 | // Perform unqualified name lookup in the current scope. |
| 310 | LookupName(Found, S); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 311 | |
| 312 | if (!ObjectType.isNull()) |
| 313 | AllowFunctionTemplatesInLookup = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Douglas Gregor | c119dd5 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 316 | if (Found.empty() && !isDependent) { |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 317 | // If we did not find any names, attempt to correct any typos. |
| 318 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 319 | Found.clear(); |
Kaelyn Uhrain | 637b5b3 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 320 | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 321 | auto FilterCCC = llvm::make_unique<CorrectionCandidateCallback>(); |
| 322 | FilterCCC->WantTypeSpecifiers = false; |
| 323 | FilterCCC->WantExpressionKeywords = false; |
| 324 | FilterCCC->WantRemainingKeywords = false; |
| 325 | FilterCCC->WantCXXNamedCasts = true; |
| 326 | if (TypoCorrection Corrected = CorrectTypo( |
| 327 | Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS, |
| 328 | std::move(FilterCCC), CTK_ErrorRecovery, LookupCtx)) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 329 | Found.setLookupName(Corrected.getCorrection()); |
| 330 | if (Corrected.getCorrectionDecl()) |
| 331 | Found.addDecl(Corrected.getCorrectionDecl()); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 332 | FilterAcceptableTemplateNames(Found); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 333 | if (!Found.empty()) { |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 334 | if (LookupCtx) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 335 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 336 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 337 | Name.getAsString() == CorrectedStr; |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 338 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
| 339 | << Name << LookupCtx << DroppedSpecifier |
| 340 | << SS.getRange()); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 341 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 342 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 343 | } |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 344 | } |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 345 | } else { |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 346 | Found.setLookupName(Name); |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 350 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 351 | if (Found.empty()) { |
| 352 | if (isDependent) |
| 353 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 354 | return; |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 355 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 356 | |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 357 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && |
Richard Smith | e7d67f2 | 2013-09-03 21:22:41 +0000 | [diff] [blame] | 358 | !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 359 | // C++03 [basic.lookup.classref]p1: |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 360 | // [...] If the lookup in the class of the object expression finds a |
| 361 | // template, the name is also looked up in the context of the entire |
| 362 | // postfix-expression and [...] |
| 363 | // |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 364 | // Note: C++11 does not perform this second lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 365 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 366 | LookupOrdinaryName); |
| 367 | LookupName(FoundOuter, S); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 368 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 369 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 370 | if (FoundOuter.empty()) { |
| 371 | // - if the name is not found, the name found in the class of the |
| 372 | // object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 373 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 374 | FoundOuter.isAmbiguous()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 375 | // - if the name is found in the context of the entire |
| 376 | // postfix-expression and does not name a class template, the name |
| 377 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 378 | FoundOuter.clear(); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 379 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 380 | // - if the name found is a class template, it must refer to the same |
| 381 | // entity as the one found in the class of the object expression, |
| 382 | // otherwise the program is ill-formed. |
| 383 | if (!Found.isSingleResult() || |
| 384 | Found.getFoundDecl()->getCanonicalDecl() |
| 385 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 386 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 2f96e9f | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 387 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 388 | << Found.getLookupName() |
| 389 | << ObjectType; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 390 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 391 | diag::note_ambig_member_ref_object_type) |
| 392 | << ObjectType; |
| 393 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 394 | diag::note_ambig_member_ref_scope); |
| 395 | |
| 396 | // Recover by taking the template that we found in the object |
| 397 | // expression's type. |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 403 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 404 | /// was just parsed. This is only possible with an explicit scope |
| 405 | /// specifier naming a dependent type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 406 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 407 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 408 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 409 | const DeclarationNameInfo &NameInfo, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 410 | bool isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 411 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 412 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 413 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 414 | if (!isAddressOfOperand && |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 415 | isa<CXXMethodDecl>(DC) && |
| 416 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 417 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 418 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 419 | // Since the 'this' expression is synthesized, we don't need to |
| 420 | // perform the double-lookup check. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 421 | NamedDecl *FirstQualifierInScope = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 422 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 423 | return CXXDependentScopeMemberExpr::Create( |
| 424 | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
| 425 | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
| 426 | FirstQualifierInScope, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 429 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 430 | } |
| 431 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 432 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 433 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 434 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 435 | const DeclarationNameInfo &NameInfo, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 436 | const TemplateArgumentListInfo *TemplateArgs) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 437 | return DependentScopeDeclRefExpr::Create( |
| 438 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 439 | TemplateArgs); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 442 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 443 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 444 | /// declaration at location Loc. Returns true to indicate that this is |
| 445 | /// an error, and false otherwise. |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 446 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 447 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 448 | |
| 449 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 450 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 451 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 452 | |
| 453 | // C++ [temp.local]p4: |
| 454 | // A template-parameter shall not be redeclared within its |
| 455 | // scope (including nested scopes). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 457 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 458 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 459 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 462 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 463 | /// the parameter D to reference the templated declaration and return a pointer |
| 464 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 465 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 466 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 467 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 468 | return Temp; |
| 469 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 470 | return nullptr; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 473 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 474 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 475 | assert(Kind == Template && |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 476 | "Only template template arguments can be pack expansions here"); |
| 477 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 478 | "Template template argument pack expansion without packs"); |
| 479 | ParsedTemplateArgument Result(*this); |
| 480 | Result.EllipsisLoc = EllipsisLoc; |
| 481 | return Result; |
| 482 | } |
| 483 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 484 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 485 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 486 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 487 | switch (Arg.getKind()) { |
| 488 | case ParsedTemplateArgument::Type: { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 489 | TypeSourceInfo *DI; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 490 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 491 | if (!DI) |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 492 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 493 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 494 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 495 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 496 | case ParsedTemplateArgument::NonType: { |
| 497 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 498 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 499 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 500 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 501 | case ParsedTemplateArgument::Template: { |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 502 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 503 | TemplateArgument TArg; |
| 504 | if (Arg.getEllipsisLoc().isValid()) |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 505 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 506 | else |
| 507 | TArg = Template; |
| 508 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 509 | Arg.getScopeSpec().getWithLocInContext( |
| 510 | SemaRef.Context), |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 511 | Arg.getLocation(), |
| 512 | Arg.getEllipsisLoc()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 515 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 516 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 517 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 518 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 519 | /// \brief Translates template arguments as provided by the parser |
| 520 | /// into template arguments used by semantic analysis. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 521 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 522 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 523 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 524 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 525 | TemplateArgsIn[I])); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 526 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 527 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 528 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 529 | SourceLocation Loc, |
| 530 | IdentifierInfo *Name) { |
| 531 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
| 532 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 533 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 534 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 535 | } |
| 536 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 537 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 538 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 539 | /// the keyword "typename" was used to declare the type parameter |
| 540 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 541 | /// "class" or "typename" keyword. ParamName is the name of the |
| 542 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 543 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 544 | /// If the type parameter has a default argument, it will be added |
| 545 | /// later via ActOnTypeParameterDefault. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 546 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 547 | SourceLocation EllipsisLoc, |
| 548 | SourceLocation KeyLoc, |
| 549 | IdentifierInfo *ParamName, |
| 550 | SourceLocation ParamNameLoc, |
| 551 | unsigned Depth, unsigned Position, |
| 552 | SourceLocation EqualLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 553 | ParsedType DefaultArg) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | assert(S->isTemplateParamScope() && |
| 555 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 556 | bool Invalid = false; |
| 557 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 558 | SourceLocation Loc = ParamNameLoc; |
| 559 | if (!ParamName) |
| 560 | Loc = KeyLoc; |
| 561 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 562 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 563 | TemplateTypeParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 564 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 565 | KeyLoc, Loc, Depth, Position, ParamName, |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 566 | Typename, IsParameterPack); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 567 | Param->setAccess(AS_public); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 568 | if (Invalid) |
| 569 | Param->setInvalidDecl(); |
| 570 | |
| 571 | if (ParamName) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 572 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 573 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 574 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 575 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 576 | IdResolver.AddDecl(Param); |
| 577 | } |
| 578 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 579 | // C++0x [temp.param]p9: |
| 580 | // A default template-argument may be specified for any kind of |
| 581 | // template-parameter that is not a template parameter pack. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 582 | if (DefaultArg && IsParameterPack) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 583 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 584 | DefaultArg = ParsedType(); |
| 585 | } |
| 586 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 587 | // Handle the default argument, if provided. |
| 588 | if (DefaultArg) { |
| 589 | TypeSourceInfo *DefaultTInfo; |
| 590 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 591 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 592 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 593 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 594 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 595 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 596 | UPPC_DefaultArgument)) |
| 597 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 598 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 599 | // Check the template argument itself. |
| 600 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 601 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 602 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 603 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 604 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 605 | Param->setDefaultArgument(DefaultTInfo, false); |
| 606 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 607 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 608 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 611 | /// \brief Check that the type of a non-type template parameter is |
| 612 | /// well-formed. |
| 613 | /// |
| 614 | /// \returns the (possibly-promoted) parameter type if valid; |
| 615 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 616 | QualType |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 617 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 618 | // We don't allow variably-modified types as the type of non-type template |
| 619 | // parameters. |
| 620 | if (T->isVariablyModifiedType()) { |
| 621 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 622 | << T; |
| 623 | return QualType(); |
| 624 | } |
| 625 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 626 | // C++ [temp.param]p4: |
| 627 | // |
| 628 | // A non-type template-parameter shall have one of the following |
| 629 | // (optionally cv-qualified) types: |
| 630 | // |
| 631 | // -- integral or enumeration type, |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 632 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | // -- pointer to object or pointer to function, |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 634 | T->isPointerType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | // -- reference to object or reference to function, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 636 | T->isReferenceType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 637 | // -- pointer to member, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 638 | T->isMemberPointerType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 639 | // -- std::nullptr_t. |
| 640 | T->isNullPtrType() || |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 641 | // If T is a dependent type, we can't do the check now, so we |
| 642 | // assume that it is well-formed. |
Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 643 | T->isDependentType()) { |
| 644 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 645 | // are ignored when determining its type. |
| 646 | return T.getUnqualifiedType(); |
| 647 | } |
| 648 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 649 | // C++ [temp.param]p8: |
| 650 | // |
| 651 | // A non-type template-parameter of type "array of T" or |
| 652 | // "function returning T" is adjusted to be of type "pointer to |
| 653 | // T" or "pointer to function returning T", respectively. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 654 | else if (T->isArrayType() || T->isFunctionType()) |
| 655 | return Context.getDecayedType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 656 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 657 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 658 | << T; |
| 659 | |
| 660 | return QualType(); |
| 661 | } |
| 662 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 663 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 664 | unsigned Depth, |
| 665 | unsigned Position, |
| 666 | SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 667 | Expr *Default) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 668 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 669 | QualType T = TInfo->getType(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 670 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 671 | assert(S->isTemplateParamScope() && |
| 672 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 673 | bool Invalid = false; |
| 674 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 675 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 676 | if (T.isNull()) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 677 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 678 | Invalid = true; |
| 679 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 680 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 681 | IdentifierInfo *ParamName = D.getIdentifier(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 682 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 683 | NonTypeTemplateParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 684 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 685 | D.getLocStart(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 686 | D.getIdentifierLoc(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 687 | Depth, Position, ParamName, T, |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 688 | IsParameterPack, TInfo); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 689 | Param->setAccess(AS_public); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 690 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 691 | if (Invalid) |
| 692 | Param->setInvalidDecl(); |
| 693 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 694 | if (ParamName) { |
| 695 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 696 | ParamName); |
| 697 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 698 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 699 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 700 | IdResolver.AddDecl(Param); |
| 701 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 702 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 703 | // C++0x [temp.param]p9: |
| 704 | // A default template-argument may be specified for any kind of |
| 705 | // template-parameter that is not a template parameter pack. |
| 706 | if (Default && IsParameterPack) { |
| 707 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 708 | Default = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 711 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 712 | if (Default) { |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 713 | // Check for unexpanded parameter packs. |
| 714 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 715 | return Param; |
| 716 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 717 | TemplateArgument Converted; |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 718 | ExprResult DefaultRes = |
| 719 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 720 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 721 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 722 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 723 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 724 | Default = DefaultRes.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 725 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 726 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 727 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 728 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 729 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 730 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 731 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 732 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 733 | /// parameter (e.g. T in template <template \<typename> class T> class array) |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 734 | /// has been parsed. S is the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 735 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 736 | SourceLocation TmpLoc, |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 737 | TemplateParameterList *Params, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 738 | SourceLocation EllipsisLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 739 | IdentifierInfo *Name, |
| 740 | SourceLocation NameLoc, |
| 741 | unsigned Depth, |
| 742 | unsigned Position, |
| 743 | SourceLocation EqualLoc, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 744 | ParsedTemplateArgument Default) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 745 | assert(S->isTemplateParamScope() && |
| 746 | "Template template parameter not in template parameter scope!"); |
| 747 | |
| 748 | // Construct the parameter object. |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 749 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 750 | TemplateTemplateParmDecl *Param = |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 751 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 752 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 753 | Depth, Position, IsParameterPack, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 754 | Name, Params); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 755 | Param->setAccess(AS_public); |
| 756 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 757 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 758 | // into the scope and lookup mechanisms. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 759 | if (Name) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 760 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 761 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 762 | S->AddDecl(Param); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 763 | IdResolver.AddDecl(Param); |
| 764 | } |
| 765 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 766 | if (Params->size() == 0) { |
| 767 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 768 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 769 | Param->setInvalidDecl(); |
| 770 | } |
| 771 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 772 | // C++0x [temp.param]p9: |
| 773 | // A default template-argument may be specified for any kind of |
| 774 | // template-parameter that is not a template parameter pack. |
| 775 | if (IsParameterPack && !Default.isInvalid()) { |
| 776 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 777 | Default = ParsedTemplateArgument(); |
| 778 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 779 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 780 | if (!Default.isInvalid()) { |
| 781 | // Check only that we have a template template argument. We don't want to |
| 782 | // try to check well-formedness now, because our template template parameter |
| 783 | // might have dependent types in its template parameters, which we wouldn't |
| 784 | // be able to match now. |
| 785 | // |
| 786 | // If none of the template template parameter's template arguments mention |
| 787 | // other template parameters, we could actually perform more checking here. |
| 788 | // However, it isn't worth doing. |
| 789 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 790 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 791 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 792 | << DefaultArg.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 793 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 794 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 795 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 796 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 797 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 798 | DefaultArg.getArgument().getAsTemplate(), |
| 799 | UPPC_DefaultArgument)) |
| 800 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 801 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 802 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 803 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 804 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 805 | return Param; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 808 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 809 | /// contains the template parameters in Params/NumParams. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 810 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 811 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 812 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 813 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 814 | SourceLocation LAngleLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 815 | Decl **Params, unsigned NumParams, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 816 | SourceLocation RAngleLoc) { |
| 817 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 818 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 819 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 820 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 821 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 822 | RAngleLoc); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 823 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 824 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 825 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 826 | if (SS.isSet()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 827 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 828 | } |
| 829 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 830 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 831 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 832 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 833 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 834 | AttributeList *Attr, |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 835 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 836 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 837 | SourceLocation FriendLoc, |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 838 | unsigned NumOuterTemplateParamLists, |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 839 | TemplateParameterList** OuterTemplateParamLists, |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 840 | SkipBodyInfo *SkipBody) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 841 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 842 | "No template parameters"); |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 843 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 844 | bool Invalid = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 845 | |
| 846 | // Check that we can declare a template here. |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 847 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 848 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 849 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 850 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 851 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 852 | |
| 853 | // There is no such thing as an unnamed class template. |
| 854 | if (!Name) { |
| 855 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 856 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 859 | // Find any previous declaration with this name. For a friend with no |
| 860 | // scope explicitly specified, we only look for tag declarations (per |
| 861 | // C++11 [basic.lookup.elab]p2). |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 862 | DeclContext *SemanticContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 863 | LookupResult Previous(*this, Name, NameLoc, |
| 864 | (SS.isEmpty() && TUK == TUK_Friend) |
| 865 | ? LookupTagName : LookupOrdinaryName, |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 866 | ForRedeclaration); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 867 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 868 | SemanticContext = computeDeclContext(SS, true); |
| 869 | if (!SemanticContext) { |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 870 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 871 | // in the AST, and historically we have just ignored such friend |
| 872 | // class templates, so don't complain here. |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 873 | Diag(NameLoc, TUK == TUK_Friend |
| 874 | ? diag::warn_template_qualified_friend_ignored |
| 875 | : diag::err_template_qualified_declarator_no_match) |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 876 | << SS.getScopeRep() << SS.getRange(); |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 877 | return TUK != TUK_Friend; |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 878 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 879 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 880 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 881 | return true; |
| 882 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 883 | // If we're adding a template to a dependent context, we may need to |
| 884 | // rebuilding some of the types used within the template parameter list, |
| 885 | // now that we know what the current instantiation is. |
| 886 | if (SemanticContext->isDependentContext()) { |
| 887 | ContextRAII SavedContext(*this, SemanticContext); |
| 888 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 889 | Invalid = true; |
Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 890 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
| 891 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 892 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 893 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 894 | } else { |
| 895 | SemanticContext = CurContext; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 896 | LookupName(Previous, S); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 897 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 898 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 899 | if (Previous.isAmbiguous()) |
| 900 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 901 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 902 | NamedDecl *PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 903 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 904 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 905 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 906 | // If there is a previous declaration with the same name, check |
| 907 | // whether this is a valid redeclaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 908 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 909 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 910 | |
| 911 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 912 | // class template partial specialization, or class template specialization. |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 913 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 914 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 915 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 916 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 917 | PrevClassTemplate |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 918 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 919 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 920 | PrevClassTemplate |
| 921 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 922 | ->getSpecializedTemplate(); |
| 923 | } |
| 924 | } |
| 925 | |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 926 | if (TUK == TUK_Friend) { |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 927 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 928 | // [...] When looking for a prior declaration of a class or a function |
| 929 | // 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] | 930 | // function is neither a qualified name nor a template-id, scopes outside |
| 931 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 932 | if (!SS.isSet()) { |
| 933 | DeclContext *OutermostContext = CurContext; |
| 934 | while (!OutermostContext->isFileContext()) |
| 935 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 936 | |
Richard Smith | 61e582f | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 937 | if (PrevDecl && |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 938 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 939 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 940 | SemanticContext = PrevDecl->getDeclContext(); |
| 941 | } else { |
| 942 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 943 | // context we computed is the semantic context for our new |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 944 | // declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 945 | PrevDecl = PrevClassTemplate = nullptr; |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 946 | SemanticContext = OutermostContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 947 | |
| 948 | // Check that the chosen semantic context doesn't already contain a |
| 949 | // declaration of this name as a non-tag type. |
| 950 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
| 951 | ForRedeclaration); |
| 952 | DeclContext *LookupContext = SemanticContext; |
| 953 | while (LookupContext->isTransparentContext()) |
| 954 | LookupContext = LookupContext->getLookupParent(); |
| 955 | LookupQualifiedName(Previous, LookupContext); |
| 956 | |
| 957 | if (Previous.isAmbiguous()) |
| 958 | return true; |
| 959 | |
| 960 | if (Previous.begin() != Previous.end()) |
| 961 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 962 | } |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 963 | } |
Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 964 | } else if (PrevDecl && |
| 965 | !isDeclInScope(PrevDecl, SemanticContext, S, SS.isValid())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 966 | PrevDecl = PrevClassTemplate = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 967 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 968 | if (PrevClassTemplate) { |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 969 | // Ensure that the template parameter lists are compatible. Skip this check |
| 970 | // for a friend in a dependent context: the template parameter list itself |
| 971 | // could be dependent. |
| 972 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 973 | !TemplateParameterListsAreEqual(TemplateParams, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 974 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 975 | /*Complain=*/true, |
| 976 | TPL_TemplateMatch)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 977 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 978 | |
| 979 | // C++ [temp.class]p4: |
| 980 | // In a redeclaration, partial specialization, explicit |
| 981 | // specialization or explicit instantiation of a class template, |
| 982 | // the class-key shall agree in kind with the original class |
| 983 | // template declaration (7.1.5.3). |
| 984 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 985 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
| 986 | TUK == TUK_Definition, KWLoc, *Name)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 987 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 988 | << Name |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 989 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 990 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 991 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 994 | // Check for redefinition of this class template. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 995 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 996 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 997 | // If we have a prior definition that is not visible, treat this as |
| 998 | // simply making that previous definition visible. |
| 999 | NamedDecl *Hidden = nullptr; |
| 1000 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1001 | SkipBody->ShouldSkip = true; |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1002 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
| 1003 | assert(Tmpl && "original definition of a class template is not a " |
| 1004 | "class template?"); |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 1005 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 1006 | makeMergedDefinitionVisible(Tmpl, KWLoc); |
Richard Smith | be3980b | 2015-03-27 00:41:57 +0000 | [diff] [blame] | 1007 | return Def; |
| 1008 | } |
| 1009 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1010 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1011 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1012 | // FIXME: Would it make sense to try to "forget" the previous |
| 1013 | // definition, as part of error recovery? |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1014 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1015 | } |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1016 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1017 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 1018 | // Maybe we will complain about the shadowed template parameter. |
| 1019 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 1020 | // Just pretend that we didn't see the previous declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1021 | PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1022 | } else if (PrevDecl) { |
| 1023 | // C++ [temp]p5: |
| 1024 | // A class template shall not have the same name as any other |
| 1025 | // template, class, function, object, enumeration, enumerator, |
| 1026 | // namespace, or type in the same scope (3.3), except as specified |
| 1027 | // in (14.5.4). |
| 1028 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1029 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1030 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1033 | // Check the template parameter list of this declaration, possibly |
| 1034 | // merging in the template parameter list from the previous class |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1035 | // template declaration. Skip this check for a friend in a dependent |
| 1036 | // context, because the template parameter list might be dependent. |
| 1037 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1038 | CheckTemplateParameterList( |
| 1039 | TemplateParams, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1040 | PrevClassTemplate ? PrevClassTemplate->getTemplateParameters() |
| 1041 | : nullptr, |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1042 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1043 | SemanticContext->isDependentContext()) |
| 1044 | ? TPC_ClassTemplateMember |
| 1045 | : TUK == TUK_Friend ? TPC_FriendClassTemplate |
| 1046 | : TPC_ClassTemplate)) |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1047 | Invalid = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1048 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1049 | if (SS.isSet()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1050 | // 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] | 1051 | // template out-of-line. |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1052 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1053 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1054 | : diag::err_member_decl_does_not_match) |
| 1055 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1056 | Invalid = true; |
| 1057 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1061 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1062 | PrevClassTemplate? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1063 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1064 | /*DelayTypeCreation=*/true); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1065 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1066 | if (NumOuterTemplateParamLists > 0) |
| 1067 | NewClass->setTemplateParameterListsInfo(Context, |
| 1068 | NumOuterTemplateParamLists, |
| 1069 | OuterTemplateParamLists); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1070 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1071 | // Add alignment attributes if necessary; these attributes are checked when |
| 1072 | // the ASTContext lays out the structure. |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1073 | if (TUK == TUK_Definition) { |
| 1074 | AddAlignmentAttributesForRecord(NewClass); |
| 1075 | AddMsStructLayoutForRecord(NewClass); |
| 1076 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1077 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1078 | ClassTemplateDecl *NewTemplate |
| 1079 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1080 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1081 | NewClass, PrevClassTemplate); |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1082 | NewClass->setDescribedClassTemplate(NewTemplate); |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1083 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1084 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1085 | NewTemplate->setModulePrivate(); |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1086 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1087 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1088 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1089 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1090 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1091 | (void)T; |
| 1092 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1093 | // 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] | 1094 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1095 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1096 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1097 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1098 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1099 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1100 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1101 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1102 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1103 | // Set the lexical context of these templates |
| 1104 | NewClass->setLexicalDeclContext(CurContext); |
| 1105 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1106 | |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1107 | if (TUK == TUK_Definition) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1108 | NewClass->startDefinition(); |
| 1109 | |
| 1110 | if (Attr) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1111 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1112 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1113 | if (PrevClassTemplate) |
| 1114 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1115 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1116 | AddPushedVisibilityAttribute(NewClass); |
| 1117 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1118 | if (TUK != TUK_Friend) { |
| 1119 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1120 | Scope *Outer = S; |
| 1121 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1122 | Outer = Outer->getParent(); |
| 1123 | PushOnScopeChains(NewTemplate, Outer); |
| 1124 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1125 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1126 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1127 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1128 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1129 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1130 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1131 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1132 | // Friend templates are visible in fairly strange ways. |
| 1133 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1134 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1135 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1136 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1137 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1138 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1139 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1140 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1141 | FriendDecl *Friend = FriendDecl::Create( |
| 1142 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1143 | Friend->setAccess(AS_public); |
| 1144 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1145 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1146 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1147 | if (Invalid) { |
| 1148 | NewTemplate->setInvalidDecl(); |
| 1149 | NewClass->setInvalidDecl(); |
| 1150 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1151 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1152 | ActOnDocumentableDecl(NewTemplate); |
| 1153 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1154 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1157 | /// \brief Diagnose the presence of a default template argument on a |
| 1158 | /// template parameter, which is ill-formed in certain contexts. |
| 1159 | /// |
| 1160 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1161 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1162 | Sema::TemplateParamListContext TPC, |
| 1163 | SourceLocation ParamLoc, |
| 1164 | SourceRange DefArgRange) { |
| 1165 | switch (TPC) { |
| 1166 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1167 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1168 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1169 | return false; |
| 1170 | |
| 1171 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1172 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1173 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1174 | // A default template-argument shall not be specified in a |
| 1175 | // function template declaration or a function template |
| 1176 | // definition [...] |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1177 | // If a friend function template declaration specifies a default |
| 1178 | // template-argument, that declaration shall be a definition and shall be |
| 1179 | // the only declaration of the function template in the translation unit. |
| 1180 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1181 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1182 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1183 | : diag::ext_template_parameter_default_in_function_template) |
| 1184 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1185 | return false; |
| 1186 | |
| 1187 | case Sema::TPC_ClassTemplateMember: |
| 1188 | // C++0x [temp.param]p9: |
| 1189 | // A default template-argument shall not be specified in the |
| 1190 | // template-parameter-lists of the definition of a member of a |
| 1191 | // class template that appears outside of the member's class. |
| 1192 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1193 | << DefArgRange; |
| 1194 | return true; |
| 1195 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1196 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1197 | case Sema::TPC_FriendFunctionTemplate: |
| 1198 | // C++ [temp.param]p9: |
| 1199 | // A default template-argument shall not be specified in a |
| 1200 | // friend template declaration. |
| 1201 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1202 | << DefArgRange; |
| 1203 | return true; |
| 1204 | |
| 1205 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1206 | // for friend function templates if there is only a single |
| 1207 | // declaration (and it is a definition). Strange! |
| 1208 | } |
| 1209 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1210 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1213 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1214 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1215 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1216 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1217 | // A template template parameter which is a parameter pack is also a pack |
| 1218 | // expansion. |
| 1219 | if (TTP->isParameterPack()) |
| 1220 | return false; |
| 1221 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1222 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1223 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1224 | NamedDecl *P = Params->getParam(I); |
| 1225 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1226 | if (!NTTP->isParameterPack() && |
| 1227 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1228 | NTTP->getTypeSourceInfo(), |
| 1229 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1230 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1231 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1232 | continue; |
| 1233 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1234 | |
| 1235 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1236 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1237 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1238 | return true; |
| 1239 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1240 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1241 | return false; |
| 1242 | } |
| 1243 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1244 | /// \brief Checks the validity of a template parameter list, possibly |
| 1245 | /// considering the template parameter list from a previous |
| 1246 | /// declaration. |
| 1247 | /// |
| 1248 | /// If an "old" template parameter list is provided, it must be |
| 1249 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1250 | /// template parameter list. |
| 1251 | /// |
| 1252 | /// \param NewParams Template parameter list for a new template |
| 1253 | /// declaration. This template parameter list will be updated with any |
| 1254 | /// default arguments that are carried through from the previous |
| 1255 | /// template parameter list. |
| 1256 | /// |
| 1257 | /// \param OldParams If provided, template parameter list from a |
| 1258 | /// previous declaration of the same template. Default template |
| 1259 | /// arguments will be merged from the old template parameter list to |
| 1260 | /// the new template parameter list. |
| 1261 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1262 | /// \param TPC Describes the context in which we are checking the given |
| 1263 | /// template parameter list. |
| 1264 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1265 | /// \returns true if an error occurred, false otherwise. |
| 1266 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1267 | TemplateParameterList *OldParams, |
| 1268 | TemplateParamListContext TPC) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1269 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1271 | // C++ [temp.param]p10: |
| 1272 | // The set of default template-arguments available for use with a |
| 1273 | // template declaration or definition is obtained by merging the |
| 1274 | // default arguments from the definition (if in scope) and all |
| 1275 | // declarations in scope in the same way default function |
| 1276 | // arguments are (8.3.6). |
| 1277 | bool SawDefaultArgument = false; |
| 1278 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1279 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1280 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1281 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1282 | if (OldParams) |
| 1283 | OldParam = OldParams->begin(); |
| 1284 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1285 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1286 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1287 | NewParamEnd = NewParams->end(); |
| 1288 | NewParam != NewParamEnd; ++NewParam) { |
| 1289 | // Variables used to diagnose redundant default arguments |
| 1290 | bool RedundantDefaultArg = false; |
| 1291 | SourceLocation OldDefaultLoc; |
| 1292 | SourceLocation NewDefaultLoc; |
| 1293 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1294 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1295 | bool MissingDefaultArg = false; |
| 1296 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1297 | // Variable used to diagnose non-final parameter packs |
| 1298 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1299 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1300 | if (TemplateTypeParmDecl *NewTypeParm |
| 1301 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1302 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1303 | if (NewTypeParm->hasDefaultArgument() && |
| 1304 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1305 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1306 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1307 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1308 | NewTypeParm->removeDefaultArgument(); |
| 1309 | |
| 1310 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1311 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1312 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1313 | |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1314 | if (NewTypeParm->isParameterPack()) { |
| 1315 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1316 | "Parameter packs can't have a default argument!"); |
| 1317 | SawParameterPack = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1318 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1319 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1320 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1321 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1322 | SawDefaultArgument = true; |
| 1323 | RedundantDefaultArg = true; |
| 1324 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1325 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1326 | // Merge the default argument from the old declaration to the |
| 1327 | // new declaration. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1328 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1329 | true); |
| 1330 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1331 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1332 | SawDefaultArgument = true; |
| 1333 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1334 | } else if (SawDefaultArgument) |
| 1335 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1336 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1337 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1338 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1339 | if (!NewNonTypeParm->isParameterPack() && |
| 1340 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1341 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1342 | UPPC_NonTypeTemplateParameterType)) { |
| 1343 | Invalid = true; |
| 1344 | continue; |
| 1345 | } |
| 1346 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1347 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1348 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1349 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1350 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1351 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1352 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1355 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1356 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1357 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1358 | if (NewNonTypeParm->isParameterPack()) { |
| 1359 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1360 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1361 | if (!NewNonTypeParm->isPackExpansion()) |
| 1362 | SawParameterPack = true; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1363 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 1364 | NewNonTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1365 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1366 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1367 | SawDefaultArgument = true; |
| 1368 | RedundantDefaultArg = true; |
| 1369 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1370 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1371 | // Merge the default argument from the old declaration to the |
| 1372 | // new declaration. |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1373 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1374 | // expression that points to a previous non-type template |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1375 | // parameter. |
| 1376 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1377 | OldNonTypeParm->getDefaultArgument(), |
| 1378 | /*Inherited=*/ true); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1379 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1380 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1381 | SawDefaultArgument = true; |
| 1382 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1383 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1385 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1386 | TemplateTemplateParmDecl *NewTemplateParm |
| 1387 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1388 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1389 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1390 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1391 | Invalid = true; |
| 1392 | continue; |
| 1393 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1394 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1395 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1396 | if (NewTemplateParm->hasDefaultArgument() && |
| 1397 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1398 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1399 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1400 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1401 | |
| 1402 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1403 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1404 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1405 | if (NewTemplateParm->isParameterPack()) { |
| 1406 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1407 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1408 | if (!NewTemplateParm->isPackExpansion()) |
| 1409 | SawParameterPack = true; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1410 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1411 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1412 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1413 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1414 | SawDefaultArgument = true; |
| 1415 | RedundantDefaultArg = true; |
| 1416 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1417 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1418 | // Merge the default argument from the old declaration to the |
| 1419 | // new declaration. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1420 | // FIXME: We need to create a new kind of "default argument" expression |
| 1421 | // that points to a previous template template parameter. |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1422 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1423 | OldTemplateParm->getDefaultArgument(), |
| 1424 | /*Inherited=*/ true); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1425 | PreviousDefaultArgLoc |
| 1426 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1427 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1428 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1429 | PreviousDefaultArgLoc |
| 1430 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1431 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1435 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1436 | // If a template parameter of a primary class template or alias template |
| 1437 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1438 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1439 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 1440 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1441 | Diag((*NewParam)->getLocation(), |
| 1442 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1443 | Invalid = true; |
| 1444 | } |
| 1445 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1446 | if (RedundantDefaultArg) { |
| 1447 | // C++ [temp.param]p12: |
| 1448 | // A template-parameter shall not be given default arguments |
| 1449 | // by two different declarations in the same scope. |
| 1450 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1451 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1452 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1453 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1454 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1455 | // If a template-parameter of a class template has a default |
| 1456 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1457 | // have a default template-argument supplied or be a template parameter |
| 1458 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1459 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1460 | diag::err_template_param_default_arg_missing); |
| 1461 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1462 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1463 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | // If we have an old template parameter list that we're merging |
| 1467 | // in, move on to the next parameter. |
| 1468 | if (OldParams) |
| 1469 | ++OldParam; |
| 1470 | } |
| 1471 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1472 | // We were missing some default arguments at the end of the list, so remove |
| 1473 | // all of the default arguments. |
| 1474 | if (RemoveDefaultArguments) { |
| 1475 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1476 | NewParamEnd = NewParams->end(); |
| 1477 | NewParam != NewParamEnd; ++NewParam) { |
| 1478 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1479 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1480 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1481 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1482 | NTTP->removeDefaultArgument(); |
| 1483 | else |
| 1484 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1485 | } |
| 1486 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1487 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1488 | return Invalid; |
| 1489 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1490 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1491 | namespace { |
| 1492 | |
| 1493 | /// A class which looks for a use of a certain level of template |
| 1494 | /// parameter. |
| 1495 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1496 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1497 | |
| 1498 | unsigned Depth; |
| 1499 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1500 | SourceLocation MatchLoc; |
| 1501 | |
| 1502 | DependencyChecker(unsigned Depth) : Depth(Depth), Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1503 | |
| 1504 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1505 | NamedDecl *ND = Params->getParam(0); |
| 1506 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1507 | Depth = PD->getDepth(); |
| 1508 | } else if (NonTypeTemplateParmDecl *PD = |
| 1509 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1510 | Depth = PD->getDepth(); |
| 1511 | } else { |
| 1512 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1513 | } |
| 1514 | } |
| 1515 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1516 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1517 | if (ParmDepth >= Depth) { |
| 1518 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1519 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1520 | return true; |
| 1521 | } |
| 1522 | return false; |
| 1523 | } |
| 1524 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1525 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1526 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 1527 | } |
| 1528 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1529 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1530 | return !Matches(T->getDepth()); |
| 1531 | } |
| 1532 | |
| 1533 | bool TraverseTemplateName(TemplateName N) { |
| 1534 | if (TemplateTemplateParmDecl *PD = |
| 1535 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1536 | if (Matches(PD->getDepth())) |
| 1537 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1538 | return super::TraverseTemplateName(N); |
| 1539 | } |
| 1540 | |
| 1541 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1542 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1543 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 1544 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1545 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1546 | return super::VisitDeclRefExpr(E); |
| 1547 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1548 | |
| 1549 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 1550 | return TraverseType(T->getReplacementType()); |
| 1551 | } |
| 1552 | |
| 1553 | bool |
| 1554 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 1555 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 1556 | } |
| 1557 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1558 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1559 | return TraverseType(T->getInjectedSpecializationType()); |
| 1560 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1561 | }; |
| 1562 | } |
| 1563 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1564 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1565 | /// list. |
| 1566 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1567 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1568 | DependencyChecker Checker(Params); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1569 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1570 | return Checker.Match; |
| 1571 | } |
| 1572 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1573 | // Find the source range corresponding to the named type in the given |
| 1574 | // nested-name-specifier, if any. |
| 1575 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1576 | QualType T, |
| 1577 | const CXXScopeSpec &SS) { |
| 1578 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1579 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1580 | if (const Type *CurType = NNS->getAsType()) { |
| 1581 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1582 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1583 | } else |
| 1584 | break; |
| 1585 | |
| 1586 | NNSLoc = NNSLoc.getPrefix(); |
| 1587 | } |
| 1588 | |
| 1589 | return SourceRange(); |
| 1590 | } |
| 1591 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1592 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1593 | /// specifier, returning the template parameter list that applies to the |
| 1594 | /// name. |
| 1595 | /// |
| 1596 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1597 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1598 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1599 | /// \param DeclLoc The location of the declaration itself. |
| 1600 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1601 | /// \param SS the scope specifier that will be matched to the given template |
| 1602 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1603 | /// being declared. |
| 1604 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1605 | /// \param TemplateId The template-id following the scope specifier, if there |
| 1606 | /// is one. Used to check for a missing 'template<>'. |
| 1607 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1608 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1609 | /// innermost template parameter lists. |
| 1610 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1611 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1612 | /// matching template parameters to scope specifiers in friend |
| 1613 | /// declarations. |
| 1614 | /// |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1615 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1616 | /// declared is an explicit specialization, false otherwise. |
| 1617 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1619 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1620 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1621 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1622 | /// 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] | 1623 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1624 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 1625 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1626 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1627 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
| 1628 | bool &IsExplicitSpecialization, bool &Invalid) { |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1629 | IsExplicitSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1630 | Invalid = false; |
| 1631 | |
| 1632 | // The sequence of nested types to which we will match up the template |
| 1633 | // parameter lists. We first build this list by starting with the type named |
| 1634 | // 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] | 1635 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1636 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1637 | if (SS.getScopeRep()) { |
| 1638 | if (CXXRecordDecl *Record |
| 1639 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1640 | T = Context.getTypeDeclType(Record); |
| 1641 | else |
| 1642 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1643 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1644 | |
| 1645 | // If we found an explicit specialization that prevents us from needing |
| 1646 | // 'template<>' headers, this will be set to the location of that |
| 1647 | // explicit specialization. |
| 1648 | SourceLocation ExplicitSpecLoc; |
| 1649 | |
| 1650 | while (!T.isNull()) { |
| 1651 | NestedTypes.push_back(T); |
| 1652 | |
| 1653 | // Retrieve the parent of a record type. |
| 1654 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1655 | // If this type is an explicit specialization, we're done. |
| 1656 | if (ClassTemplateSpecializationDecl *Spec |
| 1657 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1658 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1659 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1660 | ExplicitSpecLoc = Spec->getLocation(); |
| 1661 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1662 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1663 | } else if (Record->getTemplateSpecializationKind() |
| 1664 | == TSK_ExplicitSpecialization) { |
| 1665 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1666 | break; |
| 1667 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1668 | |
| 1669 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1670 | T = Context.getTypeDeclType(Parent); |
| 1671 | else |
| 1672 | T = QualType(); |
| 1673 | continue; |
| 1674 | } |
| 1675 | |
| 1676 | if (const TemplateSpecializationType *TST |
| 1677 | = T->getAs<TemplateSpecializationType>()) { |
| 1678 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1679 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1680 | T = Context.getTypeDeclType(Parent); |
| 1681 | else |
| 1682 | T = QualType(); |
| 1683 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1684 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | // Look one step prior in a dependent template specialization type. |
| 1688 | if (const DependentTemplateSpecializationType *DependentTST |
| 1689 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1690 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1691 | T = QualType(NNS->getAsType(), 0); |
| 1692 | else |
| 1693 | T = QualType(); |
| 1694 | continue; |
| 1695 | } |
| 1696 | |
| 1697 | // Look one step prior in a dependent name type. |
| 1698 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1699 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1700 | T = QualType(NNS->getAsType(), 0); |
| 1701 | else |
| 1702 | T = QualType(); |
| 1703 | continue; |
| 1704 | } |
| 1705 | |
| 1706 | // Retrieve the parent of an enumeration type. |
| 1707 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1708 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1709 | // check here. |
| 1710 | EnumDecl *Enum = EnumT->getDecl(); |
| 1711 | |
| 1712 | // Get to the parent type. |
| 1713 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1714 | T = Context.getTypeDeclType(Parent); |
| 1715 | else |
| 1716 | T = QualType(); |
| 1717 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1718 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1719 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1720 | T = QualType(); |
| 1721 | } |
| 1722 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1723 | // to the innermost while checking template-parameter-lists. |
| 1724 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1725 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1726 | // C++0x [temp.expl.spec]p17: |
| 1727 | // A member or a member template may be nested within many |
| 1728 | // enclosing class templates. In an explicit specialization for |
| 1729 | // such a member, the member declaration shall be preceded by a |
| 1730 | // template<> for each enclosing class template that is |
| 1731 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1732 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1733 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1734 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1735 | if (SawNonEmptyTemplateParameterList) { |
| 1736 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1737 | << !Recovery << Range; |
| 1738 | Invalid = true; |
| 1739 | IsExplicitSpecialization = false; |
| 1740 | return true; |
| 1741 | } |
| 1742 | |
| 1743 | return false; |
| 1744 | }; |
| 1745 | |
| 1746 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 1747 | // Check that we can have an explicit specialization here. |
| 1748 | if (CheckExplicitSpecialization(Range, true)) |
| 1749 | return true; |
| 1750 | |
| 1751 | // We don't have a template header, but we should. |
| 1752 | SourceLocation ExpectedTemplateLoc; |
| 1753 | if (!ParamLists.empty()) |
| 1754 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1755 | else |
| 1756 | ExpectedTemplateLoc = DeclStartLoc; |
| 1757 | |
| 1758 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1759 | << Range |
| 1760 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1761 | return false; |
| 1762 | }; |
| 1763 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1764 | unsigned ParamIdx = 0; |
| 1765 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1766 | ++TypeIdx) { |
| 1767 | T = NestedTypes[TypeIdx]; |
| 1768 | |
| 1769 | // Whether we expect a 'template<>' header. |
| 1770 | bool NeedEmptyTemplateHeader = false; |
| 1771 | |
| 1772 | // Whether we expect a template header with parameters. |
| 1773 | bool NeedNonemptyTemplateHeader = false; |
| 1774 | |
| 1775 | // For a dependent type, the set of template parameters that we |
| 1776 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1777 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1778 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1779 | // C++0x [temp.expl.spec]p15: |
| 1780 | // A member or a member template may be nested within many enclosing |
| 1781 | // class templates. In an explicit specialization for such a member, the |
| 1782 | // member declaration shall be preceded by a template<> for each |
| 1783 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1784 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1785 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1786 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1787 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1788 | NeedNonemptyTemplateHeader = true; |
| 1789 | } else if (Record->isDependentType()) { |
| 1790 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1791 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1792 | ->getTemplateParameters(); |
| 1793 | NeedNonemptyTemplateHeader = true; |
| 1794 | } |
| 1795 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1796 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1797 | // C++0x [temp.expl.spec]p4: |
| 1798 | // Members of an explicitly specialized class template are defined |
| 1799 | // in the same manner as members of normal classes, and not using |
| 1800 | // the template<> syntax. |
| 1801 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1802 | NeedEmptyTemplateHeader = true; |
| 1803 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1804 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1805 | } else if (Record->getTemplateSpecializationKind()) { |
| 1806 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1807 | != TSK_ExplicitSpecialization && |
| 1808 | TypeIdx == NumTypes - 1) |
| 1809 | IsExplicitSpecialization = true; |
| 1810 | |
| 1811 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1812 | } |
| 1813 | } else if (const TemplateSpecializationType *TST |
| 1814 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 1815 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1816 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1817 | NeedNonemptyTemplateHeader = true; |
| 1818 | } |
| 1819 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1820 | // FIXME: We actually could/should check the template arguments here |
| 1821 | // against the corresponding template parameter list. |
| 1822 | NeedNonemptyTemplateHeader = false; |
| 1823 | } |
| 1824 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1825 | // C++ [temp.expl.spec]p16: |
| 1826 | // In an explicit specialization declaration for a member of a class |
| 1827 | // template or a member template that ap- pears in namespace scope, the |
| 1828 | // member template and some of its enclosing class templates may remain |
| 1829 | // unspecialized, except that the declaration shall not explicitly |
| 1830 | // specialize a class member template if its en- closing class templates |
| 1831 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1832 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1833 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1834 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 1835 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1836 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1837 | } else |
| 1838 | SawNonEmptyTemplateParameterList = true; |
| 1839 | } |
| 1840 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1841 | if (NeedEmptyTemplateHeader) { |
| 1842 | // If we're on the last of the types, and we need a 'template<>' header |
| 1843 | // here, then it's an explicit specialization. |
| 1844 | if (TypeIdx == NumTypes - 1) |
| 1845 | IsExplicitSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1846 | |
| 1847 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1848 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1849 | // The header has template parameters when it shouldn't. Complain. |
| 1850 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1851 | diag::err_template_param_list_matches_nontemplate) |
| 1852 | << T |
| 1853 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1854 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1855 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1856 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1857 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1858 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1859 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1860 | // Consume this template header. |
| 1861 | ++ParamIdx; |
| 1862 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1863 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1864 | |
| 1865 | if (!IsFriend) |
| 1866 | if (DiagnoseMissingExplicitSpecialization( |
| 1867 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1868 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1869 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1870 | continue; |
| 1871 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1872 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1873 | if (NeedNonemptyTemplateHeader) { |
| 1874 | // In friend declarations we can have template-ids which don't |
| 1875 | // depend on the corresponding template parameter lists. But |
| 1876 | // assume that empty parameter lists are supposed to match this |
| 1877 | // template-id. |
| 1878 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1879 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1880 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1881 | ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1882 | else |
| 1883 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1884 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1885 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1886 | if (ParamIdx < ParamLists.size()) { |
| 1887 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1888 | if (ExpectedTemplateParams && |
| 1889 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1890 | ExpectedTemplateParams, |
| 1891 | true, TPL_TemplateMatch)) |
| 1892 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1893 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1894 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1895 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1896 | TPC_ClassTemplateMember)) |
| 1897 | Invalid = true; |
| 1898 | |
| 1899 | ++ParamIdx; |
| 1900 | continue; |
| 1901 | } |
| 1902 | |
| 1903 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1904 | << T |
| 1905 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1906 | Invalid = true; |
| 1907 | continue; |
| 1908 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1909 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1910 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1911 | // If there were at least as many template-ids as there were template |
| 1912 | // parameter lists, then there are no template parameter lists remaining for |
| 1913 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1914 | if (ParamIdx >= ParamLists.size()) { |
| 1915 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1916 | // We don't have a template header for the declaration itself, but we |
| 1917 | // should. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1918 | IsExplicitSpecialization = true; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1919 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 1920 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1921 | |
| 1922 | // Fabricate an empty template parameter list for the invented header. |
| 1923 | return TemplateParameterList::Create(Context, SourceLocation(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1924 | SourceLocation(), nullptr, 0, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1925 | SourceLocation()); |
| 1926 | } |
| 1927 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1928 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1929 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1930 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1931 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1932 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1933 | bool HasAnyExplicitSpecHeader = false; |
| 1934 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1935 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1936 | if (ParamLists[I]->size() == 0) |
| 1937 | HasAnyExplicitSpecHeader = true; |
| 1938 | else |
| 1939 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1940 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1941 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1942 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1943 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 1944 | : diag::err_template_spec_extra_headers) |
| 1945 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1946 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1947 | |
| 1948 | // If there was a specialization somewhere, such that 'template<>' is |
| 1949 | // not required, and there were any 'template<>' headers, note where the |
| 1950 | // specialization occurred. |
| 1951 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1952 | Diag(ExplicitSpecLoc, |
| 1953 | diag::note_explicit_template_spec_does_not_need_header) |
| 1954 | << NestedTypes.back(); |
| 1955 | |
| 1956 | // We have a template parameter list with no corresponding scope, which |
| 1957 | // means that the resulting template declaration can't be instantiated |
| 1958 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1959 | if (!AllExplicitSpecHeaders) |
| 1960 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1961 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1962 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1963 | // C++ [temp.expl.spec]p16: |
| 1964 | // In an explicit specialization declaration for a member of a class |
| 1965 | // template or a member template that ap- pears in namespace scope, the |
| 1966 | // member template and some of its enclosing class templates may remain |
| 1967 | // unspecialized, except that the declaration shall not explicitly |
| 1968 | // specialize a class member template if its en- closing class templates |
| 1969 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1970 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1971 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 1972 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1973 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1974 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1975 | // Return the last template parameter list, which corresponds to the |
| 1976 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1977 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1980 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1981 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1982 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1983 | << (isa<FunctionTemplateDecl>(Template) |
| 1984 | ? 0 |
| 1985 | : isa<ClassTemplateDecl>(Template) |
| 1986 | ? 1 |
| 1987 | : isa<VarTemplateDecl>(Template) |
| 1988 | ? 2 |
| 1989 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 1990 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1991 | return; |
| 1992 | } |
| 1993 | |
| 1994 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1995 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1996 | IEnd = OST->end(); |
| 1997 | I != IEnd; ++I) |
| 1998 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1999 | << 0 << (*I)->getDeclName(); |
| 2000 | |
| 2001 | return; |
| 2002 | } |
| 2003 | } |
| 2004 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2005 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 2006 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2007 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 2008 | DependentTemplateName *DTN |
| 2009 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2010 | if (DTN && DTN->isIdentifier()) |
| 2011 | // When building a template-id where the template-name is dependent, |
| 2012 | // assume the template is a type template. Either our assumption is |
| 2013 | // correct, or the code is ill-formed and will be diagnosed when the |
| 2014 | // dependent name is substituted. |
| 2015 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 2016 | DTN->getQualifier(), |
| 2017 | DTN->getIdentifier(), |
| 2018 | TemplateArgs); |
| 2019 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2020 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 2021 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
| 2022 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2023 | // We might have a substituted template template parameter pack. If so, |
| 2024 | // build a template specialization type for it. |
| 2025 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 2026 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2027 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2028 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 2029 | << Name; |
| 2030 | NoteAllFoundTemplates(Name); |
| 2031 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2032 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2033 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2034 | // Check that the template argument list is well-formed for this |
| 2035 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2036 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2037 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2038 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2039 | return QualType(); |
| 2040 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2041 | QualType CanonType; |
| 2042 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2043 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2044 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 2045 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2046 | // Find the canonical type for this type alias template specialization. |
| 2047 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 2048 | if (Pattern->isInvalidDecl()) |
| 2049 | return QualType(); |
| 2050 | |
| 2051 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2052 | Converted.data(), Converted.size()); |
| 2053 | |
| 2054 | // Only substitute for the innermost template argument list. |
| 2055 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 0c4a34b | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 2056 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 2057 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 2058 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 2059 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2060 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2061 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2062 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2063 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2064 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2065 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2066 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 2067 | TemplateArgLists, AliasTemplate->getLocation(), |
| 2068 | AliasTemplate->getDeclName()); |
| 2069 | if (CanonType.isNull()) |
| 2070 | return QualType(); |
| 2071 | } else if (Name.isDependent() || |
| 2072 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2073 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2074 | // This class template specialization is a dependent |
| 2075 | // type. Therefore, its canonical type is another class template |
| 2076 | // specialization type that contains all of the converted |
| 2077 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 2078 | // A<T, T> have identical types when A is declared as: |
| 2079 | // |
| 2080 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2081 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2082 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2083 | Converted.data(), |
| 2084 | Converted.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2086 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2087 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2088 | // In the future, we need to teach getTemplateSpecializationType to only |
| 2089 | // build the canonical type and return that to us. |
| 2090 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2091 | |
| 2092 | // This might work out to be a current instantiation, in which |
| 2093 | // case the canonical type needs to be the InjectedClassNameType. |
| 2094 | // |
| 2095 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2096 | // changes to CurContext don't change the set of current |
| 2097 | // instantiations. |
| 2098 | if (isa<ClassTemplateDecl>(Template)) { |
| 2099 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2100 | // If we get out to a namespace, we're done. |
| 2101 | if (Ctx->isFileContext()) break; |
| 2102 | |
| 2103 | // If this isn't a record, keep looking. |
| 2104 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2105 | if (!Record) continue; |
| 2106 | |
| 2107 | // Look for one of the two cases with InjectedClassNameTypes |
| 2108 | // and check whether it's the same template. |
| 2109 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2110 | !Record->getDescribedClassTemplate()) |
| 2111 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2112 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2113 | // Fetch the injected class name type and check whether its |
| 2114 | // injected type is equal to the type we just built. |
| 2115 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2116 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2117 | ->getInjectedSpecializationType(); |
| 2118 | |
| 2119 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2120 | continue; |
| 2121 | |
| 2122 | // If so, the canonical type of this TST is the injected |
| 2123 | // class name type of the record we just found. |
| 2124 | assert(ICNT.isCanonical()); |
| 2125 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2126 | break; |
| 2127 | } |
| 2128 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2129 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2130 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2131 | // Find the class template specialization declaration that |
| 2132 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2133 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2134 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2135 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2136 | if (!Decl) { |
| 2137 | // This is the first time we have referenced this class template |
| 2138 | // specialization. Create the canonical declaration and add it to |
| 2139 | // the set of specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2140 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2141 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2142 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2143 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2144 | ClassTemplate->getLocation(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2145 | ClassTemplate, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2146 | Converted.data(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2147 | Converted.size(), nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2148 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 2149 | if (ClassTemplate->isOutOfLine()) |
| 2150 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 2153 | // Diagnose uses of this specialization. |
| 2154 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 2155 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2156 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2157 | assert(isa<RecordType>(CanonType) && |
| 2158 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2159 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2160 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2161 | // Build the fully-sugared type for this class template |
| 2162 | // specialization, which refers back to the class template |
| 2163 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2164 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2167 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2168 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2169 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2170 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2171 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2172 | SourceLocation RAngleLoc, |
| 2173 | bool IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2174 | if (SS.isInvalid()) |
| 2175 | return true; |
| 2176 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2177 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2178 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2179 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2180 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2181 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2182 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2183 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2184 | QualType T |
| 2185 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2186 | DTN->getQualifier(), |
| 2187 | DTN->getIdentifier(), |
| 2188 | TemplateArgs); |
| 2189 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2190 | TypeLocBuilder TLB; |
| 2191 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2192 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2193 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2194 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2195 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2196 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2197 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2198 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2199 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2200 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2201 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2202 | } |
| 2203 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2204 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2205 | |
| 2206 | if (Result.isNull()) |
| 2207 | return true; |
| 2208 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2209 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2210 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2211 | TemplateSpecializationTypeLoc SpecTL |
| 2212 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2213 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2214 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2215 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2216 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2217 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2218 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2219 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2220 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2221 | // constructor or destructor name (in such a case, the scope specifier |
| 2222 | // will be attached to the enclosing Decl or Expr node). |
| 2223 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2224 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2225 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2226 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2227 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2228 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2229 | } |
| 2230 | |
| 2231 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2232 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2233 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2234 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2235 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2236 | SourceLocation TagLoc, |
| 2237 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2238 | SourceLocation TemplateKWLoc, |
| 2239 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2240 | SourceLocation TemplateLoc, |
| 2241 | SourceLocation LAngleLoc, |
| 2242 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2243 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2244 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2245 | |
| 2246 | // Translate the parser's template argument list in our AST format. |
| 2247 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2248 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2249 | |
| 2250 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2251 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2252 | ElaboratedTypeKeyword Keyword |
| 2253 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2254 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2255 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2256 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2257 | DTN->getQualifier(), |
| 2258 | DTN->getIdentifier(), |
| 2259 | TemplateArgs); |
| 2260 | |
| 2261 | // Build type-source information. |
| 2262 | TypeLocBuilder TLB; |
| 2263 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2264 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2265 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 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 | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2269 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2270 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +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 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2275 | |
| 2276 | if (TypeAliasTemplateDecl *TAT = |
| 2277 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2278 | // C++0x [dcl.type.elab]p2: |
| 2279 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2280 | // resolves to an alias template specialization, the |
| 2281 | // elaborated-type-specifier is ill-formed. |
| 2282 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2283 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2284 | } |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2285 | |
| 2286 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2287 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2288 | return TypeResult(true); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2289 | |
| 2290 | // Check the tag kind |
| 2291 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2292 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2293 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2294 | IdentifierInfo *Id = D->getIdentifier(); |
| 2295 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2296 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2297 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 2298 | TagLoc, *Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2299 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2300 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2301 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2302 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2303 | } |
| 2304 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2305 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2306 | // Provide source-location information for the template specialization. |
| 2307 | TypeLocBuilder TLB; |
| 2308 | TemplateSpecializationTypeLoc SpecTL |
| 2309 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2310 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2311 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2312 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2313 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2314 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2315 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2316 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2317 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2318 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2319 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2320 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2321 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2322 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2323 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2324 | } |
| 2325 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2326 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2327 | Sema &S, SourceLocation NameLoc, TemplateParameterList *TemplateParams, |
| 2328 | unsigned ExplicitArgs, SmallVectorImpl<TemplateArgument> &TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2329 | |
| 2330 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 2331 | NamedDecl *PrevDecl, |
| 2332 | SourceLocation Loc, |
| 2333 | bool IsPartialSpecialization); |
| 2334 | |
| 2335 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2336 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2337 | static bool isTemplateArgumentTemplateParameter( |
| 2338 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 2339 | switch (Arg.getKind()) { |
| 2340 | case TemplateArgument::Null: |
| 2341 | case TemplateArgument::NullPtr: |
| 2342 | case TemplateArgument::Integral: |
| 2343 | case TemplateArgument::Declaration: |
| 2344 | case TemplateArgument::Pack: |
| 2345 | case TemplateArgument::TemplateExpansion: |
| 2346 | return false; |
| 2347 | |
| 2348 | case TemplateArgument::Type: { |
| 2349 | QualType Type = Arg.getAsType(); |
| 2350 | const TemplateTypeParmType *TPT = |
| 2351 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 2352 | return TPT && !Type.hasQualifiers() && |
| 2353 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 2354 | } |
| 2355 | |
| 2356 | case TemplateArgument::Expression: { |
| 2357 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 2358 | if (!DRE || !DRE->getDecl()) |
| 2359 | return false; |
| 2360 | const NonTypeTemplateParmDecl *NTTP = |
| 2361 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 2362 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 2363 | } |
| 2364 | |
| 2365 | case TemplateArgument::Template: |
| 2366 | const TemplateTemplateParmDecl *TTP = |
| 2367 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 2368 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 2369 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 2370 | } |
| 2371 | llvm_unreachable("unexpected kind of template argument"); |
| 2372 | } |
| 2373 | |
| 2374 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 2375 | ArrayRef<TemplateArgument> Args) { |
| 2376 | if (Params->size() != Args.size()) |
| 2377 | return false; |
| 2378 | |
| 2379 | unsigned Depth = Params->getDepth(); |
| 2380 | |
| 2381 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 2382 | TemplateArgument Arg = Args[I]; |
| 2383 | |
| 2384 | // If the parameter is a pack expansion, the argument must be a pack |
| 2385 | // whose only element is a pack expansion. |
| 2386 | if (Params->getParam(I)->isParameterPack()) { |
| 2387 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 2388 | !Arg.pack_begin()->isPackExpansion()) |
| 2389 | return false; |
| 2390 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 2391 | } |
| 2392 | |
| 2393 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 2394 | return false; |
| 2395 | } |
| 2396 | |
| 2397 | return true; |
| 2398 | } |
| 2399 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2400 | /// Convert the parser's template argument list representation into our form. |
| 2401 | static TemplateArgumentListInfo |
| 2402 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 2403 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 2404 | TemplateId.RAngleLoc); |
| 2405 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 2406 | TemplateId.NumArgs); |
| 2407 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 2408 | return TemplateArgs; |
| 2409 | } |
| 2410 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2411 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2412 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 2413 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2414 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2415 | // D must be variable template id. |
| 2416 | assert(D.getName().getKind() == UnqualifiedId::IK_TemplateId && |
| 2417 | "Variable template specialization is declared with a template it."); |
| 2418 | |
| 2419 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2420 | TemplateArgumentListInfo TemplateArgs = |
| 2421 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2422 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 2423 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 2424 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2425 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2426 | TemplateName Name = TemplateId->Template.get(); |
| 2427 | |
| 2428 | // The template-id must name a variable template. |
| 2429 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2430 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 2431 | if (!VarTemplate) { |
| 2432 | NamedDecl *FnTemplate; |
| 2433 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 2434 | FnTemplate = *OTS->begin(); |
| 2435 | else |
| 2436 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 2437 | if (FnTemplate) |
| 2438 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 2439 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2440 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 2441 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2442 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2443 | |
| 2444 | // Check for unexpanded parameter packs in any of the template arguments. |
| 2445 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 2446 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 2447 | UPPC_PartialSpecialization)) |
| 2448 | return true; |
| 2449 | |
| 2450 | // Check that the template argument list is well-formed for this |
| 2451 | // template. |
| 2452 | SmallVector<TemplateArgument, 4> Converted; |
| 2453 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 2454 | false, Converted)) |
| 2455 | return true; |
| 2456 | |
| 2457 | // Check that the type of this variable template specialization |
| 2458 | // matches the expected type. |
| 2459 | TypeSourceInfo *ExpectedDI; |
| 2460 | { |
| 2461 | // Do substitution on the type of the declaration |
| 2462 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
| 2463 | Converted.data(), Converted.size()); |
| 2464 | InstantiatingTemplate Inst(*this, TemplateKWLoc, VarTemplate); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2465 | if (Inst.isInvalid()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2466 | return true; |
| 2467 | VarDecl *Templated = VarTemplate->getTemplatedDecl(); |
| 2468 | ExpectedDI = |
| 2469 | SubstType(Templated->getTypeSourceInfo(), |
| 2470 | MultiLevelTemplateArgumentList(TemplateArgList), |
| 2471 | Templated->getTypeSpecStartLoc(), Templated->getDeclName()); |
| 2472 | } |
| 2473 | if (!ExpectedDI) |
| 2474 | return true; |
| 2475 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2476 | // Find the variable template (partial) specialization declaration that |
| 2477 | // corresponds to these arguments. |
| 2478 | if (IsPartialSpecialization) { |
| 2479 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2480 | *this, TemplateNameLoc, VarTemplate->getTemplateParameters(), |
| 2481 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2482 | return true; |
| 2483 | |
| 2484 | bool InstantiationDependent; |
| 2485 | if (!Name.isDependent() && |
| 2486 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2487 | TemplateArgs.getArgumentArray(), TemplateArgs.size(), |
| 2488 | InstantiationDependent)) { |
| 2489 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 2490 | << VarTemplate->getDeclName(); |
| 2491 | IsPartialSpecialization = false; |
| 2492 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2493 | |
| 2494 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 2495 | Converted)) { |
| 2496 | // C++ [temp.class.spec]p9b3: |
| 2497 | // |
| 2498 | // -- The argument list of the specialization shall not be identical |
| 2499 | // to the implicit argument list of the primary template. |
| 2500 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 2501 | << /*variable template*/ 1 |
| 2502 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 2503 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 2504 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 2505 | // of the primary template. |
| 2506 | return true; |
| 2507 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2510 | void *InsertPos = nullptr; |
| 2511 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2512 | |
| 2513 | if (IsPartialSpecialization) |
| 2514 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2515 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2516 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2517 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2518 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2519 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2520 | |
| 2521 | // Check whether we can declare a variable template specialization in |
| 2522 | // the current scope. |
| 2523 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 2524 | TemplateNameLoc, |
| 2525 | IsPartialSpecialization)) |
| 2526 | return true; |
| 2527 | |
| 2528 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 2529 | // Since the only prior variable template specialization with these |
| 2530 | // arguments was referenced but not declared, reuse that |
| 2531 | // declaration node as our own, updating its source location and |
| 2532 | // the list of outer template parameters to reflect our new declaration. |
| 2533 | Specialization = PrevDecl; |
| 2534 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2535 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2536 | } else if (IsPartialSpecialization) { |
| 2537 | // Create a new class template partial specialization declaration node. |
| 2538 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 2539 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2540 | VarTemplatePartialSpecializationDecl *Partial = |
| 2541 | VarTemplatePartialSpecializationDecl::Create( |
| 2542 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 2543 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 2544 | Converted.data(), Converted.size(), TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2545 | |
| 2546 | if (!PrevPartial) |
| 2547 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 2548 | Specialization = Partial; |
| 2549 | |
| 2550 | // If we are providing an explicit specialization of a member variable |
| 2551 | // template specialization, make a note of that. |
| 2552 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2553 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2554 | |
| 2555 | // Check that all of the template parameters of the variable template |
| 2556 | // partial specialization are deducible from the template |
| 2557 | // arguments. If not, this variable template partial specialization |
| 2558 | // will never be used. |
| 2559 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 2560 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 2561 | TemplateParams->getDepth(), DeducibleParams); |
| 2562 | |
| 2563 | if (!DeducibleParams.all()) { |
| 2564 | unsigned NumNonDeducible = |
| 2565 | DeducibleParams.size() - DeducibleParams.count(); |
| 2566 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2567 | << /*variable template*/ 1 << (NumNonDeducible > 1) |
| 2568 | << SourceRange(TemplateNameLoc, RAngleLoc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2569 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 2570 | if (!DeducibleParams[I]) { |
| 2571 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 2572 | if (Param->getDeclName()) |
| 2573 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
| 2574 | << Param->getDeclName(); |
| 2575 | else |
| 2576 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 2577 | << "(anonymous)"; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2578 | } |
| 2579 | } |
| 2580 | } |
| 2581 | } else { |
| 2582 | // Create a new class template specialization declaration node for |
| 2583 | // this explicit specialization or friend declaration. |
| 2584 | Specialization = VarTemplateSpecializationDecl::Create( |
| 2585 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
| 2586 | VarTemplate, DI->getType(), DI, SC, Converted.data(), Converted.size()); |
| 2587 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 2588 | |
| 2589 | if (!PrevDecl) |
| 2590 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 2591 | } |
| 2592 | |
| 2593 | // C++ [temp.expl.spec]p6: |
| 2594 | // If a template, a member template or the member of a class template is |
| 2595 | // explicitly specialized then that specialization shall be declared |
| 2596 | // before the first use of that specialization that would cause an implicit |
| 2597 | // instantiation to take place, in every translation unit in which such a |
| 2598 | // use occurs; no diagnostic is required. |
| 2599 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 2600 | bool Okay = false; |
| 2601 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 2602 | // Is there any previous explicit specialization declaration? |
| 2603 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 2604 | Okay = true; |
| 2605 | break; |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | if (!Okay) { |
| 2610 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 2611 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 2612 | << Name << Range; |
| 2613 | |
| 2614 | Diag(PrevDecl->getPointOfInstantiation(), |
| 2615 | diag::note_instantiation_required_here) |
| 2616 | << (PrevDecl->getTemplateSpecializationKind() != |
| 2617 | TSK_ImplicitInstantiation); |
| 2618 | return true; |
| 2619 | } |
| 2620 | } |
| 2621 | |
| 2622 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 2623 | Specialization->setLexicalDeclContext(CurContext); |
| 2624 | |
| 2625 | // Add the specialization into its lexical context, so that it can |
| 2626 | // be seen when iterating through the list of declarations in that |
| 2627 | // context. However, specializations are not found by name lookup. |
| 2628 | CurContext->addDecl(Specialization); |
| 2629 | |
| 2630 | // Note that this is an explicit specialization. |
| 2631 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 2632 | |
| 2633 | if (PrevDecl) { |
| 2634 | // Check that this isn't a redefinition of this specialization, |
| 2635 | // merging with previous declarations. |
| 2636 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
| 2637 | ForRedeclaration); |
| 2638 | PrevSpec.addDecl(PrevDecl); |
| 2639 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2640 | } else if (Specialization->isStaticDataMember() && |
| 2641 | Specialization->isOutOfLine()) { |
| 2642 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | // Link instantiations of static data members back to the template from |
| 2646 | // which they were instantiated. |
| 2647 | if (Specialization->isStaticDataMember()) |
| 2648 | Specialization->setInstantiationOfStaticDataMember( |
| 2649 | VarTemplate->getTemplatedDecl(), |
| 2650 | Specialization->getSpecializationKind()); |
| 2651 | |
| 2652 | return Specialization; |
| 2653 | } |
| 2654 | |
| 2655 | namespace { |
| 2656 | /// \brief A partial specialization whose template arguments have matched |
| 2657 | /// a given template-id. |
| 2658 | struct PartialSpecMatchResult { |
| 2659 | VarTemplatePartialSpecializationDecl *Partial; |
| 2660 | TemplateArgumentList *Args; |
| 2661 | }; |
| 2662 | } |
| 2663 | |
| 2664 | DeclResult |
| 2665 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2666 | SourceLocation TemplateNameLoc, |
| 2667 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2668 | assert(Template && "A variable template id without template?"); |
| 2669 | |
| 2670 | // Check that the template argument list is well-formed for this template. |
| 2671 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2672 | if (CheckTemplateArgumentList( |
| 2673 | Template, TemplateNameLoc, |
| 2674 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2675 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2676 | return true; |
| 2677 | |
| 2678 | // Find the variable template specialization declaration that |
| 2679 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2680 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2681 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2682 | Converted, InsertPos)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2683 | // If we already have a variable template specialization, return it. |
| 2684 | return Spec; |
| 2685 | |
| 2686 | // This is the first time we have referenced this variable template |
| 2687 | // specialization. Create the canonical declaration and add it to |
| 2688 | // the set of specializations, based on the closest partial specialization |
| 2689 | // that it represents. That is, |
| 2690 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 2691 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
| 2692 | Converted.data(), Converted.size()); |
| 2693 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 2694 | bool AmbiguousPartialSpec = false; |
| 2695 | typedef PartialSpecMatchResult MatchResult; |
| 2696 | SmallVector<MatchResult, 4> Matched; |
| 2697 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
| 2698 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation); |
| 2699 | |
| 2700 | // 1. Attempt to find the closest partial specialization that this |
| 2701 | // specializes, if any. |
| 2702 | // If any of the template arguments is dependent, then this is probably |
| 2703 | // a placeholder for an incomplete declarative context; which must be |
| 2704 | // complete by instantiation time. Thus, do not search through the partial |
| 2705 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2706 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 2707 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 2708 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2709 | bool InstantiationDependent = false; |
| 2710 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 2711 | TemplateArgs, InstantiationDependent)) { |
| 2712 | |
| 2713 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 2714 | Template->getPartialSpecializations(PartialSpecs); |
| 2715 | |
| 2716 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 2717 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 2718 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 2719 | |
| 2720 | if (TemplateDeductionResult Result = |
| 2721 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 2722 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2723 | // TODO: Actually use the failed-deduction info? |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2724 | FailedCandidates.addCandidate() |
| 2725 | .set(Partial, MakeDeductionFailureInfo(Context, Result, Info)); |
| 2726 | (void)Result; |
| 2727 | } else { |
| 2728 | Matched.push_back(PartialSpecMatchResult()); |
| 2729 | Matched.back().Partial = Partial; |
| 2730 | Matched.back().Args = Info.take(); |
| 2731 | } |
| 2732 | } |
| 2733 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2734 | if (Matched.size() >= 1) { |
| 2735 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 2736 | if (Matched.size() == 1) { |
| 2737 | // -- If exactly one matching specialization is found, the |
| 2738 | // instantiation is generated from that specialization. |
| 2739 | // We don't need to do anything for this. |
| 2740 | } else { |
| 2741 | // -- If more than one matching specialization is found, the |
| 2742 | // partial order rules (14.5.4.2) are used to determine |
| 2743 | // whether one of the specializations is more specialized |
| 2744 | // than the others. If none of the specializations is more |
| 2745 | // specialized than all of the other matching |
| 2746 | // specializations, then the use of the variable template is |
| 2747 | // ambiguous and the program is ill-formed. |
| 2748 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 2749 | PEnd = Matched.end(); |
| 2750 | P != PEnd; ++P) { |
| 2751 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 2752 | PointOfInstantiation) == |
| 2753 | P->Partial) |
| 2754 | Best = P; |
| 2755 | } |
| 2756 | |
| 2757 | // Determine if the best partial specialization is more specialized than |
| 2758 | // the others. |
| 2759 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2760 | PEnd = Matched.end(); |
| 2761 | P != PEnd; ++P) { |
| 2762 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 2763 | P->Partial, Best->Partial, |
| 2764 | PointOfInstantiation) != Best->Partial) { |
| 2765 | AmbiguousPartialSpec = true; |
| 2766 | break; |
| 2767 | } |
| 2768 | } |
| 2769 | } |
| 2770 | |
| 2771 | // Instantiate using the best variable template partial specialization. |
| 2772 | InstantiationPattern = Best->Partial; |
| 2773 | InstantiationArgs = Best->Args; |
| 2774 | } else { |
| 2775 | // -- If no match is found, the instantiation is generated |
| 2776 | // from the primary template. |
| 2777 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 2778 | } |
| 2779 | } |
| 2780 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2781 | // 2. Create the canonical declaration. |
| 2782 | // Note that we do not instantiate the variable just yet, since |
| 2783 | // instantiation is handled in DoMarkVarDeclReferenced(). |
| 2784 | // FIXME: LateAttrs et al.? |
| 2785 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 2786 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 2787 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 2788 | if (!Decl) |
| 2789 | return true; |
| 2790 | |
| 2791 | if (AmbiguousPartialSpec) { |
| 2792 | // Partial ordering did not produce a clear winner. Complain. |
| 2793 | Decl->setInvalidDecl(); |
| 2794 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 2795 | << Decl; |
| 2796 | |
| 2797 | // Print the matching partial specializations. |
| 2798 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2799 | PEnd = Matched.end(); |
| 2800 | P != PEnd; ++P) |
| 2801 | Diag(P->Partial->getLocation(), diag::note_partial_spec_match) |
| 2802 | << getTemplateArgumentBindingsText( |
| 2803 | P->Partial->getTemplateParameters(), *P->Args); |
| 2804 | return true; |
| 2805 | } |
| 2806 | |
| 2807 | if (VarTemplatePartialSpecializationDecl *D = |
| 2808 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 2809 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 2810 | |
| 2811 | assert(Decl && "No variable template specialization?"); |
| 2812 | return Decl; |
| 2813 | } |
| 2814 | |
| 2815 | ExprResult |
| 2816 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 2817 | const DeclarationNameInfo &NameInfo, |
| 2818 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2819 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2820 | |
| 2821 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 2822 | *TemplateArgs); |
| 2823 | if (Decl.isInvalid()) |
| 2824 | return ExprError(); |
| 2825 | |
| 2826 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 2827 | if (!Var->getTemplateSpecializationKind()) |
| 2828 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 2829 | NameInfo.getLoc()); |
| 2830 | |
| 2831 | // Build an ordinary singleton decl ref. |
| 2832 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2833 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2836 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2837 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2838 | LookupResult &R, |
| 2839 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2840 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2841 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2842 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2843 | // 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] | 2844 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2845 | // foo<int> could identify a single function unambiguously |
| 2846 | // This approach does NOT work, since f<int>(1); |
| 2847 | // gets resolved prior to resorting to overload resolution |
| 2848 | // i.e., template<class T> void f(double); |
| 2849 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2850 | |
| 2851 | // These should be filtered out by our callers. |
| 2852 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2853 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2854 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2855 | // In C++1y, check variable template ids. |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 2856 | bool InstantiationDependent; |
| 2857 | if (R.getAsSingle<VarTemplateDecl>() && |
| 2858 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2859 | *TemplateArgs, InstantiationDependent)) { |
| 2860 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 2861 | R.getAsSingle<VarTemplateDecl>(), |
| 2862 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2865 | // We don't want lookup warnings at this point. |
| 2866 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2867 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2868 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2869 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2870 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2871 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2872 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2873 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2874 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2875 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2876 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2879 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2880 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2881 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2882 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2883 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2884 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2885 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2886 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2887 | DeclContext *DC; |
| 2888 | if (!(DC = computeDeclContext(SS, false)) || |
| 2889 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2890 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 2891 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2892 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2893 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2894 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2895 | LookupTemplateName(R, (Scope*)nullptr, SS, QualType(), /*Entering*/ false, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2896 | MemberOfUnknownSpecialization); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2897 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2898 | if (R.isAmbiguous()) |
| 2899 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2900 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2901 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2902 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2903 | << NameInfo.getName() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2904 | return ExprError(); |
| 2905 | } |
| 2906 | |
| 2907 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2908 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 2909 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 2910 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2911 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2912 | return ExprError(); |
| 2913 | } |
| 2914 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2915 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2918 | /// \brief Form a dependent template name. |
| 2919 | /// |
| 2920 | /// This action forms a dependent template name given the template |
| 2921 | /// name and its (presumably dependent) scope specifier. For |
| 2922 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2923 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2924 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2925 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2926 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2927 | SourceLocation TemplateKWLoc, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2928 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2929 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2930 | bool EnteringContext, |
| 2931 | TemplateTy &Result) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2932 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 2933 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2934 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2935 | diag::warn_cxx98_compat_template_outside_of_template : |
| 2936 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2937 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2938 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2939 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2940 | if (SS.isSet()) |
| 2941 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2942 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2943 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2944 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2945 | // C++0x [temp.names]p5: |
| 2946 | // If a name prefixed by the keyword template is not the name of |
| 2947 | // a template, the program is ill-formed. [Note: the keyword |
| 2948 | // template may not be applied to non-template members of class |
| 2949 | // templates. -end note ] [ Note: as is the case with the |
| 2950 | // typename prefix, the template prefix is allowed in cases |
| 2951 | // where it is not strictly necessary; i.e., when the |
| 2952 | // nested-name-specifier or the expression on the left of the -> |
| 2953 | // or . is not dependent on a template-parameter, or the use |
| 2954 | // does not appear in the scope of a template. -end note] |
| 2955 | // |
| 2956 | // Note: C++03 was more strict here, because it banned the use of |
| 2957 | // the "template" keyword prior to a template-name that was not a |
| 2958 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2959 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2960 | // 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] | 2961 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 2962 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2963 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2964 | MemberOfUnknownSpecialization); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2965 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2966 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | 5ecbb1b | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2967 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2968 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2969 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2970 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2971 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2972 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2973 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2974 | << Name.getSourceRange() |
| 2975 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2976 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2977 | } else { |
| 2978 | // We found something; return it. |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2979 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2980 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 2983 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2984 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2985 | switch (Name.getKind()) { |
| 2986 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2987 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2988 | Name.Identifier)); |
| 2989 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2990 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2991 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2992 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2993 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 2994 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2995 | |
| 2996 | case UnqualifiedId::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 2997 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2998 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2999 | default: |
| 3000 | break; |
| 3001 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3002 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3003 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3004 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3005 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3006 | << Name.getSourceRange() |
| 3007 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3008 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3009 | } |
| 3010 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3011 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3012 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3013 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3014 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3015 | QualType ArgType; |
| 3016 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3017 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3018 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3019 | switch(Arg.getKind()) { |
| 3020 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3021 | // C++ [temp.arg.type]p1: |
| 3022 | // A template-argument for a template-parameter which is a |
| 3023 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3024 | ArgType = Arg.getAsType(); |
| 3025 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3026 | break; |
| 3027 | case TemplateArgument::Template: { |
| 3028 | // We have a template type parameter but the template argument |
| 3029 | // is a template without any arguments. |
| 3030 | SourceRange SR = AL.getSourceRange(); |
| 3031 | TemplateName Name = Arg.getAsTemplate(); |
| 3032 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 3033 | << Name << SR; |
| 3034 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 3035 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3036 | |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3037 | return true; |
| 3038 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3039 | case TemplateArgument::Expression: { |
| 3040 | // We have a template type parameter but the template argument is an |
| 3041 | // expression; see if maybe it is missing the "typename" keyword. |
| 3042 | CXXScopeSpec SS; |
| 3043 | DeclarationNameInfo NameInfo; |
| 3044 | |
| 3045 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 3046 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3047 | NameInfo = ArgExpr->getNameInfo(); |
| 3048 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 3049 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 3050 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3051 | NameInfo = ArgExpr->getNameInfo(); |
| 3052 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 3053 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3054 | if (ArgExpr->isImplicitAccess()) { |
| 3055 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3056 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 3057 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3060 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3061 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 3062 | LookupParsedName(Result, CurScope, &SS); |
| 3063 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3064 | if (Result.getAsSingle<TypeDecl>() || |
| 3065 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3066 | LookupResult::NotFoundInCurrentInstantiation) { |
| 3067 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3068 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3069 | Diag(Loc, getLangOpts().MSVCCompat |
| 3070 | ? diag::ext_ms_template_type_arg_missing_typename |
| 3071 | : diag::err_template_arg_must_be_type_suggest) |
| 3072 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3073 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3074 | |
| 3075 | // Recover by synthesizing a type using the location information that we |
| 3076 | // already have. |
| 3077 | ArgType = |
| 3078 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 3079 | TypeLocBuilder TLB; |
| 3080 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 3081 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 3082 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3083 | TL.setNameLoc(NameInfo.getLoc()); |
| 3084 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 3085 | |
| 3086 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 3087 | // properly. |
| 3088 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 3089 | TemplateArgumentLocInfo(TSI)); |
| 3090 | |
| 3091 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3092 | } |
| 3093 | } |
| 3094 | // fallthrough |
| 3095 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3096 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3097 | // We have a template type parameter but the template argument |
| 3098 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 3099 | SourceRange SR = AL.getSourceRange(); |
| 3100 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3101 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3102 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3103 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3104 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3105 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3106 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3107 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3108 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3109 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3110 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3111 | ArgType = Context.getCanonicalType(ArgType); |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3112 | |
| 3113 | // Objective-C ARC: |
| 3114 | // If an explicitly-specified template argument type is a lifetime type |
| 3115 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3116 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3117 | ArgType->isObjCLifetimeType() && |
| 3118 | !ArgType.getObjCLifetime()) { |
| 3119 | Qualifiers Qs; |
| 3120 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 3121 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 3122 | } |
| 3123 | |
| 3124 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3125 | return false; |
| 3126 | } |
| 3127 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3128 | /// \brief Substitute template arguments into the default template argument for |
| 3129 | /// the given template type parameter. |
| 3130 | /// |
| 3131 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3132 | /// the substitution. |
| 3133 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3134 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3135 | /// for. |
| 3136 | /// |
| 3137 | /// \param TemplateLoc the location of the template name that started the |
| 3138 | /// template-id we are checking. |
| 3139 | /// |
| 3140 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3141 | /// terminates the template-id. |
| 3142 | /// |
| 3143 | /// \param Param the template template parameter whose default we are |
| 3144 | /// substituting into. |
| 3145 | /// |
| 3146 | /// \param Converted the list of template arguments provided for template |
| 3147 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3148 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3149 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3150 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3151 | TemplateDecl *Template, |
| 3152 | SourceLocation TemplateLoc, |
| 3153 | SourceLocation RAngleLoc, |
| 3154 | TemplateTypeParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3155 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3156 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3157 | |
| 3158 | // If the argument type is dependent, instantiate it now based |
| 3159 | // on the previously-computed template arguments. |
| 3160 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3161 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3162 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3163 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3164 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3165 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3166 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3167 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3168 | Converted.data(), Converted.size()); |
| 3169 | |
| 3170 | // Only substitute for the innermost template argument list. |
| 3171 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3172 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3173 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3174 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3175 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3176 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3177 | ArgType = |
| 3178 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 3179 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
| 3182 | return ArgType; |
| 3183 | } |
| 3184 | |
| 3185 | /// \brief Substitute template arguments into the default template argument for |
| 3186 | /// the given non-type template parameter. |
| 3187 | /// |
| 3188 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3189 | /// the substitution. |
| 3190 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3191 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3192 | /// for. |
| 3193 | /// |
| 3194 | /// \param TemplateLoc the location of the template name that started the |
| 3195 | /// template-id we are checking. |
| 3196 | /// |
| 3197 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3198 | /// terminates the template-id. |
| 3199 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3200 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3201 | /// substituting into. |
| 3202 | /// |
| 3203 | /// \param Converted the list of template arguments provided for template |
| 3204 | /// parameters that precede \p Param in the template parameter list. |
| 3205 | /// |
| 3206 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3207 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3208 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3209 | TemplateDecl *Template, |
| 3210 | SourceLocation TemplateLoc, |
| 3211 | SourceLocation RAngleLoc, |
| 3212 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3213 | SmallVectorImpl<TemplateArgument> &Converted) { |
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()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3218 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +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()); |
Eli Friedman | c25372b | 2012-04-26 22:43:24 +0000 | [diff] [blame] | 3230 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3231 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3234 | /// \brief Substitute template arguments into the default template argument for |
| 3235 | /// the given template template parameter. |
| 3236 | /// |
| 3237 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3238 | /// the substitution. |
| 3239 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3240 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3241 | /// for. |
| 3242 | /// |
| 3243 | /// \param TemplateLoc the location of the template name that started the |
| 3244 | /// template-id we are checking. |
| 3245 | /// |
| 3246 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3247 | /// terminates the template-id. |
| 3248 | /// |
| 3249 | /// \param Param the template template parameter whose default we are |
| 3250 | /// substituting into. |
| 3251 | /// |
| 3252 | /// \param Converted the list of template arguments provided for template |
| 3253 | /// parameters that precede \p Param in the template parameter list. |
| 3254 | /// |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3255 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 3256 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3257 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3258 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 3259 | static TemplateName |
| 3260 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3261 | TemplateDecl *Template, |
| 3262 | SourceLocation TemplateLoc, |
| 3263 | SourceLocation RAngleLoc, |
| 3264 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3265 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3266 | NestedNameSpecifierLoc &QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3267 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, Template, Converted, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3268 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3269 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3270 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3271 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3272 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3273 | Converted.data(), Converted.size()); |
| 3274 | |
| 3275 | // Only substitute for the innermost template argument list. |
| 3276 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3277 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3278 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3279 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3280 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3281 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3282 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3283 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3284 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3285 | QualifierLoc = |
| 3286 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3287 | if (!QualifierLoc) |
| 3288 | return TemplateName(); |
| 3289 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3290 | |
| 3291 | return SemaRef.SubstTemplateName( |
| 3292 | QualifierLoc, |
| 3293 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 3294 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 3295 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3298 | /// \brief If the given template parameter has a default template |
| 3299 | /// argument, substitute into that default template argument and |
| 3300 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3301 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3302 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 3303 | SourceLocation TemplateLoc, |
| 3304 | SourceLocation RAngleLoc, |
| 3305 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3306 | SmallVectorImpl<TemplateArgument> |
| 3307 | &Converted, |
| 3308 | bool &HasDefaultArg) { |
| 3309 | HasDefaultArg = false; |
| 3310 | |
| 3311 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3312 | if (!TypeParm->hasDefaultArgument()) |
| 3313 | return TemplateArgumentLoc(); |
| 3314 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3315 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3316 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3317 | TemplateLoc, |
| 3318 | RAngleLoc, |
| 3319 | TypeParm, |
| 3320 | Converted); |
| 3321 | if (DI) |
| 3322 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 3323 | |
| 3324 | return TemplateArgumentLoc(); |
| 3325 | } |
| 3326 | |
| 3327 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 3328 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3329 | if (!NonTypeParm->hasDefaultArgument()) |
| 3330 | return TemplateArgumentLoc(); |
| 3331 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3332 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3333 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3334 | TemplateLoc, |
| 3335 | RAngleLoc, |
| 3336 | NonTypeParm, |
| 3337 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3338 | if (Arg.isInvalid()) |
| 3339 | return TemplateArgumentLoc(); |
| 3340 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3341 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3342 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 3343 | } |
| 3344 | |
| 3345 | TemplateTemplateParmDecl *TempTempParm |
| 3346 | = cast<TemplateTemplateParmDecl>(Param); |
| 3347 | if (!TempTempParm->hasDefaultArgument()) |
| 3348 | return TemplateArgumentLoc(); |
| 3349 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3350 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3351 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3352 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3353 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3354 | RAngleLoc, |
| 3355 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3356 | Converted, |
| 3357 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3358 | if (TName.isNull()) |
| 3359 | return TemplateArgumentLoc(); |
| 3360 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3361 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3362 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3363 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 3364 | } |
| 3365 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3366 | /// \brief Check that the given template argument corresponds to the given |
| 3367 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3368 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3369 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3370 | /// checked. |
| 3371 | /// |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3372 | /// \param Arg The template argument, which may be updated due to conversions. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3373 | /// |
| 3374 | /// \param Template The template in which the template argument resides. |
| 3375 | /// |
| 3376 | /// \param TemplateLoc The location of the template name for the template |
| 3377 | /// whose argument list we're matching. |
| 3378 | /// |
| 3379 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 3380 | /// the template argument list. |
| 3381 | /// |
| 3382 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 3383 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 3384 | /// |
| 3385 | /// \param Converted The checked, converted argument will be added to the |
| 3386 | /// end of this small vector. |
| 3387 | /// |
| 3388 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 3389 | /// explicitly written, deduced, etc. |
| 3390 | /// |
| 3391 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3392 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3393 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 3394 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3395 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3396 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3397 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3398 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3399 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3400 | // Check template type parameters. |
| 3401 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3402 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3403 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3404 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3405 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3406 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3407 | // with the template arguments we've seen thus far. But if the |
| 3408 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3409 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3410 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 3411 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3412 | |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3413 | if (NTTPType->isDependentType() && |
| 3414 | !isa<TemplateTemplateParmDecl>(Template) && |
| 3415 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3416 | // Do substitution on the type of the non-type template parameter. |
| 3417 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3418 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3419 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3420 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3421 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3422 | |
| 3423 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3424 | Converted.data(), Converted.size()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3425 | NTTPType = SubstType(NTTPType, |
| 3426 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 3427 | NTTP->getLocation(), |
| 3428 | NTTP->getDeclName()); |
| 3429 | // If that worked, check the non-type template parameter type |
| 3430 | // for validity. |
| 3431 | if (!NTTPType.isNull()) |
| 3432 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 3433 | NTTP->getLocation()); |
| 3434 | if (NTTPType.isNull()) |
| 3435 | return true; |
| 3436 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3437 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3438 | switch (Arg.getArgument().getKind()) { |
| 3439 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3440 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3441 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3442 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3443 | TemplateArgument Result; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3444 | ExprResult Res = |
| 3445 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 3446 | Result, CTAK); |
| 3447 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3448 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3449 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3450 | // If the resulting expression is new, then use it in place of the |
| 3451 | // old expression in the template argument. |
| 3452 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 3453 | TemplateArgument TA(Res.get()); |
| 3454 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 3455 | } |
| 3456 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3457 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3458 | break; |
| 3459 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3460 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3461 | case TemplateArgument::Declaration: |
| 3462 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3463 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3464 | // We've already checked this template argument, so just copy |
| 3465 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3466 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3467 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3468 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3469 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3470 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3471 | // We were given a template template argument. It may not be ill-formed; |
| 3472 | // see below. |
| 3473 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3474 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 3475 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3476 | // We have a template argument such as \c T::template X, which we |
| 3477 | // parsed as a template template argument. However, since we now |
| 3478 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3479 | // template name into an expression. |
| 3480 | |
| 3481 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 3482 | Arg.getTemplateNameLoc()); |
| 3483 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 3484 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3485 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3486 | // FIXME: the template-template arg was a DependentTemplateName, |
| 3487 | // so it was provided with a template keyword. However, its source |
| 3488 | // location is not stored in the template argument structure. |
| 3489 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3490 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 3491 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 3492 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3493 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3494 | // If we parsed the template argument as a pack expansion, create a |
| 3495 | // pack expansion expression. |
| 3496 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3497 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3498 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3499 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3500 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3501 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3502 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3503 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3504 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3505 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3506 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3507 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3508 | break; |
| 3509 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3510 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3511 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3512 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3513 | // therefore cannot be a non-type template argument. |
| 3514 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 3515 | << Arg.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3516 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3517 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3518 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3519 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3520 | case TemplateArgument::Type: { |
| 3521 | // We have a non-type template parameter but the template |
| 3522 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3523 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3524 | // C++ [temp.arg]p2: |
| 3525 | // In a template-argument, an ambiguity between a type-id and |
| 3526 | // an expression is resolved to a type-id, regardless of the |
| 3527 | // form of the corresponding template-parameter. |
| 3528 | // |
| 3529 | // We warn specifically about this case, since it can be rather |
| 3530 | // confusing for users. |
| 3531 | QualType T = Arg.getArgument().getAsType(); |
| 3532 | SourceRange SR = Arg.getSourceRange(); |
| 3533 | if (T->isFunctionType()) |
| 3534 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 3535 | else |
| 3536 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 3537 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3538 | return true; |
| 3539 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3540 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3541 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3542 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3543 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3544 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3545 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3546 | } |
| 3547 | |
| 3548 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3549 | // Check template template parameters. |
| 3550 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3551 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3552 | // Substitute into the template parameter list of the template |
| 3553 | // template parameter, since previously-supplied template arguments |
| 3554 | // may appear within the template template parameter. |
| 3555 | { |
| 3556 | // Set up a template instantiation context. |
| 3557 | LocalInstantiationScope Scope(*this); |
| 3558 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3559 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3560 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3561 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3562 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3563 | |
| 3564 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3565 | Converted.data(), Converted.size()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3566 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3567 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3568 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 3569 | if (!TempParm) |
| 3570 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3571 | } |
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 | switch (Arg.getArgument().getKind()) { |
| 3574 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3575 | llvm_unreachable("Should never see a NULL template argument here"); |
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 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3578 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3579 | if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3580 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3581 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3582 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3583 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3584 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3585 | case TemplateArgument::Expression: |
| 3586 | case TemplateArgument::Type: |
| 3587 | // We have a template template parameter but the template |
| 3588 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3589 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3590 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3591 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3592 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3593 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3594 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3595 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3596 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3597 | case TemplateArgument::NullPtr: |
| 3598 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3599 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3600 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3601 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3602 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3603 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3604 | return false; |
| 3605 | } |
| 3606 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3607 | /// \brief Diagnose an arity mismatch in the |
| 3608 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 3609 | SourceLocation TemplateLoc, |
| 3610 | TemplateArgumentListInfo &TemplateArgs) { |
| 3611 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 3612 | unsigned NumParams = Params->size(); |
| 3613 | unsigned NumArgs = TemplateArgs.size(); |
| 3614 | |
| 3615 | SourceRange Range; |
| 3616 | if (NumArgs > NumParams) |
| 3617 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
| 3618 | TemplateArgs.getRAngleLoc()); |
| 3619 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3620 | << (NumArgs > NumParams) |
| 3621 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3622 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3623 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3624 | << Template << Range; |
| 3625 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3626 | << Params->getSourceRange(); |
| 3627 | return true; |
| 3628 | } |
| 3629 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3630 | /// \brief Check whether the template parameter is a pack expansion, and if so, |
| 3631 | /// determine the number of parameters produced by that expansion. For instance: |
| 3632 | /// |
| 3633 | /// \code |
| 3634 | /// template<typename ...Ts> struct A { |
| 3635 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 3636 | /// }; |
| 3637 | /// \endcode |
| 3638 | /// |
| 3639 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 3640 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3641 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3642 | if (NonTypeTemplateParmDecl *NTTP |
| 3643 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3644 | if (NTTP->isExpandedParameterPack()) |
| 3645 | return NTTP->getNumExpansionTypes(); |
| 3646 | } |
| 3647 | |
| 3648 | if (TemplateTemplateParmDecl *TTP |
| 3649 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 3650 | if (TTP->isExpandedParameterPack()) |
| 3651 | return TTP->getNumExpansionTemplateParameters(); |
| 3652 | } |
| 3653 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 3654 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3657 | /// \brief Check that the given template argument list is well-formed |
| 3658 | /// for specializing the given template. |
| 3659 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 3660 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3661 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 3662 | bool PartialTemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3663 | SmallVectorImpl<TemplateArgument> &Converted) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3664 | // Make a copy of the template arguments for processing. Only make the |
| 3665 | // changes at the end when successful in matching the arguments to the |
| 3666 | // template. |
| 3667 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 3668 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3669 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3670 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3671 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3672 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3673 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3674 | // [...] The type and form of each template-argument specified in |
| 3675 | // a template-id shall match the type and form specified for the |
| 3676 | // corresponding parameter declared by the template in its |
| 3677 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3678 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3679 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3680 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3681 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3682 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 3683 | ParamEnd = Params->end(); |
| 3684 | Param != ParamEnd; /* increment in loop */) { |
| 3685 | // If we have an expanded parameter pack, make sure we don't have too |
| 3686 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3687 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3688 | if (*Expansions == ArgumentPack.size()) { |
| 3689 | // We're done with this parameter pack. Pack up its arguments and add |
| 3690 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3691 | Converted.push_back( |
| 3692 | TemplateArgument::CreatePackCopy(Context, |
| 3693 | ArgumentPack.data(), |
| 3694 | ArgumentPack.size())); |
| 3695 | ArgumentPack.clear(); |
| 3696 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3697 | // This argument is assigned to the next parameter. |
| 3698 | ++Param; |
| 3699 | continue; |
| 3700 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 3701 | // Not enough arguments for this parameter pack. |
| 3702 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3703 | << false |
| 3704 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3705 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3706 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3707 | << Template; |
| 3708 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3709 | << Params->getSourceRange(); |
| 3710 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3711 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3712 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3713 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3714 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3715 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3716 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3717 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3718 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3719 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3720 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3721 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3722 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3723 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 3724 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3725 | // 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] | 3726 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3727 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3728 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3729 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3730 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3731 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 3732 | return true; |
| 3733 | } |
| 3734 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3735 | // We're now done with this argument. |
| 3736 | ++ArgIdx; |
| 3737 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3738 | if ((*Param)->isTemplateParameterPack()) { |
| 3739 | // The template parameter was a template parameter pack, so take the |
| 3740 | // deduced argument and place it on the argument pack. Note that we |
| 3741 | // stay on the same template parameter so that we can deduce more |
| 3742 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 3743 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3744 | } else { |
| 3745 | // Move to the next template parameter. |
| 3746 | ++Param; |
| 3747 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3748 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3749 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 3750 | // the remaining arguments, because we don't know what parameters they'll |
| 3751 | // match up with. |
| 3752 | if (PackExpansionIntoNonPack) { |
| 3753 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3754 | // If we were part way through filling in an expanded parameter pack, |
| 3755 | // fall back to just producing individual arguments. |
| 3756 | Converted.insert(Converted.end(), |
| 3757 | ArgumentPack.begin(), ArgumentPack.end()); |
| 3758 | ArgumentPack.clear(); |
| 3759 | } |
| 3760 | |
| 3761 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3762 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3763 | ++ArgIdx; |
| 3764 | } |
| 3765 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3766 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3767 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3768 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3769 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3770 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3771 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3772 | // If we're checking a partial template argument list, we're done. |
| 3773 | if (PartialTemplateArgs) { |
| 3774 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 3775 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3776 | ArgumentPack.data(), |
| 3777 | ArgumentPack.size())); |
| 3778 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3779 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3780 | } |
| 3781 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3782 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3783 | // 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] | 3784 | if ((*Param)->isTemplateParameterPack()) { |
| 3785 | assert(!getExpandedPackSize(*Param) && |
| 3786 | "Should have dealt with this already"); |
| 3787 | |
| 3788 | // A non-expanded parameter pack before the end of the parameter list |
| 3789 | // only occurs for an ill-formed template parameter list, unless we've |
| 3790 | // got a partial argument list for a function template, so just bail out. |
| 3791 | if (Param + 1 != ParamEnd) |
| 3792 | return true; |
| 3793 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3794 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3795 | ArgumentPack.data(), |
| 3796 | ArgumentPack.size())); |
| 3797 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3798 | |
| 3799 | ++Param; |
| 3800 | continue; |
| 3801 | } |
| 3802 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3803 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3804 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3805 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3806 | // Retrieve the default template argument from the template |
| 3807 | // parameter. For each kind of template parameter, we substitute the |
| 3808 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3809 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3810 | // the default argument. |
| 3811 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3812 | if (!TTP->hasDefaultArgument()) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3813 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3814 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3815 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3816 | Template, |
| 3817 | TemplateLoc, |
| 3818 | RAngleLoc, |
| 3819 | TTP, |
| 3820 | Converted); |
| 3821 | if (!ArgType) |
| 3822 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3823 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3824 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 3825 | ArgType); |
| 3826 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3827 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3828 | if (!NTTP->hasDefaultArgument()) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3829 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3830 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3831 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3832 | TemplateLoc, |
| 3833 | RAngleLoc, |
| 3834 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3835 | Converted); |
| 3836 | if (E.isInvalid()) |
| 3837 | return true; |
| 3838 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3839 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3840 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 3841 | } else { |
| 3842 | TemplateTemplateParmDecl *TempParm |
| 3843 | = cast<TemplateTemplateParmDecl>(*Param); |
| 3844 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3845 | if (!TempParm->hasDefaultArgument()) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3846 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3847 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3848 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3849 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3850 | TemplateLoc, |
| 3851 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3852 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3853 | Converted, |
| 3854 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3855 | if (Name.isNull()) |
| 3856 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3857 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3858 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3859 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3860 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3861 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3862 | // Introduce an instantiation record that describes where we are using |
| 3863 | // the default template argument. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3864 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 3865 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3866 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3867 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3868 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3869 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3870 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3871 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3872 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3873 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3874 | // Core issue 150 (assumed resolution): if this is a template template |
| 3875 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3876 | // template definition. |
| 3877 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3878 | NewArgs.addArgument(Arg); |
| 3879 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3880 | // Move to the next template parameter and argument. |
| 3881 | ++Param; |
| 3882 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3883 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3884 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 3885 | // If we're performing a partial argument substitution, allow any trailing |
| 3886 | // pack expansions; they might be empty. This can happen even if |
| 3887 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 3888 | // still dependent). |
| 3889 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 3890 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3891 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 3892 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 3893 | } |
| 3894 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3895 | // If we have any leftover arguments, then there were too many arguments. |
| 3896 | // Complain and fail. |
| 3897 | if (ArgIdx < NumArgs) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3898 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
| 3899 | |
| 3900 | // No problems found with the new argument list, propagate changes back |
| 3901 | // to caller. |
| 3902 | TemplateArgs = NewArgs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3903 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3904 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3905 | } |
| 3906 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3907 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3908 | class UnnamedLocalNoLinkageFinder |
| 3909 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3910 | { |
| 3911 | Sema &S; |
| 3912 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3913 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3914 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3915 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3916 | public: |
| 3917 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3918 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3919 | bool Visit(QualType T) { |
| 3920 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3921 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3922 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3923 | #define TYPE(Class, Parent) \ |
| 3924 | bool Visit##Class##Type(const Class##Type *); |
| 3925 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 3926 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3927 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 3928 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3929 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3930 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3931 | bool VisitTagDecl(const TagDecl *Tag); |
| 3932 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3933 | }; |
| 3934 | } |
| 3935 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3936 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3937 | return false; |
| 3938 | } |
| 3939 | |
| 3940 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3941 | return Visit(T->getElementType()); |
| 3942 | } |
| 3943 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3944 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3945 | return Visit(T->getPointeeType()); |
| 3946 | } |
| 3947 | |
| 3948 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3949 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3950 | return Visit(T->getPointeeType()); |
| 3951 | } |
| 3952 | |
| 3953 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3954 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3955 | return Visit(T->getPointeeType()); |
| 3956 | } |
| 3957 | |
| 3958 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3959 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3960 | return Visit(T->getPointeeType()); |
| 3961 | } |
| 3962 | |
| 3963 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3964 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3965 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3966 | } |
| 3967 | |
| 3968 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3969 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3970 | return Visit(T->getElementType()); |
| 3971 | } |
| 3972 | |
| 3973 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3974 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3975 | return Visit(T->getElementType()); |
| 3976 | } |
| 3977 | |
| 3978 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3979 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3980 | return Visit(T->getElementType()); |
| 3981 | } |
| 3982 | |
| 3983 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3984 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3985 | return Visit(T->getElementType()); |
| 3986 | } |
| 3987 | |
| 3988 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3989 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3990 | return Visit(T->getElementType()); |
| 3991 | } |
| 3992 | |
| 3993 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3994 | return Visit(T->getElementType()); |
| 3995 | } |
| 3996 | |
| 3997 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 3998 | return Visit(T->getElementType()); |
| 3999 | } |
| 4000 | |
| 4001 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 4002 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4003 | for (const auto &A : T->param_types()) { |
| 4004 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4005 | return true; |
| 4006 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4007 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4008 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4009 | } |
| 4010 | |
| 4011 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 4012 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4013 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4014 | } |
| 4015 | |
| 4016 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 4017 | const UnresolvedUsingType*) { |
| 4018 | return false; |
| 4019 | } |
| 4020 | |
| 4021 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 4022 | return false; |
| 4023 | } |
| 4024 | |
| 4025 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 4026 | return Visit(T->getUnderlyingType()); |
| 4027 | } |
| 4028 | |
| 4029 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 4030 | return false; |
| 4031 | } |
| 4032 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4033 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 4034 | const UnaryTransformType*) { |
| 4035 | return false; |
| 4036 | } |
| 4037 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4038 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 4039 | return Visit(T->getDeducedType()); |
| 4040 | } |
| 4041 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4042 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 4043 | return VisitTagDecl(T->getDecl()); |
| 4044 | } |
| 4045 | |
| 4046 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 4047 | return VisitTagDecl(T->getDecl()); |
| 4048 | } |
| 4049 | |
| 4050 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 4051 | const TemplateTypeParmType*) { |
| 4052 | return false; |
| 4053 | } |
| 4054 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4055 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 4056 | const SubstTemplateTypeParmPackType *) { |
| 4057 | return false; |
| 4058 | } |
| 4059 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4060 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 4061 | const TemplateSpecializationType*) { |
| 4062 | return false; |
| 4063 | } |
| 4064 | |
| 4065 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 4066 | const InjectedClassNameType* T) { |
| 4067 | return VisitTagDecl(T->getDecl()); |
| 4068 | } |
| 4069 | |
| 4070 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 4071 | const DependentNameType* T) { |
| 4072 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4073 | } |
| 4074 | |
| 4075 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 4076 | const DependentTemplateSpecializationType* T) { |
| 4077 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4078 | } |
| 4079 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4080 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 4081 | const PackExpansionType* T) { |
| 4082 | return Visit(T->getPattern()); |
| 4083 | } |
| 4084 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4085 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 4086 | return false; |
| 4087 | } |
| 4088 | |
| 4089 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 4090 | const ObjCInterfaceType *) { |
| 4091 | return false; |
| 4092 | } |
| 4093 | |
| 4094 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 4095 | const ObjCObjectPointerType *) { |
| 4096 | return false; |
| 4097 | } |
| 4098 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4099 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 4100 | return Visit(T->getValueType()); |
| 4101 | } |
| 4102 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4103 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 4104 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4105 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4106 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4107 | diag::warn_cxx98_compat_template_arg_local_type : |
| 4108 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4109 | << S.Context.getTypeDeclType(Tag) << SR; |
| 4110 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 4113 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4114 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4115 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4116 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 4117 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4118 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 4119 | return true; |
| 4120 | } |
| 4121 | |
| 4122 | return false; |
| 4123 | } |
| 4124 | |
| 4125 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 4126 | NestedNameSpecifier *NNS) { |
| 4127 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 4128 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4129 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4130 | switch (NNS->getKind()) { |
| 4131 | case NestedNameSpecifier::Identifier: |
| 4132 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4133 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4134 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 4135 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4136 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4137 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4138 | case NestedNameSpecifier::TypeSpec: |
| 4139 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 4140 | return Visit(QualType(NNS->getAsType(), 0)); |
| 4141 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4142 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4146 | /// \brief Check a template argument against its corresponding |
| 4147 | /// template type parameter. |
| 4148 | /// |
| 4149 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 4150 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4151 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4152 | TypeSourceInfo *ArgInfo) { |
| 4153 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4154 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 4155 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 4156 | |
| 4157 | if (Arg->isVariablyModifiedType()) { |
| 4158 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4159 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4160 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4163 | // C++03 [temp.arg.type]p2: |
| 4164 | // A local type, a type with no linkage, an unnamed type or a type |
| 4165 | // compounded from any of these types shall not be used as a |
| 4166 | // template-argument for a template type-parameter. |
| 4167 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4168 | // 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] | 4169 | // a warning. |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 4170 | bool NeedsCheck; |
| 4171 | if (LangOpts.CPlusPlus11) |
| 4172 | NeedsCheck = |
| 4173 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 4174 | SR.getBegin()) || |
| 4175 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_local_type, |
| 4176 | SR.getBegin()); |
| 4177 | else |
| 4178 | NeedsCheck = Arg->hasUnnamedOrLocalType(); |
| 4179 | |
| 4180 | if (NeedsCheck) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4181 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 4182 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 4183 | } |
| 4184 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4185 | return false; |
| 4186 | } |
| 4187 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4188 | enum NullPointerValueKind { |
| 4189 | NPV_NotNullPointer, |
| 4190 | NPV_NullPointer, |
| 4191 | NPV_Error |
| 4192 | }; |
| 4193 | |
| 4194 | /// \brief Determine whether the given template argument is a null pointer |
| 4195 | /// value of the appropriate type. |
| 4196 | static NullPointerValueKind |
| 4197 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 4198 | QualType ParamType, Expr *Arg) { |
| 4199 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 4200 | return NPV_NotNullPointer; |
| 4201 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 4202 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4203 | return NPV_NotNullPointer; |
| 4204 | |
| 4205 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4206 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 4207 | if (ArgRV.isInvalid()) |
| 4208 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4209 | Arg = ArgRV.get(); |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4210 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4211 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4212 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4213 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4214 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4215 | EvalResult.HasSideEffects) { |
| 4216 | SourceLocation DiagLoc = Arg->getExprLoc(); |
| 4217 | |
| 4218 | // If our only note is the usual "invalid subexpression" note, just point |
| 4219 | // the caret at its location rather than producing an essentially |
| 4220 | // redundant note. |
| 4221 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 4222 | diag::note_invalid_subexpr_in_const_expr) { |
| 4223 | DiagLoc = Notes[0].first; |
| 4224 | Notes.clear(); |
| 4225 | } |
| 4226 | |
| 4227 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 4228 | << Arg->getType() << Arg->getSourceRange(); |
| 4229 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 4230 | S.Diag(Notes[I].first, Notes[I].second); |
| 4231 | |
| 4232 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4233 | return NPV_Error; |
| 4234 | } |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4235 | |
| 4236 | // C++11 [temp.arg.nontype]p1: |
| 4237 | // - an address constant expression of type std::nullptr_t |
| 4238 | if (Arg->getType()->isNullPtrType()) |
| 4239 | return NPV_NullPointer; |
| 4240 | |
| 4241 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 4242 | // - a constant expression that evaluates to a null member pointer value |
| 4243 | // (4.11); or |
| 4244 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 4245 | (EvalResult.Val.isMemberPointer() && |
| 4246 | !EvalResult.Val.getMemberPointerDecl())) { |
| 4247 | // If our expression has an appropriate type, we've succeeded. |
| 4248 | bool ObjCLifetimeConversion; |
| 4249 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 4250 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 4251 | ObjCLifetimeConversion)) |
| 4252 | return NPV_NullPointer; |
| 4253 | |
| 4254 | // The types didn't match, but we know we got a null pointer; complain, |
| 4255 | // then recover as if the types were correct. |
| 4256 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 4257 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4258 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4259 | return NPV_NullPointer; |
| 4260 | } |
| 4261 | |
| 4262 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 4263 | // constant, suggest a cast to the appropriate type. |
| 4264 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 4265 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 4266 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4267 | << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 4268 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()), |
| 4269 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4270 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4271 | return NPV_NullPointer; |
| 4272 | } |
| 4273 | |
| 4274 | // FIXME: If we ever want to support general, address-constant expressions |
| 4275 | // as non-type template arguments, we should return the ExprResult here to |
| 4276 | // be interpreted by the caller. |
| 4277 | return NPV_NotNullPointer; |
| 4278 | } |
| 4279 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4280 | /// \brief Checks whether the given template argument is compatible with its |
| 4281 | /// template parameter. |
| 4282 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 4283 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 4284 | Expr *Arg, QualType ArgType) { |
| 4285 | bool ObjCLifetimeConversion; |
| 4286 | if (ParamType->isPointerType() && |
| 4287 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 4288 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 4289 | ObjCLifetimeConversion)) { |
| 4290 | // For pointer-to-object types, qualification conversions are |
| 4291 | // permitted. |
| 4292 | } else { |
| 4293 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 4294 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 4295 | // C++ [temp.arg.nontype]p5b3: |
| 4296 | // For a non-type template-parameter of type reference to |
| 4297 | // object, no conversions apply. The type referred to by the |
| 4298 | // reference may be more cv-qualified than the (otherwise |
| 4299 | // identical) type of the template- argument. The |
| 4300 | // template-parameter is bound directly to the |
| 4301 | // template-argument, which shall be an lvalue. |
| 4302 | |
| 4303 | // FIXME: Other qualifiers? |
| 4304 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 4305 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 4306 | |
| 4307 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 4308 | S.Diag(Arg->getLocStart(), |
| 4309 | diag::err_template_arg_ref_bind_ignores_quals) |
| 4310 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 4311 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4312 | return true; |
| 4313 | } |
| 4314 | } |
| 4315 | } |
| 4316 | |
| 4317 | // At this point, the template argument refers to an object or |
| 4318 | // function with external linkage. We now need to check whether the |
| 4319 | // argument and parameter types are compatible. |
| 4320 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 4321 | ParamType.getNonReferenceType())) { |
| 4322 | // We can't perform this conversion or binding. |
| 4323 | if (ParamType->isReferenceType()) |
| 4324 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 4325 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
| 4326 | else |
| 4327 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4328 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
| 4329 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4330 | return true; |
| 4331 | } |
| 4332 | } |
| 4333 | |
| 4334 | return false; |
| 4335 | } |
| 4336 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4337 | /// \brief Checks whether the given template argument is the address |
| 4338 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4339 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4340 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 4341 | NonTypeTemplateParmDecl *Param, |
| 4342 | QualType ParamType, |
| 4343 | Expr *ArgIn, |
| 4344 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4345 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4346 | Expr *Arg = ArgIn; |
| 4347 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4348 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4349 | bool AddressTaken = false; |
| 4350 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4351 | if (S.getLangOpts().MicrosoftExt) { |
| 4352 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 4353 | // dereference and address-of operators. |
| 4354 | Arg = Arg->IgnoreParenCasts(); |
| 4355 | |
| 4356 | bool ExtWarnMSTemplateArg = false; |
| 4357 | UnaryOperatorKind FirstOpKind; |
| 4358 | SourceLocation FirstOpLoc; |
| 4359 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4360 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 4361 | if (UnOpKind == UO_Deref) |
| 4362 | ExtWarnMSTemplateArg = true; |
| 4363 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 4364 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 4365 | if (!AddrOpLoc.isValid()) { |
| 4366 | FirstOpKind = UnOpKind; |
| 4367 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 4368 | } |
| 4369 | } else |
| 4370 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4371 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4372 | if (FirstOpLoc.isValid()) { |
| 4373 | if (ExtWarnMSTemplateArg) |
| 4374 | S.Diag(ArgIn->getLocStart(), diag::ext_ms_deref_template_argument) |
| 4375 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4376 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4377 | if (FirstOpKind == UO_AddrOf) |
| 4378 | AddressTaken = true; |
| 4379 | else if (Arg->getType()->isPointerType()) { |
| 4380 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 4381 | // constant expression. |
| 4382 | assert(FirstOpKind == UO_Deref); |
| 4383 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4384 | << Arg->getSourceRange(); |
| 4385 | } |
| 4386 | } |
| 4387 | } else { |
| 4388 | // See through any implicit casts we added to fix the type. |
| 4389 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4390 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4391 | // C++ [temp.arg.nontype]p1: |
| 4392 | // |
| 4393 | // A template-argument for a non-type, non-template |
| 4394 | // template-parameter shall be one of: [...] |
| 4395 | // |
| 4396 | // -- the address of an object or function with external |
| 4397 | // linkage, including function templates and function |
| 4398 | // template-ids but excluding non-static class members, |
| 4399 | // expressed as & id-expression where the & is optional if |
| 4400 | // the name refers to a function or array, or if the |
| 4401 | // corresponding template-parameter is a reference; or |
| 4402 | |
| 4403 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4404 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4405 | bool ExtraParens = false; |
| 4406 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 4407 | if (!Invalid && !ExtraParens) { |
| 4408 | S.Diag(Arg->getLocStart(), |
| 4409 | S.getLangOpts().CPlusPlus11 |
| 4410 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 4411 | : diag::ext_template_arg_extra_parens) |
| 4412 | << Arg->getSourceRange(); |
| 4413 | ExtraParens = true; |
| 4414 | } |
| 4415 | |
| 4416 | Arg = Parens->getSubExpr(); |
| 4417 | } |
| 4418 | |
| 4419 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4420 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4421 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4422 | |
| 4423 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4424 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 4425 | Arg = UnOp->getSubExpr(); |
| 4426 | AddressTaken = true; |
| 4427 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 4428 | } |
| 4429 | } |
| 4430 | |
| 4431 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4432 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4433 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4434 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4435 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4436 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 4437 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 4438 | |
| 4439 | // If our parameter has pointer type, check for a null template value. |
| 4440 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 4441 | NullPointerValueKind NPV; |
| 4442 | // dllimport'd entities aren't constant but are available inside of template |
| 4443 | // arguments. |
| 4444 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 4445 | NPV = NPV_NotNullPointer; |
| 4446 | else |
| 4447 | NPV = isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn); |
| 4448 | switch (NPV) { |
| 4449 | case NPV_NullPointer: |
| 4450 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4451 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4452 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4453 | return false; |
| 4454 | |
| 4455 | case NPV_Error: |
| 4456 | return true; |
| 4457 | |
| 4458 | case NPV_NotNullPointer: |
| 4459 | break; |
| 4460 | } |
| 4461 | } |
| 4462 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4463 | // Stop checking the precise nature of the argument if it is value dependent, |
| 4464 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4465 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4466 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4467 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4468 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4469 | |
| 4470 | if (isa<CXXUuidofExpr>(Arg)) { |
| 4471 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 4472 | ArgIn, Arg, ArgType)) |
| 4473 | return true; |
| 4474 | |
| 4475 | Converted = TemplateArgument(ArgIn); |
| 4476 | return false; |
| 4477 | } |
| 4478 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4479 | if (!DRE) { |
| 4480 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4481 | << Arg->getSourceRange(); |
| 4482 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4483 | return true; |
| 4484 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4485 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4486 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4487 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4488 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4489 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4490 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4491 | return true; |
| 4492 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4493 | |
| 4494 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4495 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4496 | if (!Method->isStatic()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4497 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4498 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4499 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4500 | return true; |
| 4501 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4502 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4503 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4504 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 4505 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4506 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4507 | // A non-type template argument must refer to an object or function. |
| 4508 | if (!Func && !Var) { |
| 4509 | // We found something, but we don't know specifically what it is. |
| 4510 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 4511 | << Arg->getSourceRange(); |
| 4512 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 4513 | return true; |
| 4514 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4515 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4516 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4517 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4518 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4519 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 4520 | diag::ext_template_arg_object_internal) |
| 4521 | << !Func << Entity << Arg->getSourceRange(); |
| 4522 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4523 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4524 | } else if (!Entity->hasLinkage()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4525 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 4526 | << !Func << Entity << Arg->getSourceRange(); |
| 4527 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4528 | << !Func; |
| 4529 | return true; |
| 4530 | } |
| 4531 | |
| 4532 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4533 | // If the template parameter has pointer type, the function decays. |
| 4534 | if (ParamType->isPointerType() && !AddressTaken) |
| 4535 | ArgType = S.Context.getPointerType(Func->getType()); |
| 4536 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 4537 | // If we originally had an address-of operator, but the |
| 4538 | // parameter has reference type, complain and (if things look |
| 4539 | // like they will work) drop the address-of operator. |
| 4540 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 4541 | ParamType.getNonReferenceType())) { |
| 4542 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4543 | << ParamType; |
| 4544 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4545 | return true; |
| 4546 | } |
| 4547 | |
| 4548 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4549 | << ParamType |
| 4550 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4551 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4552 | |
| 4553 | ArgType = Func->getType(); |
| 4554 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4555 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4556 | // A value of reference type is not an object. |
| 4557 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4558 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4559 | diag::err_template_arg_reference_var) |
| 4560 | << Var->getType() << Arg->getSourceRange(); |
| 4561 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4562 | return true; |
| 4563 | } |
| 4564 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4565 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 4566 | if (Var->getTLSKind()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4567 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 4568 | << Arg->getSourceRange(); |
| 4569 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 4570 | return true; |
| 4571 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4572 | |
| 4573 | // If the template parameter has pointer type, we must have taken |
| 4574 | // the address of this object. |
| 4575 | if (ParamType->isReferenceType()) { |
| 4576 | if (AddressTaken) { |
| 4577 | // If we originally had an address-of operator, but the |
| 4578 | // parameter has reference type, complain and (if things look |
| 4579 | // like they will work) drop the address-of operator. |
| 4580 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 4581 | ParamType.getNonReferenceType())) { |
| 4582 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4583 | << ParamType; |
| 4584 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4585 | return true; |
| 4586 | } |
| 4587 | |
| 4588 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4589 | << ParamType |
| 4590 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4591 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4592 | |
| 4593 | ArgType = Var->getType(); |
| 4594 | } |
| 4595 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 4596 | if (Var->getType()->isArrayType()) { |
| 4597 | // Array-to-pointer decay. |
| 4598 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 4599 | } else { |
| 4600 | // If the template parameter has pointer type but the address of |
| 4601 | // this object was not taken, complain and (possibly) recover by |
| 4602 | // taking the address of the entity. |
| 4603 | ArgType = S.Context.getPointerType(Var->getType()); |
| 4604 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 4605 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4606 | << ParamType; |
| 4607 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4608 | return true; |
| 4609 | } |
| 4610 | |
| 4611 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4612 | << ParamType |
| 4613 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 4614 | |
| 4615 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4616 | } |
| 4617 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4618 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4619 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4620 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 4621 | Arg, ArgType)) |
| 4622 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4623 | |
| 4624 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4625 | Converted = |
| 4626 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 4627 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4628 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | /// \brief Checks whether the given template argument is a pointer to |
| 4632 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4633 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 4634 | NonTypeTemplateParmDecl *Param, |
| 4635 | QualType ParamType, |
| 4636 | Expr *&ResultArg, |
| 4637 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4638 | bool Invalid = false; |
| 4639 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4640 | // Check for a null pointer value. |
| 4641 | Expr *Arg = ResultArg; |
| 4642 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 4643 | case NPV_Error: |
| 4644 | return true; |
| 4645 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 4646 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4647 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4648 | /*isNullPtr*/true); |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 4649 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 4650 | S.RequireCompleteType(Arg->getExprLoc(), ParamType, 0); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4651 | return false; |
| 4652 | case NPV_NotNullPointer: |
| 4653 | break; |
| 4654 | } |
| 4655 | |
| 4656 | bool ObjCLifetimeConversion; |
| 4657 | if (S.IsQualificationConversion(Arg->getType(), |
| 4658 | ParamType.getNonReferenceType(), |
| 4659 | false, ObjCLifetimeConversion)) { |
| 4660 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4661 | Arg->getValueKind()).get(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4662 | ResultArg = Arg; |
| 4663 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), |
| 4664 | ParamType.getNonReferenceType())) { |
| 4665 | // We can't perform this conversion. |
| 4666 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4667 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4668 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4669 | return true; |
| 4670 | } |
| 4671 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4672 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4673 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4674 | Arg = Cast->getSubExpr(); |
| 4675 | |
| 4676 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4677 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4678 | // A template-argument for a non-type, non-template |
| 4679 | // template-parameter shall be one of: [...] |
| 4680 | // |
| 4681 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4682 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4683 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4684 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4685 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4686 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4687 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4688 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4689 | S.Diag(Arg->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4690 | S.getLangOpts().CPlusPlus11 ? |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4691 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 4692 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4693 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4694 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4695 | } |
| 4696 | |
| 4697 | Arg = Parens->getSubExpr(); |
| 4698 | } |
| 4699 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4700 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4701 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4702 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4703 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4704 | // A pointer-to-member constant written &Class::member. |
| 4705 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4706 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4707 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 4708 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4709 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4710 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4711 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4712 | // A constant of pointer-to-member type. |
| 4713 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 4714 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 4715 | if (VD->getType()->isMemberPointerType()) { |
David Majnemer | cd053cd | 2013-12-10 00:40:58 +0000 | [diff] [blame] | 4716 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4717 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4718 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4719 | } else { |
| 4720 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4721 | Converted = TemplateArgument(VD, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4722 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4723 | return Invalid; |
| 4724 | } |
| 4725 | } |
| 4726 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4727 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4728 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4729 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4730 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4731 | if (!DRE) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4732 | return S.Diag(Arg->getLocStart(), |
| 4733 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4734 | << Arg->getSourceRange(); |
| 4735 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4736 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 4737 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 4738 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4739 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4740 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4741 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 4742 | "Only non-static member pointers can make it here"); |
| 4743 | |
| 4744 | // Okay: this is the address of a non-static member, and therefore |
| 4745 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4746 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4747 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4748 | } else { |
| 4749 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4750 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4751 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4752 | return Invalid; |
| 4753 | } |
| 4754 | |
| 4755 | // 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] | 4756 | S.Diag(Arg->getLocStart(), |
| 4757 | diag::err_template_arg_not_pointer_to_member_form) |
| 4758 | << Arg->getSourceRange(); |
| 4759 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4760 | return true; |
| 4761 | } |
| 4762 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4763 | /// \brief Check a template argument against its corresponding |
| 4764 | /// non-type template parameter. |
| 4765 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4766 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4767 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4768 | /// returns the converted template argument. \p ParamType is the |
| 4769 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4770 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4771 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4772 | TemplateArgument &Converted, |
| 4773 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4774 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4775 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4776 | // If either the parameter has a dependent type or the argument is |
| 4777 | // type-dependent, there's nothing we can check now. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4778 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4779 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 4780 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4781 | return Arg; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4782 | } |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4783 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4784 | // We should have already dropped all cv-qualifiers by now. |
| 4785 | assert(!ParamType.hasQualifiers() && |
| 4786 | "non-type template parameter type cannot be qualified"); |
| 4787 | |
| 4788 | if (CTAK == CTAK_Deduced && |
| 4789 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 4790 | // C++ [temp.deduct.type]p17: |
| 4791 | // If, in the declaration of a function template with a non-type |
| 4792 | // template-parameter, the non-type template-parameter is used |
| 4793 | // in an expression in the function parameter-list and, if the |
| 4794 | // corresponding template-argument is deduced, the |
| 4795 | // template-argument type shall match the type of the |
| 4796 | // template-parameter exactly, except that a template-argument |
| 4797 | // deduced from an array bound may be of any integral type. |
| 4798 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 4799 | << Arg->getType().getUnqualifiedType() |
| 4800 | << ParamType.getUnqualifiedType(); |
| 4801 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4802 | return ExprError(); |
| 4803 | } |
| 4804 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4805 | if (getLangOpts().CPlusPlus1z) { |
| 4806 | // FIXME: We can do some limited checking for a value-dependent but not |
| 4807 | // type-dependent argument. |
| 4808 | if (Arg->isValueDependent()) { |
| 4809 | Converted = TemplateArgument(Arg); |
| 4810 | return Arg; |
| 4811 | } |
| 4812 | |
| 4813 | // C++1z [temp.arg.nontype]p1: |
| 4814 | // A template-argument for a non-type template parameter shall be |
| 4815 | // a converted constant expression of the type of the template-parameter. |
| 4816 | APValue Value; |
| 4817 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 4818 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 4819 | if (ArgResult.isInvalid()) |
| 4820 | return ExprError(); |
| 4821 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4822 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 4823 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4824 | // Convert the APValue to a TemplateArgument. |
| 4825 | switch (Value.getKind()) { |
| 4826 | case APValue::Uninitialized: |
| 4827 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4828 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4829 | break; |
| 4830 | case APValue::Int: |
| 4831 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4832 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4833 | break; |
| 4834 | case APValue::MemberPointer: { |
| 4835 | assert(ParamType->isMemberPointerType()); |
| 4836 | |
| 4837 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 4838 | if (!Value.getMemberPointerPath().empty()) { |
| 4839 | Diag(Arg->getLocStart(), |
| 4840 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 4841 | << Value.getMemberPointerDecl() << ParamType |
| 4842 | << Arg->getSourceRange(); |
| 4843 | return ExprError(); |
| 4844 | } |
| 4845 | |
| 4846 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4847 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 4848 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4849 | break; |
| 4850 | } |
| 4851 | case APValue::LValue: { |
| 4852 | // For a non-type template-parameter of pointer or reference type, |
| 4853 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4854 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 4855 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4856 | // -- a temporary object |
| 4857 | // -- a string literal |
| 4858 | // -- the result of a typeid expression, or |
| 4859 | // -- a predefind __func__ variable |
| 4860 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 4861 | if (isa<CXXUuidofExpr>(E)) { |
| 4862 | Converted = TemplateArgument(const_cast<Expr*>(E)); |
| 4863 | break; |
| 4864 | } |
| 4865 | Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4866 | << Arg->getSourceRange(); |
| 4867 | return ExprError(); |
| 4868 | } |
| 4869 | auto *VD = const_cast<ValueDecl *>( |
| 4870 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 4871 | // -- a subobject |
| 4872 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 4873 | VD && VD->getType()->isArrayType() && |
| 4874 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 4875 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 4876 | // Per defect report (no number yet): |
| 4877 | // ... other than a pointer to the first element of a complete array |
| 4878 | // object. |
| 4879 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 4880 | Value.isLValueOnePastTheEnd()) { |
| 4881 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 4882 | << Value.getAsString(Context, ParamType); |
| 4883 | return ExprError(); |
| 4884 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4885 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4886 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4887 | assert((!VD || !ParamType->isNullPtrType()) && |
| 4888 | "non-null value of type nullptr_t?"); |
| 4889 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 4890 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4891 | break; |
| 4892 | } |
| 4893 | case APValue::AddrLabelDiff: |
| 4894 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
| 4895 | case APValue::Float: |
| 4896 | case APValue::ComplexInt: |
| 4897 | case APValue::ComplexFloat: |
| 4898 | case APValue::Vector: |
| 4899 | case APValue::Array: |
| 4900 | case APValue::Struct: |
| 4901 | case APValue::Union: |
| 4902 | llvm_unreachable("invalid kind for template argument"); |
| 4903 | } |
| 4904 | |
| 4905 | return ArgResult.get(); |
| 4906 | } |
| 4907 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4908 | // C++ [temp.arg.nontype]p5: |
| 4909 | // The following conversions are performed on each expression used |
| 4910 | // as a non-type template-argument. If a non-type |
| 4911 | // template-argument cannot be converted to the type of the |
| 4912 | // corresponding template-parameter then the program is |
| 4913 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4914 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4915 | // C++11: |
| 4916 | // -- for a non-type template-parameter of integral or |
| 4917 | // enumeration type, conversions permitted in a converted |
| 4918 | // constant expression are applied. |
| 4919 | // |
| 4920 | // C++98: |
| 4921 | // -- for a non-type template-parameter of integral or |
| 4922 | // enumeration type, integral promotions (4.5) and integral |
| 4923 | // conversions (4.7) are applied. |
| 4924 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4925 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4926 | // We can't check arbitrary value-dependent arguments. |
| 4927 | // FIXME: If there's no viable conversion to the template parameter type, |
| 4928 | // we should be able to diagnose that prior to instantiation. |
| 4929 | if (Arg->isValueDependent()) { |
| 4930 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4931 | return Arg; |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4932 | } |
| 4933 | |
| 4934 | // C++ [temp.arg.nontype]p1: |
| 4935 | // A template-argument for a non-type, non-template template-parameter |
| 4936 | // shall be one of: |
| 4937 | // |
| 4938 | // -- for a non-type template-parameter of integral or enumeration |
| 4939 | // type, a converted constant expression of the type of the |
| 4940 | // template-parameter; or |
| 4941 | llvm::APSInt Value; |
| 4942 | ExprResult ArgResult = |
| 4943 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 4944 | CCEK_TemplateArg); |
| 4945 | if (ArgResult.isInvalid()) |
| 4946 | return ExprError(); |
| 4947 | |
| 4948 | // Widen the argument value to sizeof(parameter type). This is almost |
| 4949 | // always a no-op, except when the parameter type is bool. In |
| 4950 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 4951 | QualType IntegerType = ParamType; |
| 4952 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 4953 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 4954 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 4955 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4956 | Converted = TemplateArgument(Context, Value, |
| 4957 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4958 | return ArgResult; |
| 4959 | } |
| 4960 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4961 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 4962 | if (ArgResult.isInvalid()) |
| 4963 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4964 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4965 | |
| 4966 | QualType ArgType = Arg->getType(); |
| 4967 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4968 | // C++ [temp.arg.nontype]p1: |
| 4969 | // A template-argument for a non-type, non-template |
| 4970 | // template-parameter shall be one of: |
| 4971 | // |
| 4972 | // -- an integral constant-expression of integral or enumeration |
| 4973 | // type; or |
| 4974 | // -- the name of a non-type template-parameter; or |
| 4975 | SourceLocation NonConstantLoc; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4976 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4977 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4978 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4979 | diag::err_template_arg_not_integral_or_enumeral) |
| 4980 | << ArgType << Arg->getSourceRange(); |
| 4981 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4982 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4983 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 4984 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 4985 | QualType T; |
| 4986 | |
| 4987 | public: |
| 4988 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 4989 | |
| 4990 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 4991 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 4992 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 4993 | } |
| 4994 | } Diagnoser(ArgType); |
| 4995 | |
| 4996 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4997 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4998 | if (!Arg) |
| 4999 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5000 | } |
| 5001 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5002 | // From here on out, all we care about is the unqualified form |
| 5003 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5004 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5005 | |
| 5006 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 5007 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5008 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5009 | } else if (ParamType->isBooleanType()) { |
| 5010 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5011 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5012 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 5013 | !ParamType->isEnumeralType()) { |
| 5014 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5015 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5016 | } else { |
| 5017 | // We can't perform this conversion. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5018 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5019 | diag::err_template_arg_not_convertible) |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5020 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5021 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5022 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5023 | } |
| 5024 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5025 | // Add the value of this argument to the list of converted |
| 5026 | // arguments. We use the bitwidth and signedness of the template |
| 5027 | // parameter. |
| 5028 | if (Arg->isValueDependent()) { |
| 5029 | // The argument is value-dependent. Create a new |
| 5030 | // TemplateArgument with the converted expression. |
| 5031 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5032 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5033 | } |
| 5034 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5035 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5036 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 5037 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5038 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5039 | if (ParamType->isBooleanType()) { |
| 5040 | // Value must be zero or one. |
| 5041 | Value = Value != 0; |
| 5042 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 5043 | if (Value.getBitWidth() != AllowedBits) |
| 5044 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5045 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5046 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5047 | llvm::APSInt OldValue = Value; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5048 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5049 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5050 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5051 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5052 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5053 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5054 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5055 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5056 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5057 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5058 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5059 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5060 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5061 | << Arg->getSourceRange(); |
| 5062 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5063 | } |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5064 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5065 | // Complain if we overflowed the template parameter's type. |
| 5066 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5067 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5068 | RequiredBits = OldValue.getActiveBits(); |
| 5069 | else if (OldValue.isUnsigned()) |
| 5070 | RequiredBits = OldValue.getActiveBits() + 1; |
| 5071 | else |
| 5072 | RequiredBits = OldValue.getMinSignedBits(); |
| 5073 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5074 | Diag(Arg->getLocStart(), |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5075 | diag::warn_template_arg_too_large) |
| 5076 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5077 | << Arg->getSourceRange(); |
| 5078 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5079 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5080 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5081 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5082 | Converted = TemplateArgument(Context, Value, |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 5083 | ParamType->isEnumeralType() |
| 5084 | ? Context.getCanonicalType(ParamType) |
| 5085 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5086 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5087 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5088 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5089 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5090 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 5091 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5092 | // Handle pointer-to-function, reference-to-function, and |
| 5093 | // pointer-to-member-function all in (roughly) the same way. |
| 5094 | if (// -- For a non-type template-parameter of type pointer to |
| 5095 | // function, only the function-to-pointer conversion (4.3) is |
| 5096 | // applied. If the template-argument represents a set of |
| 5097 | // overloaded functions (or a pointer to such), the matching |
| 5098 | // function is selected from the set (13.4). |
| 5099 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5100 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5101 | // -- For a non-type template-parameter of type reference to |
| 5102 | // function, no conversions apply. If the template-argument |
| 5103 | // represents a set of overloaded functions, the matching |
| 5104 | // function is selected from the set (13.4). |
| 5105 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5106 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5107 | // -- For a non-type template-parameter of type pointer to |
| 5108 | // member function, no conversions apply. If the |
| 5109 | // template-argument represents a set of overloaded member |
| 5110 | // functions, the matching member function is selected from |
| 5111 | // the set (13.4). |
| 5112 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5113 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5114 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5115 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5116 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5117 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5118 | true, |
| 5119 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5120 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5121 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5122 | |
| 5123 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5124 | ArgType = Arg->getType(); |
| 5125 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5126 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5127 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5128 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5129 | if (!ParamType->isMemberPointerType()) { |
| 5130 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5131 | ParamType, |
| 5132 | Arg, Converted)) |
| 5133 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5134 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5135 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5136 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5137 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5138 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5139 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5140 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5141 | } |
| 5142 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 5143 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5144 | // -- for a non-type template-parameter of type pointer to |
| 5145 | // object, qualification conversions (4.4) and the |
| 5146 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5147 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5148 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5149 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5150 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5151 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5152 | ParamType, |
| 5153 | Arg, Converted)) |
| 5154 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5155 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5156 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5157 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5158 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5159 | // -- For a non-type template-parameter of type reference to |
| 5160 | // object, no conversions apply. The type referred to by the |
| 5161 | // reference may be more cv-qualified than the (otherwise |
| 5162 | // identical) type of the template-argument. The |
| 5163 | // template-parameter is bound directly to the |
| 5164 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5165 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5166 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5167 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5168 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5169 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 5170 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5171 | true, |
| 5172 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5173 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5174 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5175 | |
| 5176 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5177 | ArgType = Arg->getType(); |
| 5178 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5179 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5180 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5181 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5182 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5183 | ParamType, |
| 5184 | Arg, Converted)) |
| 5185 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5186 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5187 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5188 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5189 | // Deal with parameters of type std::nullptr_t. |
| 5190 | if (ParamType->isNullPtrType()) { |
| 5191 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 5192 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5193 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5194 | } |
| 5195 | |
| 5196 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 5197 | case NPV_NotNullPointer: |
| 5198 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 5199 | << Arg->getType() << ParamType; |
| 5200 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5201 | return ExprError(); |
| 5202 | |
| 5203 | case NPV_Error: |
| 5204 | return ExprError(); |
| 5205 | |
| 5206 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 5207 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5208 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 5209 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5210 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5211 | } |
| 5212 | } |
| 5213 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5214 | // -- For a non-type template-parameter of type pointer to data |
| 5215 | // member, qualification conversions (4.4) are applied. |
| 5216 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 5217 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5218 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5219 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5220 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5221 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5222 | } |
| 5223 | |
| 5224 | /// \brief Check a template argument against its corresponding |
| 5225 | /// template template parameter. |
| 5226 | /// |
| 5227 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 5228 | /// It returns true if an error occurred, and false otherwise. |
| 5229 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 5230 | TemplateArgumentLoc &Arg, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5231 | unsigned ArgumentPackIndex) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5232 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5233 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 5234 | if (!Template) { |
| 5235 | // Any dependent template name is fine. |
| 5236 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 5237 | return false; |
| 5238 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5239 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5240 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5241 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5242 | // the name of a class template or an alias template, expressed as an |
| 5243 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5244 | // primary class templates are considered when matching the |
| 5245 | // template template argument with the corresponding parameter; |
| 5246 | // partial specializations are not considered even if their |
| 5247 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5248 | // |
| 5249 | // Note that we also allow template template parameters here, which |
| 5250 | // will happen when we are dealing with, e.g., class template |
| 5251 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5252 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5253 | !isa<TemplateTemplateParmDecl>(Template) && |
| 5254 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5255 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5256 | "Only function templates are possible here"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5257 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5258 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5259 | << Template; |
| 5260 | } |
| 5261 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5262 | TemplateParameterList *Params = Param->getTemplateParameters(); |
| 5263 | if (Param->isExpandedParameterPack()) |
| 5264 | Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); |
| 5265 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5266 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5267 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5268 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5269 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5270 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5271 | } |
| 5272 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5273 | /// \brief Given a non-type template argument that refers to a |
| 5274 | /// declaration and the type of its corresponding non-type template |
| 5275 | /// parameter, produce an expression that properly refers to that |
| 5276 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5277 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5278 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 5279 | QualType ParamType, |
| 5280 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 5281 | // C++ [temp.param]p8: |
| 5282 | // |
| 5283 | // A non-type template-parameter of type "array of T" or |
| 5284 | // "function returning T" is adjusted to be of type "pointer to |
| 5285 | // T" or "pointer to function returning T", respectively. |
| 5286 | if (ParamType->isArrayType()) |
| 5287 | ParamType = Context.getArrayDecayedType(ParamType); |
| 5288 | else if (ParamType->isFunctionType()) |
| 5289 | ParamType = Context.getPointerType(ParamType); |
| 5290 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5291 | // For a NULL non-type template argument, return nullptr casted to the |
| 5292 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5293 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5294 | return ImpCastExprToType( |
| 5295 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 5296 | ParamType, |
| 5297 | ParamType->getAs<MemberPointerType>() |
| 5298 | ? CK_NullToMemberPointer |
| 5299 | : CK_NullToPointer); |
| 5300 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5301 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 5302 | "Only declaration template arguments permitted here"); |
| 5303 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5304 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 5305 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5306 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 5307 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 5308 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5309 | // If the value is a class member, we might have a pointer-to-member. |
| 5310 | // Determine whether the non-type template template parameter is of |
| 5311 | // pointer-to-member type. If so, we need to build an appropriate |
| 5312 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 5313 | // would refer to the member itself. |
| 5314 | if (ParamType->isMemberPointerType()) { |
| 5315 | QualType ClassType |
| 5316 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 5317 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5318 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5319 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5320 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 5321 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5322 | |
| 5323 | // The actual value-ness of this is unimportant, but for |
| 5324 | // internal consistency's sake, references to instance methods |
| 5325 | // are r-values. |
| 5326 | ExprValueKind VK = VK_LValue; |
| 5327 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 5328 | VK = VK_RValue; |
| 5329 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5330 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5331 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5332 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5333 | Loc, |
| 5334 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5335 | if (RefExpr.isInvalid()) |
| 5336 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5337 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5338 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5339 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5340 | // We might need to perform a trailing qualification conversion, since |
| 5341 | // the element type on the parameter could be more qualified than the |
| 5342 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5343 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5344 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5345 | ParamType.getUnqualifiedType(), false, |
| 5346 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5347 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5348 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5349 | assert(!RefExpr.isInvalid() && |
| 5350 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5351 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5352 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5353 | } |
| 5354 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5355 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5356 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5357 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5358 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5359 | // When the non-type template parameter is a pointer, take the |
| 5360 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5361 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5362 | if (RefExpr.isInvalid()) |
| 5363 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5364 | |
| 5365 | if (T->isFunctionType() || T->isArrayType()) { |
| 5366 | // Decay functions and arrays. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5367 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5368 | if (RefExpr.isInvalid()) |
| 5369 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5370 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5371 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5372 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5373 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5374 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5375 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5376 | } |
| 5377 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5378 | ExprValueKind VK = VK_RValue; |
| 5379 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5380 | // If the non-type template parameter has reference type, qualify the |
| 5381 | // resulting declaration reference with the extra qualifiers on the |
| 5382 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5383 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 5384 | VK = VK_LValue; |
| 5385 | T = Context.getQualifiedType(T, |
| 5386 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5387 | } else if (isa<FunctionDecl>(VD)) { |
| 5388 | // References to functions are always lvalues. |
| 5389 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5390 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5391 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5392 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5393 | } |
| 5394 | |
| 5395 | /// \brief Construct a new expression that refers to the given |
| 5396 | /// integral template argument with the given source-location |
| 5397 | /// information. |
| 5398 | /// |
| 5399 | /// This routine takes care of the mapping from an integral template |
| 5400 | /// argument (which may have any integral type) to the appropriate |
| 5401 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5402 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5403 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 5404 | SourceLocation Loc) { |
| 5405 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 5406 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5407 | QualType OrigT = Arg.getIntegralType(); |
| 5408 | |
| 5409 | // If this is an enum type that we're instantiating, we need to use an integer |
| 5410 | // type the same size as the enumerator. We don't want to build an |
| 5411 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 5412 | // any integral type with C++11 enum classes, make sure we create the right |
| 5413 | // type of literal for it. |
| 5414 | QualType T = OrigT; |
| 5415 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 5416 | T = ET->getDecl()->getIntegerType(); |
| 5417 | |
| 5418 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5419 | if (T->isAnyCharacterType()) { |
| 5420 | CharacterLiteral::CharacterKind Kind; |
| 5421 | if (T->isWideCharType()) |
| 5422 | Kind = CharacterLiteral::Wide; |
| 5423 | else if (T->isChar16Type()) |
| 5424 | Kind = CharacterLiteral::UTF16; |
| 5425 | else if (T->isChar32Type()) |
| 5426 | Kind = CharacterLiteral::UTF32; |
| 5427 | else |
| 5428 | Kind = CharacterLiteral::Ascii; |
| 5429 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5430 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 5431 | Kind, T, Loc); |
| 5432 | } else if (T->isBooleanType()) { |
| 5433 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 5434 | T, Loc); |
| 5435 | } else if (T->isNullPtrType()) { |
| 5436 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 5437 | } else { |
| 5438 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5439 | } |
| 5440 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5441 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5442 | // FIXME: This is a hack. We need a better way to handle substituted |
| 5443 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5444 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 5445 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5446 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5447 | Loc, Loc); |
| 5448 | } |
| 5449 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5450 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5451 | } |
| 5452 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5453 | /// \brief Match two template parameters within template parameter lists. |
| 5454 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 5455 | bool Complain, |
| 5456 | Sema::TemplateParameterListEqualKind Kind, |
| 5457 | SourceLocation TemplateArgLoc) { |
| 5458 | // Check the actual kind (type, non-type, template). |
| 5459 | if (Old->getKind() != New->getKind()) { |
| 5460 | if (Complain) { |
| 5461 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 5462 | if (TemplateArgLoc.isValid()) { |
| 5463 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5464 | NextDiag = diag::note_template_param_different_kind; |
| 5465 | } |
| 5466 | S.Diag(New->getLocation(), NextDiag) |
| 5467 | << (Kind != Sema::TPL_TemplateMatch); |
| 5468 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 5469 | << (Kind != Sema::TPL_TemplateMatch); |
| 5470 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5471 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5472 | return false; |
| 5473 | } |
| 5474 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5475 | // Check that both are parameter packs are neither are parameter packs. |
| 5476 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5477 | // template template parameter, the template template parameter can have |
| 5478 | // a parameter pack where the template template argument does not. |
| 5479 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 5480 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5481 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5482 | if (Complain) { |
| 5483 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 5484 | if (TemplateArgLoc.isValid()) { |
| 5485 | S.Diag(TemplateArgLoc, |
| 5486 | diag::err_template_arg_template_params_mismatch); |
| 5487 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 5488 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5489 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5490 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 5491 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 5492 | : 2; |
| 5493 | S.Diag(New->getLocation(), NextDiag) |
| 5494 | << ParamKind << New->isParameterPack(); |
| 5495 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 5496 | << ParamKind << Old->isParameterPack(); |
| 5497 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5498 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5499 | return false; |
| 5500 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5501 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5502 | // For non-type template parameters, check the type of the parameter. |
| 5503 | if (NonTypeTemplateParmDecl *OldNTTP |
| 5504 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 5505 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5506 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5507 | // If we are matching a template template argument to a template |
| 5508 | // template parameter and one of the non-type template parameter types |
| 5509 | // is dependent, then we must wait until template instantiation time |
| 5510 | // to actually compare the arguments. |
| 5511 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5512 | (OldNTTP->getType()->isDependentType() || |
| 5513 | NewNTTP->getType()->isDependentType())) |
| 5514 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5515 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5516 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 5517 | if (Complain) { |
| 5518 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 5519 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5520 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5521 | diag::err_template_arg_template_params_mismatch); |
| 5522 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 5523 | } |
| 5524 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 5525 | << NewNTTP->getType() |
| 5526 | << (Kind != Sema::TPL_TemplateMatch); |
| 5527 | S.Diag(OldNTTP->getLocation(), |
| 5528 | diag::note_template_nontype_parm_prev_declaration) |
| 5529 | << OldNTTP->getType(); |
| 5530 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5531 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5532 | return false; |
| 5533 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5534 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5535 | return true; |
| 5536 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5537 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5538 | // For template template parameters, check the template parameter types. |
| 5539 | // The template parameter lists of template template |
| 5540 | // parameters must agree. |
| 5541 | if (TemplateTemplateParmDecl *OldTTP |
| 5542 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5543 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5544 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 5545 | OldTTP->getTemplateParameters(), |
| 5546 | Complain, |
| 5547 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5548 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5549 | : Kind), |
| 5550 | TemplateArgLoc); |
| 5551 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5552 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5553 | return true; |
| 5554 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5555 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5556 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 5557 | /// lists. |
| 5558 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5559 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5560 | TemplateParameterList *New, |
| 5561 | TemplateParameterList *Old, |
| 5562 | Sema::TemplateParameterListEqualKind Kind, |
| 5563 | SourceLocation TemplateArgLoc) { |
| 5564 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 5565 | if (TemplateArgLoc.isValid()) { |
| 5566 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5567 | NextDiag = diag::note_template_param_list_different_arity; |
| 5568 | } |
| 5569 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 5570 | << (New->size() > Old->size()) |
| 5571 | << (Kind != Sema::TPL_TemplateMatch) |
| 5572 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 5573 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 5574 | << (Kind != Sema::TPL_TemplateMatch) |
| 5575 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 5576 | } |
| 5577 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5578 | /// \brief Determine whether the given template parameter lists are |
| 5579 | /// equivalent. |
| 5580 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5581 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5582 | /// source code as part of a new template declaration. |
| 5583 | /// |
| 5584 | /// \param Old The old template parameter list, typically found via |
| 5585 | /// name lookup of the template declared with this template parameter |
| 5586 | /// list. |
| 5587 | /// |
| 5588 | /// \param Complain If true, this routine will produce a diagnostic if |
| 5589 | /// the template parameter lists are not equivalent. |
| 5590 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5591 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5592 | /// |
| 5593 | /// \param TemplateArgLoc If this source location is valid, then we |
| 5594 | /// are actually checking the template parameter list of a template |
| 5595 | /// argument (New) against the template parameter list of its |
| 5596 | /// corresponding template template parameter (Old). We produce |
| 5597 | /// slightly different diagnostics in this scenario. |
| 5598 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5599 | /// \returns True if the template parameter lists are equal, false |
| 5600 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5601 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5602 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 5603 | TemplateParameterList *Old, |
| 5604 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5605 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5606 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5607 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 5608 | if (Complain) |
| 5609 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5610 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5611 | |
| 5612 | return false; |
| 5613 | } |
| 5614 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5615 | // C++0x [temp.arg.template]p3: |
| 5616 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5617 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5618 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5619 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5620 | // template-parameter-list of P. [...] |
| 5621 | TemplateParameterList::iterator NewParm = New->begin(); |
| 5622 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5623 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5624 | OldParmEnd = Old->end(); |
| 5625 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 5626 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 5627 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5628 | if (NewParm == NewParmEnd) { |
| 5629 | if (Complain) |
| 5630 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5631 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5632 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5633 | return false; |
| 5634 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5635 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5636 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5637 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5638 | return false; |
| 5639 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5640 | ++NewParm; |
| 5641 | continue; |
| 5642 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5643 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5644 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5645 | // [...] When P's template- parameter-list contains a template parameter |
| 5646 | // pack (14.5.3), the template parameter pack will match zero or more |
| 5647 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5648 | // template-parameter-list of A with the same type and form as the |
| 5649 | // template parameter pack in P (ignoring whether those template |
| 5650 | // parameters are template parameter packs). |
| 5651 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 5652 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5653 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5654 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5655 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5656 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5657 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5658 | // Make sure we exhausted all of the arguments. |
| 5659 | if (NewParm != NewParmEnd) { |
| 5660 | if (Complain) |
| 5661 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5662 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5663 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5664 | return false; |
| 5665 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5666 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5667 | return true; |
| 5668 | } |
| 5669 | |
| 5670 | /// \brief Check whether a template can be declared within this scope. |
| 5671 | /// |
| 5672 | /// If the template declaration is valid in this scope, returns |
| 5673 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5674 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5675 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 5676 | if (!S) |
| 5677 | return false; |
| 5678 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5679 | // Find the nearest enclosing declaration scope. |
| 5680 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5681 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5682 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5683 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5684 | // C++ [temp]p4: |
| 5685 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 5686 | DeclContext *Ctx = S->getEntity(); |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5687 | if (Ctx && Ctx->isExternCContext()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5688 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5689 | << TemplateParams->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5690 | |
Eli Friedman | dfbd0c4 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 5691 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5692 | Ctx = Ctx->getParent(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5693 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5694 | // C++ [temp]p2: |
| 5695 | // A template-declaration can appear only as a namespace scope or |
| 5696 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 5697 | if (Ctx) { |
| 5698 | if (Ctx->isFileContext()) |
| 5699 | return false; |
| 5700 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 5701 | // C++ [temp.mem]p2: |
| 5702 | // A local class shall not have member templates. |
| 5703 | if (RD->isLocalClass()) |
| 5704 | return Diag(TemplateParams->getTemplateLoc(), |
| 5705 | diag::err_template_inside_local_class) |
| 5706 | << TemplateParams->getSourceRange(); |
| 5707 | else |
| 5708 | return false; |
| 5709 | } |
| 5710 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5711 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5712 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5713 | diag::err_template_outside_namespace_or_class_scope) |
| 5714 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5715 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5716 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5717 | /// \brief Determine what kind of template specialization the given declaration |
| 5718 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5719 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5720 | if (!D) |
| 5721 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5722 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5723 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 5724 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5725 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 5726 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5727 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 5728 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5729 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5730 | return TSK_Undeclared; |
| 5731 | } |
| 5732 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5733 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5734 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5735 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5736 | /// This routine determines whether a template specialization can be declared |
| 5737 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5738 | /// |
| 5739 | /// \param S the semantic analysis object for which this check is being |
| 5740 | /// performed. |
| 5741 | /// |
| 5742 | /// \param Specialized the entity being specialized or instantiated, which |
| 5743 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5744 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5745 | /// member class). |
| 5746 | /// |
| 5747 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 5748 | /// |
| 5749 | /// \param Loc the location of the explicit specialization or instantiation of |
| 5750 | /// this entity. |
| 5751 | /// |
| 5752 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 5753 | /// a class template. |
| 5754 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5755 | /// \returns true if there was an error that we cannot recover from, false |
| 5756 | /// otherwise. |
| 5757 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 5758 | NamedDecl *Specialized, |
| 5759 | NamedDecl *PrevDecl, |
| 5760 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5761 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5762 | // Keep these "kind" numbers in sync with the %select statements in the |
| 5763 | // various diagnostics emitted by this routine. |
| 5764 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5765 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5766 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5767 | else if (isa<VarTemplateDecl>(Specialized)) |
| 5768 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5769 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5770 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5771 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5772 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5773 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5774 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5775 | else if (isa<RecordDecl>(Specialized)) |
| 5776 | EntityKind = 7; |
| 5777 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 5778 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5779 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5780 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5781 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5782 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5783 | return true; |
| 5784 | } |
| 5785 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5786 | // C++ [temp.expl.spec]p2: |
| 5787 | // An explicit specialization shall be declared in the namespace |
| 5788 | // of which the template is a member, or, for member templates, in |
| 5789 | // the namespace of which the enclosing class or enclosing class |
| 5790 | // template is a member. An explicit specialization of a member |
| 5791 | // function, member class or static data member of a class |
| 5792 | // template shall be declared in the namespace of which the class |
| 5793 | // template is a member. Such a declaration may also be a |
| 5794 | // definition. If the declaration is not a definition, the |
| 5795 | // specialization may be defined later in the name- space in which |
| 5796 | // the explicit specialization was declared, or in a namespace |
| 5797 | // that encloses the one in which the explicit specialization was |
| 5798 | // declared. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5799 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5800 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5801 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5802 | return true; |
| 5803 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5804 | |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5805 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5806 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 5807 | // Do not warn for class scope explicit specialization during |
| 5808 | // instantiation, warning was already emitted during pattern |
| 5809 | // semantic analysis. |
| 5810 | if (!S.ActiveTemplateInstantiations.size()) |
| 5811 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 5812 | << Specialized; |
| 5813 | } else { |
| 5814 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5815 | << Specialized; |
| 5816 | return true; |
| 5817 | } |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5818 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5819 | |
Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 5820 | if (S.CurContext->isRecord() && |
| 5821 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 5822 | // Make sure that we're specializing in the right record context. |
| 5823 | // Otherwise, things can go horribly wrong. |
| 5824 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5825 | << Specialized; |
| 5826 | return true; |
| 5827 | } |
| 5828 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5829 | // C++ [temp.class.spec]p6: |
| 5830 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5831 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 5832 | // and 14.5.2). |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5833 | DeclContext *SpecializedContext |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5834 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5835 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5836 | |
| 5837 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
| 5838 | // namespace. |
| 5839 | // Note that HandleDeclarator() performs this check for explicit |
| 5840 | // specializations of function templates, static data members, and member |
| 5841 | // functions, so we skip the check here for those kinds of entities. |
| 5842 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
| 5843 | // Should we refactor that check, so that it occurs later? |
| 5844 | if (!DC->Encloses(SpecializedContext) && |
| 5845 | !(isa<FunctionTemplateDecl>(Specialized) || |
| 5846 | isa<FunctionDecl>(Specialized) || |
| 5847 | isa<VarTemplateDecl>(Specialized) || |
| 5848 | isa<VarDecl>(Specialized))) { |
| 5849 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 5850 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 5851 | << EntityKind << Specialized; |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 5852 | else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 5853 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
| 5854 | if (S.getLangOpts().MicrosoftExt) |
| 5855 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 5856 | S.Diag(Loc, Diag) << EntityKind << Specialized |
| 5857 | << cast<NamedDecl>(SpecializedContext); |
| 5858 | } else |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5859 | llvm_unreachable("unexpected namespace context for specialization"); |
| 5860 | |
| 5861 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 5862 | } else if ((!PrevDecl || |
| 5863 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 5864 | getTemplateSpecializationKind(PrevDecl) == |
| 5865 | TSK_ImplicitInstantiation)) { |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5866 | // C++ [temp.exp.spec]p2: |
| 5867 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5868 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5869 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5870 | // An explicit specialization of a member function, member class or |
| 5871 | // static data member of a class template shall be declared in the |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5872 | // namespace of which the class template is a member. |
| 5873 | // |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5874 | // C++11 [temp.expl.spec]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5875 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5876 | // the specialized template. |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5877 | // C++11 [temp.explicit]p3: |
| 5878 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 5879 | // template. |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5880 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5881 | bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext); |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5882 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5883 | assert(!IsCPlusPlus11Extension && |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5884 | "DC encloses TU but isn't in enclosing namespace set"); |
| 5885 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | 8ce6315 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 5886 | << EntityKind << Specialized; |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5887 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 5888 | int Diag; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5889 | if (!IsCPlusPlus11Extension) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5890 | Diag = diag::err_template_spec_decl_out_of_scope; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5891 | else if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5892 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 5893 | else |
| 5894 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 5895 | S.Diag(Loc, Diag) |
| 5896 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 5897 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5898 | |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5899 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5900 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5901 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5902 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5903 | return false; |
| 5904 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5905 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5906 | static SourceRange findTemplateParameter(unsigned Depth, Expr *E) { |
| 5907 | if (!E->isInstantiationDependent()) |
| 5908 | return SourceLocation(); |
| 5909 | DependencyChecker Checker(Depth); |
| 5910 | Checker.TraverseStmt(E); |
| 5911 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 5912 | return E->getSourceRange(); |
| 5913 | return Checker.MatchLoc; |
| 5914 | } |
| 5915 | |
| 5916 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 5917 | if (!TL.getType()->isDependentType()) |
| 5918 | return SourceLocation(); |
| 5919 | DependencyChecker Checker(Depth); |
| 5920 | Checker.TraverseTypeLoc(TL); |
| 5921 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 5922 | return TL.getSourceRange(); |
| 5923 | return Checker.MatchLoc; |
| 5924 | } |
| 5925 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5926 | /// \brief Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5927 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5928 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5929 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 5930 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5931 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 5932 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5933 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5934 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 5935 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5936 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5937 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5938 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5939 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5940 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5941 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5942 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5943 | |
| 5944 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5945 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 5946 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5947 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 5948 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 5949 | |
| 5950 | // Strip off any implicit casts we added as part of type checking. |
| 5951 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 5952 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5953 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5954 | // C++ [temp.class.spec]p8: |
| 5955 | // A non-type argument is non-specialized if it is the name of a |
| 5956 | // non-type parameter. All other non-type arguments are |
| 5957 | // specialized. |
| 5958 | // |
| 5959 | // Below, we check the two conditions that only apply to |
| 5960 | // specialized non-type arguments, so skip any non-specialized |
| 5961 | // arguments. |
| 5962 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 5963 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5964 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5965 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5966 | // C++ [temp.class.spec]p9: |
| 5967 | // Within the argument list of a class template partial |
| 5968 | // specialization, the following restrictions apply: |
| 5969 | // -- A partially specialized non-type argument expression |
| 5970 | // shall not involve a template parameter of the partial |
| 5971 | // specialization except when the argument expression is a |
| 5972 | // simple identifier. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5973 | SourceRange ParamUseRange = |
| 5974 | findTemplateParameter(Param->getDepth(), ArgExpr); |
| 5975 | if (ParamUseRange.isValid()) { |
| 5976 | if (IsDefaultArgument) { |
| 5977 | S.Diag(TemplateNameLoc, |
| 5978 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 5979 | S.Diag(ParamUseRange.getBegin(), |
| 5980 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 5981 | << ParamUseRange; |
| 5982 | } else { |
| 5983 | S.Diag(ParamUseRange.getBegin(), |
| 5984 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 5985 | << ParamUseRange; |
| 5986 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5987 | return true; |
| 5988 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5989 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5990 | // -- The type of a template parameter corresponding to a |
| 5991 | // specialized non-type argument shall not be dependent on a |
| 5992 | // parameter of the specialization. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5993 | // |
| 5994 | // FIXME: We need to delay this check until instantiation in some cases: |
| 5995 | // |
| 5996 | // template<template<typename> class X> struct A { |
| 5997 | // template<typename T, X<T> N> struct B; |
| 5998 | // template<typename T> struct B<T, 0>; |
| 5999 | // }; |
| 6000 | // template<typename> using X = int; |
| 6001 | // A<X>::B<int, 0> b; |
| 6002 | ParamUseRange = findTemplateParameter( |
| 6003 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
| 6004 | if (ParamUseRange.isValid()) { |
| 6005 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getLocStart(), |
| 6006 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 6007 | << Param->getType() << ParamUseRange; |
| 6008 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
| 6009 | << (IsDefaultArgument ? ParamUseRange : SourceRange()); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6010 | return true; |
| 6011 | } |
| 6012 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6013 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6014 | return false; |
| 6015 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6016 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6017 | /// \brief Check the non-type template arguments of a class template |
| 6018 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 6019 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6020 | /// \param TemplateNameLoc the location of the template name. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6021 | /// \param TemplateParams the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6022 | /// template. |
| 6023 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6024 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6025 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6026 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6027 | /// \returns \c true if there was an error, \c false otherwise. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6028 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6029 | Sema &S, SourceLocation TemplateNameLoc, |
| 6030 | TemplateParameterList *TemplateParams, unsigned NumExplicit, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6031 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6032 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 6033 | |
| 6034 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6035 | NonTypeTemplateParmDecl *Param |
| 6036 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 6037 | if (!Param) |
| 6038 | continue; |
| 6039 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6040 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
| 6041 | S, TemplateNameLoc, Param, &ArgList[I], 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6042 | return true; |
| 6043 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6044 | |
| 6045 | return false; |
| 6046 | } |
| 6047 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6048 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6049 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 6050 | TagUseKind TUK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6051 | SourceLocation KWLoc, |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6052 | SourceLocation ModulePrivateLoc, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6053 | TemplateIdAnnotation &TemplateId, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6054 | AttributeList *Attr, |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame^] | 6055 | MultiTemplateParamsArg |
| 6056 | TemplateParameterLists, |
| 6057 | SkipBodyInfo *SkipBody) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6058 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 6059 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6060 | CXXScopeSpec &SS = TemplateId.SS; |
| 6061 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6062 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 6063 | // store the location of the outermost template keyword in the declaration. |
| 6064 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6065 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 6066 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 6067 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 6068 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6069 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6070 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6071 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6072 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6073 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 6074 | |
| 6075 | if (!ClassTemplate) { |
| 6076 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6077 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6078 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 6079 | return true; |
| 6080 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6081 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6082 | bool isExplicitSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6083 | bool isPartialSpecialization = false; |
| 6084 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6085 | // Check the validity of the template headers that introduce this |
| 6086 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6087 | // FIXME: We probably shouldn't complain about these headers for |
| 6088 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6089 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 6090 | TemplateParameterList *TemplateParams = |
| 6091 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6092 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
| 6093 | TemplateParameterLists, TUK == TUK_Friend, isExplicitSpecialization, |
| 6094 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6095 | if (Invalid) |
| 6096 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6097 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6098 | if (TemplateParams && TemplateParams->size() > 0) { |
| 6099 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6100 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 6101 | if (TUK == TUK_Friend) { |
| 6102 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 6103 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6104 | return true; |
| 6105 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6106 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6107 | // C++ [temp.class.spec]p10: |
| 6108 | // The template parameter list of a specialization shall not |
| 6109 | // contain default template argument values. |
| 6110 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6111 | Decl *Param = TemplateParams->getParam(I); |
| 6112 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 6113 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6114 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6115 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 6116 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6117 | } |
| 6118 | } else if (NonTypeTemplateParmDecl *NTTP |
| 6119 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 6120 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6121 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6122 | diag::err_default_arg_in_partial_spec) |
| 6123 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6124 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6125 | } |
| 6126 | } else { |
| 6127 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6128 | if (TTP->hasDefaultArgument()) { |
| 6129 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6130 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6131 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6132 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6133 | } |
| 6134 | } |
| 6135 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6136 | } else if (TemplateParams) { |
| 6137 | if (TUK == TUK_Friend) |
| 6138 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6139 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6140 | SourceRange(TemplateParams->getTemplateLoc(), |
| 6141 | TemplateParams->getRAngleLoc())) |
| 6142 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6143 | else |
| 6144 | isExplicitSpecialization = true; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6145 | } else { |
| 6146 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6147 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6148 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6149 | // Check that the specialization uses the same tag kind as the |
| 6150 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6151 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6152 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6153 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6154 | Kind, TUK == TUK_Definition, KWLoc, |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6155 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6156 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6157 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6158 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6159 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6160 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6161 | diag::note_previous_use); |
| 6162 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6163 | } |
| 6164 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6165 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6166 | TemplateArgumentListInfo TemplateArgs = |
| 6167 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6168 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6169 | // Check for unexpanded parameter packs in any of the template arguments. |
| 6170 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6171 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6172 | UPPC_PartialSpecialization)) |
| 6173 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6174 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6175 | // Check that the template argument list is well-formed for this |
| 6176 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6177 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6178 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6179 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6180 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6181 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6182 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6183 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6184 | if (isPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6185 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6186 | *this, TemplateNameLoc, ClassTemplate->getTemplateParameters(), |
| 6187 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6188 | return true; |
| 6189 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6190 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6191 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6192 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6193 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6194 | TemplateArgs.size(), |
| 6195 | InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6196 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 6197 | << ClassTemplate->getDeclName(); |
| 6198 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6199 | } |
| 6200 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6201 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6202 | void *InsertPos = nullptr; |
| 6203 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6204 | |
| 6205 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6206 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6207 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6208 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6209 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6210 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6211 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6212 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6213 | // Check whether we can declare a class template specialization in |
| 6214 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6215 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6216 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 6217 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6218 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6219 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6220 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6221 | // The canonical type |
| 6222 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 6223 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6224 | // Build the canonical type that describes the converted template |
| 6225 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6226 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6227 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6228 | Converted.data(), |
| 6229 | Converted.size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6230 | |
| 6231 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6232 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 6233 | // C++ [temp.class.spec]p9b3: |
| 6234 | // |
| 6235 | // -- The argument list of the specialization shall not be identical |
| 6236 | // to the implicit argument list of the primary template. |
| 6237 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6238 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6239 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6240 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 6241 | ClassTemplate->getIdentifier(), |
| 6242 | TemplateNameLoc, |
| 6243 | Attr, |
| 6244 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6245 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 6246 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6247 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6248 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6249 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6250 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6251 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6252 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 6253 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6254 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6255 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6256 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6257 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 6258 | TemplateParams, |
| 6259 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6260 | Converted.data(), |
| 6261 | Converted.size(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6262 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6263 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 6264 | PrevPartial); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6265 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6266 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6267 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6268 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6269 | TemplateParameterLists.data()); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6270 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6271 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6272 | if (!PrevPartial) |
| 6273 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6274 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6275 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6276 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6277 | // template specialization, make a note of that. |
| 6278 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 6279 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6280 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6281 | // Check that all of the template parameters of the class template |
| 6282 | // partial specialization are deducible from the template |
| 6283 | // arguments. If not, this class template partial specialization |
| 6284 | // will never be used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6285 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6286 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6287 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 6288 | DeducibleParams); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6289 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6290 | if (!DeducibleParams.all()) { |
| 6291 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6292 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6293 | << /*class template*/0 << (NumNonDeducible > 1) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6294 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 6295 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 6296 | if (!DeducibleParams[I]) { |
| 6297 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 6298 | if (Param->getDeclName()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6299 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6300 | diag::note_partial_spec_unused_parameter) |
| 6301 | << Param->getDeclName(); |
| 6302 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6303 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6304 | diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 6305 | << "(anonymous)"; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6306 | } |
| 6307 | } |
| 6308 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6309 | } else { |
| 6310 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6311 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6312 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6313 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6314 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6315 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6316 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6317 | Converted.data(), |
| 6318 | Converted.size(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6319 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6320 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6321 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6322 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6323 | TemplateParameterLists.size(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6324 | TemplateParameterLists.data()); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6325 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6326 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6327 | if (!PrevDecl) |
| 6328 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6329 | |
| 6330 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6331 | } |
| 6332 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6333 | // C++ [temp.expl.spec]p6: |
| 6334 | // 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] | 6335 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6336 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6337 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6338 | // use occurs; no diagnostic is required. |
| 6339 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6340 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6341 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6342 | // Is there any previous explicit specialization declaration? |
| 6343 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6344 | Okay = true; |
| 6345 | break; |
| 6346 | } |
| 6347 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6348 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6349 | if (!Okay) { |
| 6350 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 6351 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 6352 | << Context.getTypeDeclType(Specialization) << Range; |
| 6353 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6354 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6355 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6356 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6357 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6358 | return true; |
| 6359 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6360 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6361 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6362 | // If this is not a friend, note that this is an explicit specialization. |
| 6363 | if (TUK != TUK_Friend) |
| 6364 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6365 | |
| 6366 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6367 | if (TUK == TUK_Definition) { |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame^] | 6368 | RecordDecl *Def = Specialization->getDefinition(); |
| 6369 | NamedDecl *Hidden = nullptr; |
| 6370 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 6371 | SkipBody->ShouldSkip = true; |
| 6372 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 6373 | // From here on out, treat this as just a redeclaration. |
| 6374 | TUK = TUK_Declaration; |
| 6375 | } else if (Def) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6376 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6377 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6378 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6379 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 6380 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6381 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6382 | } |
| 6383 | } |
| 6384 | |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 6385 | if (Attr) |
| 6386 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6387 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 6388 | // Add alignment attributes if necessary; these attributes are checked when |
| 6389 | // the ASTContext lays out the structure. |
| 6390 | if (TUK == TUK_Definition) { |
| 6391 | AddAlignmentAttributesForRecord(Specialization); |
| 6392 | AddMsStructLayoutForRecord(Specialization); |
| 6393 | } |
| 6394 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6395 | if (ModulePrivateLoc.isValid()) |
| 6396 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 6397 | << (isPartialSpecialization? 1 : 0) |
| 6398 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 6399 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 6400 | // Build the fully-sugared type for this class template |
| 6401 | // specialization as the user wrote in the specialization |
| 6402 | // itself. This means that we'll pretty-print the type retrieved |
| 6403 | // from the specialization's declaration the way that the user |
| 6404 | // actually wrote the specialization, rather than formatting the |
| 6405 | // name based on the "canonical" representation used to store the |
| 6406 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6407 | TypeSourceInfo *WrittenTy |
| 6408 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6409 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6410 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6411 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6412 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6413 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6414 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 6415 | // C++ [temp.expl.spec]p9: |
| 6416 | // A template explicit specialization is in the scope of the |
| 6417 | // namespace in which the template was defined. |
| 6418 | // |
| 6419 | // We actually implement this paragraph where we set the semantic |
| 6420 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 6421 | // but we also maintain the lexical context where the actual |
| 6422 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6423 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6424 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6425 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6426 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6427 | Specialization->startDefinition(); |
| 6428 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6429 | if (TUK == TUK_Friend) { |
| 6430 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 6431 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6432 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6433 | /*FIXME:*/KWLoc); |
| 6434 | Friend->setAccess(AS_public); |
| 6435 | CurContext->addDecl(Friend); |
| 6436 | } else { |
| 6437 | // Add the specialization into its lexical context, so that it can |
| 6438 | // be seen when iterating through the list of declarations in that |
| 6439 | // context. However, specializations are not found by name lookup. |
| 6440 | CurContext->addDecl(Specialization); |
| 6441 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6442 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6443 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6444 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6445 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6446 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6447 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6448 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 6449 | ActOnDocumentableDecl(NewDecl); |
| 6450 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6451 | } |
| 6452 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6453 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6454 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6455 | Declarator &D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6456 | assert(getCurFunctionDecl() == nullptr && "Function parsing confused"); |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6457 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6458 | |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6459 | if (FTI.hasPrototype) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6460 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6461 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6462 | |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6463 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6464 | |
Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 6465 | D.setFunctionDefinitionKind(FDK_Definition); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6466 | Decl *DP = HandleDeclarator(ParentScope, D, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6467 | TemplateParameterLists); |
Argyrios Kyrtzidis | 6fada2d | 2012-12-14 06:53:58 +0000 | [diff] [blame] | 6468 | return ActOnStartOfFunctionDef(FnBodyScope, DP); |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6469 | } |
| 6470 | |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6471 | /// \brief Strips various properties off an implicit instantiation |
| 6472 | /// that has just been explicitly specialized. |
| 6473 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6474 | D->dropAttr<DLLImportAttr>(); |
| 6475 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6476 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6477 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6478 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6479 | } |
| 6480 | |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6481 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 6482 | // declaration or definition. |
| 6483 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6484 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6485 | // Explicit instantiations following a specialization have no effect and |
| 6486 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 6487 | // until a valid name loc is found. |
| 6488 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6489 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 6490 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6491 | PrevDiagLoc = Prev->getLocation(); |
| 6492 | } |
| 6493 | assert(PrevDiagLoc.isValid() && |
| 6494 | "Explicit instantiation without point of instantiation?"); |
| 6495 | return PrevDiagLoc; |
| 6496 | } |
| 6497 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6498 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6499 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6500 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6501 | /// new specialization/instantiation will have any effect. |
| 6502 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6503 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6504 | /// instantiation. |
| 6505 | /// |
| 6506 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 6507 | /// |
| 6508 | /// \param PrevDecl the previous declaration of the entity. |
| 6509 | /// |
| 6510 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 6511 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6512 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6513 | /// declaration was instantiated (either implicitly or explicitly). |
| 6514 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6515 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6516 | /// specialization or instantiation has no effect and should be ignored. |
| 6517 | /// |
| 6518 | /// \returns true if there was an error that should prevent the introduction of |
| 6519 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6520 | bool |
| 6521 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 6522 | TemplateSpecializationKind NewTSK, |
| 6523 | NamedDecl *PrevDecl, |
| 6524 | TemplateSpecializationKind PrevTSK, |
| 6525 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6526 | bool &HasNoEffect) { |
| 6527 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6528 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6529 | switch (NewTSK) { |
| 6530 | case TSK_Undeclared: |
| 6531 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 6532 | assert( |
| 6533 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 6534 | "previous declaration must be implicit!"); |
| 6535 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6536 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6537 | case TSK_ExplicitSpecialization: |
| 6538 | switch (PrevTSK) { |
| 6539 | case TSK_Undeclared: |
| 6540 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6541 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6542 | // explicitly specialized or has merely been mentioned without any |
| 6543 | // instantiation. |
| 6544 | return false; |
| 6545 | |
| 6546 | case TSK_ImplicitInstantiation: |
| 6547 | if (PrevPointOfInstantiation.isInvalid()) { |
| 6548 | // The declaration itself has not actually been instantiated, so it is |
| 6549 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6550 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6551 | return false; |
| 6552 | } |
| 6553 | // Fall through |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6554 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6555 | case TSK_ExplicitInstantiationDeclaration: |
| 6556 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6557 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 6558 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6559 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6560 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6561 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6562 | // 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] | 6563 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6564 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6565 | // implicit instantiation to take place, in every translation unit in |
| 6566 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6567 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6568 | // Is there any previous explicit specialization declaration? |
| 6569 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 6570 | return false; |
| 6571 | } |
| 6572 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6573 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6574 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6575 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6576 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6577 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6578 | return true; |
| 6579 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6580 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6581 | case TSK_ExplicitInstantiationDeclaration: |
| 6582 | switch (PrevTSK) { |
| 6583 | case TSK_ExplicitInstantiationDeclaration: |
| 6584 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6585 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6586 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6587 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6588 | case TSK_Undeclared: |
| 6589 | case TSK_ImplicitInstantiation: |
| 6590 | // We're explicitly instantiating something that may have already been |
| 6591 | // implicitly instantiated; that's fine. |
| 6592 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6593 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6594 | case TSK_ExplicitSpecialization: |
| 6595 | // C++0x [temp.explicit]p4: |
| 6596 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6597 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6598 | // specialization for that template, the explicit instantiation has no |
| 6599 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6600 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6601 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6602 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6603 | case TSK_ExplicitInstantiationDefinition: |
| 6604 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6605 | // If an entity is the subject of both an explicit instantiation |
| 6606 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6607 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6608 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6609 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6610 | |
| 6611 | // Explicit instantiations following a specialization have no effect and |
| 6612 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 6613 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6614 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 6615 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6616 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6617 | return false; |
| 6618 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6619 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6620 | case TSK_ExplicitInstantiationDefinition: |
| 6621 | switch (PrevTSK) { |
| 6622 | case TSK_Undeclared: |
| 6623 | case TSK_ImplicitInstantiation: |
| 6624 | // We're explicitly instantiating something that may have already been |
| 6625 | // implicitly instantiated; that's fine. |
| 6626 | return false; |
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_ExplicitSpecialization: |
| 6629 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 6630 | // For a given set of template parameters, if an explicit |
| 6631 | // instantiation of a template appears after a declaration of |
| 6632 | // an explicit specialization for that template, the explicit |
| 6633 | // instantiation has no effect. |
| 6634 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6635 | // 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] | 6636 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6637 | // has been explicitly specialized. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6638 | Diag(NewLoc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6639 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 6640 | diag::ext_explicit_instantiation_after_specialization) |
| 6641 | << PrevDecl; |
| 6642 | Diag(PrevDecl->getLocation(), |
| 6643 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6644 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6645 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6646 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6647 | case TSK_ExplicitInstantiationDeclaration: |
| 6648 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6649 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6650 | |
| 6651 | // C++0x [temp.explicit]p4: |
| 6652 | // For a given set of template parameters, if an explicit instantiation |
| 6653 | // of a template appears after a declaration of an explicit |
| 6654 | // specialization for that template, the explicit instantiation has no |
| 6655 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6656 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6657 | // Is there any previous explicit specialization declaration? |
| 6658 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6659 | HasNoEffect = true; |
| 6660 | break; |
| 6661 | } |
| 6662 | } |
| 6663 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6664 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6665 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6666 | case TSK_ExplicitInstantiationDefinition: |
| 6667 | // C++0x [temp.spec]p5: |
| 6668 | // For a given template and a given set of template-arguments, |
| 6669 | // - an explicit instantiation definition shall appear at most once |
| 6670 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6671 | |
| 6672 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 6673 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 6674 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6675 | : diag::err_explicit_instantiation_duplicate) |
| 6676 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6677 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6678 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6679 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6680 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6681 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6682 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6683 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6684 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6685 | } |
| 6686 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6687 | /// \brief Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6688 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6689 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6690 | /// The only possible way to get a dependent function template specialization |
| 6691 | /// is with a friend declaration, like so: |
| 6692 | /// |
| 6693 | /// \code |
| 6694 | /// template \<class T> void foo(T); |
| 6695 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6696 | /// friend void foo<>(T); |
| 6697 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6698 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6699 | /// |
| 6700 | /// There really isn't any useful analysis we can do here, so we |
| 6701 | /// just store the information. |
| 6702 | bool |
| 6703 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 6704 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 6705 | LookupResult &Previous) { |
| 6706 | // Remove anything from Previous that isn't a function template in |
| 6707 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6708 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6709 | LookupResult::Filter F = Previous.makeFilter(); |
| 6710 | while (F.hasNext()) { |
| 6711 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 6712 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6713 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 6714 | D->getDeclContext()->getRedeclContext())) |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6715 | F.erase(); |
| 6716 | } |
| 6717 | F.done(); |
| 6718 | |
| 6719 | // Should this be diagnosed here? |
| 6720 | if (Previous.empty()) return true; |
| 6721 | |
| 6722 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 6723 | ExplicitTemplateArgs); |
| 6724 | return false; |
| 6725 | } |
| 6726 | |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6727 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6728 | /// specialization. |
| 6729 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6730 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6731 | /// explicit function template specialization. On successful completion, |
| 6732 | /// the function declaration \p FD will become a function template |
| 6733 | /// specialization. |
| 6734 | /// |
| 6735 | /// \param FD the function declaration, which will be updated to become a |
| 6736 | /// function template specialization. |
| 6737 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6738 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 6739 | /// if any. Note that this may be valid info even when 0 arguments are |
| 6740 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 6741 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6742 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6743 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6744 | /// this function specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6745 | bool Sema::CheckFunctionTemplateSpecialization( |
| 6746 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 6747 | LookupResult &Previous) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6748 | // The set of function template specializations that could match this |
| 6749 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6750 | UnresolvedSet<8> Candidates; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6751 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6752 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6753 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6754 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6755 | I != E; ++I) { |
| 6756 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 6757 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6758 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6759 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6760 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 6761 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6762 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6763 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6764 | // When matching a constexpr member function template specialization |
| 6765 | // against the primary template, we don't yet know whether the |
| 6766 | // specialization has an implicit 'const' (because we don't know whether |
| 6767 | // it will be a static member function until we know which template it |
| 6768 | // specializes), so adjust it now assuming it specializes this template. |
| 6769 | QualType FT = FD->getType(); |
| 6770 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6771 | CXXMethodDecl *OldMD = |
| 6772 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6773 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6774 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6775 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6776 | EPI.TypeQuals |= Qualifiers::Const; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6777 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6778 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6779 | } |
| 6780 | } |
| 6781 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6782 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6783 | // A trailing template-argument can be left unspecified in the |
| 6784 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6785 | // provided it can be deduced from the function argument type. |
| 6786 | // Perform template argument deduction to determine whether we may be |
| 6787 | // specializing this template. |
| 6788 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6789 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6790 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 6791 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 6792 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
| 6793 | ExplicitTemplateArgs, FT, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6794 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6795 | // that we can provide nifty diagnostics. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6796 | FailedCandidates.addCandidate() |
| 6797 | .set(FunTmpl->getTemplatedDecl(), |
| 6798 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6799 | (void)TDK; |
| 6800 | continue; |
| 6801 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6802 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6803 | // Record this candidate. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6804 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6805 | } |
| 6806 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6807 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 6808 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6809 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 6810 | Candidates.begin(), Candidates.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6811 | FD->getLocation(), |
| 6812 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 6813 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6814 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6815 | PDiag(diag::note_function_template_spec_matched)); |
| 6816 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6817 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6818 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6819 | |
| 6820 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 6821 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 6822 | |
| 6823 | FunctionTemplateSpecializationInfo *SpecInfo |
| 6824 | = Specialization->getTemplateSpecializationInfo(); |
| 6825 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6826 | |
| 6827 | // Note: do not overwrite location info if previous template |
| 6828 | // specialization kind was explicit. |
| 6829 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6830 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6831 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6832 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 6833 | // function can differ from the template declaration with respect to |
| 6834 | // the constexpr specifier. |
| 6835 | Specialization->setConstexpr(FD->isConstexpr()); |
| 6836 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6837 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6838 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6839 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6840 | |
| 6841 | // If this is a friend declaration, then we're not really declaring |
| 6842 | // an explicit specialization. |
| 6843 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6844 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6845 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6846 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6847 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6848 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6849 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6850 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6851 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6852 | |
| 6853 | // C++ [temp.expl.spec]p6: |
| 6854 | // 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] | 6855 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6856 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6857 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6858 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6859 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6860 | if (!isFriend && |
| 6861 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6862 | TSK_ExplicitSpecialization, |
| 6863 | Specialization, |
| 6864 | SpecInfo->getTemplateSpecializationKind(), |
| 6865 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6866 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6867 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6868 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6869 | // Mark the prior declaration as an explicit specialization, so that later |
| 6870 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6871 | if (!isFriend) { |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6872 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6873 | MarkUnusedFileScopedDecl(Specialization); |
| 6874 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6875 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6876 | // Turn the given function declaration into a function template |
| 6877 | // specialization, with the template arguments from the previous |
| 6878 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6879 | // Take copies of (semantic and syntactic) template argument lists. |
| 6880 | const TemplateArgumentList* TemplArgs = new (Context) |
| 6881 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 6882 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6883 | TemplArgs, /*InsertPos=*/nullptr, |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6884 | SpecInfo->getTemplateSpecializationKind(), |
Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 6885 | ExplicitTemplateArgs); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6886 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6887 | // The "previous declaration" for this function template specialization is |
| 6888 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6889 | Previous.clear(); |
| 6890 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6891 | return false; |
| 6892 | } |
| 6893 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6894 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6895 | /// specialization. |
| 6896 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6897 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6898 | /// explicit member function specialization. On successful completion, |
| 6899 | /// the function declaration \p FD will become a member function |
| 6900 | /// specialization. |
| 6901 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6902 | /// \param Member the member declaration, which will be updated to become a |
| 6903 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6904 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6905 | /// \param Previous the set of declarations, one of which may be specialized |
| 6906 | /// by this function specialization; the set will be modified to contain the |
| 6907 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6908 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6909 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6910 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 6911 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6912 | // Try to find the member we are instantiating. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6913 | NamedDecl *Instantiation = nullptr; |
| 6914 | NamedDecl *InstantiatedFrom = nullptr; |
| 6915 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6916 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6917 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6918 | // Nowhere to look anyway. |
| 6919 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6920 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6921 | I != E; ++I) { |
| 6922 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 6923 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 6924 | QualType Adjusted = Function->getType(); |
| 6925 | if (!hasExplicitCallingConv(Adjusted)) |
| 6926 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
| 6927 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6928 | Instantiation = Method; |
| 6929 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6930 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6931 | break; |
| 6932 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6933 | } |
| 6934 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6935 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6936 | VarDecl *PrevVar; |
| 6937 | if (Previous.isSingleResult() && |
| 6938 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6939 | if (PrevVar->isStaticDataMember()) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6940 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6941 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6942 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6943 | } |
| 6944 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6945 | CXXRecordDecl *PrevRecord; |
| 6946 | if (Previous.isSingleResult() && |
| 6947 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 6948 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6949 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6950 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6951 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6952 | } else if (isa<EnumDecl>(Member)) { |
| 6953 | EnumDecl *PrevEnum; |
| 6954 | if (Previous.isSingleResult() && |
| 6955 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
| 6956 | Instantiation = PrevEnum; |
| 6957 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 6958 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 6959 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6960 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6961 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6962 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6963 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6964 | // specializations are always out-of-line, the caller will complain about |
| 6965 | // this mismatch later. |
| 6966 | return false; |
| 6967 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 6968 | |
| 6969 | // If this is a friend, just bail out here before we start turning |
| 6970 | // things into explicit specializations. |
| 6971 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 6972 | // Preserve instantiation information. |
| 6973 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 6974 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 6975 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 6976 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 6977 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 6978 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 6979 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 6980 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 6981 | } |
| 6982 | |
| 6983 | Previous.clear(); |
| 6984 | Previous.addDecl(Instantiation); |
| 6985 | return false; |
| 6986 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6987 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6988 | // Make sure that this is a specialization of a member. |
| 6989 | if (!InstantiatedFrom) { |
| 6990 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 6991 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6992 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 6993 | return true; |
| 6994 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6995 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6996 | // C++ [temp.expl.spec]p6: |
| 6997 | // 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] | 6998 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6999 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7000 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7001 | // use occurs; no diagnostic is required. |
| 7002 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7003 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7004 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7005 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 7006 | TSK_ExplicitSpecialization, |
| 7007 | Instantiation, |
| 7008 | MSInfo->getTemplateSpecializationKind(), |
| 7009 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7010 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7011 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7012 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7013 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7014 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7015 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7016 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7017 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7018 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 7019 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7020 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7021 | // the original declaration to note that it is an explicit specialization |
| 7022 | // (if it was previously an implicit instantiation). This latter step |
| 7023 | // makes bookkeeping easier. |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7024 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7025 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 7026 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 7027 | TSK_ImplicitInstantiation) { |
| 7028 | InstantiationFunction->setTemplateSpecializationKind( |
| 7029 | TSK_ExplicitSpecialization); |
| 7030 | InstantiationFunction->setLocation(Member->getLocation()); |
| 7031 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7032 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7033 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 7034 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7035 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7036 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7037 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7038 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 7039 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 7040 | TSK_ImplicitInstantiation) { |
| 7041 | InstantiationVar->setTemplateSpecializationKind( |
| 7042 | TSK_ExplicitSpecialization); |
| 7043 | InstantiationVar->setLocation(Member->getLocation()); |
| 7044 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7045 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7046 | cast<VarDecl>(Member)->setInstantiationOfStaticDataMember( |
| 7047 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7048 | MarkUnusedFileScopedDecl(InstantiationVar); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7049 | } else if (isa<CXXRecordDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7050 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 7051 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 7052 | TSK_ImplicitInstantiation) { |
| 7053 | InstantiationClass->setTemplateSpecializationKind( |
| 7054 | TSK_ExplicitSpecialization); |
| 7055 | InstantiationClass->setLocation(Member->getLocation()); |
| 7056 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7057 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7058 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7059 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7060 | TSK_ExplicitSpecialization); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7061 | } else { |
| 7062 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); |
| 7063 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); |
| 7064 | if (InstantiationEnum->getTemplateSpecializationKind() == |
| 7065 | TSK_ImplicitInstantiation) { |
| 7066 | InstantiationEnum->setTemplateSpecializationKind( |
| 7067 | TSK_ExplicitSpecialization); |
| 7068 | InstantiationEnum->setLocation(Member->getLocation()); |
| 7069 | } |
| 7070 | |
| 7071 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( |
| 7072 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7073 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7074 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7075 | // Save the caller the trouble of having to figure out which declaration |
| 7076 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7077 | Previous.clear(); |
| 7078 | Previous.addDecl(Instantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7079 | return false; |
| 7080 | } |
| 7081 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7082 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7083 | /// |
| 7084 | /// \returns true if a serious error occurs, false otherwise. |
| 7085 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7086 | SourceLocation InstLoc, |
| 7087 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7088 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 7089 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7090 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7091 | if (CurContext->isRecord()) { |
| 7092 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 7093 | << D; |
| 7094 | return true; |
| 7095 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7096 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7097 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7098 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7099 | // template. If the name declared in the explicit instantiation is an |
| 7100 | // unqualified name, the explicit instantiation shall appear in the |
| 7101 | // namespace where its template is declared or, if that namespace is inline |
| 7102 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7103 | // |
| 7104 | // 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] | 7105 | if (WasQualifiedName) { |
| 7106 | if (CurContext->Encloses(OrigContext)) |
| 7107 | return false; |
| 7108 | } else { |
| 7109 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 7110 | return false; |
| 7111 | } |
| 7112 | |
| 7113 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 7114 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7115 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7116 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7117 | diag::err_explicit_instantiation_out_of_scope : |
| 7118 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7119 | << D << NS; |
| 7120 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7121 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7122 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7123 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 7124 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 7125 | << D << NS; |
| 7126 | } else |
| 7127 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7128 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7129 | diag::err_explicit_instantiation_must_be_global : |
| 7130 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 7131 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7132 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7133 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7134 | } |
| 7135 | |
| 7136 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 7137 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 7138 | if (!SS.isSet()) |
| 7139 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7140 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7141 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7142 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7143 | // or a static data member of a class template specialization, the name of |
| 7144 | // the class template specialization in the qualified-id for the member |
| 7145 | // name shall be a simple-template-id. |
| 7146 | // |
| 7147 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7148 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 7149 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7150 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7151 | if (isa<TemplateSpecializationType>(T)) |
| 7152 | return true; |
| 7153 | |
| 7154 | return false; |
| 7155 | } |
| 7156 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7157 | // Explicit instantiation of a class template specialization |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7158 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7159 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7160 | SourceLocation ExternLoc, |
| 7161 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7162 | unsigned TagSpec, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7163 | SourceLocation KWLoc, |
| 7164 | const CXXScopeSpec &SS, |
| 7165 | TemplateTy TemplateD, |
| 7166 | SourceLocation TemplateNameLoc, |
| 7167 | SourceLocation LAngleLoc, |
| 7168 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7169 | SourceLocation RAngleLoc, |
| 7170 | AttributeList *Attr) { |
| 7171 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 7172 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7173 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7174 | // Check that the specialization uses the same tag kind as the |
| 7175 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7176 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7177 | assert(Kind != TTK_Enum && |
| 7178 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7179 | |
| 7180 | if (isa<TypeAliasTemplateDecl>(TD)) { |
| 7181 | Diag(KWLoc, diag::err_tag_reference_non_tag) << Kind; |
| 7182 | Diag(TD->getTemplatedDecl()->getLocation(), |
| 7183 | diag::note_previous_use); |
| 7184 | return true; |
| 7185 | } |
| 7186 | |
| 7187 | ClassTemplateDecl *ClassTemplate = cast<ClassTemplateDecl>(TD); |
| 7188 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7189 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7190 | Kind, /*isDefinition*/false, KWLoc, |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7191 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7192 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7193 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7194 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7195 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7196 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7197 | diag::note_previous_use); |
| 7198 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7199 | } |
| 7200 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7201 | // C++0x [temp.explicit]p2: |
| 7202 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7203 | // definition and an explicit instantiation declaration. An explicit |
| 7204 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 7205 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 7206 | ? TSK_ExplicitInstantiationDefinition |
| 7207 | : TSK_ExplicitInstantiationDeclaration; |
| 7208 | |
| 7209 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 7210 | // Check for dllexport class template instantiation declarations. |
| 7211 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7212 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7213 | Diag(ExternLoc, |
| 7214 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7215 | Diag(A->getLoc(), diag::note_attribute); |
| 7216 | break; |
| 7217 | } |
| 7218 | } |
| 7219 | |
| 7220 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 7221 | Diag(ExternLoc, |
| 7222 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7223 | Diag(A->getLocation(), diag::note_attribute); |
| 7224 | } |
| 7225 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7226 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7227 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7228 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 7229 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7230 | |
| 7231 | // Check that the template argument list is well-formed for this |
| 7232 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7233 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7234 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7235 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7236 | return true; |
| 7237 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7238 | // Find the class template specialization declaration that |
| 7239 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7240 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7241 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7242 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7243 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7244 | TemplateSpecializationKind PrevDecl_TSK |
| 7245 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 7246 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7247 | // C++0x [temp.explicit]p2: |
| 7248 | // [...] An explicit instantiation shall appear in an enclosing |
| 7249 | // namespace of its template. [...] |
| 7250 | // |
| 7251 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7252 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 7253 | SS.isSet())) |
| 7254 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7255 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7256 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7257 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7258 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7259 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7260 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7261 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7262 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7263 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7264 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7265 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7266 | // Even though HasNoEffect == true means that this explicit instantiation |
| 7267 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 7268 | |
| 7269 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 7270 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7271 | // Since the only prior class template specialization with these |
| 7272 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7273 | // declaration node as our own, updating the source location |
| 7274 | // for the template name to reflect our new declaration. |
| 7275 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7276 | Specialization = PrevDecl; |
| 7277 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7278 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7279 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7280 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7281 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7282 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7283 | // Create a new class template specialization declaration node for |
| 7284 | // this explicit specialization. |
| 7285 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7286 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7287 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7288 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7289 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7290 | Converted.data(), |
| 7291 | Converted.size(), |
| 7292 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7293 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7294 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7295 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7296 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7297 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7298 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7299 | } |
| 7300 | |
| 7301 | // Build the fully-sugared type for this explicit instantiation as |
| 7302 | // the user wrote in the explicit instantiation itself. This means |
| 7303 | // that we'll pretty-print the type retrieved from the |
| 7304 | // specialization's declaration the way that the user actually wrote |
| 7305 | // the explicit instantiation, rather than formatting the name based |
| 7306 | // on the "canonical" representation used to store the template |
| 7307 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7308 | TypeSourceInfo *WrittenTy |
| 7309 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7310 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7311 | Context.getTypeDeclType(Specialization)); |
| 7312 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7313 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7314 | // Set source locations for keywords. |
| 7315 | Specialization->setExternLoc(ExternLoc); |
| 7316 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | 40bcfd7 | 2013-04-22 23:23:42 +0000 | [diff] [blame] | 7317 | Specialization->setRBraceLoc(SourceLocation()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7318 | |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 7319 | if (Attr) |
| 7320 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 7321 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7322 | // Add the explicit instantiation into its lexical context. However, |
| 7323 | // since explicit instantiations are never found by name lookup, we |
| 7324 | // just put it into the declaration context directly. |
| 7325 | Specialization->setLexicalDeclContext(CurContext); |
| 7326 | CurContext->addDecl(Specialization); |
| 7327 | |
| 7328 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 7329 | if (HasNoEffect) { |
| 7330 | // Set the template specialization kind. |
| 7331 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7332 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 7333 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7334 | |
| 7335 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7336 | // A definition of a class template or class member template |
| 7337 | // shall be in scope at the point of the explicit instantiation of |
| 7338 | // the class template or class member template. |
| 7339 | // |
| 7340 | // This check comes when we actually try to perform the |
| 7341 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7342 | ClassTemplateSpecializationDecl *Def |
| 7343 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7344 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7345 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7346 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7347 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7348 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7349 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 7350 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7351 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7352 | // Instantiate the members of this class template specialization. |
| 7353 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7354 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7355 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7356 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 7357 | |
| 7358 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 7359 | // TSK_ExplicitInstantiationDefinition |
| 7360 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 7361 | TSK == TSK_ExplicitInstantiationDefinition) |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7362 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7363 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7364 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7365 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7366 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7367 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7368 | // Set the template specialization kind. |
| 7369 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7370 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7371 | } |
| 7372 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7373 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7374 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7375 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7376 | SourceLocation ExternLoc, |
| 7377 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7378 | unsigned TagSpec, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7379 | SourceLocation KWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7380 | CXXScopeSpec &SS, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7381 | IdentifierInfo *Name, |
| 7382 | SourceLocation NameLoc, |
| 7383 | AttributeList *Attr) { |
| 7384 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 7385 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7386 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7387 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7388 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7389 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7390 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 7391 | SourceLocation(), false, TypeResult(), |
| 7392 | /*IsTypeSpecifier*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7393 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 7394 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7395 | if (!TagD) |
| 7396 | return true; |
| 7397 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7398 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7399 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7400 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 7401 | if (Tag->isInvalidDecl()) |
| 7402 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7403 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7404 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 7405 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 7406 | if (!Pattern) { |
| 7407 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 7408 | << Context.getTypeDeclType(Record); |
| 7409 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 7410 | return true; |
| 7411 | } |
| 7412 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7413 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7414 | // If the explicit instantiation is for a class or member class, the |
| 7415 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7416 | // simple-template-id. |
| 7417 | // |
| 7418 | // C++98 has the same restriction, just worded differently. |
| 7419 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7420 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7421 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7422 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7423 | // C++0x [temp.explicit]p2: |
| 7424 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7425 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7426 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 7427 | TemplateSpecializationKind TSK |
| 7428 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7429 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7430 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7431 | // C++0x [temp.explicit]p2: |
| 7432 | // [...] An explicit instantiation shall appear in an enclosing |
| 7433 | // namespace of its template. [...] |
| 7434 | // |
| 7435 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7436 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7437 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7438 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7439 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7440 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7441 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7442 | PrevDecl = Record; |
| 7443 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7444 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7445 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7446 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7447 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7448 | PrevDecl, |
| 7449 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7450 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7451 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7452 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7453 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7454 | return TagD; |
| 7455 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7456 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7457 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7458 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7459 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7460 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7461 | // 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] | 7462 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7463 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7464 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7465 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 7466 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 7467 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7468 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 7469 | << Pattern; |
| 7470 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7471 | } else { |
| 7472 | if (InstantiateClass(NameLoc, Record, Def, |
| 7473 | getTemplateInstantiationArgs(Record), |
| 7474 | TSK)) |
| 7475 | return true; |
| 7476 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7477 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7478 | if (!RecordDef) |
| 7479 | return true; |
| 7480 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7481 | } |
| 7482 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7483 | // Instantiate all of the members of the class. |
| 7484 | InstantiateClassMembers(NameLoc, RecordDef, |
| 7485 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7486 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7487 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7488 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 7489 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7490 | // FIXME: We don't have any representation for explicit instantiations of |
| 7491 | // member classes. Such a representation is not needed for compilation, but it |
| 7492 | // should be available for clients that want to see all of the declarations in |
| 7493 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7494 | return TagD; |
| 7495 | } |
| 7496 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7497 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 7498 | SourceLocation ExternLoc, |
| 7499 | SourceLocation TemplateLoc, |
| 7500 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7501 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7502 | // TODO: check if/when DNInfo should replace Name. |
| 7503 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 7504 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7505 | if (!Name) { |
| 7506 | if (!D.isInvalidType()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7507 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7508 | diag::err_explicit_instantiation_requires_name) |
| 7509 | << D.getDeclSpec().getSourceRange() |
| 7510 | << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7511 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7512 | return true; |
| 7513 | } |
| 7514 | |
| 7515 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 7516 | // we find one that is. |
| 7517 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 7518 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 7519 | S = S->getParent(); |
| 7520 | |
| 7521 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7522 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 7523 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7524 | if (R.isNull()) |
| 7525 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7526 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7527 | // C++ [dcl.stc]p1: |
| 7528 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 7529 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7530 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7531 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 7532 | << Name; |
| 7533 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7534 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 7535 | != DeclSpec::SCS_unspecified) { |
| 7536 | // Complain about then remove the storage class specifier. |
| 7537 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 7538 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 7539 | |
| 7540 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7541 | } |
| 7542 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 7543 | // C++0x [temp.explicit]p1: |
| 7544 | // [...] An explicit instantiation of a function template shall not use the |
| 7545 | // inline or constexpr specifiers. |
| 7546 | // Presumably, this also applies to member functions of class templates as |
| 7547 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7548 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7549 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7550 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7551 | diag::err_explicit_instantiation_inline : |
| 7552 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7553 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7554 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7555 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 7556 | // not already specified. |
| 7557 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 7558 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7559 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7560 | // C++0x [temp.explicit]p2: |
| 7561 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7562 | // definition and an explicit instantiation declaration. An explicit |
| 7563 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7564 | TemplateSpecializationKind TSK |
| 7565 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7566 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7567 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7568 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7569 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7570 | |
| 7571 | if (!R->isFunctionType()) { |
| 7572 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7573 | // A [...] static data member of a class template can be explicitly |
| 7574 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7575 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7576 | // C++1y [temp.explicit]p1: |
| 7577 | // A [...] variable [...] template specialization can be explicitly |
| 7578 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7579 | if (Previous.isAmbiguous()) |
| 7580 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7581 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 7582 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7583 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7584 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7585 | if (!PrevTemplate) { |
| 7586 | if (!Prev || !Prev->isStaticDataMember()) { |
| 7587 | // We expect to see a data data member here. |
| 7588 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 7589 | << Name; |
| 7590 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7591 | P != PEnd; ++P) |
| 7592 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 7593 | return true; |
| 7594 | } |
| 7595 | |
| 7596 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 7597 | // FIXME: Check for explicit specialization? |
| 7598 | Diag(D.getIdentifierLoc(), |
| 7599 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 7600 | << Prev; |
| 7601 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 7602 | // FIXME: Can we provide a note showing where this was declared? |
| 7603 | return true; |
| 7604 | } |
| 7605 | } else { |
| 7606 | // Explicitly instantiate a variable template. |
| 7607 | |
| 7608 | // C++1y [dcl.spec.auto]p6: |
| 7609 | // ... A program that uses auto or decltype(auto) in a context not |
| 7610 | // explicitly allowed in this section is ill-formed. |
| 7611 | // |
| 7612 | // This includes auto-typed variable template instantiations. |
| 7613 | if (R->isUndeducedType()) { |
| 7614 | Diag(T->getTypeLoc().getLocStart(), |
| 7615 | diag::err_auto_not_allowed_var_inst); |
| 7616 | return true; |
| 7617 | } |
| 7618 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7619 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 7620 | // C++1y [temp.explicit]p3: |
| 7621 | // If the explicit instantiation is for a variable, the unqualified-id |
| 7622 | // in the declaration shall be a template-id. |
| 7623 | Diag(D.getIdentifierLoc(), |
| 7624 | diag::err_explicit_instantiation_without_template_id) |
| 7625 | << PrevTemplate; |
| 7626 | Diag(PrevTemplate->getLocation(), |
| 7627 | diag::note_explicit_instantiation_here); |
| 7628 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7629 | } |
| 7630 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7631 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7632 | TemplateArgumentListInfo TemplateArgs = |
| 7633 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7634 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7635 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 7636 | D.getIdentifierLoc(), TemplateArgs); |
| 7637 | if (Res.isInvalid()) |
| 7638 | return true; |
| 7639 | |
| 7640 | // Ignore access control bits, we don't need them for redeclaration |
| 7641 | // checking. |
| 7642 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7643 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7644 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7645 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7646 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7647 | // or a static data member of a class template specialization, the name of |
| 7648 | // the class template specialization in the qualified-id for the member |
| 7649 | // name shall be a simple-template-id. |
| 7650 | // |
| 7651 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7652 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 7653 | // This does not apply to variable template specializations, where the |
| 7654 | // template-id is in the unqualified-id instead. |
| 7655 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7656 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7657 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7658 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7659 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7660 | // Check the scope of this explicit instantiation. |
| 7661 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7662 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7663 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 7664 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 7665 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7666 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7667 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7668 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7669 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7670 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7671 | if (!HasNoEffect) { |
| 7672 | // Instantiate static data member or variable template. |
| 7673 | |
| 7674 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 7675 | if (PrevTemplate) { |
| 7676 | // Merge attributes. |
| 7677 | if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList()) |
| 7678 | ProcessDeclAttributeList(S, Prev, Attr); |
| 7679 | } |
| 7680 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7681 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 7682 | } |
| 7683 | |
| 7684 | // Check the new variable specialization against the parsed input. |
| 7685 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
| 7686 | Diag(T->getTypeLoc().getLocStart(), |
| 7687 | diag::err_invalid_var_template_spec_type) |
| 7688 | << 0 << PrevTemplate << R << Prev->getType(); |
| 7689 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 7690 | << 2 << PrevTemplate->getDeclName(); |
| 7691 | return true; |
| 7692 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7693 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7694 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7695 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7696 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7697 | |
| 7698 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 7699 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7700 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7701 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7702 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7703 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7704 | HasExplicitTemplateArgs = true; |
| 7705 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7706 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7707 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7708 | // A [...] function [...] can be explicitly instantiated from its template. |
| 7709 | // A member function [...] of a class template can be explicitly |
| 7710 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7711 | // template. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7712 | UnresolvedSet<8> Matches; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7713 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7714 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7715 | P != PEnd; ++P) { |
| 7716 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7717 | if (!HasExplicitTemplateArgs) { |
| 7718 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 7719 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType()); |
| 7720 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7721 | Matches.clear(); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7722 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7723 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7724 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 7725 | break; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7726 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7727 | } |
| 7728 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7729 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7730 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 7731 | if (!FunTmpl) |
| 7732 | continue; |
| 7733 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7734 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7735 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7736 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7737 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7738 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 7739 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7740 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7741 | // Keep track of almost-matches. |
| 7742 | FailedCandidates.addCandidate() |
| 7743 | .set(FunTmpl->getTemplatedDecl(), |
| 7744 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7745 | (void)TDK; |
| 7746 | continue; |
| 7747 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7748 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7749 | Matches.addDecl(Specialization, P.getAccess()); |
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 | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7752 | // Find the most specialized function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7753 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 7754 | Matches.begin(), Matches.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7755 | D.getIdentifierLoc(), |
| 7756 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 7757 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 7758 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7759 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7760 | if (Result == Matches.end()) |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7761 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7762 | |
| 7763 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 7764 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7765 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 7766 | // C++11 [except.spec]p4 |
| 7767 | // In an explicit instantiation an exception-specification may be specified, |
| 7768 | // but is not required. |
| 7769 | // If an exception-specification is specified in an explicit instantiation |
| 7770 | // directive, it shall be compatible with the exception-specifications of |
| 7771 | // other declarations of that function. |
| 7772 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 7773 | if (FPT->hasExceptionSpec()) { |
| 7774 | unsigned DiagID = |
| 7775 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 7776 | if (getLangOpts().MicrosoftExt) |
| 7777 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 7778 | bool Result = CheckEquivalentExceptionSpec( |
| 7779 | PDiag(DiagID) << Specialization->getType(), |
| 7780 | PDiag(diag::note_explicit_instantiation_here), |
| 7781 | Specialization->getType()->getAs<FunctionProtoType>(), |
| 7782 | Specialization->getLocation(), FPT, D.getLocStart()); |
| 7783 | // In Microsoft mode, mismatching exception specifications just cause a |
| 7784 | // warning. |
| 7785 | if (!getLangOpts().MicrosoftExt && Result) |
| 7786 | return true; |
| 7787 | } |
| 7788 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7789 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7790 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7791 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 7792 | << Specialization |
| 7793 | << (Specialization->getTemplateSpecializationKind() == |
| 7794 | TSK_ExplicitSpecialization); |
| 7795 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 7796 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7797 | } |
| 7798 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7799 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7800 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 7801 | PrevDecl = Specialization; |
| 7802 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7803 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7804 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7805 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7806 | PrevDecl, |
| 7807 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7808 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7809 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7810 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7811 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7812 | // FIXME: We may still want to build some representation of this |
| 7813 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7814 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7815 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7816 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 7817 | |
| 7818 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 2aa7acf | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 7819 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 7820 | if (Attr) |
| 7821 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7822 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7823 | if (Specialization->isDefined()) { |
| 7824 | // Let the ASTConsumer know that this function has been explicitly |
| 7825 | // instantiated now, and its linkage might have changed. |
| 7826 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 7827 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 7828 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7829 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7830 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7831 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7832 | // or a static data member of a class template specialization, the name of |
| 7833 | // the class template specialization in the qualified-id for the member |
| 7834 | // name shall be a simple-template-id. |
| 7835 | // |
| 7836 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7837 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7838 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7839 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7840 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7841 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7842 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7843 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7844 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7845 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7846 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7847 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7848 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7849 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7850 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7851 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7852 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7853 | } |
| 7854 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7855 | TypeResult |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7856 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 7857 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 7858 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 7859 | // This has to hold, because SS is expected to be defined. |
| 7860 | assert(Name && "Expected a name in a dependent tag"); |
| 7861 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7862 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7863 | if (!NNS) |
| 7864 | return true; |
| 7865 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7866 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 7867 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 7868 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 7869 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7870 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 7871 | return true; |
| 7872 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7873 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7874 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7875 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7876 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 7877 | |
| 7878 | // Create type-source location information for this type. |
| 7879 | TypeLocBuilder TLB; |
| 7880 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7881 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7882 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 7883 | TL.setNameLoc(NameLoc); |
| 7884 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7885 | } |
| 7886 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7887 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7888 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 7889 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 7890 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7891 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7892 | return true; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7893 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7894 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 7895 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7896 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7897 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 7898 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7899 | << FixItHint::CreateRemoval(TypenameLoc); |
| 7900 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 7901 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 7902 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 7903 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 7904 | if (T.isNull()) |
| 7905 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7906 | |
| 7907 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 7908 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7909 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7910 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 7911 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 7912 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7913 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7914 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7915 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 7916 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7917 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7918 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7919 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7920 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7921 | } |
| 7922 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7923 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7924 | Sema::ActOnTypenameType(Scope *S, |
| 7925 | SourceLocation TypenameLoc, |
| 7926 | const CXXScopeSpec &SS, |
| 7927 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7928 | TemplateTy TemplateIn, |
| 7929 | SourceLocation TemplateNameLoc, |
| 7930 | SourceLocation LAngleLoc, |
| 7931 | ASTTemplateArgsPtr TemplateArgsIn, |
| 7932 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7933 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 7934 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7935 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7936 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 7937 | diag::ext_typename_outside_of_template) |
| 7938 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7939 | |
| 7940 | // Translate the parser's template argument list in our AST format. |
| 7941 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 7942 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 7943 | |
| 7944 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7945 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 7946 | // Construct a dependent template specialization type. |
| 7947 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7948 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7949 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 7950 | DTN->getQualifier(), |
| 7951 | DTN->getIdentifier(), |
| 7952 | TemplateArgs); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7953 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7954 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 7955 | TypeLocBuilder Builder; |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7956 | DependentTemplateSpecializationTypeLoc SpecTL |
| 7957 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7958 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 7959 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 7960 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7961 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7962 | SpecTL.setLAngleLoc(LAngleLoc); |
| 7963 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7964 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 7965 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7966 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 7967 | } |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7968 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7969 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 7970 | if (T.isNull()) |
| 7971 | return true; |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7972 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7973 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7974 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7975 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7976 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7977 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 7978 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7979 | SpecTL.setLAngleLoc(LAngleLoc); |
| 7980 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7981 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 7982 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 7983 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7984 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 7985 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7986 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 7987 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 7988 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7989 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 7990 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 7991 | } |
| 7992 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7993 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7994 | /// Determine whether this failed name lookup should be treated as being |
| 7995 | /// disabled by a usage of std::enable_if. |
| 7996 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
| 7997 | SourceRange &CondRange) { |
| 7998 | // We must be looking for a ::type... |
| 7999 | if (!II.isStr("type")) |
| 8000 | return false; |
| 8001 | |
| 8002 | // ... within an explicitly-written template specialization... |
| 8003 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 8004 | return false; |
| 8005 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8006 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 8007 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 8008 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8009 | return false; |
| 8010 | const TemplateSpecializationType *EnableIfTST = |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8011 | cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr()); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8012 | |
| 8013 | // ... which names a complete class template declaration... |
| 8014 | const TemplateDecl *EnableIfDecl = |
| 8015 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 8016 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 8017 | return false; |
| 8018 | |
| 8019 | // ... called "enable_if". |
| 8020 | const IdentifierInfo *EnableIfII = |
| 8021 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 8022 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 8023 | return false; |
| 8024 | |
| 8025 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8026 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8027 | return true; |
| 8028 | } |
| 8029 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8030 | /// \brief Build the type that describes a C++ typename specifier, |
| 8031 | /// e.g., "typename T::type". |
| 8032 | QualType |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8033 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 8034 | SourceLocation KeywordLoc, |
| 8035 | NestedNameSpecifierLoc QualifierLoc, |
| 8036 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8037 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8038 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8039 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8040 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8041 | DeclContext *Ctx = computeDeclContext(SS); |
| 8042 | if (!Ctx) { |
| 8043 | // If the nested-name-specifier is dependent and couldn't be |
| 8044 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8045 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 8046 | return Context.getDependentNameType(Keyword, |
| 8047 | QualifierLoc.getNestedNameSpecifier(), |
| 8048 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 8049 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8050 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8051 | // If the nested-name-specifier refers to the current instantiation, |
| 8052 | // the "typename" keyword itself is superfluous. In C++03, the |
| 8053 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 8054 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 8055 | // 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] | 8056 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8057 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 8058 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8059 | |
| 8060 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8061 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 8062 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8063 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8064 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8065 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8066 | case LookupResult::NotFound: { |
| 8067 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 8068 | // a more specific diagnostic. |
| 8069 | SourceRange CondRange; |
| 8070 | if (isEnableIf(QualifierLoc, II, CondRange)) { |
| 8071 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
| 8072 | << Ctx << CondRange; |
| 8073 | return QualType(); |
| 8074 | } |
| 8075 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 8076 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8077 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8078 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8079 | |
| 8080 | case LookupResult::FoundUnresolvedValue: { |
| 8081 | // We found a using declaration that is a value. Most likely, the using |
| 8082 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8083 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8084 | IILoc); |
| 8085 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 8086 | << Name << Ctx << FullRange; |
| 8087 | if (UnresolvedUsingValueDecl *Using |
| 8088 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8089 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8090 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 8091 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 8092 | } |
| 8093 | } |
| 8094 | // Fall through to create a dependent typename type, from which we can recover |
| 8095 | // better. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8096 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 8097 | case LookupResult::NotFoundInCurrentInstantiation: |
| 8098 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8099 | return Context.getDependentNameType(Keyword, |
| 8100 | QualifierLoc.getNestedNameSpecifier(), |
| 8101 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8102 | |
| 8103 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8104 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8105 | // We found a type. Build an ElaboratedType, since the |
| 8106 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 8107 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8108 | return Context.getElaboratedType(ETK_Typename, |
| 8109 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8110 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8111 | } |
| 8112 | |
| 8113 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 8114 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8115 | break; |
| 8116 | |
| 8117 | case LookupResult::FoundOverloaded: |
| 8118 | DiagID = diag::err_typename_nested_not_type; |
| 8119 | Referenced = *Result.begin(); |
| 8120 | break; |
| 8121 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 8122 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8123 | return QualType(); |
| 8124 | } |
| 8125 | |
| 8126 | // If we get here, it's because name lookup did not find a |
| 8127 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8128 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8129 | IILoc); |
| 8130 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8131 | if (Referenced) |
| 8132 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 8133 | << Name; |
| 8134 | return QualType(); |
| 8135 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8136 | |
| 8137 | namespace { |
| 8138 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 8139 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8140 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8141 | SourceLocation Loc; |
| 8142 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8143 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8144 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 8145 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8146 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8147 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8148 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8149 | DeclarationName Entity) |
| 8150 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8151 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8152 | |
| 8153 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8154 | /// transformed. |
| 8155 | /// |
| 8156 | /// For the purposes of type reconstruction, a type has already been |
| 8157 | /// transformed if it is NULL or if it is not dependent. |
| 8158 | bool AlreadyTransformed(QualType T) { |
| 8159 | return T.isNull() || !T->isDependentType(); |
| 8160 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8161 | |
| 8162 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8163 | /// rebuilt. |
| 8164 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8165 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8166 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 8167 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8168 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 8169 | /// \brief Sets the "base" location and entity when that |
| 8170 | /// information is known based on another transformation. |
| 8171 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 8172 | this->Loc = Loc; |
| 8173 | this->Entity = Entity; |
| 8174 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 8175 | |
| 8176 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 8177 | // Lambdas never need to be transformed. |
| 8178 | return E; |
| 8179 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8180 | }; |
| 8181 | } |
| 8182 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8183 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 8184 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8185 | /// 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] | 8186 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8187 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8188 | /// partial specialization thereof). This routine will rebuild that type now |
| 8189 | /// that we have entered the declarator's scope, which may produce different |
| 8190 | /// canonical types, e.g., |
| 8191 | /// |
| 8192 | /// \code |
| 8193 | /// template<typename T> |
| 8194 | /// struct X { |
| 8195 | /// typedef T* pointer; |
| 8196 | /// pointer data(); |
| 8197 | /// }; |
| 8198 | /// |
| 8199 | /// template<typename T> |
| 8200 | /// typename X<T>::pointer X<T>::data() { ... } |
| 8201 | /// \endcode |
| 8202 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 8203 | /// 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] | 8204 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 8205 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8206 | /// 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] | 8207 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 8208 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8209 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 8210 | SourceLocation Loc, |
| 8211 | DeclarationName Name) { |
| 8212 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8213 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8214 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8215 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 8216 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 8217 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8218 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8219 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8220 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 8221 | DeclarationName()); |
| 8222 | return Rebuilder.TransformExpr(E); |
| 8223 | } |
| 8224 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8225 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8226 | if (SS.isInvalid()) |
| 8227 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8228 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8229 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8230 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 8231 | DeclarationName()); |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8232 | NestedNameSpecifierLoc Rebuilt |
| 8233 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 8234 | if (!Rebuilt) |
| 8235 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8236 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8237 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8238 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8239 | } |
| 8240 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8241 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 8242 | /// instantiation. |
| 8243 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 8244 | TemplateParameterList *Params) { |
| 8245 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 8246 | Decl *Param = Params->getParam(I); |
| 8247 | |
| 8248 | // There is nothing to rebuild in a type parameter. |
| 8249 | if (isa<TemplateTypeParmDecl>(Param)) |
| 8250 | continue; |
| 8251 | |
| 8252 | // Rebuild the template parameter list of a template template parameter. |
| 8253 | if (TemplateTemplateParmDecl *TTP |
| 8254 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 8255 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 8256 | TTP->getTemplateParameters())) |
| 8257 | return true; |
| 8258 | |
| 8259 | continue; |
| 8260 | } |
| 8261 | |
| 8262 | // Rebuild the type of a non-type template parameter. |
| 8263 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 8264 | TypeSourceInfo *NewTSI |
| 8265 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 8266 | NTTP->getLocation(), |
| 8267 | NTTP->getDeclName()); |
| 8268 | if (!NewTSI) |
| 8269 | return true; |
| 8270 | |
| 8271 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 8272 | NTTP->setTypeSourceInfo(NewTSI); |
| 8273 | NTTP->setType(NewTSI->getType()); |
| 8274 | } |
| 8275 | } |
| 8276 | |
| 8277 | return false; |
| 8278 | } |
| 8279 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8280 | /// \brief Produces a formatted string that describes the binding of |
| 8281 | /// template parameters to template arguments. |
| 8282 | std::string |
| 8283 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8284 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8285 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8286 | } |
| 8287 | |
| 8288 | std::string |
| 8289 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8290 | const TemplateArgument *Args, |
| 8291 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8292 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8293 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8294 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8295 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8296 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8297 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8298 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8299 | if (I >= NumArgs) |
| 8300 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8301 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8302 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8303 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8304 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8305 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8306 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8307 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8308 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8309 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8310 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8311 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8312 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8313 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 8314 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8315 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8316 | |
| 8317 | Out << ']'; |
| 8318 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8319 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8320 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8321 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 8322 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8323 | if (!FD) |
| 8324 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8325 | |
| 8326 | LateParsedTemplate *LPT = new LateParsedTemplate; |
| 8327 | |
| 8328 | // Take tokens to avoid allocations |
| 8329 | LPT->Toks.swap(Toks); |
| 8330 | LPT->D = FnD; |
Chandler Carruth | 52cee4d | 2015-03-26 09:08:15 +0000 | [diff] [blame] | 8331 | LateParsedTemplateMap.insert(std::make_pair(FD, LPT)); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8332 | |
| 8333 | FD->setLateTemplateParsed(true); |
| 8334 | } |
| 8335 | |
| 8336 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 8337 | if (!FD) |
| 8338 | return; |
| 8339 | FD->setLateTemplateParsed(false); |
| 8340 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8341 | |
| 8342 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 8343 | DeclContext *DC = CurContext; |
| 8344 | |
| 8345 | while (DC) { |
| 8346 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 8347 | const FunctionDecl *FD = RD->isLocalClass(); |
| 8348 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 8349 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 8350 | return false; |
| 8351 | |
| 8352 | DC = DC->getParent(); |
| 8353 | } |
| 8354 | return false; |
| 8355 | } |