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; |
Richard Smith | c7d48d1 | 2015-05-20 17:50:35 +0000 | [diff] [blame^] | 1313 | // FIXME: There might be a visible declaration of this template parameter. |
| 1314 | if (OldTypeParm && !LookupResult::isVisible(*this, OldTypeParm)) |
| 1315 | OldTypeParm = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1316 | |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1317 | if (NewTypeParm->isParameterPack()) { |
| 1318 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1319 | "Parameter packs can't have a default argument!"); |
| 1320 | SawParameterPack = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1321 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1322 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1323 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1324 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1325 | SawDefaultArgument = true; |
| 1326 | RedundantDefaultArg = true; |
| 1327 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1328 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1329 | // Merge the default argument from the old declaration to the |
| 1330 | // new declaration. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1331 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1332 | true); |
| 1333 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1334 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1335 | SawDefaultArgument = true; |
| 1336 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1337 | } else if (SawDefaultArgument) |
| 1338 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1339 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1340 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1341 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1342 | if (!NewNonTypeParm->isParameterPack() && |
| 1343 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1344 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1345 | UPPC_NonTypeTemplateParameterType)) { |
| 1346 | Invalid = true; |
| 1347 | continue; |
| 1348 | } |
| 1349 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1350 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1351 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1352 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1353 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1354 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1355 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1358 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1359 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1360 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1361 | if (NewNonTypeParm->isParameterPack()) { |
| 1362 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1363 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1364 | if (!NewNonTypeParm->isPackExpansion()) |
| 1365 | SawParameterPack = true; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1366 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 1367 | NewNonTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1368 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1369 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1370 | SawDefaultArgument = true; |
| 1371 | RedundantDefaultArg = true; |
| 1372 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1373 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1374 | // Merge the default argument from the old declaration to the |
| 1375 | // new declaration. |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1376 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1377 | // expression that points to a previous non-type template |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1378 | // parameter. |
| 1379 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1380 | OldNonTypeParm->getDefaultArgument(), |
| 1381 | /*Inherited=*/ true); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1382 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1383 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1384 | SawDefaultArgument = true; |
| 1385 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1386 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1387 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1388 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1389 | TemplateTemplateParmDecl *NewTemplateParm |
| 1390 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1391 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1392 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1393 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1394 | Invalid = true; |
| 1395 | continue; |
| 1396 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1397 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1398 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1399 | if (NewTemplateParm->hasDefaultArgument() && |
| 1400 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1401 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1402 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1403 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1404 | |
| 1405 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1406 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1407 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1408 | if (NewTemplateParm->isParameterPack()) { |
| 1409 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1410 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1411 | if (!NewTemplateParm->isPackExpansion()) |
| 1412 | SawParameterPack = true; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1413 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1414 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1415 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1416 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1417 | SawDefaultArgument = true; |
| 1418 | RedundantDefaultArg = true; |
| 1419 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1420 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1421 | // Merge the default argument from the old declaration to the |
| 1422 | // new declaration. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1423 | // FIXME: We need to create a new kind of "default argument" expression |
| 1424 | // that points to a previous template template parameter. |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1425 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1426 | OldTemplateParm->getDefaultArgument(), |
| 1427 | /*Inherited=*/ true); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1428 | PreviousDefaultArgLoc |
| 1429 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1430 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1431 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1432 | PreviousDefaultArgLoc |
| 1433 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1434 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1438 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1439 | // If a template parameter of a primary class template or alias template |
| 1440 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1441 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1442 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 1443 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1444 | Diag((*NewParam)->getLocation(), |
| 1445 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1446 | Invalid = true; |
| 1447 | } |
| 1448 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1449 | if (RedundantDefaultArg) { |
| 1450 | // C++ [temp.param]p12: |
| 1451 | // A template-parameter shall not be given default arguments |
| 1452 | // by two different declarations in the same scope. |
| 1453 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1454 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1455 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1456 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1457 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1458 | // If a template-parameter of a class template has a default |
| 1459 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1460 | // have a default template-argument supplied or be a template parameter |
| 1461 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1463 | diag::err_template_param_default_arg_missing); |
| 1464 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1465 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1466 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | // If we have an old template parameter list that we're merging |
| 1470 | // in, move on to the next parameter. |
| 1471 | if (OldParams) |
| 1472 | ++OldParam; |
| 1473 | } |
| 1474 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1475 | // We were missing some default arguments at the end of the list, so remove |
| 1476 | // all of the default arguments. |
| 1477 | if (RemoveDefaultArguments) { |
| 1478 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1479 | NewParamEnd = NewParams->end(); |
| 1480 | NewParam != NewParamEnd; ++NewParam) { |
| 1481 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1482 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1483 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1484 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1485 | NTTP->removeDefaultArgument(); |
| 1486 | else |
| 1487 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1488 | } |
| 1489 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1490 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1491 | return Invalid; |
| 1492 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1493 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1494 | namespace { |
| 1495 | |
| 1496 | /// A class which looks for a use of a certain level of template |
| 1497 | /// parameter. |
| 1498 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1499 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1500 | |
| 1501 | unsigned Depth; |
| 1502 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1503 | SourceLocation MatchLoc; |
| 1504 | |
| 1505 | DependencyChecker(unsigned Depth) : Depth(Depth), Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1506 | |
| 1507 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1508 | NamedDecl *ND = Params->getParam(0); |
| 1509 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1510 | Depth = PD->getDepth(); |
| 1511 | } else if (NonTypeTemplateParmDecl *PD = |
| 1512 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1513 | Depth = PD->getDepth(); |
| 1514 | } else { |
| 1515 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1516 | } |
| 1517 | } |
| 1518 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1519 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1520 | if (ParmDepth >= Depth) { |
| 1521 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1522 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1523 | return true; |
| 1524 | } |
| 1525 | return false; |
| 1526 | } |
| 1527 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1528 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1529 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 1530 | } |
| 1531 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1532 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1533 | return !Matches(T->getDepth()); |
| 1534 | } |
| 1535 | |
| 1536 | bool TraverseTemplateName(TemplateName N) { |
| 1537 | if (TemplateTemplateParmDecl *PD = |
| 1538 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1539 | if (Matches(PD->getDepth())) |
| 1540 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1541 | return super::TraverseTemplateName(N); |
| 1542 | } |
| 1543 | |
| 1544 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1545 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1546 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 1547 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1548 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1549 | return super::VisitDeclRefExpr(E); |
| 1550 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1551 | |
| 1552 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 1553 | return TraverseType(T->getReplacementType()); |
| 1554 | } |
| 1555 | |
| 1556 | bool |
| 1557 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 1558 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 1559 | } |
| 1560 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1561 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1562 | return TraverseType(T->getInjectedSpecializationType()); |
| 1563 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1564 | }; |
| 1565 | } |
| 1566 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1567 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1568 | /// list. |
| 1569 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1570 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1571 | DependencyChecker Checker(Params); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1572 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1573 | return Checker.Match; |
| 1574 | } |
| 1575 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1576 | // Find the source range corresponding to the named type in the given |
| 1577 | // nested-name-specifier, if any. |
| 1578 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1579 | QualType T, |
| 1580 | const CXXScopeSpec &SS) { |
| 1581 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1582 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1583 | if (const Type *CurType = NNS->getAsType()) { |
| 1584 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1585 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1586 | } else |
| 1587 | break; |
| 1588 | |
| 1589 | NNSLoc = NNSLoc.getPrefix(); |
| 1590 | } |
| 1591 | |
| 1592 | return SourceRange(); |
| 1593 | } |
| 1594 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1595 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1596 | /// specifier, returning the template parameter list that applies to the |
| 1597 | /// name. |
| 1598 | /// |
| 1599 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1600 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1601 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1602 | /// \param DeclLoc The location of the declaration itself. |
| 1603 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1604 | /// \param SS the scope specifier that will be matched to the given template |
| 1605 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1606 | /// being declared. |
| 1607 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1608 | /// \param TemplateId The template-id following the scope specifier, if there |
| 1609 | /// is one. Used to check for a missing 'template<>'. |
| 1610 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1611 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1612 | /// innermost template parameter lists. |
| 1613 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1614 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1615 | /// matching template parameters to scope specifiers in friend |
| 1616 | /// declarations. |
| 1617 | /// |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1618 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1619 | /// declared is an explicit specialization, false otherwise. |
| 1620 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1621 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1622 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1623 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1624 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1625 | /// 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] | 1626 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1627 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 1628 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1629 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1630 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
| 1631 | bool &IsExplicitSpecialization, bool &Invalid) { |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1632 | IsExplicitSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1633 | Invalid = false; |
| 1634 | |
| 1635 | // The sequence of nested types to which we will match up the template |
| 1636 | // parameter lists. We first build this list by starting with the type named |
| 1637 | // 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] | 1638 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1639 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1640 | if (SS.getScopeRep()) { |
| 1641 | if (CXXRecordDecl *Record |
| 1642 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1643 | T = Context.getTypeDeclType(Record); |
| 1644 | else |
| 1645 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1646 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1647 | |
| 1648 | // If we found an explicit specialization that prevents us from needing |
| 1649 | // 'template<>' headers, this will be set to the location of that |
| 1650 | // explicit specialization. |
| 1651 | SourceLocation ExplicitSpecLoc; |
| 1652 | |
| 1653 | while (!T.isNull()) { |
| 1654 | NestedTypes.push_back(T); |
| 1655 | |
| 1656 | // Retrieve the parent of a record type. |
| 1657 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1658 | // If this type is an explicit specialization, we're done. |
| 1659 | if (ClassTemplateSpecializationDecl *Spec |
| 1660 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1661 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1662 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1663 | ExplicitSpecLoc = Spec->getLocation(); |
| 1664 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1665 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1666 | } else if (Record->getTemplateSpecializationKind() |
| 1667 | == TSK_ExplicitSpecialization) { |
| 1668 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1669 | break; |
| 1670 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1671 | |
| 1672 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1673 | T = Context.getTypeDeclType(Parent); |
| 1674 | else |
| 1675 | T = QualType(); |
| 1676 | continue; |
| 1677 | } |
| 1678 | |
| 1679 | if (const TemplateSpecializationType *TST |
| 1680 | = T->getAs<TemplateSpecializationType>()) { |
| 1681 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1682 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1683 | T = Context.getTypeDeclType(Parent); |
| 1684 | else |
| 1685 | T = QualType(); |
| 1686 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1687 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | // Look one step prior in a dependent template specialization type. |
| 1691 | if (const DependentTemplateSpecializationType *DependentTST |
| 1692 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1693 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1694 | T = QualType(NNS->getAsType(), 0); |
| 1695 | else |
| 1696 | T = QualType(); |
| 1697 | continue; |
| 1698 | } |
| 1699 | |
| 1700 | // Look one step prior in a dependent name type. |
| 1701 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1702 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1703 | T = QualType(NNS->getAsType(), 0); |
| 1704 | else |
| 1705 | T = QualType(); |
| 1706 | continue; |
| 1707 | } |
| 1708 | |
| 1709 | // Retrieve the parent of an enumeration type. |
| 1710 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1711 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1712 | // check here. |
| 1713 | EnumDecl *Enum = EnumT->getDecl(); |
| 1714 | |
| 1715 | // Get to the parent type. |
| 1716 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1717 | T = Context.getTypeDeclType(Parent); |
| 1718 | else |
| 1719 | T = QualType(); |
| 1720 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1721 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1722 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1723 | T = QualType(); |
| 1724 | } |
| 1725 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1726 | // to the innermost while checking template-parameter-lists. |
| 1727 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1728 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1729 | // C++0x [temp.expl.spec]p17: |
| 1730 | // A member or a member template may be nested within many |
| 1731 | // enclosing class templates. In an explicit specialization for |
| 1732 | // such a member, the member declaration shall be preceded by a |
| 1733 | // template<> for each enclosing class template that is |
| 1734 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1735 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1736 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1737 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1738 | if (SawNonEmptyTemplateParameterList) { |
| 1739 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1740 | << !Recovery << Range; |
| 1741 | Invalid = true; |
| 1742 | IsExplicitSpecialization = false; |
| 1743 | return true; |
| 1744 | } |
| 1745 | |
| 1746 | return false; |
| 1747 | }; |
| 1748 | |
| 1749 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 1750 | // Check that we can have an explicit specialization here. |
| 1751 | if (CheckExplicitSpecialization(Range, true)) |
| 1752 | return true; |
| 1753 | |
| 1754 | // We don't have a template header, but we should. |
| 1755 | SourceLocation ExpectedTemplateLoc; |
| 1756 | if (!ParamLists.empty()) |
| 1757 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1758 | else |
| 1759 | ExpectedTemplateLoc = DeclStartLoc; |
| 1760 | |
| 1761 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1762 | << Range |
| 1763 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1764 | return false; |
| 1765 | }; |
| 1766 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1767 | unsigned ParamIdx = 0; |
| 1768 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1769 | ++TypeIdx) { |
| 1770 | T = NestedTypes[TypeIdx]; |
| 1771 | |
| 1772 | // Whether we expect a 'template<>' header. |
| 1773 | bool NeedEmptyTemplateHeader = false; |
| 1774 | |
| 1775 | // Whether we expect a template header with parameters. |
| 1776 | bool NeedNonemptyTemplateHeader = false; |
| 1777 | |
| 1778 | // For a dependent type, the set of template parameters that we |
| 1779 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1780 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1781 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1782 | // C++0x [temp.expl.spec]p15: |
| 1783 | // A member or a member template may be nested within many enclosing |
| 1784 | // class templates. In an explicit specialization for such a member, the |
| 1785 | // member declaration shall be preceded by a template<> for each |
| 1786 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1787 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1788 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1789 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1790 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1791 | NeedNonemptyTemplateHeader = true; |
| 1792 | } else if (Record->isDependentType()) { |
| 1793 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1794 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1795 | ->getTemplateParameters(); |
| 1796 | NeedNonemptyTemplateHeader = true; |
| 1797 | } |
| 1798 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1799 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1800 | // C++0x [temp.expl.spec]p4: |
| 1801 | // Members of an explicitly specialized class template are defined |
| 1802 | // in the same manner as members of normal classes, and not using |
| 1803 | // the template<> syntax. |
| 1804 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1805 | NeedEmptyTemplateHeader = true; |
| 1806 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1807 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1808 | } else if (Record->getTemplateSpecializationKind()) { |
| 1809 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1810 | != TSK_ExplicitSpecialization && |
| 1811 | TypeIdx == NumTypes - 1) |
| 1812 | IsExplicitSpecialization = true; |
| 1813 | |
| 1814 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1815 | } |
| 1816 | } else if (const TemplateSpecializationType *TST |
| 1817 | = T->getAs<TemplateSpecializationType>()) { |
Nico Weber | 2890061 | 2015-01-30 02:35:21 +0000 | [diff] [blame] | 1818 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1819 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1820 | NeedNonemptyTemplateHeader = true; |
| 1821 | } |
| 1822 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1823 | // FIXME: We actually could/should check the template arguments here |
| 1824 | // against the corresponding template parameter list. |
| 1825 | NeedNonemptyTemplateHeader = false; |
| 1826 | } |
| 1827 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1828 | // C++ [temp.expl.spec]p16: |
| 1829 | // In an explicit specialization declaration for a member of a class |
| 1830 | // template or a member template that ap- pears in namespace scope, the |
| 1831 | // member template and some of its enclosing class templates may remain |
| 1832 | // unspecialized, except that the declaration shall not explicitly |
| 1833 | // specialize a class member template if its en- closing class templates |
| 1834 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1835 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1836 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1837 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 1838 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1839 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1840 | } else |
| 1841 | SawNonEmptyTemplateParameterList = true; |
| 1842 | } |
| 1843 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1844 | if (NeedEmptyTemplateHeader) { |
| 1845 | // If we're on the last of the types, and we need a 'template<>' header |
| 1846 | // here, then it's an explicit specialization. |
| 1847 | if (TypeIdx == NumTypes - 1) |
| 1848 | IsExplicitSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1849 | |
| 1850 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1851 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1852 | // The header has template parameters when it shouldn't. Complain. |
| 1853 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1854 | diag::err_template_param_list_matches_nontemplate) |
| 1855 | << T |
| 1856 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1857 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1858 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1859 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1860 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1861 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1862 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1863 | // Consume this template header. |
| 1864 | ++ParamIdx; |
| 1865 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1866 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1867 | |
| 1868 | if (!IsFriend) |
| 1869 | if (DiagnoseMissingExplicitSpecialization( |
| 1870 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1871 | return nullptr; |
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 | continue; |
| 1874 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1875 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1876 | if (NeedNonemptyTemplateHeader) { |
| 1877 | // In friend declarations we can have template-ids which don't |
| 1878 | // depend on the corresponding template parameter lists. But |
| 1879 | // assume that empty parameter lists are supposed to match this |
| 1880 | // template-id. |
| 1881 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1882 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1883 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1884 | ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1885 | else |
| 1886 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1887 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1888 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1889 | if (ParamIdx < ParamLists.size()) { |
| 1890 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1891 | if (ExpectedTemplateParams && |
| 1892 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1893 | ExpectedTemplateParams, |
| 1894 | true, TPL_TemplateMatch)) |
| 1895 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1896 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1897 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1898 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1899 | TPC_ClassTemplateMember)) |
| 1900 | Invalid = true; |
| 1901 | |
| 1902 | ++ParamIdx; |
| 1903 | continue; |
| 1904 | } |
| 1905 | |
| 1906 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1907 | << T |
| 1908 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1909 | Invalid = true; |
| 1910 | continue; |
| 1911 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1912 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1913 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1914 | // If there were at least as many template-ids as there were template |
| 1915 | // parameter lists, then there are no template parameter lists remaining for |
| 1916 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1917 | if (ParamIdx >= ParamLists.size()) { |
| 1918 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1919 | // We don't have a template header for the declaration itself, but we |
| 1920 | // should. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1921 | IsExplicitSpecialization = true; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1922 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 1923 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1924 | |
| 1925 | // Fabricate an empty template parameter list for the invented header. |
| 1926 | return TemplateParameterList::Create(Context, SourceLocation(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1927 | SourceLocation(), nullptr, 0, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1928 | SourceLocation()); |
| 1929 | } |
| 1930 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1931 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1932 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1933 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1934 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1935 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1936 | bool HasAnyExplicitSpecHeader = false; |
| 1937 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1938 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1939 | if (ParamLists[I]->size() == 0) |
| 1940 | HasAnyExplicitSpecHeader = true; |
| 1941 | else |
| 1942 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1943 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1944 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1945 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1946 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 1947 | : diag::err_template_spec_extra_headers) |
| 1948 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1949 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1950 | |
| 1951 | // If there was a specialization somewhere, such that 'template<>' is |
| 1952 | // not required, and there were any 'template<>' headers, note where the |
| 1953 | // specialization occurred. |
| 1954 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1955 | Diag(ExplicitSpecLoc, |
| 1956 | diag::note_explicit_template_spec_does_not_need_header) |
| 1957 | << NestedTypes.back(); |
| 1958 | |
| 1959 | // We have a template parameter list with no corresponding scope, which |
| 1960 | // means that the resulting template declaration can't be instantiated |
| 1961 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1962 | if (!AllExplicitSpecHeaders) |
| 1963 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1964 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1965 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1966 | // C++ [temp.expl.spec]p16: |
| 1967 | // In an explicit specialization declaration for a member of a class |
| 1968 | // template or a member template that ap- pears in namespace scope, the |
| 1969 | // member template and some of its enclosing class templates may remain |
| 1970 | // unspecialized, except that the declaration shall not explicitly |
| 1971 | // specialize a class member template if its en- closing class templates |
| 1972 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1973 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1974 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 1975 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1976 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1977 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1978 | // Return the last template parameter list, which corresponds to the |
| 1979 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1980 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1983 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1984 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1985 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1986 | << (isa<FunctionTemplateDecl>(Template) |
| 1987 | ? 0 |
| 1988 | : isa<ClassTemplateDecl>(Template) |
| 1989 | ? 1 |
| 1990 | : isa<VarTemplateDecl>(Template) |
| 1991 | ? 2 |
| 1992 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 1993 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1994 | return; |
| 1995 | } |
| 1996 | |
| 1997 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1998 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1999 | IEnd = OST->end(); |
| 2000 | I != IEnd; ++I) |
| 2001 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 2002 | << 0 << (*I)->getDeclName(); |
| 2003 | |
| 2004 | return; |
| 2005 | } |
| 2006 | } |
| 2007 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2008 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 2009 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2010 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 2011 | DependentTemplateName *DTN |
| 2012 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2013 | if (DTN && DTN->isIdentifier()) |
| 2014 | // When building a template-id where the template-name is dependent, |
| 2015 | // assume the template is a type template. Either our assumption is |
| 2016 | // correct, or the code is ill-formed and will be diagnosed when the |
| 2017 | // dependent name is substituted. |
| 2018 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 2019 | DTN->getQualifier(), |
| 2020 | DTN->getIdentifier(), |
| 2021 | TemplateArgs); |
| 2022 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2023 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 2024 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
| 2025 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2026 | // We might have a substituted template template parameter pack. If so, |
| 2027 | // build a template specialization type for it. |
| 2028 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 2029 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2030 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2031 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 2032 | << Name; |
| 2033 | NoteAllFoundTemplates(Name); |
| 2034 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2035 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2036 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2037 | // Check that the template argument list is well-formed for this |
| 2038 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2039 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2040 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2041 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2042 | return QualType(); |
| 2043 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2044 | QualType CanonType; |
| 2045 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2046 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2047 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 2048 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2049 | // Find the canonical type for this type alias template specialization. |
| 2050 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 2051 | if (Pattern->isInvalidDecl()) |
| 2052 | return QualType(); |
| 2053 | |
| 2054 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2055 | Converted.data(), Converted.size()); |
| 2056 | |
| 2057 | // Only substitute for the innermost template argument list. |
| 2058 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 0c4a34b | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 2059 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 2060 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 2061 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 2062 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2063 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2064 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2065 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2066 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2067 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2068 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2069 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 2070 | TemplateArgLists, AliasTemplate->getLocation(), |
| 2071 | AliasTemplate->getDeclName()); |
| 2072 | if (CanonType.isNull()) |
| 2073 | return QualType(); |
| 2074 | } else if (Name.isDependent() || |
| 2075 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2076 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2077 | // This class template specialization is a dependent |
| 2078 | // type. Therefore, its canonical type is another class template |
| 2079 | // specialization type that contains all of the converted |
| 2080 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 2081 | // A<T, T> have identical types when A is declared as: |
| 2082 | // |
| 2083 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2084 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2086 | Converted.data(), |
| 2087 | Converted.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2088 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2089 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2090 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2091 | // In the future, we need to teach getTemplateSpecializationType to only |
| 2092 | // build the canonical type and return that to us. |
| 2093 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2094 | |
| 2095 | // This might work out to be a current instantiation, in which |
| 2096 | // case the canonical type needs to be the InjectedClassNameType. |
| 2097 | // |
| 2098 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2099 | // changes to CurContext don't change the set of current |
| 2100 | // instantiations. |
| 2101 | if (isa<ClassTemplateDecl>(Template)) { |
| 2102 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2103 | // If we get out to a namespace, we're done. |
| 2104 | if (Ctx->isFileContext()) break; |
| 2105 | |
| 2106 | // If this isn't a record, keep looking. |
| 2107 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2108 | if (!Record) continue; |
| 2109 | |
| 2110 | // Look for one of the two cases with InjectedClassNameTypes |
| 2111 | // and check whether it's the same template. |
| 2112 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2113 | !Record->getDescribedClassTemplate()) |
| 2114 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2115 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2116 | // Fetch the injected class name type and check whether its |
| 2117 | // injected type is equal to the type we just built. |
| 2118 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2119 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2120 | ->getInjectedSpecializationType(); |
| 2121 | |
| 2122 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2123 | continue; |
| 2124 | |
| 2125 | // If so, the canonical type of this TST is the injected |
| 2126 | // class name type of the record we just found. |
| 2127 | assert(ICNT.isCanonical()); |
| 2128 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2129 | break; |
| 2130 | } |
| 2131 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2132 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2133 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2134 | // Find the class template specialization declaration that |
| 2135 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2136 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2137 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2138 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2139 | if (!Decl) { |
| 2140 | // This is the first time we have referenced this class template |
| 2141 | // specialization. Create the canonical declaration and add it to |
| 2142 | // the set of specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2143 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2144 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2145 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2146 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2147 | ClassTemplate->getLocation(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2148 | ClassTemplate, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2149 | Converted.data(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2150 | Converted.size(), nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2151 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 2152 | if (ClassTemplate->isOutOfLine()) |
| 2153 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 2156 | // Diagnose uses of this specialization. |
| 2157 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 2158 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2159 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2160 | assert(isa<RecordType>(CanonType) && |
| 2161 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2162 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2163 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2164 | // Build the fully-sugared type for this class template |
| 2165 | // specialization, which refers back to the class template |
| 2166 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2167 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2170 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2171 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2172 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2173 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2174 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2175 | SourceLocation RAngleLoc, |
| 2176 | bool IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2177 | if (SS.isInvalid()) |
| 2178 | return true; |
| 2179 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2180 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2181 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2182 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2183 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2184 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2185 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2186 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2187 | QualType T |
| 2188 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2189 | DTN->getQualifier(), |
| 2190 | DTN->getIdentifier(), |
| 2191 | TemplateArgs); |
| 2192 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2193 | TypeLocBuilder TLB; |
| 2194 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2195 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2196 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2197 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2198 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2199 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2200 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2201 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2202 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2203 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2204 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2205 | } |
| 2206 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2207 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2208 | |
| 2209 | if (Result.isNull()) |
| 2210 | return true; |
| 2211 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2212 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2213 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2214 | TemplateSpecializationTypeLoc SpecTL |
| 2215 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2216 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2217 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2218 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2219 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2220 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2221 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2222 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2223 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2224 | // constructor or destructor name (in such a case, the scope specifier |
| 2225 | // will be attached to the enclosing Decl or Expr node). |
| 2226 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2227 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2228 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2229 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2230 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2231 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2232 | } |
| 2233 | |
| 2234 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2235 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2236 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2237 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2238 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2239 | SourceLocation TagLoc, |
| 2240 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2241 | SourceLocation TemplateKWLoc, |
| 2242 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2243 | SourceLocation TemplateLoc, |
| 2244 | SourceLocation LAngleLoc, |
| 2245 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2246 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2247 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2248 | |
| 2249 | // Translate the parser's template argument list in our AST format. |
| 2250 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2251 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2252 | |
| 2253 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2254 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2255 | ElaboratedTypeKeyword Keyword |
| 2256 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2257 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2258 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2259 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2260 | DTN->getQualifier(), |
| 2261 | DTN->getIdentifier(), |
| 2262 | TemplateArgs); |
| 2263 | |
| 2264 | // Build type-source information. |
| 2265 | TypeLocBuilder TLB; |
| 2266 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2267 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2268 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2269 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2270 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2271 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2272 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2273 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2274 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2275 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2276 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2277 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2278 | |
| 2279 | if (TypeAliasTemplateDecl *TAT = |
| 2280 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2281 | // C++0x [dcl.type.elab]p2: |
| 2282 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2283 | // resolves to an alias template specialization, the |
| 2284 | // elaborated-type-specifier is ill-formed. |
| 2285 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2286 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2287 | } |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2288 | |
| 2289 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2290 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2291 | return TypeResult(true); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2292 | |
| 2293 | // Check the tag kind |
| 2294 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2295 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2296 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2297 | IdentifierInfo *Id = D->getIdentifier(); |
| 2298 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2299 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2300 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 2301 | TagLoc, *Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2302 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2303 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2304 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2305 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2306 | } |
| 2307 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2308 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2309 | // Provide source-location information for the template specialization. |
| 2310 | TypeLocBuilder TLB; |
| 2311 | TemplateSpecializationTypeLoc SpecTL |
| 2312 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2313 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2314 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2315 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2316 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2317 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2318 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2319 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2320 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2321 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2322 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2323 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2324 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2325 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2326 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2329 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2330 | Sema &S, SourceLocation NameLoc, TemplateParameterList *TemplateParams, |
| 2331 | unsigned ExplicitArgs, SmallVectorImpl<TemplateArgument> &TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2332 | |
| 2333 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 2334 | NamedDecl *PrevDecl, |
| 2335 | SourceLocation Loc, |
| 2336 | bool IsPartialSpecialization); |
| 2337 | |
| 2338 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2339 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2340 | static bool isTemplateArgumentTemplateParameter( |
| 2341 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 2342 | switch (Arg.getKind()) { |
| 2343 | case TemplateArgument::Null: |
| 2344 | case TemplateArgument::NullPtr: |
| 2345 | case TemplateArgument::Integral: |
| 2346 | case TemplateArgument::Declaration: |
| 2347 | case TemplateArgument::Pack: |
| 2348 | case TemplateArgument::TemplateExpansion: |
| 2349 | return false; |
| 2350 | |
| 2351 | case TemplateArgument::Type: { |
| 2352 | QualType Type = Arg.getAsType(); |
| 2353 | const TemplateTypeParmType *TPT = |
| 2354 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 2355 | return TPT && !Type.hasQualifiers() && |
| 2356 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 2357 | } |
| 2358 | |
| 2359 | case TemplateArgument::Expression: { |
| 2360 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 2361 | if (!DRE || !DRE->getDecl()) |
| 2362 | return false; |
| 2363 | const NonTypeTemplateParmDecl *NTTP = |
| 2364 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 2365 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 2366 | } |
| 2367 | |
| 2368 | case TemplateArgument::Template: |
| 2369 | const TemplateTemplateParmDecl *TTP = |
| 2370 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 2371 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 2372 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 2373 | } |
| 2374 | llvm_unreachable("unexpected kind of template argument"); |
| 2375 | } |
| 2376 | |
| 2377 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 2378 | ArrayRef<TemplateArgument> Args) { |
| 2379 | if (Params->size() != Args.size()) |
| 2380 | return false; |
| 2381 | |
| 2382 | unsigned Depth = Params->getDepth(); |
| 2383 | |
| 2384 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 2385 | TemplateArgument Arg = Args[I]; |
| 2386 | |
| 2387 | // If the parameter is a pack expansion, the argument must be a pack |
| 2388 | // whose only element is a pack expansion. |
| 2389 | if (Params->getParam(I)->isParameterPack()) { |
| 2390 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 2391 | !Arg.pack_begin()->isPackExpansion()) |
| 2392 | return false; |
| 2393 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 2394 | } |
| 2395 | |
| 2396 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 2397 | return false; |
| 2398 | } |
| 2399 | |
| 2400 | return true; |
| 2401 | } |
| 2402 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2403 | /// Convert the parser's template argument list representation into our form. |
| 2404 | static TemplateArgumentListInfo |
| 2405 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 2406 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 2407 | TemplateId.RAngleLoc); |
| 2408 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 2409 | TemplateId.NumArgs); |
| 2410 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 2411 | return TemplateArgs; |
| 2412 | } |
| 2413 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2414 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2415 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 2416 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2417 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2418 | // D must be variable template id. |
| 2419 | assert(D.getName().getKind() == UnqualifiedId::IK_TemplateId && |
| 2420 | "Variable template specialization is declared with a template it."); |
| 2421 | |
| 2422 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2423 | TemplateArgumentListInfo TemplateArgs = |
| 2424 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2425 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 2426 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 2427 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2428 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2429 | TemplateName Name = TemplateId->Template.get(); |
| 2430 | |
| 2431 | // The template-id must name a variable template. |
| 2432 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2433 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 2434 | if (!VarTemplate) { |
| 2435 | NamedDecl *FnTemplate; |
| 2436 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 2437 | FnTemplate = *OTS->begin(); |
| 2438 | else |
| 2439 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 2440 | if (FnTemplate) |
| 2441 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 2442 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2443 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 2444 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2445 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2446 | |
| 2447 | // Check for unexpanded parameter packs in any of the template arguments. |
| 2448 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 2449 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 2450 | UPPC_PartialSpecialization)) |
| 2451 | return true; |
| 2452 | |
| 2453 | // Check that the template argument list is well-formed for this |
| 2454 | // template. |
| 2455 | SmallVector<TemplateArgument, 4> Converted; |
| 2456 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 2457 | false, Converted)) |
| 2458 | return true; |
| 2459 | |
| 2460 | // Check that the type of this variable template specialization |
| 2461 | // matches the expected type. |
| 2462 | TypeSourceInfo *ExpectedDI; |
| 2463 | { |
| 2464 | // Do substitution on the type of the declaration |
| 2465 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
| 2466 | Converted.data(), Converted.size()); |
| 2467 | InstantiatingTemplate Inst(*this, TemplateKWLoc, VarTemplate); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2468 | if (Inst.isInvalid()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2469 | return true; |
| 2470 | VarDecl *Templated = VarTemplate->getTemplatedDecl(); |
| 2471 | ExpectedDI = |
| 2472 | SubstType(Templated->getTypeSourceInfo(), |
| 2473 | MultiLevelTemplateArgumentList(TemplateArgList), |
| 2474 | Templated->getTypeSpecStartLoc(), Templated->getDeclName()); |
| 2475 | } |
| 2476 | if (!ExpectedDI) |
| 2477 | return true; |
| 2478 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2479 | // Find the variable template (partial) specialization declaration that |
| 2480 | // corresponds to these arguments. |
| 2481 | if (IsPartialSpecialization) { |
| 2482 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2483 | *this, TemplateNameLoc, VarTemplate->getTemplateParameters(), |
| 2484 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2485 | return true; |
| 2486 | |
| 2487 | bool InstantiationDependent; |
| 2488 | if (!Name.isDependent() && |
| 2489 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2490 | TemplateArgs.getArgumentArray(), TemplateArgs.size(), |
| 2491 | InstantiationDependent)) { |
| 2492 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 2493 | << VarTemplate->getDeclName(); |
| 2494 | IsPartialSpecialization = false; |
| 2495 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2496 | |
| 2497 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 2498 | Converted)) { |
| 2499 | // C++ [temp.class.spec]p9b3: |
| 2500 | // |
| 2501 | // -- The argument list of the specialization shall not be identical |
| 2502 | // to the implicit argument list of the primary template. |
| 2503 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 2504 | << /*variable template*/ 1 |
| 2505 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 2506 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 2507 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 2508 | // of the primary template. |
| 2509 | return true; |
| 2510 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2513 | void *InsertPos = nullptr; |
| 2514 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2515 | |
| 2516 | if (IsPartialSpecialization) |
| 2517 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2518 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2519 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2520 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2521 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2522 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2523 | |
| 2524 | // Check whether we can declare a variable template specialization in |
| 2525 | // the current scope. |
| 2526 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 2527 | TemplateNameLoc, |
| 2528 | IsPartialSpecialization)) |
| 2529 | return true; |
| 2530 | |
| 2531 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 2532 | // Since the only prior variable template specialization with these |
| 2533 | // arguments was referenced but not declared, reuse that |
| 2534 | // declaration node as our own, updating its source location and |
| 2535 | // the list of outer template parameters to reflect our new declaration. |
| 2536 | Specialization = PrevDecl; |
| 2537 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2538 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2539 | } else if (IsPartialSpecialization) { |
| 2540 | // Create a new class template partial specialization declaration node. |
| 2541 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 2542 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2543 | VarTemplatePartialSpecializationDecl *Partial = |
| 2544 | VarTemplatePartialSpecializationDecl::Create( |
| 2545 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 2546 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 2547 | Converted.data(), Converted.size(), TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2548 | |
| 2549 | if (!PrevPartial) |
| 2550 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 2551 | Specialization = Partial; |
| 2552 | |
| 2553 | // If we are providing an explicit specialization of a member variable |
| 2554 | // template specialization, make a note of that. |
| 2555 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2556 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2557 | |
| 2558 | // Check that all of the template parameters of the variable template |
| 2559 | // partial specialization are deducible from the template |
| 2560 | // arguments. If not, this variable template partial specialization |
| 2561 | // will never be used. |
| 2562 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 2563 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 2564 | TemplateParams->getDepth(), DeducibleParams); |
| 2565 | |
| 2566 | if (!DeducibleParams.all()) { |
| 2567 | unsigned NumNonDeducible = |
| 2568 | DeducibleParams.size() - DeducibleParams.count(); |
| 2569 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2570 | << /*variable template*/ 1 << (NumNonDeducible > 1) |
| 2571 | << SourceRange(TemplateNameLoc, RAngleLoc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2572 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 2573 | if (!DeducibleParams[I]) { |
| 2574 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 2575 | if (Param->getDeclName()) |
| 2576 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
| 2577 | << Param->getDeclName(); |
| 2578 | else |
| 2579 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 2580 | << "(anonymous)"; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2581 | } |
| 2582 | } |
| 2583 | } |
| 2584 | } else { |
| 2585 | // Create a new class template specialization declaration node for |
| 2586 | // this explicit specialization or friend declaration. |
| 2587 | Specialization = VarTemplateSpecializationDecl::Create( |
| 2588 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
| 2589 | VarTemplate, DI->getType(), DI, SC, Converted.data(), Converted.size()); |
| 2590 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 2591 | |
| 2592 | if (!PrevDecl) |
| 2593 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 2594 | } |
| 2595 | |
| 2596 | // C++ [temp.expl.spec]p6: |
| 2597 | // If a template, a member template or the member of a class template is |
| 2598 | // explicitly specialized then that specialization shall be declared |
| 2599 | // before the first use of that specialization that would cause an implicit |
| 2600 | // instantiation to take place, in every translation unit in which such a |
| 2601 | // use occurs; no diagnostic is required. |
| 2602 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 2603 | bool Okay = false; |
| 2604 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 2605 | // Is there any previous explicit specialization declaration? |
| 2606 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 2607 | Okay = true; |
| 2608 | break; |
| 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | if (!Okay) { |
| 2613 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 2614 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 2615 | << Name << Range; |
| 2616 | |
| 2617 | Diag(PrevDecl->getPointOfInstantiation(), |
| 2618 | diag::note_instantiation_required_here) |
| 2619 | << (PrevDecl->getTemplateSpecializationKind() != |
| 2620 | TSK_ImplicitInstantiation); |
| 2621 | return true; |
| 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 2626 | Specialization->setLexicalDeclContext(CurContext); |
| 2627 | |
| 2628 | // Add the specialization into its lexical context, so that it can |
| 2629 | // be seen when iterating through the list of declarations in that |
| 2630 | // context. However, specializations are not found by name lookup. |
| 2631 | CurContext->addDecl(Specialization); |
| 2632 | |
| 2633 | // Note that this is an explicit specialization. |
| 2634 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 2635 | |
| 2636 | if (PrevDecl) { |
| 2637 | // Check that this isn't a redefinition of this specialization, |
| 2638 | // merging with previous declarations. |
| 2639 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
| 2640 | ForRedeclaration); |
| 2641 | PrevSpec.addDecl(PrevDecl); |
| 2642 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2643 | } else if (Specialization->isStaticDataMember() && |
| 2644 | Specialization->isOutOfLine()) { |
| 2645 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2646 | } |
| 2647 | |
| 2648 | // Link instantiations of static data members back to the template from |
| 2649 | // which they were instantiated. |
| 2650 | if (Specialization->isStaticDataMember()) |
| 2651 | Specialization->setInstantiationOfStaticDataMember( |
| 2652 | VarTemplate->getTemplatedDecl(), |
| 2653 | Specialization->getSpecializationKind()); |
| 2654 | |
| 2655 | return Specialization; |
| 2656 | } |
| 2657 | |
| 2658 | namespace { |
| 2659 | /// \brief A partial specialization whose template arguments have matched |
| 2660 | /// a given template-id. |
| 2661 | struct PartialSpecMatchResult { |
| 2662 | VarTemplatePartialSpecializationDecl *Partial; |
| 2663 | TemplateArgumentList *Args; |
| 2664 | }; |
| 2665 | } |
| 2666 | |
| 2667 | DeclResult |
| 2668 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2669 | SourceLocation TemplateNameLoc, |
| 2670 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2671 | assert(Template && "A variable template id without template?"); |
| 2672 | |
| 2673 | // Check that the template argument list is well-formed for this template. |
| 2674 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2675 | if (CheckTemplateArgumentList( |
| 2676 | Template, TemplateNameLoc, |
| 2677 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2678 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2679 | return true; |
| 2680 | |
| 2681 | // Find the variable template specialization declaration that |
| 2682 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2683 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2684 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2685 | Converted, InsertPos)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2686 | // If we already have a variable template specialization, return it. |
| 2687 | return Spec; |
| 2688 | |
| 2689 | // This is the first time we have referenced this variable template |
| 2690 | // specialization. Create the canonical declaration and add it to |
| 2691 | // the set of specializations, based on the closest partial specialization |
| 2692 | // that it represents. That is, |
| 2693 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 2694 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
| 2695 | Converted.data(), Converted.size()); |
| 2696 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 2697 | bool AmbiguousPartialSpec = false; |
| 2698 | typedef PartialSpecMatchResult MatchResult; |
| 2699 | SmallVector<MatchResult, 4> Matched; |
| 2700 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
| 2701 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation); |
| 2702 | |
| 2703 | // 1. Attempt to find the closest partial specialization that this |
| 2704 | // specializes, if any. |
| 2705 | // If any of the template arguments is dependent, then this is probably |
| 2706 | // a placeholder for an incomplete declarative context; which must be |
| 2707 | // complete by instantiation time. Thus, do not search through the partial |
| 2708 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2709 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 2710 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 2711 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2712 | bool InstantiationDependent = false; |
| 2713 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 2714 | TemplateArgs, InstantiationDependent)) { |
| 2715 | |
| 2716 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 2717 | Template->getPartialSpecializations(PartialSpecs); |
| 2718 | |
| 2719 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 2720 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 2721 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 2722 | |
| 2723 | if (TemplateDeductionResult Result = |
| 2724 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 2725 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2726 | // TODO: Actually use the failed-deduction info? |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2727 | FailedCandidates.addCandidate() |
| 2728 | .set(Partial, MakeDeductionFailureInfo(Context, Result, Info)); |
| 2729 | (void)Result; |
| 2730 | } else { |
| 2731 | Matched.push_back(PartialSpecMatchResult()); |
| 2732 | Matched.back().Partial = Partial; |
| 2733 | Matched.back().Args = Info.take(); |
| 2734 | } |
| 2735 | } |
| 2736 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2737 | if (Matched.size() >= 1) { |
| 2738 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 2739 | if (Matched.size() == 1) { |
| 2740 | // -- If exactly one matching specialization is found, the |
| 2741 | // instantiation is generated from that specialization. |
| 2742 | // We don't need to do anything for this. |
| 2743 | } else { |
| 2744 | // -- If more than one matching specialization is found, the |
| 2745 | // partial order rules (14.5.4.2) are used to determine |
| 2746 | // whether one of the specializations is more specialized |
| 2747 | // than the others. If none of the specializations is more |
| 2748 | // specialized than all of the other matching |
| 2749 | // specializations, then the use of the variable template is |
| 2750 | // ambiguous and the program is ill-formed. |
| 2751 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 2752 | PEnd = Matched.end(); |
| 2753 | P != PEnd; ++P) { |
| 2754 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 2755 | PointOfInstantiation) == |
| 2756 | P->Partial) |
| 2757 | Best = P; |
| 2758 | } |
| 2759 | |
| 2760 | // Determine if the best partial specialization is more specialized than |
| 2761 | // the others. |
| 2762 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2763 | PEnd = Matched.end(); |
| 2764 | P != PEnd; ++P) { |
| 2765 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 2766 | P->Partial, Best->Partial, |
| 2767 | PointOfInstantiation) != Best->Partial) { |
| 2768 | AmbiguousPartialSpec = true; |
| 2769 | break; |
| 2770 | } |
| 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | // Instantiate using the best variable template partial specialization. |
| 2775 | InstantiationPattern = Best->Partial; |
| 2776 | InstantiationArgs = Best->Args; |
| 2777 | } else { |
| 2778 | // -- If no match is found, the instantiation is generated |
| 2779 | // from the primary template. |
| 2780 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 2781 | } |
| 2782 | } |
| 2783 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2784 | // 2. Create the canonical declaration. |
| 2785 | // Note that we do not instantiate the variable just yet, since |
| 2786 | // instantiation is handled in DoMarkVarDeclReferenced(). |
| 2787 | // FIXME: LateAttrs et al.? |
| 2788 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 2789 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 2790 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 2791 | if (!Decl) |
| 2792 | return true; |
| 2793 | |
| 2794 | if (AmbiguousPartialSpec) { |
| 2795 | // Partial ordering did not produce a clear winner. Complain. |
| 2796 | Decl->setInvalidDecl(); |
| 2797 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 2798 | << Decl; |
| 2799 | |
| 2800 | // Print the matching partial specializations. |
| 2801 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2802 | PEnd = Matched.end(); |
| 2803 | P != PEnd; ++P) |
| 2804 | Diag(P->Partial->getLocation(), diag::note_partial_spec_match) |
| 2805 | << getTemplateArgumentBindingsText( |
| 2806 | P->Partial->getTemplateParameters(), *P->Args); |
| 2807 | return true; |
| 2808 | } |
| 2809 | |
| 2810 | if (VarTemplatePartialSpecializationDecl *D = |
| 2811 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 2812 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 2813 | |
| 2814 | assert(Decl && "No variable template specialization?"); |
| 2815 | return Decl; |
| 2816 | } |
| 2817 | |
| 2818 | ExprResult |
| 2819 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 2820 | const DeclarationNameInfo &NameInfo, |
| 2821 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2822 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2823 | |
| 2824 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 2825 | *TemplateArgs); |
| 2826 | if (Decl.isInvalid()) |
| 2827 | return ExprError(); |
| 2828 | |
| 2829 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 2830 | if (!Var->getTemplateSpecializationKind()) |
| 2831 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 2832 | NameInfo.getLoc()); |
| 2833 | |
| 2834 | // Build an ordinary singleton decl ref. |
| 2835 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2836 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2839 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2840 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2841 | LookupResult &R, |
| 2842 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2843 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2844 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2845 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2846 | // 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] | 2847 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2848 | // foo<int> could identify a single function unambiguously |
| 2849 | // This approach does NOT work, since f<int>(1); |
| 2850 | // gets resolved prior to resorting to overload resolution |
| 2851 | // i.e., template<class T> void f(double); |
| 2852 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2853 | |
| 2854 | // These should be filtered out by our callers. |
| 2855 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2856 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2857 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2858 | // In C++1y, check variable template ids. |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 2859 | bool InstantiationDependent; |
| 2860 | if (R.getAsSingle<VarTemplateDecl>() && |
| 2861 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2862 | *TemplateArgs, InstantiationDependent)) { |
| 2863 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 2864 | R.getAsSingle<VarTemplateDecl>(), |
| 2865 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2868 | // We don't want lookup warnings at this point. |
| 2869 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2870 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2871 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2872 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2873 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2874 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2875 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2876 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2877 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2878 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2879 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2882 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2883 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2884 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2885 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2886 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2887 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2888 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2889 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2890 | DeclContext *DC; |
| 2891 | if (!(DC = computeDeclContext(SS, false)) || |
| 2892 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2893 | RequireCompleteDeclContext(SS, DC)) |
Reid Kleckner | 034531d | 2014-12-18 18:17:42 +0000 | [diff] [blame] | 2894 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2895 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2896 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2897 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2898 | LookupTemplateName(R, (Scope*)nullptr, SS, QualType(), /*Entering*/ false, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2899 | MemberOfUnknownSpecialization); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2900 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2901 | if (R.isAmbiguous()) |
| 2902 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2903 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2904 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2905 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2906 | << NameInfo.getName() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2907 | return ExprError(); |
| 2908 | } |
| 2909 | |
| 2910 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2911 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 2912 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 2913 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2914 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2915 | return ExprError(); |
| 2916 | } |
| 2917 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2918 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2921 | /// \brief Form a dependent template name. |
| 2922 | /// |
| 2923 | /// This action forms a dependent template name given the template |
| 2924 | /// name and its (presumably dependent) scope specifier. For |
| 2925 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2926 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2927 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2928 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2929 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2930 | SourceLocation TemplateKWLoc, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2931 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2932 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2933 | bool EnteringContext, |
| 2934 | TemplateTy &Result) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2935 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 2936 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2937 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2938 | diag::warn_cxx98_compat_template_outside_of_template : |
| 2939 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2940 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2941 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2942 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2943 | if (SS.isSet()) |
| 2944 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2945 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2946 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2947 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2948 | // C++0x [temp.names]p5: |
| 2949 | // If a name prefixed by the keyword template is not the name of |
| 2950 | // a template, the program is ill-formed. [Note: the keyword |
| 2951 | // template may not be applied to non-template members of class |
| 2952 | // templates. -end note ] [ Note: as is the case with the |
| 2953 | // typename prefix, the template prefix is allowed in cases |
| 2954 | // where it is not strictly necessary; i.e., when the |
| 2955 | // nested-name-specifier or the expression on the left of the -> |
| 2956 | // or . is not dependent on a template-parameter, or the use |
| 2957 | // does not appear in the scope of a template. -end note] |
| 2958 | // |
| 2959 | // Note: C++03 was more strict here, because it banned the use of |
| 2960 | // the "template" keyword prior to a template-name that was not a |
| 2961 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2962 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2963 | // 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] | 2964 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 2965 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2966 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2967 | MemberOfUnknownSpecialization); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2968 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2969 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | 5ecbb1b | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2970 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2971 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2972 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2973 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2974 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2975 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2976 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2977 | << Name.getSourceRange() |
| 2978 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2979 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2980 | } else { |
| 2981 | // We found something; return it. |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2982 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2983 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 2986 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2987 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2988 | switch (Name.getKind()) { |
| 2989 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2990 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2991 | Name.Identifier)); |
| 2992 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2993 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2994 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2995 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2996 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 2997 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2998 | |
| 2999 | case UnqualifiedId::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 3000 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 3001 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3002 | default: |
| 3003 | break; |
| 3004 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3005 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3006 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 3007 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3008 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 3009 | << Name.getSourceRange() |
| 3010 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 3011 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3014 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3015 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3016 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3017 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3018 | QualType ArgType; |
| 3019 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3020 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3021 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3022 | switch(Arg.getKind()) { |
| 3023 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3024 | // C++ [temp.arg.type]p1: |
| 3025 | // A template-argument for a template-parameter which is a |
| 3026 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3027 | ArgType = Arg.getAsType(); |
| 3028 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3029 | break; |
| 3030 | case TemplateArgument::Template: { |
| 3031 | // We have a template type parameter but the template argument |
| 3032 | // is a template without any arguments. |
| 3033 | SourceRange SR = AL.getSourceRange(); |
| 3034 | TemplateName Name = Arg.getAsTemplate(); |
| 3035 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 3036 | << Name << SR; |
| 3037 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 3038 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3039 | |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3040 | return true; |
| 3041 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3042 | case TemplateArgument::Expression: { |
| 3043 | // We have a template type parameter but the template argument is an |
| 3044 | // expression; see if maybe it is missing the "typename" keyword. |
| 3045 | CXXScopeSpec SS; |
| 3046 | DeclarationNameInfo NameInfo; |
| 3047 | |
| 3048 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 3049 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3050 | NameInfo = ArgExpr->getNameInfo(); |
| 3051 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 3052 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 3053 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3054 | NameInfo = ArgExpr->getNameInfo(); |
| 3055 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 3056 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3057 | if (ArgExpr->isImplicitAccess()) { |
| 3058 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3059 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 3060 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3061 | } |
| 3062 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3063 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3064 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 3065 | LookupParsedName(Result, CurScope, &SS); |
| 3066 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3067 | if (Result.getAsSingle<TypeDecl>() || |
| 3068 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3069 | LookupResult::NotFoundInCurrentInstantiation) { |
| 3070 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3071 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3072 | Diag(Loc, getLangOpts().MSVCCompat |
| 3073 | ? diag::ext_ms_template_type_arg_missing_typename |
| 3074 | : diag::err_template_arg_must_be_type_suggest) |
| 3075 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3076 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3077 | |
| 3078 | // Recover by synthesizing a type using the location information that we |
| 3079 | // already have. |
| 3080 | ArgType = |
| 3081 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 3082 | TypeLocBuilder TLB; |
| 3083 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 3084 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 3085 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3086 | TL.setNameLoc(NameInfo.getLoc()); |
| 3087 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 3088 | |
| 3089 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 3090 | // properly. |
| 3091 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 3092 | TemplateArgumentLocInfo(TSI)); |
| 3093 | |
| 3094 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3095 | } |
| 3096 | } |
| 3097 | // fallthrough |
| 3098 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3099 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3100 | // We have a template type parameter but the template argument |
| 3101 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 3102 | SourceRange SR = AL.getSourceRange(); |
| 3103 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3104 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3105 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3106 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3107 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3108 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3109 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3110 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3111 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3112 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3113 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3114 | ArgType = Context.getCanonicalType(ArgType); |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3115 | |
| 3116 | // Objective-C ARC: |
| 3117 | // If an explicitly-specified template argument type is a lifetime type |
| 3118 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3119 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3120 | ArgType->isObjCLifetimeType() && |
| 3121 | !ArgType.getObjCLifetime()) { |
| 3122 | Qualifiers Qs; |
| 3123 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 3124 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 3125 | } |
| 3126 | |
| 3127 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3128 | return false; |
| 3129 | } |
| 3130 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3131 | /// \brief Substitute template arguments into the default template argument for |
| 3132 | /// the given template type parameter. |
| 3133 | /// |
| 3134 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3135 | /// the substitution. |
| 3136 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3137 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3138 | /// for. |
| 3139 | /// |
| 3140 | /// \param TemplateLoc the location of the template name that started the |
| 3141 | /// template-id we are checking. |
| 3142 | /// |
| 3143 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3144 | /// terminates the template-id. |
| 3145 | /// |
| 3146 | /// \param Param the template template parameter whose default we are |
| 3147 | /// substituting into. |
| 3148 | /// |
| 3149 | /// \param Converted the list of template arguments provided for template |
| 3150 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3151 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3152 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3153 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3154 | TemplateDecl *Template, |
| 3155 | SourceLocation TemplateLoc, |
| 3156 | SourceLocation RAngleLoc, |
| 3157 | TemplateTypeParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3158 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3159 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3160 | |
| 3161 | // If the argument type is dependent, instantiate it now based |
| 3162 | // on the previously-computed template arguments. |
| 3163 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3164 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3165 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3166 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3167 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3168 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3169 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3170 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3171 | Converted.data(), Converted.size()); |
| 3172 | |
| 3173 | // Only substitute for the innermost template argument list. |
| 3174 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3175 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3176 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3177 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3178 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3179 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3180 | ArgType = |
| 3181 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 3182 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3183 | } |
| 3184 | |
| 3185 | return ArgType; |
| 3186 | } |
| 3187 | |
| 3188 | /// \brief Substitute template arguments into the default template argument for |
| 3189 | /// the given non-type template parameter. |
| 3190 | /// |
| 3191 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3192 | /// the substitution. |
| 3193 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3194 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3195 | /// for. |
| 3196 | /// |
| 3197 | /// \param TemplateLoc the location of the template name that started the |
| 3198 | /// template-id we are checking. |
| 3199 | /// |
| 3200 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3201 | /// terminates the template-id. |
| 3202 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3203 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3204 | /// substituting into. |
| 3205 | /// |
| 3206 | /// \param Converted the list of template arguments provided for template |
| 3207 | /// parameters that precede \p Param in the template parameter list. |
| 3208 | /// |
| 3209 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3210 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3211 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3212 | TemplateDecl *Template, |
| 3213 | SourceLocation TemplateLoc, |
| 3214 | SourceLocation RAngleLoc, |
| 3215 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3216 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3217 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3218 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3219 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3220 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3221 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3222 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3223 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3224 | Converted.data(), Converted.size()); |
| 3225 | |
| 3226 | // Only substitute for the innermost template argument list. |
| 3227 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3228 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3229 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3230 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3231 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3232 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
Eli Friedman | c25372b | 2012-04-26 22:43:24 +0000 | [diff] [blame] | 3233 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3234 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3237 | /// \brief Substitute template arguments into the default template argument for |
| 3238 | /// the given template template parameter. |
| 3239 | /// |
| 3240 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3241 | /// the substitution. |
| 3242 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3243 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3244 | /// for. |
| 3245 | /// |
| 3246 | /// \param TemplateLoc the location of the template name that started the |
| 3247 | /// template-id we are checking. |
| 3248 | /// |
| 3249 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3250 | /// terminates the template-id. |
| 3251 | /// |
| 3252 | /// \param Param the template template parameter whose default we are |
| 3253 | /// substituting into. |
| 3254 | /// |
| 3255 | /// \param Converted the list of template arguments provided for template |
| 3256 | /// parameters that precede \p Param in the template parameter list. |
| 3257 | /// |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3258 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 3259 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3260 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3261 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 3262 | static TemplateName |
| 3263 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3264 | TemplateDecl *Template, |
| 3265 | SourceLocation TemplateLoc, |
| 3266 | SourceLocation RAngleLoc, |
| 3267 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3268 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3269 | NestedNameSpecifierLoc &QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3270 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, Template, Converted, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3271 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3272 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3273 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3274 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3275 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3276 | Converted.data(), Converted.size()); |
| 3277 | |
| 3278 | // Only substitute for the innermost template argument list. |
| 3279 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3280 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3281 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3282 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3283 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3284 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3285 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3286 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3287 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3288 | QualifierLoc = |
| 3289 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3290 | if (!QualifierLoc) |
| 3291 | return TemplateName(); |
| 3292 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3293 | |
| 3294 | return SemaRef.SubstTemplateName( |
| 3295 | QualifierLoc, |
| 3296 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 3297 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 3298 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3299 | } |
| 3300 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3301 | /// \brief If the given template parameter has a default template |
| 3302 | /// argument, substitute into that default template argument and |
| 3303 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3304 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3305 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 3306 | SourceLocation TemplateLoc, |
| 3307 | SourceLocation RAngleLoc, |
| 3308 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3309 | SmallVectorImpl<TemplateArgument> |
| 3310 | &Converted, |
| 3311 | bool &HasDefaultArg) { |
| 3312 | HasDefaultArg = false; |
| 3313 | |
| 3314 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3315 | if (!TypeParm->hasDefaultArgument()) |
| 3316 | return TemplateArgumentLoc(); |
| 3317 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3318 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3319 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3320 | TemplateLoc, |
| 3321 | RAngleLoc, |
| 3322 | TypeParm, |
| 3323 | Converted); |
| 3324 | if (DI) |
| 3325 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 3326 | |
| 3327 | return TemplateArgumentLoc(); |
| 3328 | } |
| 3329 | |
| 3330 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 3331 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3332 | if (!NonTypeParm->hasDefaultArgument()) |
| 3333 | return TemplateArgumentLoc(); |
| 3334 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3335 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3336 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3337 | TemplateLoc, |
| 3338 | RAngleLoc, |
| 3339 | NonTypeParm, |
| 3340 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3341 | if (Arg.isInvalid()) |
| 3342 | return TemplateArgumentLoc(); |
| 3343 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3344 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3345 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 3346 | } |
| 3347 | |
| 3348 | TemplateTemplateParmDecl *TempTempParm |
| 3349 | = cast<TemplateTemplateParmDecl>(Param); |
| 3350 | if (!TempTempParm->hasDefaultArgument()) |
| 3351 | return TemplateArgumentLoc(); |
| 3352 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3353 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3354 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3355 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3356 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3357 | RAngleLoc, |
| 3358 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3359 | Converted, |
| 3360 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3361 | if (TName.isNull()) |
| 3362 | return TemplateArgumentLoc(); |
| 3363 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3364 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3365 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3366 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 3367 | } |
| 3368 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3369 | /// \brief Check that the given template argument corresponds to the given |
| 3370 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3371 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3372 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3373 | /// checked. |
| 3374 | /// |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3375 | /// \param Arg The template argument, which may be updated due to conversions. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3376 | /// |
| 3377 | /// \param Template The template in which the template argument resides. |
| 3378 | /// |
| 3379 | /// \param TemplateLoc The location of the template name for the template |
| 3380 | /// whose argument list we're matching. |
| 3381 | /// |
| 3382 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 3383 | /// the template argument list. |
| 3384 | /// |
| 3385 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 3386 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 3387 | /// |
| 3388 | /// \param Converted The checked, converted argument will be added to the |
| 3389 | /// end of this small vector. |
| 3390 | /// |
| 3391 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 3392 | /// explicitly written, deduced, etc. |
| 3393 | /// |
| 3394 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3395 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3396 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 3397 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3398 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3399 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3400 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3401 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3402 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3403 | // Check template type parameters. |
| 3404 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3405 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3406 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3407 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3408 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3409 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3410 | // with the template arguments we've seen thus far. But if the |
| 3411 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3412 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3413 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 3414 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3415 | |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3416 | if (NTTPType->isDependentType() && |
| 3417 | !isa<TemplateTemplateParmDecl>(Template) && |
| 3418 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3419 | // Do substitution on the type of the non-type template parameter. |
| 3420 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3421 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3422 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3423 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3424 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3425 | |
| 3426 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3427 | Converted.data(), Converted.size()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3428 | NTTPType = SubstType(NTTPType, |
| 3429 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 3430 | NTTP->getLocation(), |
| 3431 | NTTP->getDeclName()); |
| 3432 | // If that worked, check the non-type template parameter type |
| 3433 | // for validity. |
| 3434 | if (!NTTPType.isNull()) |
| 3435 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 3436 | NTTP->getLocation()); |
| 3437 | if (NTTPType.isNull()) |
| 3438 | return true; |
| 3439 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3440 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3441 | switch (Arg.getArgument().getKind()) { |
| 3442 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3443 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3444 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3445 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3446 | TemplateArgument Result; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3447 | ExprResult Res = |
| 3448 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 3449 | Result, CTAK); |
| 3450 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3451 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3452 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3453 | // If the resulting expression is new, then use it in place of the |
| 3454 | // old expression in the template argument. |
| 3455 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 3456 | TemplateArgument TA(Res.get()); |
| 3457 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 3458 | } |
| 3459 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3460 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3461 | break; |
| 3462 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3463 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3464 | case TemplateArgument::Declaration: |
| 3465 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3466 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3467 | // We've already checked this template argument, so just copy |
| 3468 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3469 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3470 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3471 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3472 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3473 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3474 | // We were given a template template argument. It may not be ill-formed; |
| 3475 | // see below. |
| 3476 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3477 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 3478 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3479 | // We have a template argument such as \c T::template X, which we |
| 3480 | // parsed as a template template argument. However, since we now |
| 3481 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3482 | // template name into an expression. |
| 3483 | |
| 3484 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 3485 | Arg.getTemplateNameLoc()); |
| 3486 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 3487 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3488 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3489 | // FIXME: the template-template arg was a DependentTemplateName, |
| 3490 | // so it was provided with a template keyword. However, its source |
| 3491 | // location is not stored in the template argument structure. |
| 3492 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3493 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 3494 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 3495 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3496 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3497 | // If we parsed the template argument as a pack expansion, create a |
| 3498 | // pack expansion expression. |
| 3499 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3500 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3501 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3502 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3503 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3504 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3505 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3506 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3507 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3508 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3509 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3510 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3511 | break; |
| 3512 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3513 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3514 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3515 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3516 | // therefore cannot be a non-type template argument. |
| 3517 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 3518 | << Arg.getSourceRange(); |
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 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3521 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3522 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3523 | case TemplateArgument::Type: { |
| 3524 | // We have a non-type template parameter but the template |
| 3525 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3526 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3527 | // C++ [temp.arg]p2: |
| 3528 | // In a template-argument, an ambiguity between a type-id and |
| 3529 | // an expression is resolved to a type-id, regardless of the |
| 3530 | // form of the corresponding template-parameter. |
| 3531 | // |
| 3532 | // We warn specifically about this case, since it can be rather |
| 3533 | // confusing for users. |
| 3534 | QualType T = Arg.getArgument().getAsType(); |
| 3535 | SourceRange SR = Arg.getSourceRange(); |
| 3536 | if (T->isFunctionType()) |
| 3537 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 3538 | else |
| 3539 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 3540 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3541 | return true; |
| 3542 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3543 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3544 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3545 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3546 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3547 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3548 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3552 | // Check template template parameters. |
| 3553 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3554 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3555 | // Substitute into the template parameter list of the template |
| 3556 | // template parameter, since previously-supplied template arguments |
| 3557 | // may appear within the template template parameter. |
| 3558 | { |
| 3559 | // Set up a template instantiation context. |
| 3560 | LocalInstantiationScope Scope(*this); |
| 3561 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3562 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3563 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3564 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3565 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3566 | |
| 3567 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3568 | Converted.data(), Converted.size()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3569 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3570 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3571 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 3572 | if (!TempParm) |
| 3573 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3574 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3575 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3576 | switch (Arg.getArgument().getKind()) { |
| 3577 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3578 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3579 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3580 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3581 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3582 | if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3583 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3584 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3585 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3586 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3587 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3588 | case TemplateArgument::Expression: |
| 3589 | case TemplateArgument::Type: |
| 3590 | // We have a template template parameter but the template |
| 3591 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3592 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3593 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3594 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3595 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3596 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3597 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3598 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3599 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3600 | case TemplateArgument::NullPtr: |
| 3601 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3602 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3603 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3604 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3605 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3606 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3607 | return false; |
| 3608 | } |
| 3609 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3610 | /// \brief Diagnose an arity mismatch in the |
| 3611 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 3612 | SourceLocation TemplateLoc, |
| 3613 | TemplateArgumentListInfo &TemplateArgs) { |
| 3614 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 3615 | unsigned NumParams = Params->size(); |
| 3616 | unsigned NumArgs = TemplateArgs.size(); |
| 3617 | |
| 3618 | SourceRange Range; |
| 3619 | if (NumArgs > NumParams) |
| 3620 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
| 3621 | TemplateArgs.getRAngleLoc()); |
| 3622 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3623 | << (NumArgs > NumParams) |
| 3624 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3625 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3626 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3627 | << Template << Range; |
| 3628 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3629 | << Params->getSourceRange(); |
| 3630 | return true; |
| 3631 | } |
| 3632 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3633 | /// \brief Check whether the template parameter is a pack expansion, and if so, |
| 3634 | /// determine the number of parameters produced by that expansion. For instance: |
| 3635 | /// |
| 3636 | /// \code |
| 3637 | /// template<typename ...Ts> struct A { |
| 3638 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 3639 | /// }; |
| 3640 | /// \endcode |
| 3641 | /// |
| 3642 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 3643 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3644 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3645 | if (NonTypeTemplateParmDecl *NTTP |
| 3646 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3647 | if (NTTP->isExpandedParameterPack()) |
| 3648 | return NTTP->getNumExpansionTypes(); |
| 3649 | } |
| 3650 | |
| 3651 | if (TemplateTemplateParmDecl *TTP |
| 3652 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 3653 | if (TTP->isExpandedParameterPack()) |
| 3654 | return TTP->getNumExpansionTemplateParameters(); |
| 3655 | } |
| 3656 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 3657 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3658 | } |
| 3659 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3660 | /// \brief Check that the given template argument list is well-formed |
| 3661 | /// for specializing the given template. |
| 3662 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 3663 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3664 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 3665 | bool PartialTemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3666 | SmallVectorImpl<TemplateArgument> &Converted) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3667 | // Make a copy of the template arguments for processing. Only make the |
| 3668 | // changes at the end when successful in matching the arguments to the |
| 3669 | // template. |
| 3670 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 3671 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3672 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3673 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3674 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3675 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3676 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3677 | // [...] The type and form of each template-argument specified in |
| 3678 | // a template-id shall match the type and form specified for the |
| 3679 | // corresponding parameter declared by the template in its |
| 3680 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3681 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3682 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3683 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3684 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3685 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 3686 | ParamEnd = Params->end(); |
| 3687 | Param != ParamEnd; /* increment in loop */) { |
| 3688 | // If we have an expanded parameter pack, make sure we don't have too |
| 3689 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3690 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3691 | if (*Expansions == ArgumentPack.size()) { |
| 3692 | // We're done with this parameter pack. Pack up its arguments and add |
| 3693 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3694 | Converted.push_back( |
| 3695 | TemplateArgument::CreatePackCopy(Context, |
| 3696 | ArgumentPack.data(), |
| 3697 | ArgumentPack.size())); |
| 3698 | ArgumentPack.clear(); |
| 3699 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3700 | // This argument is assigned to the next parameter. |
| 3701 | ++Param; |
| 3702 | continue; |
| 3703 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 3704 | // Not enough arguments for this parameter pack. |
| 3705 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3706 | << false |
| 3707 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3708 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3709 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3710 | << Template; |
| 3711 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3712 | << Params->getSourceRange(); |
| 3713 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3714 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3715 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3716 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3717 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3718 | // Check the template argument we were given. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3719 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3720 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3721 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3722 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3723 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3724 | bool PackExpansionIntoNonPack = |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3725 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3726 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 3727 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3728 | // 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] | 3729 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3730 | // can't be canonicalized, so reject it now. |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3731 | Diag(NewArgs[ArgIdx].getLocation(), |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3732 | diag::err_alias_template_expansion_into_fixed_list) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3733 | << NewArgs[ArgIdx].getSourceRange(); |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3734 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 3735 | return true; |
| 3736 | } |
| 3737 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3738 | // We're now done with this argument. |
| 3739 | ++ArgIdx; |
| 3740 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3741 | if ((*Param)->isTemplateParameterPack()) { |
| 3742 | // The template parameter was a template parameter pack, so take the |
| 3743 | // deduced argument and place it on the argument pack. Note that we |
| 3744 | // stay on the same template parameter so that we can deduce more |
| 3745 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 3746 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3747 | } else { |
| 3748 | // Move to the next template parameter. |
| 3749 | ++Param; |
| 3750 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3751 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame] | 3752 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 3753 | // the remaining arguments, because we don't know what parameters they'll |
| 3754 | // match up with. |
| 3755 | if (PackExpansionIntoNonPack) { |
| 3756 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3757 | // If we were part way through filling in an expanded parameter pack, |
| 3758 | // fall back to just producing individual arguments. |
| 3759 | Converted.insert(Converted.end(), |
| 3760 | ArgumentPack.begin(), ArgumentPack.end()); |
| 3761 | ArgumentPack.clear(); |
| 3762 | } |
| 3763 | |
| 3764 | while (ArgIdx < NumArgs) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3765 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3766 | ++ArgIdx; |
| 3767 | } |
| 3768 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3769 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3770 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3771 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3772 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3773 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3774 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3775 | // If we're checking a partial template argument list, we're done. |
| 3776 | if (PartialTemplateArgs) { |
| 3777 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 3778 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3779 | ArgumentPack.data(), |
| 3780 | ArgumentPack.size())); |
| 3781 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3782 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3783 | } |
| 3784 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3785 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3786 | // 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] | 3787 | if ((*Param)->isTemplateParameterPack()) { |
| 3788 | assert(!getExpandedPackSize(*Param) && |
| 3789 | "Should have dealt with this already"); |
| 3790 | |
| 3791 | // A non-expanded parameter pack before the end of the parameter list |
| 3792 | // only occurs for an ill-formed template parameter list, unless we've |
| 3793 | // got a partial argument list for a function template, so just bail out. |
| 3794 | if (Param + 1 != ParamEnd) |
| 3795 | return true; |
| 3796 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3797 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3798 | ArgumentPack.data(), |
| 3799 | ArgumentPack.size())); |
| 3800 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3801 | |
| 3802 | ++Param; |
| 3803 | continue; |
| 3804 | } |
| 3805 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3806 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3807 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3808 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3809 | // Retrieve the default template argument from the template |
| 3810 | // parameter. For each kind of template parameter, we substitute the |
| 3811 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3812 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3813 | // the default argument. |
| 3814 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3815 | if (!TTP->hasDefaultArgument()) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3816 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3817 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3818 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3819 | Template, |
| 3820 | TemplateLoc, |
| 3821 | RAngleLoc, |
| 3822 | TTP, |
| 3823 | Converted); |
| 3824 | if (!ArgType) |
| 3825 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3826 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3827 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 3828 | ArgType); |
| 3829 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3830 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3831 | if (!NTTP->hasDefaultArgument()) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3832 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3833 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3834 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3835 | TemplateLoc, |
| 3836 | RAngleLoc, |
| 3837 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3838 | Converted); |
| 3839 | if (E.isInvalid()) |
| 3840 | return true; |
| 3841 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3842 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3843 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 3844 | } else { |
| 3845 | TemplateTemplateParmDecl *TempParm |
| 3846 | = cast<TemplateTemplateParmDecl>(*Param); |
| 3847 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3848 | if (!TempParm->hasDefaultArgument()) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3849 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3850 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3851 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3852 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3853 | TemplateLoc, |
| 3854 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3855 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3856 | Converted, |
| 3857 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3858 | if (Name.isNull()) |
| 3859 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3860 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3861 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3862 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3863 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3864 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3865 | // Introduce an instantiation record that describes where we are using |
| 3866 | // the default template argument. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3867 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 3868 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3869 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3870 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3871 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3872 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3873 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3874 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3875 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3876 | |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3877 | // Core issue 150 (assumed resolution): if this is a template template |
| 3878 | // parameter, keep track of the default template arguments from the |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3879 | // template definition. |
| 3880 | if (isTemplateTemplateParameter) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3881 | NewArgs.addArgument(Arg); |
| 3882 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3883 | // Move to the next template parameter and argument. |
| 3884 | ++Param; |
| 3885 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3886 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3887 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 3888 | // If we're performing a partial argument substitution, allow any trailing |
| 3889 | // pack expansions; they might be empty. This can happen even if |
| 3890 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 3891 | // still dependent). |
| 3892 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 3893 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3894 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 3895 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 3896 | } |
| 3897 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3898 | // If we have any leftover arguments, then there were too many arguments. |
| 3899 | // Complain and fail. |
| 3900 | if (ArgIdx < NumArgs) |
Richard Trieu | 15b6653 | 2015-01-24 02:48:32 +0000 | [diff] [blame] | 3901 | return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs); |
| 3902 | |
| 3903 | // No problems found with the new argument list, propagate changes back |
| 3904 | // to caller. |
| 3905 | TemplateArgs = NewArgs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3906 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3907 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3910 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3911 | class UnnamedLocalNoLinkageFinder |
| 3912 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3913 | { |
| 3914 | Sema &S; |
| 3915 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3916 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3917 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3918 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3919 | public: |
| 3920 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3921 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3922 | bool Visit(QualType T) { |
| 3923 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3924 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3925 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3926 | #define TYPE(Class, Parent) \ |
| 3927 | bool Visit##Class##Type(const Class##Type *); |
| 3928 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 3929 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3930 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 3931 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3932 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3933 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3934 | bool VisitTagDecl(const TagDecl *Tag); |
| 3935 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3936 | }; |
| 3937 | } |
| 3938 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3939 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3940 | return false; |
| 3941 | } |
| 3942 | |
| 3943 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3944 | return Visit(T->getElementType()); |
| 3945 | } |
| 3946 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3947 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3948 | return Visit(T->getPointeeType()); |
| 3949 | } |
| 3950 | |
| 3951 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3952 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3953 | return Visit(T->getPointeeType()); |
| 3954 | } |
| 3955 | |
| 3956 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3957 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3958 | return Visit(T->getPointeeType()); |
| 3959 | } |
| 3960 | |
| 3961 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3962 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3963 | return Visit(T->getPointeeType()); |
| 3964 | } |
| 3965 | |
| 3966 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3967 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3968 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3969 | } |
| 3970 | |
| 3971 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3972 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3973 | return Visit(T->getElementType()); |
| 3974 | } |
| 3975 | |
| 3976 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3977 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3978 | return Visit(T->getElementType()); |
| 3979 | } |
| 3980 | |
| 3981 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3982 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3983 | return Visit(T->getElementType()); |
| 3984 | } |
| 3985 | |
| 3986 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3987 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3988 | return Visit(T->getElementType()); |
| 3989 | } |
| 3990 | |
| 3991 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3992 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3993 | return Visit(T->getElementType()); |
| 3994 | } |
| 3995 | |
| 3996 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3997 | return Visit(T->getElementType()); |
| 3998 | } |
| 3999 | |
| 4000 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 4001 | return Visit(T->getElementType()); |
| 4002 | } |
| 4003 | |
| 4004 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 4005 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 4006 | for (const auto &A : T->param_types()) { |
| 4007 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4008 | return true; |
| 4009 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4010 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4011 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4012 | } |
| 4013 | |
| 4014 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 4015 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4016 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
| 4019 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 4020 | const UnresolvedUsingType*) { |
| 4021 | return false; |
| 4022 | } |
| 4023 | |
| 4024 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 4025 | return false; |
| 4026 | } |
| 4027 | |
| 4028 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 4029 | return Visit(T->getUnderlyingType()); |
| 4030 | } |
| 4031 | |
| 4032 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 4033 | return false; |
| 4034 | } |
| 4035 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4036 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 4037 | const UnaryTransformType*) { |
| 4038 | return false; |
| 4039 | } |
| 4040 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4041 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 4042 | return Visit(T->getDeducedType()); |
| 4043 | } |
| 4044 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4045 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 4046 | return VisitTagDecl(T->getDecl()); |
| 4047 | } |
| 4048 | |
| 4049 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 4050 | return VisitTagDecl(T->getDecl()); |
| 4051 | } |
| 4052 | |
| 4053 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 4054 | const TemplateTypeParmType*) { |
| 4055 | return false; |
| 4056 | } |
| 4057 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4058 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 4059 | const SubstTemplateTypeParmPackType *) { |
| 4060 | return false; |
| 4061 | } |
| 4062 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4063 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 4064 | const TemplateSpecializationType*) { |
| 4065 | return false; |
| 4066 | } |
| 4067 | |
| 4068 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 4069 | const InjectedClassNameType* T) { |
| 4070 | return VisitTagDecl(T->getDecl()); |
| 4071 | } |
| 4072 | |
| 4073 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 4074 | const DependentNameType* T) { |
| 4075 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4076 | } |
| 4077 | |
| 4078 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 4079 | const DependentTemplateSpecializationType* T) { |
| 4080 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4081 | } |
| 4082 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4083 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 4084 | const PackExpansionType* T) { |
| 4085 | return Visit(T->getPattern()); |
| 4086 | } |
| 4087 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4088 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 4089 | return false; |
| 4090 | } |
| 4091 | |
| 4092 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 4093 | const ObjCInterfaceType *) { |
| 4094 | return false; |
| 4095 | } |
| 4096 | |
| 4097 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 4098 | const ObjCObjectPointerType *) { |
| 4099 | return false; |
| 4100 | } |
| 4101 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4102 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 4103 | return Visit(T->getValueType()); |
| 4104 | } |
| 4105 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4106 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 4107 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4108 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4109 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4110 | diag::warn_cxx98_compat_template_arg_local_type : |
| 4111 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4112 | << S.Context.getTypeDeclType(Tag) << SR; |
| 4113 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4114 | } |
| 4115 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 4116 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4117 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4118 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4119 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 4120 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4121 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 4122 | return true; |
| 4123 | } |
| 4124 | |
| 4125 | return false; |
| 4126 | } |
| 4127 | |
| 4128 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 4129 | NestedNameSpecifier *NNS) { |
| 4130 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 4131 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4132 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4133 | switch (NNS->getKind()) { |
| 4134 | case NestedNameSpecifier::Identifier: |
| 4135 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4136 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4137 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 4138 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4139 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4140 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4141 | case NestedNameSpecifier::TypeSpec: |
| 4142 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 4143 | return Visit(QualType(NNS->getAsType(), 0)); |
| 4144 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4145 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
| 4148 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4149 | /// \brief Check a template argument against its corresponding |
| 4150 | /// template type parameter. |
| 4151 | /// |
| 4152 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 4153 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4154 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4155 | TypeSourceInfo *ArgInfo) { |
| 4156 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4157 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 4158 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 4159 | |
| 4160 | if (Arg->isVariablyModifiedType()) { |
| 4161 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4162 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4163 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4166 | // C++03 [temp.arg.type]p2: |
| 4167 | // A local type, a type with no linkage, an unnamed type or a type |
| 4168 | // compounded from any of these types shall not be used as a |
| 4169 | // template-argument for a template type-parameter. |
| 4170 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4171 | // 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] | 4172 | // a warning. |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 4173 | bool NeedsCheck; |
| 4174 | if (LangOpts.CPlusPlus11) |
| 4175 | NeedsCheck = |
| 4176 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 4177 | SR.getBegin()) || |
| 4178 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_local_type, |
| 4179 | SR.getBegin()); |
| 4180 | else |
| 4181 | NeedsCheck = Arg->hasUnnamedOrLocalType(); |
| 4182 | |
| 4183 | if (NeedsCheck) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4184 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 4185 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 4186 | } |
| 4187 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4188 | return false; |
| 4189 | } |
| 4190 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4191 | enum NullPointerValueKind { |
| 4192 | NPV_NotNullPointer, |
| 4193 | NPV_NullPointer, |
| 4194 | NPV_Error |
| 4195 | }; |
| 4196 | |
| 4197 | /// \brief Determine whether the given template argument is a null pointer |
| 4198 | /// value of the appropriate type. |
| 4199 | static NullPointerValueKind |
| 4200 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 4201 | QualType ParamType, Expr *Arg) { |
| 4202 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 4203 | return NPV_NotNullPointer; |
| 4204 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 4205 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4206 | return NPV_NotNullPointer; |
| 4207 | |
| 4208 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4209 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 4210 | if (ArgRV.isInvalid()) |
| 4211 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4212 | Arg = ArgRV.get(); |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4213 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4214 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4215 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4216 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4217 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4218 | EvalResult.HasSideEffects) { |
| 4219 | SourceLocation DiagLoc = Arg->getExprLoc(); |
| 4220 | |
| 4221 | // If our only note is the usual "invalid subexpression" note, just point |
| 4222 | // the caret at its location rather than producing an essentially |
| 4223 | // redundant note. |
| 4224 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 4225 | diag::note_invalid_subexpr_in_const_expr) { |
| 4226 | DiagLoc = Notes[0].first; |
| 4227 | Notes.clear(); |
| 4228 | } |
| 4229 | |
| 4230 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 4231 | << Arg->getType() << Arg->getSourceRange(); |
| 4232 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 4233 | S.Diag(Notes[I].first, Notes[I].second); |
| 4234 | |
| 4235 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4236 | return NPV_Error; |
| 4237 | } |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4238 | |
| 4239 | // C++11 [temp.arg.nontype]p1: |
| 4240 | // - an address constant expression of type std::nullptr_t |
| 4241 | if (Arg->getType()->isNullPtrType()) |
| 4242 | return NPV_NullPointer; |
| 4243 | |
| 4244 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 4245 | // - a constant expression that evaluates to a null member pointer value |
| 4246 | // (4.11); or |
| 4247 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 4248 | (EvalResult.Val.isMemberPointer() && |
| 4249 | !EvalResult.Val.getMemberPointerDecl())) { |
| 4250 | // If our expression has an appropriate type, we've succeeded. |
| 4251 | bool ObjCLifetimeConversion; |
| 4252 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 4253 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 4254 | ObjCLifetimeConversion)) |
| 4255 | return NPV_NullPointer; |
| 4256 | |
| 4257 | // The types didn't match, but we know we got a null pointer; complain, |
| 4258 | // then recover as if the types were correct. |
| 4259 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 4260 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4261 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4262 | return NPV_NullPointer; |
| 4263 | } |
| 4264 | |
| 4265 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 4266 | // constant, suggest a cast to the appropriate type. |
| 4267 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 4268 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 4269 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4270 | << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 4271 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()), |
| 4272 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4273 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4274 | return NPV_NullPointer; |
| 4275 | } |
| 4276 | |
| 4277 | // FIXME: If we ever want to support general, address-constant expressions |
| 4278 | // as non-type template arguments, we should return the ExprResult here to |
| 4279 | // be interpreted by the caller. |
| 4280 | return NPV_NotNullPointer; |
| 4281 | } |
| 4282 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4283 | /// \brief Checks whether the given template argument is compatible with its |
| 4284 | /// template parameter. |
| 4285 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 4286 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 4287 | Expr *Arg, QualType ArgType) { |
| 4288 | bool ObjCLifetimeConversion; |
| 4289 | if (ParamType->isPointerType() && |
| 4290 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 4291 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 4292 | ObjCLifetimeConversion)) { |
| 4293 | // For pointer-to-object types, qualification conversions are |
| 4294 | // permitted. |
| 4295 | } else { |
| 4296 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 4297 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 4298 | // C++ [temp.arg.nontype]p5b3: |
| 4299 | // For a non-type template-parameter of type reference to |
| 4300 | // object, no conversions apply. The type referred to by the |
| 4301 | // reference may be more cv-qualified than the (otherwise |
| 4302 | // identical) type of the template- argument. The |
| 4303 | // template-parameter is bound directly to the |
| 4304 | // template-argument, which shall be an lvalue. |
| 4305 | |
| 4306 | // FIXME: Other qualifiers? |
| 4307 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 4308 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 4309 | |
| 4310 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 4311 | S.Diag(Arg->getLocStart(), |
| 4312 | diag::err_template_arg_ref_bind_ignores_quals) |
| 4313 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 4314 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4315 | return true; |
| 4316 | } |
| 4317 | } |
| 4318 | } |
| 4319 | |
| 4320 | // At this point, the template argument refers to an object or |
| 4321 | // function with external linkage. We now need to check whether the |
| 4322 | // argument and parameter types are compatible. |
| 4323 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 4324 | ParamType.getNonReferenceType())) { |
| 4325 | // We can't perform this conversion or binding. |
| 4326 | if (ParamType->isReferenceType()) |
| 4327 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 4328 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
| 4329 | else |
| 4330 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4331 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
| 4332 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4333 | return true; |
| 4334 | } |
| 4335 | } |
| 4336 | |
| 4337 | return false; |
| 4338 | } |
| 4339 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4340 | /// \brief Checks whether the given template argument is the address |
| 4341 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4342 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4343 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 4344 | NonTypeTemplateParmDecl *Param, |
| 4345 | QualType ParamType, |
| 4346 | Expr *ArgIn, |
| 4347 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4348 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4349 | Expr *Arg = ArgIn; |
| 4350 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4351 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4352 | bool AddressTaken = false; |
| 4353 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4354 | if (S.getLangOpts().MicrosoftExt) { |
| 4355 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 4356 | // dereference and address-of operators. |
| 4357 | Arg = Arg->IgnoreParenCasts(); |
| 4358 | |
| 4359 | bool ExtWarnMSTemplateArg = false; |
| 4360 | UnaryOperatorKind FirstOpKind; |
| 4361 | SourceLocation FirstOpLoc; |
| 4362 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4363 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 4364 | if (UnOpKind == UO_Deref) |
| 4365 | ExtWarnMSTemplateArg = true; |
| 4366 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 4367 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 4368 | if (!AddrOpLoc.isValid()) { |
| 4369 | FirstOpKind = UnOpKind; |
| 4370 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 4371 | } |
| 4372 | } else |
| 4373 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4374 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4375 | if (FirstOpLoc.isValid()) { |
| 4376 | if (ExtWarnMSTemplateArg) |
| 4377 | S.Diag(ArgIn->getLocStart(), diag::ext_ms_deref_template_argument) |
| 4378 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4379 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4380 | if (FirstOpKind == UO_AddrOf) |
| 4381 | AddressTaken = true; |
| 4382 | else if (Arg->getType()->isPointerType()) { |
| 4383 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 4384 | // constant expression. |
| 4385 | assert(FirstOpKind == UO_Deref); |
| 4386 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4387 | << Arg->getSourceRange(); |
| 4388 | } |
| 4389 | } |
| 4390 | } else { |
| 4391 | // See through any implicit casts we added to fix the type. |
| 4392 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4393 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4394 | // C++ [temp.arg.nontype]p1: |
| 4395 | // |
| 4396 | // A template-argument for a non-type, non-template |
| 4397 | // template-parameter shall be one of: [...] |
| 4398 | // |
| 4399 | // -- the address of an object or function with external |
| 4400 | // linkage, including function templates and function |
| 4401 | // template-ids but excluding non-static class members, |
| 4402 | // expressed as & id-expression where the & is optional if |
| 4403 | // the name refers to a function or array, or if the |
| 4404 | // corresponding template-parameter is a reference; or |
| 4405 | |
| 4406 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4407 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4408 | bool ExtraParens = false; |
| 4409 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 4410 | if (!Invalid && !ExtraParens) { |
| 4411 | S.Diag(Arg->getLocStart(), |
| 4412 | S.getLangOpts().CPlusPlus11 |
| 4413 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 4414 | : diag::ext_template_arg_extra_parens) |
| 4415 | << Arg->getSourceRange(); |
| 4416 | ExtraParens = true; |
| 4417 | } |
| 4418 | |
| 4419 | Arg = Parens->getSubExpr(); |
| 4420 | } |
| 4421 | |
| 4422 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4423 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4424 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4425 | |
| 4426 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4427 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 4428 | Arg = UnOp->getSubExpr(); |
| 4429 | AddressTaken = true; |
| 4430 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 4431 | } |
| 4432 | } |
| 4433 | |
| 4434 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4435 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4436 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4437 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4438 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4439 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 4440 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 4441 | |
| 4442 | // If our parameter has pointer type, check for a null template value. |
| 4443 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 4444 | NullPointerValueKind NPV; |
| 4445 | // dllimport'd entities aren't constant but are available inside of template |
| 4446 | // arguments. |
| 4447 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 4448 | NPV = NPV_NotNullPointer; |
| 4449 | else |
| 4450 | NPV = isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn); |
| 4451 | switch (NPV) { |
| 4452 | case NPV_NullPointer: |
| 4453 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4454 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4455 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4456 | return false; |
| 4457 | |
| 4458 | case NPV_Error: |
| 4459 | return true; |
| 4460 | |
| 4461 | case NPV_NotNullPointer: |
| 4462 | break; |
| 4463 | } |
| 4464 | } |
| 4465 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4466 | // Stop checking the precise nature of the argument if it is value dependent, |
| 4467 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4468 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4469 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4470 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4471 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4472 | |
| 4473 | if (isa<CXXUuidofExpr>(Arg)) { |
| 4474 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 4475 | ArgIn, Arg, ArgType)) |
| 4476 | return true; |
| 4477 | |
| 4478 | Converted = TemplateArgument(ArgIn); |
| 4479 | return false; |
| 4480 | } |
| 4481 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4482 | if (!DRE) { |
| 4483 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4484 | << Arg->getSourceRange(); |
| 4485 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4486 | return true; |
| 4487 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4488 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4489 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4490 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4491 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4492 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4493 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4494 | return true; |
| 4495 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4496 | |
| 4497 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4498 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4499 | if (!Method->isStatic()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4500 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4501 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4502 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4503 | return true; |
| 4504 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4505 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4506 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4507 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 4508 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4509 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4510 | // A non-type template argument must refer to an object or function. |
| 4511 | if (!Func && !Var) { |
| 4512 | // We found something, but we don't know specifically what it is. |
| 4513 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 4514 | << Arg->getSourceRange(); |
| 4515 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 4516 | return true; |
| 4517 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4518 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4519 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4520 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4521 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4522 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 4523 | diag::ext_template_arg_object_internal) |
| 4524 | << !Func << Entity << Arg->getSourceRange(); |
| 4525 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4526 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4527 | } else if (!Entity->hasLinkage()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4528 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 4529 | << !Func << Entity << Arg->getSourceRange(); |
| 4530 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4531 | << !Func; |
| 4532 | return true; |
| 4533 | } |
| 4534 | |
| 4535 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4536 | // If the template parameter has pointer type, the function decays. |
| 4537 | if (ParamType->isPointerType() && !AddressTaken) |
| 4538 | ArgType = S.Context.getPointerType(Func->getType()); |
| 4539 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 4540 | // If we originally had an address-of operator, but the |
| 4541 | // parameter has reference type, complain and (if things look |
| 4542 | // like they will work) drop the address-of operator. |
| 4543 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 4544 | ParamType.getNonReferenceType())) { |
| 4545 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4546 | << ParamType; |
| 4547 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4548 | return true; |
| 4549 | } |
| 4550 | |
| 4551 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4552 | << ParamType |
| 4553 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4554 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4555 | |
| 4556 | ArgType = Func->getType(); |
| 4557 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4558 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4559 | // A value of reference type is not an object. |
| 4560 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4561 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4562 | diag::err_template_arg_reference_var) |
| 4563 | << Var->getType() << Arg->getSourceRange(); |
| 4564 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4565 | return true; |
| 4566 | } |
| 4567 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4568 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 4569 | if (Var->getTLSKind()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4570 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 4571 | << Arg->getSourceRange(); |
| 4572 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 4573 | return true; |
| 4574 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4575 | |
| 4576 | // If the template parameter has pointer type, we must have taken |
| 4577 | // the address of this object. |
| 4578 | if (ParamType->isReferenceType()) { |
| 4579 | if (AddressTaken) { |
| 4580 | // If we originally had an address-of operator, but the |
| 4581 | // parameter has reference type, complain and (if things look |
| 4582 | // like they will work) drop the address-of operator. |
| 4583 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 4584 | ParamType.getNonReferenceType())) { |
| 4585 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4586 | << ParamType; |
| 4587 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4588 | return true; |
| 4589 | } |
| 4590 | |
| 4591 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4592 | << ParamType |
| 4593 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4594 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4595 | |
| 4596 | ArgType = Var->getType(); |
| 4597 | } |
| 4598 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 4599 | if (Var->getType()->isArrayType()) { |
| 4600 | // Array-to-pointer decay. |
| 4601 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 4602 | } else { |
| 4603 | // If the template parameter has pointer type but the address of |
| 4604 | // this object was not taken, complain and (possibly) recover by |
| 4605 | // taking the address of the entity. |
| 4606 | ArgType = S.Context.getPointerType(Var->getType()); |
| 4607 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 4608 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4609 | << ParamType; |
| 4610 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4611 | return true; |
| 4612 | } |
| 4613 | |
| 4614 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4615 | << ParamType |
| 4616 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 4617 | |
| 4618 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4619 | } |
| 4620 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4621 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4622 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4623 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 4624 | Arg, ArgType)) |
| 4625 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4626 | |
| 4627 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4628 | Converted = |
| 4629 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 4630 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4631 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4632 | } |
| 4633 | |
| 4634 | /// \brief Checks whether the given template argument is a pointer to |
| 4635 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4636 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 4637 | NonTypeTemplateParmDecl *Param, |
| 4638 | QualType ParamType, |
| 4639 | Expr *&ResultArg, |
| 4640 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4641 | bool Invalid = false; |
| 4642 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4643 | // Check for a null pointer value. |
| 4644 | Expr *Arg = ResultArg; |
| 4645 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 4646 | case NPV_Error: |
| 4647 | return true; |
| 4648 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 4649 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4650 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4651 | /*isNullPtr*/true); |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 4652 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 4653 | S.RequireCompleteType(Arg->getExprLoc(), ParamType, 0); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4654 | return false; |
| 4655 | case NPV_NotNullPointer: |
| 4656 | break; |
| 4657 | } |
| 4658 | |
| 4659 | bool ObjCLifetimeConversion; |
| 4660 | if (S.IsQualificationConversion(Arg->getType(), |
| 4661 | ParamType.getNonReferenceType(), |
| 4662 | false, ObjCLifetimeConversion)) { |
| 4663 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4664 | Arg->getValueKind()).get(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4665 | ResultArg = Arg; |
| 4666 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), |
| 4667 | ParamType.getNonReferenceType())) { |
| 4668 | // We can't perform this conversion. |
| 4669 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4670 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4671 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4672 | return true; |
| 4673 | } |
| 4674 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4675 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4676 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4677 | Arg = Cast->getSubExpr(); |
| 4678 | |
| 4679 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4680 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4681 | // A template-argument for a non-type, non-template |
| 4682 | // template-parameter shall be one of: [...] |
| 4683 | // |
| 4684 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4685 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4686 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4687 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4688 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4689 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4690 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4691 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4692 | S.Diag(Arg->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4693 | S.getLangOpts().CPlusPlus11 ? |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4694 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 4695 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4696 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4697 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
| 4700 | Arg = Parens->getSubExpr(); |
| 4701 | } |
| 4702 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4703 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4704 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4705 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4706 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4707 | // A pointer-to-member constant written &Class::member. |
| 4708 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4709 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4710 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 4711 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4712 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4713 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4714 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4715 | // A constant of pointer-to-member type. |
| 4716 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 4717 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 4718 | if (VD->getType()->isMemberPointerType()) { |
David Majnemer | cd053cd | 2013-12-10 00:40:58 +0000 | [diff] [blame] | 4719 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4720 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4721 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4722 | } else { |
| 4723 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4724 | Converted = TemplateArgument(VD, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4725 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4726 | return Invalid; |
| 4727 | } |
| 4728 | } |
| 4729 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4730 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4731 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4732 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4733 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4734 | if (!DRE) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4735 | return S.Diag(Arg->getLocStart(), |
| 4736 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4737 | << Arg->getSourceRange(); |
| 4738 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4739 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 4740 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 4741 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4742 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4743 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4744 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 4745 | "Only non-static member pointers can make it here"); |
| 4746 | |
| 4747 | // Okay: this is the address of a non-static member, and therefore |
| 4748 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4749 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4750 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4751 | } else { |
| 4752 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4753 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4754 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4755 | return Invalid; |
| 4756 | } |
| 4757 | |
| 4758 | // 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] | 4759 | S.Diag(Arg->getLocStart(), |
| 4760 | diag::err_template_arg_not_pointer_to_member_form) |
| 4761 | << Arg->getSourceRange(); |
| 4762 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4763 | return true; |
| 4764 | } |
| 4765 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4766 | /// \brief Check a template argument against its corresponding |
| 4767 | /// non-type template parameter. |
| 4768 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4769 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4770 | /// If an error occurred, it returns ExprError(); otherwise, it |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4771 | /// returns the converted template argument. \p ParamType is the |
| 4772 | /// type of the non-type template parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4773 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4774 | QualType ParamType, Expr *Arg, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4775 | TemplateArgument &Converted, |
| 4776 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4777 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4778 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4779 | // If either the parameter has a dependent type or the argument is |
| 4780 | // type-dependent, there's nothing we can check now. |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4781 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4782 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 4783 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4784 | return Arg; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4785 | } |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4786 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4787 | // We should have already dropped all cv-qualifiers by now. |
| 4788 | assert(!ParamType.hasQualifiers() && |
| 4789 | "non-type template parameter type cannot be qualified"); |
| 4790 | |
| 4791 | if (CTAK == CTAK_Deduced && |
| 4792 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 4793 | // C++ [temp.deduct.type]p17: |
| 4794 | // If, in the declaration of a function template with a non-type |
| 4795 | // template-parameter, the non-type template-parameter is used |
| 4796 | // in an expression in the function parameter-list and, if the |
| 4797 | // corresponding template-argument is deduced, the |
| 4798 | // template-argument type shall match the type of the |
| 4799 | // template-parameter exactly, except that a template-argument |
| 4800 | // deduced from an array bound may be of any integral type. |
| 4801 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 4802 | << Arg->getType().getUnqualifiedType() |
| 4803 | << ParamType.getUnqualifiedType(); |
| 4804 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4805 | return ExprError(); |
| 4806 | } |
| 4807 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4808 | if (getLangOpts().CPlusPlus1z) { |
| 4809 | // FIXME: We can do some limited checking for a value-dependent but not |
| 4810 | // type-dependent argument. |
| 4811 | if (Arg->isValueDependent()) { |
| 4812 | Converted = TemplateArgument(Arg); |
| 4813 | return Arg; |
| 4814 | } |
| 4815 | |
| 4816 | // C++1z [temp.arg.nontype]p1: |
| 4817 | // A template-argument for a non-type template parameter shall be |
| 4818 | // a converted constant expression of the type of the template-parameter. |
| 4819 | APValue Value; |
| 4820 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 4821 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 4822 | if (ArgResult.isInvalid()) |
| 4823 | return ExprError(); |
| 4824 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4825 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 4826 | |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4827 | // Convert the APValue to a TemplateArgument. |
| 4828 | switch (Value.getKind()) { |
| 4829 | case APValue::Uninitialized: |
| 4830 | assert(ParamType->isNullPtrType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4831 | Converted = TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4832 | break; |
| 4833 | case APValue::Int: |
| 4834 | assert(ParamType->isIntegralOrEnumerationType()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4835 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4836 | break; |
| 4837 | case APValue::MemberPointer: { |
| 4838 | assert(ParamType->isMemberPointerType()); |
| 4839 | |
| 4840 | // FIXME: We need TemplateArgument representation and mangling for these. |
| 4841 | if (!Value.getMemberPointerPath().empty()) { |
| 4842 | Diag(Arg->getLocStart(), |
| 4843 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 4844 | << Value.getMemberPointerDecl() << ParamType |
| 4845 | << Arg->getSourceRange(); |
| 4846 | return ExprError(); |
| 4847 | } |
| 4848 | |
| 4849 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4850 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 4851 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4852 | break; |
| 4853 | } |
| 4854 | case APValue::LValue: { |
| 4855 | // For a non-type template-parameter of pointer or reference type, |
| 4856 | // the value of the constant expression shall not refer to |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4857 | assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 4858 | ParamType->isNullPtrType()); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4859 | // -- a temporary object |
| 4860 | // -- a string literal |
| 4861 | // -- the result of a typeid expression, or |
| 4862 | // -- a predefind __func__ variable |
| 4863 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 4864 | if (isa<CXXUuidofExpr>(E)) { |
| 4865 | Converted = TemplateArgument(const_cast<Expr*>(E)); |
| 4866 | break; |
| 4867 | } |
| 4868 | Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4869 | << Arg->getSourceRange(); |
| 4870 | return ExprError(); |
| 4871 | } |
| 4872 | auto *VD = const_cast<ValueDecl *>( |
| 4873 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 4874 | // -- a subobject |
| 4875 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 4876 | VD && VD->getType()->isArrayType() && |
| 4877 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 4878 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 4879 | // Per defect report (no number yet): |
| 4880 | // ... other than a pointer to the first element of a complete array |
| 4881 | // object. |
| 4882 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 4883 | Value.isLValueOnePastTheEnd()) { |
| 4884 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 4885 | << Value.getAsString(Context, ParamType); |
| 4886 | return ExprError(); |
| 4887 | } |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4888 | assert((VD || !ParamType->isReferenceType()) && |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4889 | "null reference should not be a constant expression"); |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 4890 | assert((!VD || !ParamType->isNullPtrType()) && |
| 4891 | "non-null value of type nullptr_t?"); |
| 4892 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 4893 | : TemplateArgument(CanonParamType, /*isNullPtr*/true); |
Richard Smith | 410cc89 | 2014-11-26 03:26:53 +0000 | [diff] [blame] | 4894 | break; |
| 4895 | } |
| 4896 | case APValue::AddrLabelDiff: |
| 4897 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
| 4898 | case APValue::Float: |
| 4899 | case APValue::ComplexInt: |
| 4900 | case APValue::ComplexFloat: |
| 4901 | case APValue::Vector: |
| 4902 | case APValue::Array: |
| 4903 | case APValue::Struct: |
| 4904 | case APValue::Union: |
| 4905 | llvm_unreachable("invalid kind for template argument"); |
| 4906 | } |
| 4907 | |
| 4908 | return ArgResult.get(); |
| 4909 | } |
| 4910 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4911 | // C++ [temp.arg.nontype]p5: |
| 4912 | // The following conversions are performed on each expression used |
| 4913 | // as a non-type template-argument. If a non-type |
| 4914 | // template-argument cannot be converted to the type of the |
| 4915 | // corresponding template-parameter then the program is |
| 4916 | // ill-formed. |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4917 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4918 | // C++11: |
| 4919 | // -- for a non-type template-parameter of integral or |
| 4920 | // enumeration type, conversions permitted in a converted |
| 4921 | // constant expression are applied. |
| 4922 | // |
| 4923 | // C++98: |
| 4924 | // -- for a non-type template-parameter of integral or |
| 4925 | // enumeration type, integral promotions (4.5) and integral |
| 4926 | // conversions (4.7) are applied. |
| 4927 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4928 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4929 | // We can't check arbitrary value-dependent arguments. |
| 4930 | // FIXME: If there's no viable conversion to the template parameter type, |
| 4931 | // we should be able to diagnose that prior to instantiation. |
| 4932 | if (Arg->isValueDependent()) { |
| 4933 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4934 | return Arg; |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | // C++ [temp.arg.nontype]p1: |
| 4938 | // A template-argument for a non-type, non-template template-parameter |
| 4939 | // shall be one of: |
| 4940 | // |
| 4941 | // -- for a non-type template-parameter of integral or enumeration |
| 4942 | // type, a converted constant expression of the type of the |
| 4943 | // template-parameter; or |
| 4944 | llvm::APSInt Value; |
| 4945 | ExprResult ArgResult = |
| 4946 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 4947 | CCEK_TemplateArg); |
| 4948 | if (ArgResult.isInvalid()) |
| 4949 | return ExprError(); |
| 4950 | |
| 4951 | // Widen the argument value to sizeof(parameter type). This is almost |
| 4952 | // always a no-op, except when the parameter type is bool. In |
| 4953 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 4954 | QualType IntegerType = ParamType; |
| 4955 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 4956 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 4957 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 4958 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4959 | Converted = TemplateArgument(Context, Value, |
| 4960 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4961 | return ArgResult; |
| 4962 | } |
| 4963 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4964 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 4965 | if (ArgResult.isInvalid()) |
| 4966 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4967 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4968 | |
| 4969 | QualType ArgType = Arg->getType(); |
| 4970 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4971 | // C++ [temp.arg.nontype]p1: |
| 4972 | // A template-argument for a non-type, non-template |
| 4973 | // template-parameter shall be one of: |
| 4974 | // |
| 4975 | // -- an integral constant-expression of integral or enumeration |
| 4976 | // type; or |
| 4977 | // -- the name of a non-type template-parameter; or |
| 4978 | SourceLocation NonConstantLoc; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4979 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4980 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4981 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4982 | diag::err_template_arg_not_integral_or_enumeral) |
| 4983 | << ArgType << Arg->getSourceRange(); |
| 4984 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4985 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4986 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 4987 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 4988 | QualType T; |
| 4989 | |
| 4990 | public: |
| 4991 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 4992 | |
| 4993 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 4994 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 4995 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 4996 | } |
| 4997 | } Diagnoser(ArgType); |
| 4998 | |
| 4999 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5000 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 5001 | if (!Arg) |
| 5002 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5003 | } |
| 5004 | |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5005 | // From here on out, all we care about is the unqualified form |
| 5006 | // of the argument type. |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5007 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5008 | |
| 5009 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 5010 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5011 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5012 | } else if (ParamType->isBooleanType()) { |
| 5013 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5014 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5015 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 5016 | !ParamType->isEnumeralType()) { |
| 5017 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5018 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5019 | } else { |
| 5020 | // We can't perform this conversion. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5021 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5022 | diag::err_template_arg_not_convertible) |
Richard Smith | d663fdd | 2014-12-17 20:42:37 +0000 | [diff] [blame] | 5023 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5024 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5025 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5026 | } |
| 5027 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5028 | // Add the value of this argument to the list of converted |
| 5029 | // arguments. We use the bitwidth and signedness of the template |
| 5030 | // parameter. |
| 5031 | if (Arg->isValueDependent()) { |
| 5032 | // The argument is value-dependent. Create a new |
| 5033 | // TemplateArgument with the converted expression. |
| 5034 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5035 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5036 | } |
| 5037 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5038 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5039 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 5040 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5041 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5042 | if (ParamType->isBooleanType()) { |
| 5043 | // Value must be zero or one. |
| 5044 | Value = Value != 0; |
| 5045 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 5046 | if (Value.getBitWidth() != AllowedBits) |
| 5047 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5048 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5049 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5050 | llvm::APSInt OldValue = Value; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5051 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5052 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5053 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5054 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 5055 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5056 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5057 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5058 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5059 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5060 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5061 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5062 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5063 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5064 | << Arg->getSourceRange(); |
| 5065 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5066 | } |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 5067 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5068 | // Complain if we overflowed the template parameter's type. |
| 5069 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5070 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5071 | RequiredBits = OldValue.getActiveBits(); |
| 5072 | else if (OldValue.isUnsigned()) |
| 5073 | RequiredBits = OldValue.getActiveBits() + 1; |
| 5074 | else |
| 5075 | RequiredBits = OldValue.getMinSignedBits(); |
| 5076 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5077 | Diag(Arg->getLocStart(), |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 5078 | diag::warn_template_arg_too_large) |
| 5079 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 5080 | << Arg->getSourceRange(); |
| 5081 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5082 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 5083 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 5084 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5085 | Converted = TemplateArgument(Context, Value, |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 5086 | ParamType->isEnumeralType() |
| 5087 | ? Context.getCanonicalType(ParamType) |
| 5088 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5089 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 5090 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5091 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 5092 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5093 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 5094 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5095 | // Handle pointer-to-function, reference-to-function, and |
| 5096 | // pointer-to-member-function all in (roughly) the same way. |
| 5097 | if (// -- For a non-type template-parameter of type pointer to |
| 5098 | // function, only the function-to-pointer conversion (4.3) is |
| 5099 | // applied. If the template-argument represents a set of |
| 5100 | // overloaded functions (or a pointer to such), the matching |
| 5101 | // function is selected from the set (13.4). |
| 5102 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5103 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5104 | // -- For a non-type template-parameter of type reference to |
| 5105 | // function, no conversions apply. If the template-argument |
| 5106 | // represents a set of overloaded functions, the matching |
| 5107 | // function is selected from the set (13.4). |
| 5108 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5109 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5110 | // -- For a non-type template-parameter of type pointer to |
| 5111 | // member function, no conversions apply. If the |
| 5112 | // template-argument represents a set of overloaded member |
| 5113 | // functions, the matching member function is selected from |
| 5114 | // the set (13.4). |
| 5115 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5116 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5117 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5118 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5119 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5120 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5121 | true, |
| 5122 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5123 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5124 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5125 | |
| 5126 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5127 | ArgType = Arg->getType(); |
| 5128 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5129 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5130 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5131 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5132 | if (!ParamType->isMemberPointerType()) { |
| 5133 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5134 | ParamType, |
| 5135 | Arg, Converted)) |
| 5136 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5137 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5138 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5139 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5140 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5141 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5142 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5143 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5144 | } |
| 5145 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 5146 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5147 | // -- for a non-type template-parameter of type pointer to |
| 5148 | // object, qualification conversions (4.4) and the |
| 5149 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5150 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5151 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5152 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5153 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5154 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5155 | ParamType, |
| 5156 | Arg, Converted)) |
| 5157 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5158 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5159 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5160 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5161 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5162 | // -- For a non-type template-parameter of type reference to |
| 5163 | // object, no conversions apply. The type referred to by the |
| 5164 | // reference may be more cv-qualified than the (otherwise |
| 5165 | // identical) type of the template-argument. The |
| 5166 | // template-parameter is bound directly to the |
| 5167 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5168 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5169 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5170 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5171 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5172 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 5173 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5174 | true, |
| 5175 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5176 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5177 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5178 | |
| 5179 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5180 | ArgType = Arg->getType(); |
| 5181 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5182 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5183 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5184 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5185 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5186 | ParamType, |
| 5187 | Arg, Converted)) |
| 5188 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5189 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5190 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5191 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5192 | // Deal with parameters of type std::nullptr_t. |
| 5193 | if (ParamType->isNullPtrType()) { |
| 5194 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 5195 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5196 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5197 | } |
| 5198 | |
| 5199 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 5200 | case NPV_NotNullPointer: |
| 5201 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 5202 | << Arg->getType() << ParamType; |
| 5203 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5204 | return ExprError(); |
| 5205 | |
| 5206 | case NPV_Error: |
| 5207 | return ExprError(); |
| 5208 | |
| 5209 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 5210 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5211 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 5212 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5213 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5214 | } |
| 5215 | } |
| 5216 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5217 | // -- For a non-type template-parameter of type pointer to data |
| 5218 | // member, qualification conversions (4.4) are applied. |
| 5219 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 5220 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5221 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5222 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5223 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5224 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5225 | } |
| 5226 | |
| 5227 | /// \brief Check a template argument against its corresponding |
| 5228 | /// template template parameter. |
| 5229 | /// |
| 5230 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 5231 | /// It returns true if an error occurred, and false otherwise. |
| 5232 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 5233 | TemplateArgumentLoc &Arg, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5234 | unsigned ArgumentPackIndex) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5235 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5236 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 5237 | if (!Template) { |
| 5238 | // Any dependent template name is fine. |
| 5239 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 5240 | return false; |
| 5241 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5242 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5243 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5244 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5245 | // the name of a class template or an alias template, expressed as an |
| 5246 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5247 | // primary class templates are considered when matching the |
| 5248 | // template template argument with the corresponding parameter; |
| 5249 | // partial specializations are not considered even if their |
| 5250 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5251 | // |
| 5252 | // Note that we also allow template template parameters here, which |
| 5253 | // will happen when we are dealing with, e.g., class template |
| 5254 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5255 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5256 | !isa<TemplateTemplateParmDecl>(Template) && |
| 5257 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5258 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5259 | "Only function templates are possible here"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5260 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5261 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5262 | << Template; |
| 5263 | } |
| 5264 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5265 | TemplateParameterList *Params = Param->getTemplateParameters(); |
| 5266 | if (Param->isExpandedParameterPack()) |
| 5267 | Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); |
| 5268 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5269 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5270 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5271 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5272 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5273 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5274 | } |
| 5275 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5276 | /// \brief Given a non-type template argument that refers to a |
| 5277 | /// declaration and the type of its corresponding non-type template |
| 5278 | /// parameter, produce an expression that properly refers to that |
| 5279 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5280 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5281 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 5282 | QualType ParamType, |
| 5283 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 5284 | // C++ [temp.param]p8: |
| 5285 | // |
| 5286 | // A non-type template-parameter of type "array of T" or |
| 5287 | // "function returning T" is adjusted to be of type "pointer to |
| 5288 | // T" or "pointer to function returning T", respectively. |
| 5289 | if (ParamType->isArrayType()) |
| 5290 | ParamType = Context.getArrayDecayedType(ParamType); |
| 5291 | else if (ParamType->isFunctionType()) |
| 5292 | ParamType = Context.getPointerType(ParamType); |
| 5293 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5294 | // For a NULL non-type template argument, return nullptr casted to the |
| 5295 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5296 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5297 | return ImpCastExprToType( |
| 5298 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 5299 | ParamType, |
| 5300 | ParamType->getAs<MemberPointerType>() |
| 5301 | ? CK_NullToMemberPointer |
| 5302 | : CK_NullToPointer); |
| 5303 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5304 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 5305 | "Only declaration template arguments permitted here"); |
| 5306 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5307 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 5308 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5309 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 5310 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 5311 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5312 | // If the value is a class member, we might have a pointer-to-member. |
| 5313 | // Determine whether the non-type template template parameter is of |
| 5314 | // pointer-to-member type. If so, we need to build an appropriate |
| 5315 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 5316 | // would refer to the member itself. |
| 5317 | if (ParamType->isMemberPointerType()) { |
| 5318 | QualType ClassType |
| 5319 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 5320 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5321 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5322 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5323 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 5324 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5325 | |
| 5326 | // The actual value-ness of this is unimportant, but for |
| 5327 | // internal consistency's sake, references to instance methods |
| 5328 | // are r-values. |
| 5329 | ExprValueKind VK = VK_LValue; |
| 5330 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 5331 | VK = VK_RValue; |
| 5332 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5333 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5334 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5335 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5336 | Loc, |
| 5337 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5338 | if (RefExpr.isInvalid()) |
| 5339 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5340 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5341 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5342 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5343 | // We might need to perform a trailing qualification conversion, since |
| 5344 | // the element type on the parameter could be more qualified than the |
| 5345 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5346 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5347 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5348 | ParamType.getUnqualifiedType(), false, |
| 5349 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5350 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5351 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5352 | assert(!RefExpr.isInvalid() && |
| 5353 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5354 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5355 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5356 | } |
| 5357 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5358 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5359 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5360 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5361 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5362 | // When the non-type template parameter is a pointer, take the |
| 5363 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5364 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5365 | if (RefExpr.isInvalid()) |
| 5366 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5367 | |
| 5368 | if (T->isFunctionType() || T->isArrayType()) { |
| 5369 | // Decay functions and arrays. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5370 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5371 | if (RefExpr.isInvalid()) |
| 5372 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5373 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5374 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5375 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5376 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5377 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5378 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5379 | } |
| 5380 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5381 | ExprValueKind VK = VK_RValue; |
| 5382 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5383 | // If the non-type template parameter has reference type, qualify the |
| 5384 | // resulting declaration reference with the extra qualifiers on the |
| 5385 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5386 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 5387 | VK = VK_LValue; |
| 5388 | T = Context.getQualifiedType(T, |
| 5389 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5390 | } else if (isa<FunctionDecl>(VD)) { |
| 5391 | // References to functions are always lvalues. |
| 5392 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5393 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5394 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5395 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5396 | } |
| 5397 | |
| 5398 | /// \brief Construct a new expression that refers to the given |
| 5399 | /// integral template argument with the given source-location |
| 5400 | /// information. |
| 5401 | /// |
| 5402 | /// This routine takes care of the mapping from an integral template |
| 5403 | /// argument (which may have any integral type) to the appropriate |
| 5404 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5405 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5406 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 5407 | SourceLocation Loc) { |
| 5408 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 5409 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5410 | QualType OrigT = Arg.getIntegralType(); |
| 5411 | |
| 5412 | // If this is an enum type that we're instantiating, we need to use an integer |
| 5413 | // type the same size as the enumerator. We don't want to build an |
| 5414 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 5415 | // any integral type with C++11 enum classes, make sure we create the right |
| 5416 | // type of literal for it. |
| 5417 | QualType T = OrigT; |
| 5418 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 5419 | T = ET->getDecl()->getIntegerType(); |
| 5420 | |
| 5421 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5422 | if (T->isAnyCharacterType()) { |
| 5423 | CharacterLiteral::CharacterKind Kind; |
| 5424 | if (T->isWideCharType()) |
| 5425 | Kind = CharacterLiteral::Wide; |
| 5426 | else if (T->isChar16Type()) |
| 5427 | Kind = CharacterLiteral::UTF16; |
| 5428 | else if (T->isChar32Type()) |
| 5429 | Kind = CharacterLiteral::UTF32; |
| 5430 | else |
| 5431 | Kind = CharacterLiteral::Ascii; |
| 5432 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5433 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 5434 | Kind, T, Loc); |
| 5435 | } else if (T->isBooleanType()) { |
| 5436 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 5437 | T, Loc); |
| 5438 | } else if (T->isNullPtrType()) { |
| 5439 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 5440 | } else { |
| 5441 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5442 | } |
| 5443 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5444 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5445 | // FIXME: This is a hack. We need a better way to handle substituted |
| 5446 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5447 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 5448 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5449 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5450 | Loc, Loc); |
| 5451 | } |
| 5452 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5453 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5454 | } |
| 5455 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5456 | /// \brief Match two template parameters within template parameter lists. |
| 5457 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 5458 | bool Complain, |
| 5459 | Sema::TemplateParameterListEqualKind Kind, |
| 5460 | SourceLocation TemplateArgLoc) { |
| 5461 | // Check the actual kind (type, non-type, template). |
| 5462 | if (Old->getKind() != New->getKind()) { |
| 5463 | if (Complain) { |
| 5464 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 5465 | if (TemplateArgLoc.isValid()) { |
| 5466 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5467 | NextDiag = diag::note_template_param_different_kind; |
| 5468 | } |
| 5469 | S.Diag(New->getLocation(), NextDiag) |
| 5470 | << (Kind != Sema::TPL_TemplateMatch); |
| 5471 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 5472 | << (Kind != Sema::TPL_TemplateMatch); |
| 5473 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5474 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5475 | return false; |
| 5476 | } |
| 5477 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5478 | // Check that both are parameter packs are neither are parameter packs. |
| 5479 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5480 | // template template parameter, the template template parameter can have |
| 5481 | // a parameter pack where the template template argument does not. |
| 5482 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 5483 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5484 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5485 | if (Complain) { |
| 5486 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 5487 | if (TemplateArgLoc.isValid()) { |
| 5488 | S.Diag(TemplateArgLoc, |
| 5489 | diag::err_template_arg_template_params_mismatch); |
| 5490 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 5491 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5492 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5493 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 5494 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 5495 | : 2; |
| 5496 | S.Diag(New->getLocation(), NextDiag) |
| 5497 | << ParamKind << New->isParameterPack(); |
| 5498 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 5499 | << ParamKind << Old->isParameterPack(); |
| 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 | return false; |
| 5503 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5504 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5505 | // For non-type template parameters, check the type of the parameter. |
| 5506 | if (NonTypeTemplateParmDecl *OldNTTP |
| 5507 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 5508 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5509 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5510 | // If we are matching a template template argument to a template |
| 5511 | // template parameter and one of the non-type template parameter types |
| 5512 | // is dependent, then we must wait until template instantiation time |
| 5513 | // to actually compare the arguments. |
| 5514 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5515 | (OldNTTP->getType()->isDependentType() || |
| 5516 | NewNTTP->getType()->isDependentType())) |
| 5517 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5518 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5519 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 5520 | if (Complain) { |
| 5521 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 5522 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5523 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5524 | diag::err_template_arg_template_params_mismatch); |
| 5525 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 5526 | } |
| 5527 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 5528 | << NewNTTP->getType() |
| 5529 | << (Kind != Sema::TPL_TemplateMatch); |
| 5530 | S.Diag(OldNTTP->getLocation(), |
| 5531 | diag::note_template_nontype_parm_prev_declaration) |
| 5532 | << OldNTTP->getType(); |
| 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 false; |
| 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 | return true; |
| 5539 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5540 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5541 | // For template template parameters, check the template parameter types. |
| 5542 | // The template parameter lists of template template |
| 5543 | // parameters must agree. |
| 5544 | if (TemplateTemplateParmDecl *OldTTP |
| 5545 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5546 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5547 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 5548 | OldTTP->getTemplateParameters(), |
| 5549 | Complain, |
| 5550 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5551 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5552 | : Kind), |
| 5553 | TemplateArgLoc); |
| 5554 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5555 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5556 | return true; |
| 5557 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5558 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5559 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 5560 | /// lists. |
| 5561 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5562 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5563 | TemplateParameterList *New, |
| 5564 | TemplateParameterList *Old, |
| 5565 | Sema::TemplateParameterListEqualKind Kind, |
| 5566 | SourceLocation TemplateArgLoc) { |
| 5567 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 5568 | if (TemplateArgLoc.isValid()) { |
| 5569 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5570 | NextDiag = diag::note_template_param_list_different_arity; |
| 5571 | } |
| 5572 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 5573 | << (New->size() > Old->size()) |
| 5574 | << (Kind != Sema::TPL_TemplateMatch) |
| 5575 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 5576 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 5577 | << (Kind != Sema::TPL_TemplateMatch) |
| 5578 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 5579 | } |
| 5580 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5581 | /// \brief Determine whether the given template parameter lists are |
| 5582 | /// equivalent. |
| 5583 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5584 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5585 | /// source code as part of a new template declaration. |
| 5586 | /// |
| 5587 | /// \param Old The old template parameter list, typically found via |
| 5588 | /// name lookup of the template declared with this template parameter |
| 5589 | /// list. |
| 5590 | /// |
| 5591 | /// \param Complain If true, this routine will produce a diagnostic if |
| 5592 | /// the template parameter lists are not equivalent. |
| 5593 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5594 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5595 | /// |
| 5596 | /// \param TemplateArgLoc If this source location is valid, then we |
| 5597 | /// are actually checking the template parameter list of a template |
| 5598 | /// argument (New) against the template parameter list of its |
| 5599 | /// corresponding template template parameter (Old). We produce |
| 5600 | /// slightly different diagnostics in this scenario. |
| 5601 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5602 | /// \returns True if the template parameter lists are equal, false |
| 5603 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5604 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5605 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 5606 | TemplateParameterList *Old, |
| 5607 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5608 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5609 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5610 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 5611 | if (Complain) |
| 5612 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5613 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5614 | |
| 5615 | return false; |
| 5616 | } |
| 5617 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5618 | // C++0x [temp.arg.template]p3: |
| 5619 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5620 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5621 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5622 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5623 | // template-parameter-list of P. [...] |
| 5624 | TemplateParameterList::iterator NewParm = New->begin(); |
| 5625 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5626 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5627 | OldParmEnd = Old->end(); |
| 5628 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 5629 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 5630 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5631 | if (NewParm == NewParmEnd) { |
| 5632 | if (Complain) |
| 5633 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5634 | TemplateArgLoc); |
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 | return false; |
| 5637 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5638 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5639 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5640 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5641 | return false; |
| 5642 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5643 | ++NewParm; |
| 5644 | continue; |
| 5645 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5646 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5647 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5648 | // [...] When P's template- parameter-list contains a template parameter |
| 5649 | // pack (14.5.3), the template parameter pack will match zero or more |
| 5650 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5651 | // template-parameter-list of A with the same type and form as the |
| 5652 | // template parameter pack in P (ignoring whether those template |
| 5653 | // parameters are template parameter packs). |
| 5654 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 5655 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5656 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5657 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5658 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5659 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5660 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5661 | // Make sure we exhausted all of the arguments. |
| 5662 | if (NewParm != NewParmEnd) { |
| 5663 | if (Complain) |
| 5664 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5665 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5666 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5667 | return false; |
| 5668 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5669 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5670 | return true; |
| 5671 | } |
| 5672 | |
| 5673 | /// \brief Check whether a template can be declared within this scope. |
| 5674 | /// |
| 5675 | /// If the template declaration is valid in this scope, returns |
| 5676 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5677 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5678 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 5679 | if (!S) |
| 5680 | return false; |
| 5681 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5682 | // Find the nearest enclosing declaration scope. |
| 5683 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5684 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5685 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5686 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5687 | // C++ [temp]p4: |
| 5688 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 5689 | DeclContext *Ctx = S->getEntity(); |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5690 | if (Ctx && Ctx->isExternCContext()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5691 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5692 | << TemplateParams->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5693 | |
Eli Friedman | dfbd0c4 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 5694 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5695 | Ctx = Ctx->getParent(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5696 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5697 | // C++ [temp]p2: |
| 5698 | // A template-declaration can appear only as a namespace scope or |
| 5699 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 5700 | if (Ctx) { |
| 5701 | if (Ctx->isFileContext()) |
| 5702 | return false; |
| 5703 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 5704 | // C++ [temp.mem]p2: |
| 5705 | // A local class shall not have member templates. |
| 5706 | if (RD->isLocalClass()) |
| 5707 | return Diag(TemplateParams->getTemplateLoc(), |
| 5708 | diag::err_template_inside_local_class) |
| 5709 | << TemplateParams->getSourceRange(); |
| 5710 | else |
| 5711 | return false; |
| 5712 | } |
| 5713 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5714 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5715 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5716 | diag::err_template_outside_namespace_or_class_scope) |
| 5717 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5718 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5719 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5720 | /// \brief Determine what kind of template specialization the given declaration |
| 5721 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5722 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5723 | if (!D) |
| 5724 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5725 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5726 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 5727 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5728 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 5729 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5730 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 5731 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5732 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5733 | return TSK_Undeclared; |
| 5734 | } |
| 5735 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5736 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5737 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5738 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5739 | /// This routine determines whether a template specialization can be declared |
| 5740 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5741 | /// |
| 5742 | /// \param S the semantic analysis object for which this check is being |
| 5743 | /// performed. |
| 5744 | /// |
| 5745 | /// \param Specialized the entity being specialized or instantiated, which |
| 5746 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5747 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5748 | /// member class). |
| 5749 | /// |
| 5750 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 5751 | /// |
| 5752 | /// \param Loc the location of the explicit specialization or instantiation of |
| 5753 | /// this entity. |
| 5754 | /// |
| 5755 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 5756 | /// a class template. |
| 5757 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5758 | /// \returns true if there was an error that we cannot recover from, false |
| 5759 | /// otherwise. |
| 5760 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 5761 | NamedDecl *Specialized, |
| 5762 | NamedDecl *PrevDecl, |
| 5763 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5764 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5765 | // Keep these "kind" numbers in sync with the %select statements in the |
| 5766 | // various diagnostics emitted by this routine. |
| 5767 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5768 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5769 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5770 | else if (isa<VarTemplateDecl>(Specialized)) |
| 5771 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5772 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5773 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5774 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5775 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5776 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5777 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5778 | else if (isa<RecordDecl>(Specialized)) |
| 5779 | EntityKind = 7; |
| 5780 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 5781 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5782 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5783 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5784 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5785 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5786 | return true; |
| 5787 | } |
| 5788 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5789 | // C++ [temp.expl.spec]p2: |
| 5790 | // An explicit specialization shall be declared in the namespace |
| 5791 | // of which the template is a member, or, for member templates, in |
| 5792 | // the namespace of which the enclosing class or enclosing class |
| 5793 | // template is a member. An explicit specialization of a member |
| 5794 | // function, member class or static data member of a class |
| 5795 | // template shall be declared in the namespace of which the class |
| 5796 | // template is a member. Such a declaration may also be a |
| 5797 | // definition. If the declaration is not a definition, the |
| 5798 | // specialization may be defined later in the name- space in which |
| 5799 | // the explicit specialization was declared, or in a namespace |
| 5800 | // that encloses the one in which the explicit specialization was |
| 5801 | // declared. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5802 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5803 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5804 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5805 | return true; |
| 5806 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5807 | |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5808 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5809 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 5810 | // Do not warn for class scope explicit specialization during |
| 5811 | // instantiation, warning was already emitted during pattern |
| 5812 | // semantic analysis. |
| 5813 | if (!S.ActiveTemplateInstantiations.size()) |
| 5814 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 5815 | << Specialized; |
| 5816 | } else { |
| 5817 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5818 | << Specialized; |
| 5819 | return true; |
| 5820 | } |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5821 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5822 | |
Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 5823 | if (S.CurContext->isRecord() && |
| 5824 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 5825 | // Make sure that we're specializing in the right record context. |
| 5826 | // Otherwise, things can go horribly wrong. |
| 5827 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5828 | << Specialized; |
| 5829 | return true; |
| 5830 | } |
| 5831 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5832 | // C++ [temp.class.spec]p6: |
| 5833 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5834 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 5835 | // and 14.5.2). |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5836 | DeclContext *SpecializedContext |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5837 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5838 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5839 | |
| 5840 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
| 5841 | // namespace. |
| 5842 | // Note that HandleDeclarator() performs this check for explicit |
| 5843 | // specializations of function templates, static data members, and member |
| 5844 | // functions, so we skip the check here for those kinds of entities. |
| 5845 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
| 5846 | // Should we refactor that check, so that it occurs later? |
| 5847 | if (!DC->Encloses(SpecializedContext) && |
| 5848 | !(isa<FunctionTemplateDecl>(Specialized) || |
| 5849 | isa<FunctionDecl>(Specialized) || |
| 5850 | isa<VarTemplateDecl>(Specialized) || |
| 5851 | isa<VarDecl>(Specialized))) { |
| 5852 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 5853 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 5854 | << EntityKind << Specialized; |
Alexey Bataev | 0068cb2 | 2015-03-20 07:21:46 +0000 | [diff] [blame] | 5855 | else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 5856 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
| 5857 | if (S.getLangOpts().MicrosoftExt) |
| 5858 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 5859 | S.Diag(Loc, Diag) << EntityKind << Specialized |
| 5860 | << cast<NamedDecl>(SpecializedContext); |
| 5861 | } else |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5862 | llvm_unreachable("unexpected namespace context for specialization"); |
| 5863 | |
| 5864 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 5865 | } else if ((!PrevDecl || |
| 5866 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 5867 | getTemplateSpecializationKind(PrevDecl) == |
| 5868 | TSK_ImplicitInstantiation)) { |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5869 | // C++ [temp.exp.spec]p2: |
| 5870 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5871 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5872 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5873 | // An explicit specialization of a member function, member class or |
| 5874 | // static data member of a class template shall be declared in the |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5875 | // namespace of which the class template is a member. |
| 5876 | // |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5877 | // C++11 [temp.expl.spec]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5878 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5879 | // the specialized template. |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5880 | // C++11 [temp.explicit]p3: |
| 5881 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 5882 | // template. |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5883 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5884 | bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext); |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5885 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5886 | assert(!IsCPlusPlus11Extension && |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5887 | "DC encloses TU but isn't in enclosing namespace set"); |
| 5888 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | 8ce6315 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 5889 | << EntityKind << Specialized; |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5890 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 5891 | int Diag; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5892 | if (!IsCPlusPlus11Extension) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5893 | Diag = diag::err_template_spec_decl_out_of_scope; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5894 | else if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5895 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 5896 | else |
| 5897 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 5898 | S.Diag(Loc, Diag) |
| 5899 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 5900 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5901 | |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5902 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5903 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5904 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5905 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5906 | return false; |
| 5907 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5908 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5909 | static SourceRange findTemplateParameter(unsigned Depth, Expr *E) { |
| 5910 | if (!E->isInstantiationDependent()) |
| 5911 | return SourceLocation(); |
| 5912 | DependencyChecker Checker(Depth); |
| 5913 | Checker.TraverseStmt(E); |
| 5914 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 5915 | return E->getSourceRange(); |
| 5916 | return Checker.MatchLoc; |
| 5917 | } |
| 5918 | |
| 5919 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 5920 | if (!TL.getType()->isDependentType()) |
| 5921 | return SourceLocation(); |
| 5922 | DependencyChecker Checker(Depth); |
| 5923 | Checker.TraverseTypeLoc(TL); |
| 5924 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 5925 | return TL.getSourceRange(); |
| 5926 | return Checker.MatchLoc; |
| 5927 | } |
| 5928 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5929 | /// \brief Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5930 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5931 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5932 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 5933 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5934 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 5935 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5936 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5937 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 5938 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5939 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5940 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5941 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5942 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5943 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5944 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5945 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5946 | |
| 5947 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5948 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 5949 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5950 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 5951 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 5952 | |
| 5953 | // Strip off any implicit casts we added as part of type checking. |
| 5954 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 5955 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5956 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5957 | // C++ [temp.class.spec]p8: |
| 5958 | // A non-type argument is non-specialized if it is the name of a |
| 5959 | // non-type parameter. All other non-type arguments are |
| 5960 | // specialized. |
| 5961 | // |
| 5962 | // Below, we check the two conditions that only apply to |
| 5963 | // specialized non-type arguments, so skip any non-specialized |
| 5964 | // arguments. |
| 5965 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 5966 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5967 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5968 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5969 | // C++ [temp.class.spec]p9: |
| 5970 | // Within the argument list of a class template partial |
| 5971 | // specialization, the following restrictions apply: |
| 5972 | // -- A partially specialized non-type argument expression |
| 5973 | // shall not involve a template parameter of the partial |
| 5974 | // specialization except when the argument expression is a |
| 5975 | // simple identifier. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5976 | SourceRange ParamUseRange = |
| 5977 | findTemplateParameter(Param->getDepth(), ArgExpr); |
| 5978 | if (ParamUseRange.isValid()) { |
| 5979 | if (IsDefaultArgument) { |
| 5980 | S.Diag(TemplateNameLoc, |
| 5981 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 5982 | S.Diag(ParamUseRange.getBegin(), |
| 5983 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 5984 | << ParamUseRange; |
| 5985 | } else { |
| 5986 | S.Diag(ParamUseRange.getBegin(), |
| 5987 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 5988 | << ParamUseRange; |
| 5989 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5990 | return true; |
| 5991 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5992 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5993 | // -- The type of a template parameter corresponding to a |
| 5994 | // specialized non-type argument shall not be dependent on a |
| 5995 | // parameter of the specialization. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5996 | // |
| 5997 | // FIXME: We need to delay this check until instantiation in some cases: |
| 5998 | // |
| 5999 | // template<template<typename> class X> struct A { |
| 6000 | // template<typename T, X<T> N> struct B; |
| 6001 | // template<typename T> struct B<T, 0>; |
| 6002 | // }; |
| 6003 | // template<typename> using X = int; |
| 6004 | // A<X>::B<int, 0> b; |
| 6005 | ParamUseRange = findTemplateParameter( |
| 6006 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
| 6007 | if (ParamUseRange.isValid()) { |
| 6008 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getLocStart(), |
| 6009 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 6010 | << Param->getType() << ParamUseRange; |
| 6011 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
| 6012 | << (IsDefaultArgument ? ParamUseRange : SourceRange()); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6013 | return true; |
| 6014 | } |
| 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 | return false; |
| 6018 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6019 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6020 | /// \brief Check the non-type template arguments of a class template |
| 6021 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 6022 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6023 | /// \param TemplateNameLoc the location of the template name. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6024 | /// \param TemplateParams the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6025 | /// template. |
| 6026 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6027 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6028 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6029 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6030 | /// \returns \c true if there was an error, \c false otherwise. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6031 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6032 | Sema &S, SourceLocation TemplateNameLoc, |
| 6033 | TemplateParameterList *TemplateParams, unsigned NumExplicit, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6034 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6035 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 6036 | |
| 6037 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6038 | NonTypeTemplateParmDecl *Param |
| 6039 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 6040 | if (!Param) |
| 6041 | continue; |
| 6042 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6043 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
| 6044 | S, TemplateNameLoc, Param, &ArgList[I], 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 6045 | return true; |
| 6046 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6047 | |
| 6048 | return false; |
| 6049 | } |
| 6050 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6051 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6052 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 6053 | TagUseKind TUK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6054 | SourceLocation KWLoc, |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6055 | SourceLocation ModulePrivateLoc, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6056 | TemplateIdAnnotation &TemplateId, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6057 | AttributeList *Attr, |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6058 | MultiTemplateParamsArg |
| 6059 | TemplateParameterLists, |
| 6060 | SkipBodyInfo *SkipBody) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6061 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 6062 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6063 | CXXScopeSpec &SS = TemplateId.SS; |
| 6064 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6065 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 6066 | // store the location of the outermost template keyword in the declaration. |
| 6067 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6068 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 6069 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 6070 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 6071 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6072 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6073 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6074 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6075 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6076 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 6077 | |
| 6078 | if (!ClassTemplate) { |
| 6079 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6080 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 6081 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 6082 | return true; |
| 6083 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6084 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6085 | bool isExplicitSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6086 | bool isPartialSpecialization = false; |
| 6087 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6088 | // Check the validity of the template headers that introduce this |
| 6089 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6090 | // FIXME: We probably shouldn't complain about these headers for |
| 6091 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6092 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 6093 | TemplateParameterList *TemplateParams = |
| 6094 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6095 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
| 6096 | TemplateParameterLists, TUK == TUK_Friend, isExplicitSpecialization, |
| 6097 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 6098 | if (Invalid) |
| 6099 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6100 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6101 | if (TemplateParams && TemplateParams->size() > 0) { |
| 6102 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6103 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 6104 | if (TUK == TUK_Friend) { |
| 6105 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 6106 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6107 | return true; |
| 6108 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6109 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6110 | // C++ [temp.class.spec]p10: |
| 6111 | // The template parameter list of a specialization shall not |
| 6112 | // contain default template argument values. |
| 6113 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 6114 | Decl *Param = TemplateParams->getParam(I); |
| 6115 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 6116 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6117 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6118 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 6119 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6120 | } |
| 6121 | } else if (NonTypeTemplateParmDecl *NTTP |
| 6122 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 6123 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6124 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6125 | diag::err_default_arg_in_partial_spec) |
| 6126 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6127 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6128 | } |
| 6129 | } else { |
| 6130 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6131 | if (TTP->hasDefaultArgument()) { |
| 6132 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6133 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6134 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6135 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6136 | } |
| 6137 | } |
| 6138 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6139 | } else if (TemplateParams) { |
| 6140 | if (TUK == TUK_Friend) |
| 6141 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6142 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6143 | SourceRange(TemplateParams->getTemplateLoc(), |
| 6144 | TemplateParams->getRAngleLoc())) |
| 6145 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6146 | else |
| 6147 | isExplicitSpecialization = true; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6148 | } else { |
| 6149 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6150 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6151 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6152 | // Check that the specialization uses the same tag kind as the |
| 6153 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6154 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6155 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6156 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6157 | Kind, TUK == TUK_Definition, KWLoc, |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6158 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6159 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6160 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6161 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6162 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6163 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6164 | diag::note_previous_use); |
| 6165 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6166 | } |
| 6167 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6168 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6169 | TemplateArgumentListInfo TemplateArgs = |
| 6170 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6171 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6172 | // Check for unexpanded parameter packs in any of the template arguments. |
| 6173 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6174 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6175 | UPPC_PartialSpecialization)) |
| 6176 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6177 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6178 | // Check that the template argument list is well-formed for this |
| 6179 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6180 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6181 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6182 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6183 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6184 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6185 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6186 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6187 | if (isPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6188 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6189 | *this, TemplateNameLoc, ClassTemplate->getTemplateParameters(), |
| 6190 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6191 | return true; |
| 6192 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6193 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6194 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6195 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6196 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6197 | TemplateArgs.size(), |
| 6198 | InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6199 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 6200 | << ClassTemplate->getDeclName(); |
| 6201 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6202 | } |
| 6203 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6204 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6205 | void *InsertPos = nullptr; |
| 6206 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6207 | |
| 6208 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6209 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6210 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6211 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6212 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6213 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6214 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6215 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6216 | // Check whether we can declare a class template specialization in |
| 6217 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6218 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6219 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 6220 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6221 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6222 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6223 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6224 | // The canonical type |
| 6225 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 6226 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6227 | // Build the canonical type that describes the converted template |
| 6228 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6229 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6230 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6231 | Converted.data(), |
| 6232 | Converted.size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6233 | |
| 6234 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6235 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 6236 | // C++ [temp.class.spec]p9b3: |
| 6237 | // |
| 6238 | // -- The argument list of the specialization shall not be identical |
| 6239 | // to the implicit argument list of the primary template. |
| 6240 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6241 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6242 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6243 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 6244 | ClassTemplate->getIdentifier(), |
| 6245 | TemplateNameLoc, |
| 6246 | Attr, |
| 6247 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6248 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 6249 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6250 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6251 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6252 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6253 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6254 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6255 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 6256 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6257 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6258 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6259 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6260 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 6261 | TemplateParams, |
| 6262 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6263 | Converted.data(), |
| 6264 | Converted.size(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6265 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6266 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 6267 | PrevPartial); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6268 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6269 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6270 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6271 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6272 | TemplateParameterLists.data()); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6273 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6274 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6275 | if (!PrevPartial) |
| 6276 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6277 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6278 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6279 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6280 | // template specialization, make a note of that. |
| 6281 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 6282 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6283 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6284 | // Check that all of the template parameters of the class template |
| 6285 | // partial specialization are deducible from the template |
| 6286 | // arguments. If not, this class template partial specialization |
| 6287 | // will never be used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6288 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6289 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6290 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 6291 | DeducibleParams); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6292 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6293 | if (!DeducibleParams.all()) { |
| 6294 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6295 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6296 | << /*class template*/0 << (NumNonDeducible > 1) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6297 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 6298 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 6299 | if (!DeducibleParams[I]) { |
| 6300 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 6301 | if (Param->getDeclName()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6302 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6303 | diag::note_partial_spec_unused_parameter) |
| 6304 | << Param->getDeclName(); |
| 6305 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6306 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6307 | diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 6308 | << "(anonymous)"; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6309 | } |
| 6310 | } |
| 6311 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6312 | } else { |
| 6313 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6314 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6315 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6316 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6317 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6318 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6319 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6320 | Converted.data(), |
| 6321 | Converted.size(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6322 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6323 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6324 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6325 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6326 | TemplateParameterLists.size(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6327 | TemplateParameterLists.data()); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6328 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6329 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6330 | if (!PrevDecl) |
| 6331 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6332 | |
| 6333 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6334 | } |
| 6335 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6336 | // C++ [temp.expl.spec]p6: |
| 6337 | // 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] | 6338 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6339 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6340 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6341 | // use occurs; no diagnostic is required. |
| 6342 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6343 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6344 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6345 | // Is there any previous explicit specialization declaration? |
| 6346 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6347 | Okay = true; |
| 6348 | break; |
| 6349 | } |
| 6350 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6351 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6352 | if (!Okay) { |
| 6353 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 6354 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 6355 | << Context.getTypeDeclType(Specialization) << Range; |
| 6356 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6357 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6358 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6359 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6360 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6361 | return true; |
| 6362 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6363 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6364 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6365 | // If this is not a friend, note that this is an explicit specialization. |
| 6366 | if (TUK != TUK_Friend) |
| 6367 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6368 | |
| 6369 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6370 | if (TUK == TUK_Definition) { |
Richard Smith | c7e6ff0 | 2015-05-18 20:36:47 +0000 | [diff] [blame] | 6371 | RecordDecl *Def = Specialization->getDefinition(); |
| 6372 | NamedDecl *Hidden = nullptr; |
| 6373 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 6374 | SkipBody->ShouldSkip = true; |
| 6375 | makeMergedDefinitionVisible(Hidden, KWLoc); |
| 6376 | // From here on out, treat this as just a redeclaration. |
| 6377 | TUK = TUK_Declaration; |
| 6378 | } else if (Def) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6379 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6380 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6381 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6382 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 6383 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6384 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6385 | } |
| 6386 | } |
| 6387 | |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 6388 | if (Attr) |
| 6389 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6390 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 6391 | // Add alignment attributes if necessary; these attributes are checked when |
| 6392 | // the ASTContext lays out the structure. |
| 6393 | if (TUK == TUK_Definition) { |
| 6394 | AddAlignmentAttributesForRecord(Specialization); |
| 6395 | AddMsStructLayoutForRecord(Specialization); |
| 6396 | } |
| 6397 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6398 | if (ModulePrivateLoc.isValid()) |
| 6399 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 6400 | << (isPartialSpecialization? 1 : 0) |
| 6401 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 6402 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 6403 | // Build the fully-sugared type for this class template |
| 6404 | // specialization as the user wrote in the specialization |
| 6405 | // itself. This means that we'll pretty-print the type retrieved |
| 6406 | // from the specialization's declaration the way that the user |
| 6407 | // actually wrote the specialization, rather than formatting the |
| 6408 | // name based on the "canonical" representation used to store the |
| 6409 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6410 | TypeSourceInfo *WrittenTy |
| 6411 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6412 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6413 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6414 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6415 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6416 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6417 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 6418 | // C++ [temp.expl.spec]p9: |
| 6419 | // A template explicit specialization is in the scope of the |
| 6420 | // namespace in which the template was defined. |
| 6421 | // |
| 6422 | // We actually implement this paragraph where we set the semantic |
| 6423 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 6424 | // but we also maintain the lexical context where the actual |
| 6425 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6426 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6427 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6428 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6429 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6430 | Specialization->startDefinition(); |
| 6431 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6432 | if (TUK == TUK_Friend) { |
| 6433 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 6434 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6435 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6436 | /*FIXME:*/KWLoc); |
| 6437 | Friend->setAccess(AS_public); |
| 6438 | CurContext->addDecl(Friend); |
| 6439 | } else { |
| 6440 | // Add the specialization into its lexical context, so that it can |
| 6441 | // be seen when iterating through the list of declarations in that |
| 6442 | // context. However, specializations are not found by name lookup. |
| 6443 | CurContext->addDecl(Specialization); |
| 6444 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6445 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6446 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6447 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6448 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6449 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6450 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6451 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 6452 | ActOnDocumentableDecl(NewDecl); |
| 6453 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6454 | } |
| 6455 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6456 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6457 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6458 | Declarator &D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6459 | assert(getCurFunctionDecl() == nullptr && "Function parsing confused"); |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6460 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6461 | |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6462 | if (FTI.hasPrototype) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6463 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6464 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6465 | |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6466 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6467 | |
Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 6468 | D.setFunctionDefinitionKind(FDK_Definition); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6469 | Decl *DP = HandleDeclarator(ParentScope, D, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6470 | TemplateParameterLists); |
Argyrios Kyrtzidis | 6fada2d | 2012-12-14 06:53:58 +0000 | [diff] [blame] | 6471 | return ActOnStartOfFunctionDef(FnBodyScope, DP); |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6472 | } |
| 6473 | |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6474 | /// \brief Strips various properties off an implicit instantiation |
| 6475 | /// that has just been explicitly specialized. |
| 6476 | static void StripImplicitInstantiation(NamedDecl *D) { |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6477 | D->dropAttr<DLLImportAttr>(); |
| 6478 | D->dropAttr<DLLExportAttr>(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6479 | |
Nico Weber | e497438 | 2014-12-19 23:52:45 +0000 | [diff] [blame] | 6480 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6481 | FD->setInlineSpecified(false); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6482 | } |
| 6483 | |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6484 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 6485 | // declaration or definition. |
| 6486 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6487 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6488 | // Explicit instantiations following a specialization have no effect and |
| 6489 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 6490 | // until a valid name loc is found. |
| 6491 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6492 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 6493 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6494 | PrevDiagLoc = Prev->getLocation(); |
| 6495 | } |
| 6496 | assert(PrevDiagLoc.isValid() && |
| 6497 | "Explicit instantiation without point of instantiation?"); |
| 6498 | return PrevDiagLoc; |
| 6499 | } |
| 6500 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6501 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6502 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6503 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6504 | /// new specialization/instantiation will have any effect. |
| 6505 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6506 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6507 | /// instantiation. |
| 6508 | /// |
| 6509 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 6510 | /// |
| 6511 | /// \param PrevDecl the previous declaration of the entity. |
| 6512 | /// |
| 6513 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 6514 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6515 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6516 | /// declaration was instantiated (either implicitly or explicitly). |
| 6517 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6518 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6519 | /// specialization or instantiation has no effect and should be ignored. |
| 6520 | /// |
| 6521 | /// \returns true if there was an error that should prevent the introduction of |
| 6522 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6523 | bool |
| 6524 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 6525 | TemplateSpecializationKind NewTSK, |
| 6526 | NamedDecl *PrevDecl, |
| 6527 | TemplateSpecializationKind PrevTSK, |
| 6528 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6529 | bool &HasNoEffect) { |
| 6530 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6531 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6532 | switch (NewTSK) { |
| 6533 | case TSK_Undeclared: |
| 6534 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 6535 | assert( |
| 6536 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 6537 | "previous declaration must be implicit!"); |
| 6538 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6539 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6540 | case TSK_ExplicitSpecialization: |
| 6541 | switch (PrevTSK) { |
| 6542 | case TSK_Undeclared: |
| 6543 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6544 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6545 | // explicitly specialized or has merely been mentioned without any |
| 6546 | // instantiation. |
| 6547 | return false; |
| 6548 | |
| 6549 | case TSK_ImplicitInstantiation: |
| 6550 | if (PrevPointOfInstantiation.isInvalid()) { |
| 6551 | // The declaration itself has not actually been instantiated, so it is |
| 6552 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6553 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6554 | return false; |
| 6555 | } |
| 6556 | // Fall through |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6557 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6558 | case TSK_ExplicitInstantiationDeclaration: |
| 6559 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6560 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 6561 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6562 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6563 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6564 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6565 | // 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] | 6566 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6567 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6568 | // implicit instantiation to take place, in every translation unit in |
| 6569 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6570 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6571 | // Is there any previous explicit specialization declaration? |
| 6572 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 6573 | return false; |
| 6574 | } |
| 6575 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6576 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6577 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6578 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6579 | << (PrevTSK != TSK_ImplicitInstantiation); |
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 | return true; |
| 6582 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6583 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6584 | case TSK_ExplicitInstantiationDeclaration: |
| 6585 | switch (PrevTSK) { |
| 6586 | case TSK_ExplicitInstantiationDeclaration: |
| 6587 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6588 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6589 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6590 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6591 | case TSK_Undeclared: |
| 6592 | case TSK_ImplicitInstantiation: |
| 6593 | // We're explicitly instantiating something that may have already been |
| 6594 | // implicitly instantiated; that's fine. |
| 6595 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6596 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6597 | case TSK_ExplicitSpecialization: |
| 6598 | // C++0x [temp.explicit]p4: |
| 6599 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6600 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6601 | // specialization for that template, the explicit instantiation has no |
| 6602 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6603 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6604 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6605 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6606 | case TSK_ExplicitInstantiationDefinition: |
| 6607 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6608 | // If an entity is the subject of both an explicit instantiation |
| 6609 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6610 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6611 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6612 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6613 | |
| 6614 | // Explicit instantiations following a specialization have no effect and |
| 6615 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 6616 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6617 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 6618 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6619 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6620 | return false; |
| 6621 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6622 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6623 | case TSK_ExplicitInstantiationDefinition: |
| 6624 | switch (PrevTSK) { |
| 6625 | case TSK_Undeclared: |
| 6626 | case TSK_ImplicitInstantiation: |
| 6627 | // We're explicitly instantiating something that may have already been |
| 6628 | // implicitly instantiated; that's fine. |
| 6629 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6630 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6631 | case TSK_ExplicitSpecialization: |
| 6632 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 6633 | // For a given set of template parameters, if an explicit |
| 6634 | // instantiation of a template appears after a declaration of |
| 6635 | // an explicit specialization for that template, the explicit |
| 6636 | // instantiation has no effect. |
| 6637 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6638 | // 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] | 6639 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6640 | // has been explicitly specialized. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6641 | Diag(NewLoc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6642 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 6643 | diag::ext_explicit_instantiation_after_specialization) |
| 6644 | << PrevDecl; |
| 6645 | Diag(PrevDecl->getLocation(), |
| 6646 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6647 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6648 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6649 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6650 | case TSK_ExplicitInstantiationDeclaration: |
| 6651 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6652 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6653 | |
| 6654 | // C++0x [temp.explicit]p4: |
| 6655 | // For a given set of template parameters, if an explicit instantiation |
| 6656 | // of a template appears after a declaration of an explicit |
| 6657 | // specialization for that template, the explicit instantiation has no |
| 6658 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6659 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6660 | // Is there any previous explicit specialization declaration? |
| 6661 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6662 | HasNoEffect = true; |
| 6663 | break; |
| 6664 | } |
| 6665 | } |
| 6666 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6667 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6668 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6669 | case TSK_ExplicitInstantiationDefinition: |
| 6670 | // C++0x [temp.spec]p5: |
| 6671 | // For a given template and a given set of template-arguments, |
| 6672 | // - an explicit instantiation definition shall appear at most once |
| 6673 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6674 | |
| 6675 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 6676 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 6677 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6678 | : diag::err_explicit_instantiation_duplicate) |
| 6679 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6680 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6681 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6682 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6683 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6684 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6685 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6686 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6687 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6688 | } |
| 6689 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6690 | /// \brief Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6691 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6692 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6693 | /// The only possible way to get a dependent function template specialization |
| 6694 | /// is with a friend declaration, like so: |
| 6695 | /// |
| 6696 | /// \code |
| 6697 | /// template \<class T> void foo(T); |
| 6698 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6699 | /// friend void foo<>(T); |
| 6700 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6701 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6702 | /// |
| 6703 | /// There really isn't any useful analysis we can do here, so we |
| 6704 | /// just store the information. |
| 6705 | bool |
| 6706 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 6707 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 6708 | LookupResult &Previous) { |
| 6709 | // Remove anything from Previous that isn't a function template in |
| 6710 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6711 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6712 | LookupResult::Filter F = Previous.makeFilter(); |
| 6713 | while (F.hasNext()) { |
| 6714 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 6715 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6716 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 6717 | D->getDeclContext()->getRedeclContext())) |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6718 | F.erase(); |
| 6719 | } |
| 6720 | F.done(); |
| 6721 | |
| 6722 | // Should this be diagnosed here? |
| 6723 | if (Previous.empty()) return true; |
| 6724 | |
| 6725 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 6726 | ExplicitTemplateArgs); |
| 6727 | return false; |
| 6728 | } |
| 6729 | |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6730 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6731 | /// specialization. |
| 6732 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6733 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6734 | /// explicit function template specialization. On successful completion, |
| 6735 | /// the function declaration \p FD will become a function template |
| 6736 | /// specialization. |
| 6737 | /// |
| 6738 | /// \param FD the function declaration, which will be updated to become a |
| 6739 | /// function template specialization. |
| 6740 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6741 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 6742 | /// if any. Note that this may be valid info even when 0 arguments are |
| 6743 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 6744 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6745 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6746 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6747 | /// this function specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6748 | bool Sema::CheckFunctionTemplateSpecialization( |
| 6749 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 6750 | LookupResult &Previous) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6751 | // The set of function template specializations that could match this |
| 6752 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6753 | UnresolvedSet<8> Candidates; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6754 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6755 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6756 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6757 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6758 | I != E; ++I) { |
| 6759 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 6760 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6761 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6762 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6763 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 6764 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6765 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6766 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6767 | // When matching a constexpr member function template specialization |
| 6768 | // against the primary template, we don't yet know whether the |
| 6769 | // specialization has an implicit 'const' (because we don't know whether |
| 6770 | // it will be a static member function until we know which template it |
| 6771 | // specializes), so adjust it now assuming it specializes this template. |
| 6772 | QualType FT = FD->getType(); |
| 6773 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6774 | CXXMethodDecl *OldMD = |
| 6775 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6776 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6777 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6778 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6779 | EPI.TypeQuals |= Qualifiers::Const; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6780 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6781 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6782 | } |
| 6783 | } |
| 6784 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6785 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6786 | // A trailing template-argument can be left unspecified in the |
| 6787 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6788 | // provided it can be deduced from the function argument type. |
| 6789 | // Perform template argument deduction to determine whether we may be |
| 6790 | // specializing this template. |
| 6791 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6792 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6793 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 6794 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 6795 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
| 6796 | ExplicitTemplateArgs, FT, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6797 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6798 | // that we can provide nifty diagnostics. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6799 | FailedCandidates.addCandidate() |
| 6800 | .set(FunTmpl->getTemplatedDecl(), |
| 6801 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6802 | (void)TDK; |
| 6803 | continue; |
| 6804 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6805 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6806 | // Record this candidate. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6807 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6808 | } |
| 6809 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6810 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 6811 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6812 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 6813 | Candidates.begin(), Candidates.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6814 | FD->getLocation(), |
| 6815 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 6816 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6817 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6818 | PDiag(diag::note_function_template_spec_matched)); |
| 6819 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6820 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6821 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6822 | |
| 6823 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 6824 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 6825 | |
| 6826 | FunctionTemplateSpecializationInfo *SpecInfo |
| 6827 | = Specialization->getTemplateSpecializationInfo(); |
| 6828 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6829 | |
| 6830 | // Note: do not overwrite location info if previous template |
| 6831 | // specialization kind was explicit. |
| 6832 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6833 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6834 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6835 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 6836 | // function can differ from the template declaration with respect to |
| 6837 | // the constexpr specifier. |
| 6838 | Specialization->setConstexpr(FD->isConstexpr()); |
| 6839 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6840 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6841 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6842 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6843 | |
| 6844 | // If this is a friend declaration, then we're not really declaring |
| 6845 | // an explicit specialization. |
| 6846 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6847 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6848 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6849 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6850 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6851 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6852 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6853 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6854 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6855 | |
| 6856 | // C++ [temp.expl.spec]p6: |
| 6857 | // 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] | 6858 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6859 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6860 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6861 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6862 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6863 | if (!isFriend && |
| 6864 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6865 | TSK_ExplicitSpecialization, |
| 6866 | Specialization, |
| 6867 | SpecInfo->getTemplateSpecializationKind(), |
| 6868 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6869 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6870 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6871 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6872 | // Mark the prior declaration as an explicit specialization, so that later |
| 6873 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6874 | if (!isFriend) { |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6875 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6876 | MarkUnusedFileScopedDecl(Specialization); |
| 6877 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6878 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6879 | // Turn the given function declaration into a function template |
| 6880 | // specialization, with the template arguments from the previous |
| 6881 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6882 | // Take copies of (semantic and syntactic) template argument lists. |
| 6883 | const TemplateArgumentList* TemplArgs = new (Context) |
| 6884 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 6885 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6886 | TemplArgs, /*InsertPos=*/nullptr, |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6887 | SpecInfo->getTemplateSpecializationKind(), |
Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 6888 | ExplicitTemplateArgs); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6889 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6890 | // The "previous declaration" for this function template specialization is |
| 6891 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6892 | Previous.clear(); |
| 6893 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6894 | return false; |
| 6895 | } |
| 6896 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6897 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6898 | /// specialization. |
| 6899 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6900 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6901 | /// explicit member function specialization. On successful completion, |
| 6902 | /// the function declaration \p FD will become a member function |
| 6903 | /// specialization. |
| 6904 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6905 | /// \param Member the member declaration, which will be updated to become a |
| 6906 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6907 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6908 | /// \param Previous the set of declarations, one of which may be specialized |
| 6909 | /// by this function specialization; the set will be modified to contain the |
| 6910 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6911 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6912 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6913 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 6914 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6915 | // Try to find the member we are instantiating. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6916 | NamedDecl *Instantiation = nullptr; |
| 6917 | NamedDecl *InstantiatedFrom = nullptr; |
| 6918 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6919 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6920 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6921 | // Nowhere to look anyway. |
| 6922 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6923 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6924 | I != E; ++I) { |
| 6925 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 6926 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 6927 | QualType Adjusted = Function->getType(); |
| 6928 | if (!hasExplicitCallingConv(Adjusted)) |
| 6929 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
| 6930 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6931 | Instantiation = Method; |
| 6932 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6933 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6934 | break; |
| 6935 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6936 | } |
| 6937 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6938 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6939 | VarDecl *PrevVar; |
| 6940 | if (Previous.isSingleResult() && |
| 6941 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6942 | if (PrevVar->isStaticDataMember()) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6943 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6944 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6945 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6946 | } |
| 6947 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6948 | CXXRecordDecl *PrevRecord; |
| 6949 | if (Previous.isSingleResult() && |
| 6950 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 6951 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6952 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6953 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6954 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6955 | } else if (isa<EnumDecl>(Member)) { |
| 6956 | EnumDecl *PrevEnum; |
| 6957 | if (Previous.isSingleResult() && |
| 6958 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
| 6959 | Instantiation = PrevEnum; |
| 6960 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 6961 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 6962 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6963 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6964 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6965 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6966 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6967 | // specializations are always out-of-line, the caller will complain about |
| 6968 | // this mismatch later. |
| 6969 | return false; |
| 6970 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 6971 | |
| 6972 | // If this is a friend, just bail out here before we start turning |
| 6973 | // things into explicit specializations. |
| 6974 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 6975 | // Preserve instantiation information. |
| 6976 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 6977 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 6978 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 6979 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 6980 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 6981 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 6982 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 6983 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 6984 | } |
| 6985 | |
| 6986 | Previous.clear(); |
| 6987 | Previous.addDecl(Instantiation); |
| 6988 | return false; |
| 6989 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6990 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6991 | // Make sure that this is a specialization of a member. |
| 6992 | if (!InstantiatedFrom) { |
| 6993 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 6994 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6995 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 6996 | return true; |
| 6997 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6998 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6999 | // C++ [temp.expl.spec]p6: |
| 7000 | // 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] | 7001 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7002 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7003 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7004 | // use occurs; no diagnostic is required. |
| 7005 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7006 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7007 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 7008 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 7009 | TSK_ExplicitSpecialization, |
| 7010 | Instantiation, |
| 7011 | MSInfo->getTemplateSpecializationKind(), |
| 7012 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7013 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7014 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7015 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7016 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7017 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7018 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7019 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 7020 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7021 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 7022 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7023 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7024 | // the original declaration to note that it is an explicit specialization |
| 7025 | // (if it was previously an implicit instantiation). This latter step |
| 7026 | // makes bookkeeping easier. |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7027 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7028 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 7029 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 7030 | TSK_ImplicitInstantiation) { |
| 7031 | InstantiationFunction->setTemplateSpecializationKind( |
| 7032 | TSK_ExplicitSpecialization); |
| 7033 | InstantiationFunction->setLocation(Member->getLocation()); |
| 7034 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7035 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7036 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 7037 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 7038 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7039 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7040 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7041 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 7042 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 7043 | TSK_ImplicitInstantiation) { |
| 7044 | InstantiationVar->setTemplateSpecializationKind( |
| 7045 | TSK_ExplicitSpecialization); |
| 7046 | InstantiationVar->setLocation(Member->getLocation()); |
| 7047 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7048 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7049 | cast<VarDecl>(Member)->setInstantiationOfStaticDataMember( |
| 7050 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 7051 | MarkUnusedFileScopedDecl(InstantiationVar); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7052 | } else if (isa<CXXRecordDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7053 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 7054 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 7055 | TSK_ImplicitInstantiation) { |
| 7056 | InstantiationClass->setTemplateSpecializationKind( |
| 7057 | TSK_ExplicitSpecialization); |
| 7058 | InstantiationClass->setLocation(Member->getLocation()); |
| 7059 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7060 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7061 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 7062 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 7063 | TSK_ExplicitSpecialization); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7064 | } else { |
| 7065 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); |
| 7066 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); |
| 7067 | if (InstantiationEnum->getTemplateSpecializationKind() == |
| 7068 | TSK_ImplicitInstantiation) { |
| 7069 | InstantiationEnum->setTemplateSpecializationKind( |
| 7070 | TSK_ExplicitSpecialization); |
| 7071 | InstantiationEnum->setLocation(Member->getLocation()); |
| 7072 | } |
| 7073 | |
| 7074 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( |
| 7075 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 7076 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7077 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7078 | // Save the caller the trouble of having to figure out which declaration |
| 7079 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7080 | Previous.clear(); |
| 7081 | Previous.addDecl(Instantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 7082 | return false; |
| 7083 | } |
| 7084 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7085 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7086 | /// |
| 7087 | /// \returns true if a serious error occurs, false otherwise. |
| 7088 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7089 | SourceLocation InstLoc, |
| 7090 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7091 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 7092 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7093 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7094 | if (CurContext->isRecord()) { |
| 7095 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 7096 | << D; |
| 7097 | return true; |
| 7098 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7099 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7100 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7101 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7102 | // template. If the name declared in the explicit instantiation is an |
| 7103 | // unqualified name, the explicit instantiation shall appear in the |
| 7104 | // namespace where its template is declared or, if that namespace is inline |
| 7105 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7106 | // |
| 7107 | // 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] | 7108 | if (WasQualifiedName) { |
| 7109 | if (CurContext->Encloses(OrigContext)) |
| 7110 | return false; |
| 7111 | } else { |
| 7112 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 7113 | return false; |
| 7114 | } |
| 7115 | |
| 7116 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 7117 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7118 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7119 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7120 | diag::err_explicit_instantiation_out_of_scope : |
| 7121 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7122 | << D << NS; |
| 7123 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7124 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7125 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7126 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 7127 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 7128 | << D << NS; |
| 7129 | } else |
| 7130 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7131 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7132 | diag::err_explicit_instantiation_must_be_global : |
| 7133 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 7134 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7135 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7136 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7137 | } |
| 7138 | |
| 7139 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 7140 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 7141 | if (!SS.isSet()) |
| 7142 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7143 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7144 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7145 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7146 | // or a static data member of a class template specialization, the name of |
| 7147 | // the class template specialization in the qualified-id for the member |
| 7148 | // name shall be a simple-template-id. |
| 7149 | // |
| 7150 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7151 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 7152 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7153 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7154 | if (isa<TemplateSpecializationType>(T)) |
| 7155 | return true; |
| 7156 | |
| 7157 | return false; |
| 7158 | } |
| 7159 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7160 | // Explicit instantiation of a class template specialization |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7161 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7162 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7163 | SourceLocation ExternLoc, |
| 7164 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7165 | unsigned TagSpec, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7166 | SourceLocation KWLoc, |
| 7167 | const CXXScopeSpec &SS, |
| 7168 | TemplateTy TemplateD, |
| 7169 | SourceLocation TemplateNameLoc, |
| 7170 | SourceLocation LAngleLoc, |
| 7171 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7172 | SourceLocation RAngleLoc, |
| 7173 | AttributeList *Attr) { |
| 7174 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 7175 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7176 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7177 | // Check that the specialization uses the same tag kind as the |
| 7178 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7179 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7180 | assert(Kind != TTK_Enum && |
| 7181 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7182 | |
| 7183 | if (isa<TypeAliasTemplateDecl>(TD)) { |
| 7184 | Diag(KWLoc, diag::err_tag_reference_non_tag) << Kind; |
| 7185 | Diag(TD->getTemplatedDecl()->getLocation(), |
| 7186 | diag::note_previous_use); |
| 7187 | return true; |
| 7188 | } |
| 7189 | |
| 7190 | ClassTemplateDecl *ClassTemplate = cast<ClassTemplateDecl>(TD); |
| 7191 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7192 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7193 | Kind, /*isDefinition*/false, KWLoc, |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7194 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7195 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7196 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7197 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7198 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7199 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7200 | diag::note_previous_use); |
| 7201 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7202 | } |
| 7203 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7204 | // C++0x [temp.explicit]p2: |
| 7205 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7206 | // definition and an explicit instantiation declaration. An explicit |
| 7207 | // instantiation declaration begins with the extern keyword. [...] |
Hans Wennborg | fd76d91 | 2015-01-15 21:18:30 +0000 | [diff] [blame] | 7208 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 7209 | ? TSK_ExplicitInstantiationDefinition |
| 7210 | : TSK_ExplicitInstantiationDeclaration; |
| 7211 | |
| 7212 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 7213 | // Check for dllexport class template instantiation declarations. |
| 7214 | for (AttributeList *A = Attr; A; A = A->getNext()) { |
| 7215 | if (A->getKind() == AttributeList::AT_DLLExport) { |
| 7216 | Diag(ExternLoc, |
| 7217 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7218 | Diag(A->getLoc(), diag::note_attribute); |
| 7219 | break; |
| 7220 | } |
| 7221 | } |
| 7222 | |
| 7223 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 7224 | Diag(ExternLoc, |
| 7225 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 7226 | Diag(A->getLocation(), diag::note_attribute); |
| 7227 | } |
| 7228 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7229 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7230 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7231 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 7232 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7233 | |
| 7234 | // Check that the template argument list is well-formed for this |
| 7235 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7236 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7237 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7238 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7239 | return true; |
| 7240 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7241 | // Find the class template specialization declaration that |
| 7242 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7243 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7244 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7245 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7246 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7247 | TemplateSpecializationKind PrevDecl_TSK |
| 7248 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 7249 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7250 | // C++0x [temp.explicit]p2: |
| 7251 | // [...] An explicit instantiation shall appear in an enclosing |
| 7252 | // namespace of its template. [...] |
| 7253 | // |
| 7254 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7255 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 7256 | SS.isSet())) |
| 7257 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7258 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7259 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7260 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7261 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7262 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7263 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7264 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7265 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7266 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7267 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7268 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7269 | // Even though HasNoEffect == true means that this explicit instantiation |
| 7270 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 7271 | |
| 7272 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 7273 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7274 | // Since the only prior class template specialization with these |
| 7275 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7276 | // declaration node as our own, updating the source location |
| 7277 | // for the template name to reflect our new declaration. |
| 7278 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7279 | Specialization = PrevDecl; |
| 7280 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7281 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7282 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7283 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7284 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7285 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7286 | // Create a new class template specialization declaration node for |
| 7287 | // this explicit specialization. |
| 7288 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7289 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7290 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7291 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7292 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7293 | Converted.data(), |
| 7294 | Converted.size(), |
| 7295 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7296 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7297 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7298 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7299 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7300 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7301 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7302 | } |
| 7303 | |
| 7304 | // Build the fully-sugared type for this explicit instantiation as |
| 7305 | // the user wrote in the explicit instantiation itself. This means |
| 7306 | // that we'll pretty-print the type retrieved from the |
| 7307 | // specialization's declaration the way that the user actually wrote |
| 7308 | // the explicit instantiation, rather than formatting the name based |
| 7309 | // on the "canonical" representation used to store the template |
| 7310 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7311 | TypeSourceInfo *WrittenTy |
| 7312 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7313 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7314 | Context.getTypeDeclType(Specialization)); |
| 7315 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7316 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7317 | // Set source locations for keywords. |
| 7318 | Specialization->setExternLoc(ExternLoc); |
| 7319 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | 40bcfd7 | 2013-04-22 23:23:42 +0000 | [diff] [blame] | 7320 | Specialization->setRBraceLoc(SourceLocation()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7321 | |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 7322 | if (Attr) |
| 7323 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 7324 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7325 | // Add the explicit instantiation into its lexical context. However, |
| 7326 | // since explicit instantiations are never found by name lookup, we |
| 7327 | // just put it into the declaration context directly. |
| 7328 | Specialization->setLexicalDeclContext(CurContext); |
| 7329 | CurContext->addDecl(Specialization); |
| 7330 | |
| 7331 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 7332 | if (HasNoEffect) { |
| 7333 | // Set the template specialization kind. |
| 7334 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7335 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 7336 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7337 | |
| 7338 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7339 | // A definition of a class template or class member template |
| 7340 | // shall be in scope at the point of the explicit instantiation of |
| 7341 | // the class template or class member template. |
| 7342 | // |
| 7343 | // This check comes when we actually try to perform the |
| 7344 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7345 | ClassTemplateSpecializationDecl *Def |
| 7346 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7347 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7348 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7349 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7350 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7351 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7352 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 7353 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7354 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7355 | // Instantiate the members of this class template specialization. |
| 7356 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7357 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7358 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7359 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 7360 | |
| 7361 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 7362 | // TSK_ExplicitInstantiationDefinition |
| 7363 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 7364 | TSK == TSK_ExplicitInstantiationDefinition) |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7365 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7366 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7367 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7368 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7369 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7370 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7371 | // Set the template specialization kind. |
| 7372 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7373 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7374 | } |
| 7375 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7376 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7377 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7378 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7379 | SourceLocation ExternLoc, |
| 7380 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7381 | unsigned TagSpec, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7382 | SourceLocation KWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7383 | CXXScopeSpec &SS, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7384 | IdentifierInfo *Name, |
| 7385 | SourceLocation NameLoc, |
| 7386 | AttributeList *Attr) { |
| 7387 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 7388 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7389 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7390 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7391 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7392 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7393 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 7394 | SourceLocation(), false, TypeResult(), |
| 7395 | /*IsTypeSpecifier*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7396 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 7397 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7398 | if (!TagD) |
| 7399 | return true; |
| 7400 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7401 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7402 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7403 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 7404 | if (Tag->isInvalidDecl()) |
| 7405 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7406 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7407 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 7408 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 7409 | if (!Pattern) { |
| 7410 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 7411 | << Context.getTypeDeclType(Record); |
| 7412 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 7413 | return true; |
| 7414 | } |
| 7415 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7416 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7417 | // If the explicit instantiation is for a class or member class, the |
| 7418 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7419 | // simple-template-id. |
| 7420 | // |
| 7421 | // C++98 has the same restriction, just worded differently. |
| 7422 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7423 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7424 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7425 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7426 | // C++0x [temp.explicit]p2: |
| 7427 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7428 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7429 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 7430 | TemplateSpecializationKind TSK |
| 7431 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7432 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7433 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7434 | // C++0x [temp.explicit]p2: |
| 7435 | // [...] An explicit instantiation shall appear in an enclosing |
| 7436 | // namespace of its template. [...] |
| 7437 | // |
| 7438 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7439 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7440 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7441 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7442 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7443 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7444 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7445 | PrevDecl = Record; |
| 7446 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7447 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7448 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7449 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7450 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7451 | PrevDecl, |
| 7452 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7453 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7454 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7455 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7456 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7457 | return TagD; |
| 7458 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7459 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7460 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7461 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7462 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7463 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7464 | // 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] | 7465 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7466 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7467 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7468 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 7469 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 7470 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7471 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 7472 | << Pattern; |
| 7473 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7474 | } else { |
| 7475 | if (InstantiateClass(NameLoc, Record, Def, |
| 7476 | getTemplateInstantiationArgs(Record), |
| 7477 | TSK)) |
| 7478 | return true; |
| 7479 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7480 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7481 | if (!RecordDef) |
| 7482 | return true; |
| 7483 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7484 | } |
| 7485 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7486 | // Instantiate all of the members of the class. |
| 7487 | InstantiateClassMembers(NameLoc, RecordDef, |
| 7488 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7489 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7490 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7491 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 7492 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7493 | // FIXME: We don't have any representation for explicit instantiations of |
| 7494 | // member classes. Such a representation is not needed for compilation, but it |
| 7495 | // should be available for clients that want to see all of the declarations in |
| 7496 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7497 | return TagD; |
| 7498 | } |
| 7499 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7500 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 7501 | SourceLocation ExternLoc, |
| 7502 | SourceLocation TemplateLoc, |
| 7503 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7504 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7505 | // TODO: check if/when DNInfo should replace Name. |
| 7506 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 7507 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7508 | if (!Name) { |
| 7509 | if (!D.isInvalidType()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7510 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7511 | diag::err_explicit_instantiation_requires_name) |
| 7512 | << D.getDeclSpec().getSourceRange() |
| 7513 | << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7514 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7515 | return true; |
| 7516 | } |
| 7517 | |
| 7518 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 7519 | // we find one that is. |
| 7520 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 7521 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 7522 | S = S->getParent(); |
| 7523 | |
| 7524 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7525 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 7526 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7527 | if (R.isNull()) |
| 7528 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7529 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7530 | // C++ [dcl.stc]p1: |
| 7531 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 7532 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7533 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7534 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 7535 | << Name; |
| 7536 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7537 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 7538 | != DeclSpec::SCS_unspecified) { |
| 7539 | // Complain about then remove the storage class specifier. |
| 7540 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 7541 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 7542 | |
| 7543 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7544 | } |
| 7545 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 7546 | // C++0x [temp.explicit]p1: |
| 7547 | // [...] An explicit instantiation of a function template shall not use the |
| 7548 | // inline or constexpr specifiers. |
| 7549 | // Presumably, this also applies to member functions of class templates as |
| 7550 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7551 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7552 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7553 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7554 | diag::err_explicit_instantiation_inline : |
| 7555 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7556 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7557 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7558 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 7559 | // not already specified. |
| 7560 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 7561 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7562 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7563 | // C++0x [temp.explicit]p2: |
| 7564 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7565 | // definition and an explicit instantiation declaration. An explicit |
| 7566 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7567 | TemplateSpecializationKind TSK |
| 7568 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7569 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7570 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7571 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7572 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7573 | |
| 7574 | if (!R->isFunctionType()) { |
| 7575 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7576 | // A [...] static data member of a class template can be explicitly |
| 7577 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7578 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7579 | // C++1y [temp.explicit]p1: |
| 7580 | // A [...] variable [...] template specialization can be explicitly |
| 7581 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7582 | if (Previous.isAmbiguous()) |
| 7583 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7584 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 7585 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7586 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7587 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7588 | if (!PrevTemplate) { |
| 7589 | if (!Prev || !Prev->isStaticDataMember()) { |
| 7590 | // We expect to see a data data member here. |
| 7591 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 7592 | << Name; |
| 7593 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7594 | P != PEnd; ++P) |
| 7595 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 7596 | return true; |
| 7597 | } |
| 7598 | |
| 7599 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 7600 | // FIXME: Check for explicit specialization? |
| 7601 | Diag(D.getIdentifierLoc(), |
| 7602 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 7603 | << Prev; |
| 7604 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 7605 | // FIXME: Can we provide a note showing where this was declared? |
| 7606 | return true; |
| 7607 | } |
| 7608 | } else { |
| 7609 | // Explicitly instantiate a variable template. |
| 7610 | |
| 7611 | // C++1y [dcl.spec.auto]p6: |
| 7612 | // ... A program that uses auto or decltype(auto) in a context not |
| 7613 | // explicitly allowed in this section is ill-formed. |
| 7614 | // |
| 7615 | // This includes auto-typed variable template instantiations. |
| 7616 | if (R->isUndeducedType()) { |
| 7617 | Diag(T->getTypeLoc().getLocStart(), |
| 7618 | diag::err_auto_not_allowed_var_inst); |
| 7619 | return true; |
| 7620 | } |
| 7621 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7622 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 7623 | // C++1y [temp.explicit]p3: |
| 7624 | // If the explicit instantiation is for a variable, the unqualified-id |
| 7625 | // in the declaration shall be a template-id. |
| 7626 | Diag(D.getIdentifierLoc(), |
| 7627 | diag::err_explicit_instantiation_without_template_id) |
| 7628 | << PrevTemplate; |
| 7629 | Diag(PrevTemplate->getLocation(), |
| 7630 | diag::note_explicit_instantiation_here); |
| 7631 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7632 | } |
| 7633 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7634 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7635 | TemplateArgumentListInfo TemplateArgs = |
| 7636 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7637 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7638 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 7639 | D.getIdentifierLoc(), TemplateArgs); |
| 7640 | if (Res.isInvalid()) |
| 7641 | return true; |
| 7642 | |
| 7643 | // Ignore access control bits, we don't need them for redeclaration |
| 7644 | // checking. |
| 7645 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7646 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7647 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7648 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7649 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7650 | // or a static data member of a class template specialization, the name of |
| 7651 | // the class template specialization in the qualified-id for the member |
| 7652 | // name shall be a simple-template-id. |
| 7653 | // |
| 7654 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7655 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 7656 | // This does not apply to variable template specializations, where the |
| 7657 | // template-id is in the unqualified-id instead. |
| 7658 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7659 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7660 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7661 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7662 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7663 | // Check the scope of this explicit instantiation. |
| 7664 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7665 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7666 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 7667 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 7668 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7669 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7670 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7671 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7672 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7673 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7674 | if (!HasNoEffect) { |
| 7675 | // Instantiate static data member or variable template. |
| 7676 | |
| 7677 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 7678 | if (PrevTemplate) { |
| 7679 | // Merge attributes. |
| 7680 | if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList()) |
| 7681 | ProcessDeclAttributeList(S, Prev, Attr); |
| 7682 | } |
| 7683 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7684 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 7685 | } |
| 7686 | |
| 7687 | // Check the new variable specialization against the parsed input. |
| 7688 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
| 7689 | Diag(T->getTypeLoc().getLocStart(), |
| 7690 | diag::err_invalid_var_template_spec_type) |
| 7691 | << 0 << PrevTemplate << R << Prev->getType(); |
| 7692 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 7693 | << 2 << PrevTemplate->getDeclName(); |
| 7694 | return true; |
| 7695 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7696 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7697 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7698 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7699 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7700 | |
| 7701 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 7702 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7703 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7704 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7705 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7706 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7707 | HasExplicitTemplateArgs = true; |
| 7708 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7709 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7710 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7711 | // A [...] function [...] can be explicitly instantiated from its template. |
| 7712 | // A member function [...] of a class template can be explicitly |
| 7713 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7714 | // template. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7715 | UnresolvedSet<8> Matches; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7716 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7717 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7718 | P != PEnd; ++P) { |
| 7719 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7720 | if (!HasExplicitTemplateArgs) { |
| 7721 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 7722 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType()); |
| 7723 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7724 | Matches.clear(); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7725 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7726 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7727 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 7728 | break; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7729 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7730 | } |
| 7731 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7732 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7733 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 7734 | if (!FunTmpl) |
| 7735 | continue; |
| 7736 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7737 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7738 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7739 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7740 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7741 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 7742 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7743 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7744 | // Keep track of almost-matches. |
| 7745 | FailedCandidates.addCandidate() |
| 7746 | .set(FunTmpl->getTemplatedDecl(), |
| 7747 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7748 | (void)TDK; |
| 7749 | continue; |
| 7750 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7751 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7752 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7753 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7754 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7755 | // Find the most specialized function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7756 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 7757 | Matches.begin(), Matches.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7758 | D.getIdentifierLoc(), |
| 7759 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 7760 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 7761 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7762 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7763 | if (Result == Matches.end()) |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7764 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7765 | |
| 7766 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 7767 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7768 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 7769 | // C++11 [except.spec]p4 |
| 7770 | // In an explicit instantiation an exception-specification may be specified, |
| 7771 | // but is not required. |
| 7772 | // If an exception-specification is specified in an explicit instantiation |
| 7773 | // directive, it shall be compatible with the exception-specifications of |
| 7774 | // other declarations of that function. |
| 7775 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 7776 | if (FPT->hasExceptionSpec()) { |
| 7777 | unsigned DiagID = |
| 7778 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 7779 | if (getLangOpts().MicrosoftExt) |
| 7780 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 7781 | bool Result = CheckEquivalentExceptionSpec( |
| 7782 | PDiag(DiagID) << Specialization->getType(), |
| 7783 | PDiag(diag::note_explicit_instantiation_here), |
| 7784 | Specialization->getType()->getAs<FunctionProtoType>(), |
| 7785 | Specialization->getLocation(), FPT, D.getLocStart()); |
| 7786 | // In Microsoft mode, mismatching exception specifications just cause a |
| 7787 | // warning. |
| 7788 | if (!getLangOpts().MicrosoftExt && Result) |
| 7789 | return true; |
| 7790 | } |
| 7791 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7792 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7793 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7794 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 7795 | << Specialization |
| 7796 | << (Specialization->getTemplateSpecializationKind() == |
| 7797 | TSK_ExplicitSpecialization); |
| 7798 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 7799 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7800 | } |
| 7801 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7802 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7803 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 7804 | PrevDecl = Specialization; |
| 7805 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7806 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7807 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7808 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7809 | PrevDecl, |
| 7810 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7811 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7812 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7813 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7814 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7815 | // FIXME: We may still want to build some representation of this |
| 7816 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7817 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7818 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7819 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 7820 | |
| 7821 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 2aa7acf | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 7822 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 7823 | if (Attr) |
| 7824 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7825 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7826 | if (Specialization->isDefined()) { |
| 7827 | // Let the ASTConsumer know that this function has been explicitly |
| 7828 | // instantiated now, and its linkage might have changed. |
| 7829 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 7830 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 7831 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7832 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7833 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7834 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7835 | // or a static data member of a class template specialization, the name of |
| 7836 | // the class template specialization in the qualified-id for the member |
| 7837 | // name shall be a simple-template-id. |
| 7838 | // |
| 7839 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7840 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7841 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7842 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7843 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7844 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7845 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7846 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7847 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7848 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7849 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7850 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7851 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7852 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7853 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7854 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7855 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7856 | } |
| 7857 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7858 | TypeResult |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7859 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 7860 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 7861 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 7862 | // This has to hold, because SS is expected to be defined. |
| 7863 | assert(Name && "Expected a name in a dependent tag"); |
| 7864 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7865 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7866 | if (!NNS) |
| 7867 | return true; |
| 7868 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7869 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 7870 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 7871 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 7872 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7873 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 7874 | return true; |
| 7875 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7876 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7877 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7878 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7879 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 7880 | |
| 7881 | // Create type-source location information for this type. |
| 7882 | TypeLocBuilder TLB; |
| 7883 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7884 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7885 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 7886 | TL.setNameLoc(NameLoc); |
| 7887 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7888 | } |
| 7889 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7890 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7891 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 7892 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 7893 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7894 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7895 | return true; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7896 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7897 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 7898 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7899 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7900 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 7901 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7902 | << FixItHint::CreateRemoval(TypenameLoc); |
| 7903 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 7904 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 7905 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 7906 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 7907 | if (T.isNull()) |
| 7908 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7909 | |
| 7910 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 7911 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7912 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7913 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 7914 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 7915 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7916 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7917 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7918 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 7919 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7920 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7921 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7922 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7923 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7924 | } |
| 7925 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7926 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7927 | Sema::ActOnTypenameType(Scope *S, |
| 7928 | SourceLocation TypenameLoc, |
| 7929 | const CXXScopeSpec &SS, |
| 7930 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7931 | TemplateTy TemplateIn, |
| 7932 | SourceLocation TemplateNameLoc, |
| 7933 | SourceLocation LAngleLoc, |
| 7934 | ASTTemplateArgsPtr TemplateArgsIn, |
| 7935 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7936 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 7937 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7938 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7939 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 7940 | diag::ext_typename_outside_of_template) |
| 7941 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7942 | |
| 7943 | // Translate the parser's template argument list in our AST format. |
| 7944 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 7945 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 7946 | |
| 7947 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7948 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 7949 | // Construct a dependent template specialization type. |
| 7950 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7951 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7952 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 7953 | DTN->getQualifier(), |
| 7954 | DTN->getIdentifier(), |
| 7955 | TemplateArgs); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7956 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7957 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 7958 | TypeLocBuilder Builder; |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7959 | DependentTemplateSpecializationTypeLoc SpecTL |
| 7960 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7961 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 7962 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 7963 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7964 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7965 | SpecTL.setLAngleLoc(LAngleLoc); |
| 7966 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7967 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 7968 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7969 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 7970 | } |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7971 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7972 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 7973 | if (T.isNull()) |
| 7974 | return true; |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7975 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7976 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7977 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7978 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7979 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7980 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 7981 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7982 | SpecTL.setLAngleLoc(LAngleLoc); |
| 7983 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7984 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 7985 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 7986 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7987 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 7988 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7989 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 7990 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 7991 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7992 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 7993 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 7994 | } |
| 7995 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7996 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7997 | /// Determine whether this failed name lookup should be treated as being |
| 7998 | /// disabled by a usage of std::enable_if. |
| 7999 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
| 8000 | SourceRange &CondRange) { |
| 8001 | // We must be looking for a ::type... |
| 8002 | if (!II.isStr("type")) |
| 8003 | return false; |
| 8004 | |
| 8005 | // ... within an explicitly-written template specialization... |
| 8006 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 8007 | return false; |
| 8008 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8009 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 8010 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 8011 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8012 | return false; |
| 8013 | const TemplateSpecializationType *EnableIfTST = |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8014 | cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr()); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8015 | |
| 8016 | // ... which names a complete class template declaration... |
| 8017 | const TemplateDecl *EnableIfDecl = |
| 8018 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 8019 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 8020 | return false; |
| 8021 | |
| 8022 | // ... called "enable_if". |
| 8023 | const IdentifierInfo *EnableIfII = |
| 8024 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 8025 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 8026 | return false; |
| 8027 | |
| 8028 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 8029 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8030 | return true; |
| 8031 | } |
| 8032 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8033 | /// \brief Build the type that describes a C++ typename specifier, |
| 8034 | /// e.g., "typename T::type". |
| 8035 | QualType |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8036 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 8037 | SourceLocation KeywordLoc, |
| 8038 | NestedNameSpecifierLoc QualifierLoc, |
| 8039 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8040 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8041 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8042 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8043 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8044 | DeclContext *Ctx = computeDeclContext(SS); |
| 8045 | if (!Ctx) { |
| 8046 | // If the nested-name-specifier is dependent and couldn't be |
| 8047 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8048 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 8049 | return Context.getDependentNameType(Keyword, |
| 8050 | QualifierLoc.getNestedNameSpecifier(), |
| 8051 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 8052 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8053 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8054 | // If the nested-name-specifier refers to the current instantiation, |
| 8055 | // the "typename" keyword itself is superfluous. In C++03, the |
| 8056 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 8057 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 8058 | // 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] | 8059 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 8060 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 8061 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8062 | |
| 8063 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8064 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | fce370e | 2014-12-01 23:15:01 +0000 | [diff] [blame] | 8065 | LookupQualifiedName(Result, Ctx, SS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8066 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 8067 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8068 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8069 | case LookupResult::NotFound: { |
| 8070 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 8071 | // a more specific diagnostic. |
| 8072 | SourceRange CondRange; |
| 8073 | if (isEnableIf(QualifierLoc, II, CondRange)) { |
| 8074 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
| 8075 | << Ctx << CondRange; |
| 8076 | return QualType(); |
| 8077 | } |
| 8078 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 8079 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8080 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 8081 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8082 | |
| 8083 | case LookupResult::FoundUnresolvedValue: { |
| 8084 | // We found a using declaration that is a value. Most likely, the using |
| 8085 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8086 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8087 | IILoc); |
| 8088 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 8089 | << Name << Ctx << FullRange; |
| 8090 | if (UnresolvedUsingValueDecl *Using |
| 8091 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 8092 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 8093 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 8094 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 8095 | } |
| 8096 | } |
| 8097 | // Fall through to create a dependent typename type, from which we can recover |
| 8098 | // better. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8099 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 8100 | case LookupResult::NotFoundInCurrentInstantiation: |
| 8101 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8102 | return Context.getDependentNameType(Keyword, |
| 8103 | QualifierLoc.getNestedNameSpecifier(), |
| 8104 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8105 | |
| 8106 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8107 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8108 | // We found a type. Build an ElaboratedType, since the |
| 8109 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 8110 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8111 | return Context.getElaboratedType(ETK_Typename, |
| 8112 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8113 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8114 | } |
| 8115 | |
| 8116 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 8117 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8118 | break; |
| 8119 | |
| 8120 | case LookupResult::FoundOverloaded: |
| 8121 | DiagID = diag::err_typename_nested_not_type; |
| 8122 | Referenced = *Result.begin(); |
| 8123 | break; |
| 8124 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 8125 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8126 | return QualType(); |
| 8127 | } |
| 8128 | |
| 8129 | // If we get here, it's because name lookup did not find a |
| 8130 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8131 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 8132 | IILoc); |
| 8133 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 8134 | if (Referenced) |
| 8135 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 8136 | << Name; |
| 8137 | return QualType(); |
| 8138 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8139 | |
| 8140 | namespace { |
| 8141 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 8142 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8143 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8144 | SourceLocation Loc; |
| 8145 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8146 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8147 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 8148 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8149 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8150 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8151 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8152 | DeclarationName Entity) |
| 8153 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8154 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8155 | |
| 8156 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8157 | /// transformed. |
| 8158 | /// |
| 8159 | /// For the purposes of type reconstruction, a type has already been |
| 8160 | /// transformed if it is NULL or if it is not dependent. |
| 8161 | bool AlreadyTransformed(QualType T) { |
| 8162 | return T.isNull() || !T->isDependentType(); |
| 8163 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8164 | |
| 8165 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8166 | /// rebuilt. |
| 8167 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8168 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8169 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 8170 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8171 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 8172 | /// \brief Sets the "base" location and entity when that |
| 8173 | /// information is known based on another transformation. |
| 8174 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 8175 | this->Loc = Loc; |
| 8176 | this->Entity = Entity; |
| 8177 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 8178 | |
| 8179 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 8180 | // Lambdas never need to be transformed. |
| 8181 | return E; |
| 8182 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8183 | }; |
| 8184 | } |
| 8185 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8186 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 8187 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8188 | /// 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] | 8189 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8190 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8191 | /// partial specialization thereof). This routine will rebuild that type now |
| 8192 | /// that we have entered the declarator's scope, which may produce different |
| 8193 | /// canonical types, e.g., |
| 8194 | /// |
| 8195 | /// \code |
| 8196 | /// template<typename T> |
| 8197 | /// struct X { |
| 8198 | /// typedef T* pointer; |
| 8199 | /// pointer data(); |
| 8200 | /// }; |
| 8201 | /// |
| 8202 | /// template<typename T> |
| 8203 | /// typename X<T>::pointer X<T>::data() { ... } |
| 8204 | /// \endcode |
| 8205 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 8206 | /// 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] | 8207 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 8208 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8209 | /// 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] | 8210 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 8211 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8212 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 8213 | SourceLocation Loc, |
| 8214 | DeclarationName Name) { |
| 8215 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8216 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8217 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8218 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 8219 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 8220 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8221 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8222 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8223 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 8224 | DeclarationName()); |
| 8225 | return Rebuilder.TransformExpr(E); |
| 8226 | } |
| 8227 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8228 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8229 | if (SS.isInvalid()) |
| 8230 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8231 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8232 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8233 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 8234 | DeclarationName()); |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8235 | NestedNameSpecifierLoc Rebuilt |
| 8236 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 8237 | if (!Rebuilt) |
| 8238 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8239 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8240 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8241 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8242 | } |
| 8243 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8244 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 8245 | /// instantiation. |
| 8246 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 8247 | TemplateParameterList *Params) { |
| 8248 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 8249 | Decl *Param = Params->getParam(I); |
| 8250 | |
| 8251 | // There is nothing to rebuild in a type parameter. |
| 8252 | if (isa<TemplateTypeParmDecl>(Param)) |
| 8253 | continue; |
| 8254 | |
| 8255 | // Rebuild the template parameter list of a template template parameter. |
| 8256 | if (TemplateTemplateParmDecl *TTP |
| 8257 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 8258 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 8259 | TTP->getTemplateParameters())) |
| 8260 | return true; |
| 8261 | |
| 8262 | continue; |
| 8263 | } |
| 8264 | |
| 8265 | // Rebuild the type of a non-type template parameter. |
| 8266 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 8267 | TypeSourceInfo *NewTSI |
| 8268 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 8269 | NTTP->getLocation(), |
| 8270 | NTTP->getDeclName()); |
| 8271 | if (!NewTSI) |
| 8272 | return true; |
| 8273 | |
| 8274 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 8275 | NTTP->setTypeSourceInfo(NewTSI); |
| 8276 | NTTP->setType(NewTSI->getType()); |
| 8277 | } |
| 8278 | } |
| 8279 | |
| 8280 | return false; |
| 8281 | } |
| 8282 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8283 | /// \brief Produces a formatted string that describes the binding of |
| 8284 | /// template parameters to template arguments. |
| 8285 | std::string |
| 8286 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8287 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8288 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8289 | } |
| 8290 | |
| 8291 | std::string |
| 8292 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8293 | const TemplateArgument *Args, |
| 8294 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8295 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8296 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8297 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8298 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8299 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8300 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8301 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8302 | if (I >= NumArgs) |
| 8303 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8304 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8305 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8306 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8307 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8308 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8309 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8310 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8311 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8312 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8313 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8314 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8315 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8316 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 8317 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8318 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8319 | |
| 8320 | Out << ']'; |
| 8321 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8322 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8323 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8324 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 8325 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8326 | if (!FD) |
| 8327 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8328 | |
| 8329 | LateParsedTemplate *LPT = new LateParsedTemplate; |
| 8330 | |
| 8331 | // Take tokens to avoid allocations |
| 8332 | LPT->Toks.swap(Toks); |
| 8333 | LPT->D = FnD; |
Chandler Carruth | 52cee4d | 2015-03-26 09:08:15 +0000 | [diff] [blame] | 8334 | LateParsedTemplateMap.insert(std::make_pair(FD, LPT)); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8335 | |
| 8336 | FD->setLateTemplateParsed(true); |
| 8337 | } |
| 8338 | |
| 8339 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 8340 | if (!FD) |
| 8341 | return; |
| 8342 | FD->setLateTemplateParsed(false); |
| 8343 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8344 | |
| 8345 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 8346 | DeclContext *DC = CurContext; |
| 8347 | |
| 8348 | while (DC) { |
| 8349 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 8350 | const FunctionDecl *FD = RD->isLocalClass(); |
| 8351 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 8352 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 8353 | return false; |
| 8354 | |
| 8355 | DC = DC->getParent(); |
| 8356 | } |
| 8357 | return false; |
| 8358 | } |