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)) |
| 110 | if (!ClassTemplates.insert(ClassTmpl)) { |
| 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 |
| 321 | CorrectionCandidateCallback FilterCCC; |
| 322 | FilterCCC.WantTypeSpecifiers = false; |
| 323 | FilterCCC.WantExpressionKeywords = false; |
| 324 | FilterCCC.WantRemainingKeywords = false; |
| 325 | FilterCCC.WantCXXNamedCasts = true; |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 326 | if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(), |
| 327 | Found.getLookupKind(), S, &SS, |
John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 328 | FilterCCC, CTK_ErrorRecovery, |
| 329 | LookupCtx)) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 330 | Found.setLookupName(Corrected.getCorrection()); |
| 331 | if (Corrected.getCorrectionDecl()) |
| 332 | Found.addDecl(Corrected.getCorrectionDecl()); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 333 | FilterAcceptableTemplateNames(Found); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 334 | if (!Found.empty()) { |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 335 | if (LookupCtx) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 336 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 337 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 338 | Name.getAsString() == CorrectedStr; |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 339 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
| 340 | << Name << LookupCtx << DroppedSpecifier |
| 341 | << SS.getRange()); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 342 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 343 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 344 | } |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 345 | } |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 346 | } else { |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 347 | Found.setLookupName(Name); |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 351 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 352 | if (Found.empty()) { |
| 353 | if (isDependent) |
| 354 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 355 | return; |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 356 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 357 | |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 358 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && |
Richard Smith | e7d67f2 | 2013-09-03 21:22:41 +0000 | [diff] [blame] | 359 | !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 360 | // C++03 [basic.lookup.classref]p1: |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 361 | // [...] If the lookup in the class of the object expression finds a |
| 362 | // template, the name is also looked up in the context of the entire |
| 363 | // postfix-expression and [...] |
| 364 | // |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 365 | // Note: C++11 does not perform this second lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 366 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 367 | LookupOrdinaryName); |
| 368 | LookupName(FoundOuter, S); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 369 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 370 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 371 | if (FoundOuter.empty()) { |
| 372 | // - if the name is not found, the name found in the class of the |
| 373 | // object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 374 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 375 | FoundOuter.isAmbiguous()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 376 | // - if the name is found in the context of the entire |
| 377 | // postfix-expression and does not name a class template, the name |
| 378 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 379 | FoundOuter.clear(); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 380 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 381 | // - if the name found is a class template, it must refer to the same |
| 382 | // entity as the one found in the class of the object expression, |
| 383 | // otherwise the program is ill-formed. |
| 384 | if (!Found.isSingleResult() || |
| 385 | Found.getFoundDecl()->getCanonicalDecl() |
| 386 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 387 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 2f96e9f | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 388 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 389 | << Found.getLookupName() |
| 390 | << ObjectType; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 391 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 392 | diag::note_ambig_member_ref_object_type) |
| 393 | << ObjectType; |
| 394 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 395 | diag::note_ambig_member_ref_scope); |
| 396 | |
| 397 | // Recover by taking the template that we found in the object |
| 398 | // expression's type. |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 404 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 405 | /// was just parsed. This is only possible with an explicit scope |
| 406 | /// specifier naming a dependent type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 407 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 408 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 409 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 410 | const DeclarationNameInfo &NameInfo, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 411 | bool isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 412 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 413 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 414 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 415 | if (!isAddressOfOperand && |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 416 | isa<CXXMethodDecl>(DC) && |
| 417 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 418 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 419 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 420 | // Since the 'this' expression is synthesized, we don't need to |
| 421 | // perform the double-lookup check. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 422 | NamedDecl *FirstQualifierInScope = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 423 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 424 | return CXXDependentScopeMemberExpr::Create( |
| 425 | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
| 426 | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
| 427 | FirstQualifierInScope, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 430 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 431 | } |
| 432 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 433 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 434 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 435 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 436 | const DeclarationNameInfo &NameInfo, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 437 | const TemplateArgumentListInfo *TemplateArgs) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 438 | return DependentScopeDeclRefExpr::Create( |
| 439 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 440 | TemplateArgs); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 443 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 444 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 445 | /// declaration at location Loc. Returns true to indicate that this is |
| 446 | /// an error, and false otherwise. |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 447 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 448 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 449 | |
| 450 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 451 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 452 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 453 | |
| 454 | // C++ [temp.local]p4: |
| 455 | // A template-parameter shall not be redeclared within its |
| 456 | // scope (including nested scopes). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 458 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 459 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 460 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 463 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 464 | /// the parameter D to reference the templated declaration and return a pointer |
| 465 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 466 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 467 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 468 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 469 | return Temp; |
| 470 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 471 | return nullptr; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 474 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 475 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 476 | assert(Kind == Template && |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 477 | "Only template template arguments can be pack expansions here"); |
| 478 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 479 | "Template template argument pack expansion without packs"); |
| 480 | ParsedTemplateArgument Result(*this); |
| 481 | Result.EllipsisLoc = EllipsisLoc; |
| 482 | return Result; |
| 483 | } |
| 484 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 485 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 486 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 487 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 488 | switch (Arg.getKind()) { |
| 489 | case ParsedTemplateArgument::Type: { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 490 | TypeSourceInfo *DI; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 491 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 492 | if (!DI) |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 493 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 494 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 495 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 496 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 497 | case ParsedTemplateArgument::NonType: { |
| 498 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 499 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 500 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 501 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 502 | case ParsedTemplateArgument::Template: { |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 503 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 504 | TemplateArgument TArg; |
| 505 | if (Arg.getEllipsisLoc().isValid()) |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 506 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 507 | else |
| 508 | TArg = Template; |
| 509 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 510 | Arg.getScopeSpec().getWithLocInContext( |
| 511 | SemaRef.Context), |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 512 | Arg.getLocation(), |
| 513 | Arg.getEllipsisLoc()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 514 | } |
| 515 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 516 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 517 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 518 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 519 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 520 | /// \brief Translates template arguments as provided by the parser |
| 521 | /// into template arguments used by semantic analysis. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 522 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 523 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 524 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 525 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 526 | TemplateArgsIn[I])); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 527 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 528 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 529 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 530 | SourceLocation Loc, |
| 531 | IdentifierInfo *Name) { |
| 532 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
| 533 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 534 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 535 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 536 | } |
| 537 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 538 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 539 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 540 | /// the keyword "typename" was used to declare the type parameter |
| 541 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 542 | /// "class" or "typename" keyword. ParamName is the name of the |
| 543 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 544 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 545 | /// If the type parameter has a default argument, it will be added |
| 546 | /// later via ActOnTypeParameterDefault. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 547 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 548 | SourceLocation EllipsisLoc, |
| 549 | SourceLocation KeyLoc, |
| 550 | IdentifierInfo *ParamName, |
| 551 | SourceLocation ParamNameLoc, |
| 552 | unsigned Depth, unsigned Position, |
| 553 | SourceLocation EqualLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 554 | ParsedType DefaultArg) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 555 | assert(S->isTemplateParamScope() && |
| 556 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 557 | bool Invalid = false; |
| 558 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 559 | SourceLocation Loc = ParamNameLoc; |
| 560 | if (!ParamName) |
| 561 | Loc = KeyLoc; |
| 562 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 563 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 564 | TemplateTypeParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 565 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 566 | KeyLoc, Loc, Depth, Position, ParamName, |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 567 | Typename, IsParameterPack); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 568 | Param->setAccess(AS_public); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 569 | if (Invalid) |
| 570 | Param->setInvalidDecl(); |
| 571 | |
| 572 | if (ParamName) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 573 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 574 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 575 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 576 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 577 | IdResolver.AddDecl(Param); |
| 578 | } |
| 579 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 580 | // C++0x [temp.param]p9: |
| 581 | // A default template-argument may be specified for any kind of |
| 582 | // template-parameter that is not a template parameter pack. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 583 | if (DefaultArg && IsParameterPack) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 584 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 585 | DefaultArg = ParsedType(); |
| 586 | } |
| 587 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 588 | // Handle the default argument, if provided. |
| 589 | if (DefaultArg) { |
| 590 | TypeSourceInfo *DefaultTInfo; |
| 591 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 592 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 593 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 594 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 595 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 596 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 597 | UPPC_DefaultArgument)) |
| 598 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 599 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 600 | // Check the template argument itself. |
| 601 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 602 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 603 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 604 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 605 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 606 | Param->setDefaultArgument(DefaultTInfo, false); |
| 607 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 608 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 609 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 612 | /// \brief Check that the type of a non-type template parameter is |
| 613 | /// well-formed. |
| 614 | /// |
| 615 | /// \returns the (possibly-promoted) parameter type if valid; |
| 616 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 617 | QualType |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 618 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 619 | // We don't allow variably-modified types as the type of non-type template |
| 620 | // parameters. |
| 621 | if (T->isVariablyModifiedType()) { |
| 622 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 623 | << T; |
| 624 | return QualType(); |
| 625 | } |
| 626 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 627 | // C++ [temp.param]p4: |
| 628 | // |
| 629 | // A non-type template-parameter shall have one of the following |
| 630 | // (optionally cv-qualified) types: |
| 631 | // |
| 632 | // -- integral or enumeration type, |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 633 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | // -- pointer to object or pointer to function, |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 635 | T->isPointerType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 636 | // -- reference to object or reference to function, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 637 | T->isReferenceType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 638 | // -- pointer to member, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 639 | T->isMemberPointerType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 640 | // -- std::nullptr_t. |
| 641 | T->isNullPtrType() || |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 642 | // If T is a dependent type, we can't do the check now, so we |
| 643 | // assume that it is well-formed. |
Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 644 | T->isDependentType()) { |
| 645 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 646 | // are ignored when determining its type. |
| 647 | return T.getUnqualifiedType(); |
| 648 | } |
| 649 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 650 | // C++ [temp.param]p8: |
| 651 | // |
| 652 | // A non-type template-parameter of type "array of T" or |
| 653 | // "function returning T" is adjusted to be of type "pointer to |
| 654 | // T" or "pointer to function returning T", respectively. |
| 655 | else if (T->isArrayType()) |
| 656 | // FIXME: Keep the type prior to promotion? |
| 657 | return Context.getArrayDecayedType(T); |
| 658 | else if (T->isFunctionType()) |
| 659 | // FIXME: Keep the type prior to promotion? |
| 660 | return Context.getPointerType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 661 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 662 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 663 | << T; |
| 664 | |
| 665 | return QualType(); |
| 666 | } |
| 667 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 668 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 669 | unsigned Depth, |
| 670 | unsigned Position, |
| 671 | SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 672 | Expr *Default) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 673 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 674 | QualType T = TInfo->getType(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 675 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 676 | assert(S->isTemplateParamScope() && |
| 677 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 678 | bool Invalid = false; |
| 679 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 680 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 681 | if (T.isNull()) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 682 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 683 | Invalid = true; |
| 684 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 685 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 686 | IdentifierInfo *ParamName = D.getIdentifier(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 687 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 688 | NonTypeTemplateParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 689 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 690 | D.getLocStart(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 691 | D.getIdentifierLoc(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 692 | Depth, Position, ParamName, T, |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 693 | IsParameterPack, TInfo); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 694 | Param->setAccess(AS_public); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 695 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 696 | if (Invalid) |
| 697 | Param->setInvalidDecl(); |
| 698 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 699 | if (ParamName) { |
| 700 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 701 | ParamName); |
| 702 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 703 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 704 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 705 | IdResolver.AddDecl(Param); |
| 706 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 707 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 708 | // C++0x [temp.param]p9: |
| 709 | // A default template-argument may be specified for any kind of |
| 710 | // template-parameter that is not a template parameter pack. |
| 711 | if (Default && IsParameterPack) { |
| 712 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 713 | Default = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 716 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 717 | if (Default) { |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 718 | // Check for unexpanded parameter packs. |
| 719 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 720 | return Param; |
| 721 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 722 | TemplateArgument Converted; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 723 | ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
| 724 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 725 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 726 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 727 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 728 | Default = DefaultRes.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 729 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 730 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 731 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 732 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 733 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 734 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 735 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 736 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 737 | /// parameter (e.g. T in template <template \<typename> class T> class array) |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 738 | /// has been parsed. S is the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 739 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 740 | SourceLocation TmpLoc, |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 741 | TemplateParameterList *Params, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 742 | SourceLocation EllipsisLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 743 | IdentifierInfo *Name, |
| 744 | SourceLocation NameLoc, |
| 745 | unsigned Depth, |
| 746 | unsigned Position, |
| 747 | SourceLocation EqualLoc, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 748 | ParsedTemplateArgument Default) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 749 | assert(S->isTemplateParamScope() && |
| 750 | "Template template parameter not in template parameter scope!"); |
| 751 | |
| 752 | // Construct the parameter object. |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 753 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 754 | TemplateTemplateParmDecl *Param = |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 755 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 756 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 757 | Depth, Position, IsParameterPack, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 758 | Name, Params); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 759 | Param->setAccess(AS_public); |
| 760 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 761 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 762 | // into the scope and lookup mechanisms. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 763 | if (Name) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 764 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 765 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 766 | S->AddDecl(Param); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 767 | IdResolver.AddDecl(Param); |
| 768 | } |
| 769 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 770 | if (Params->size() == 0) { |
| 771 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 772 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 773 | Param->setInvalidDecl(); |
| 774 | } |
| 775 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 776 | // C++0x [temp.param]p9: |
| 777 | // A default template-argument may be specified for any kind of |
| 778 | // template-parameter that is not a template parameter pack. |
| 779 | if (IsParameterPack && !Default.isInvalid()) { |
| 780 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 781 | Default = ParsedTemplateArgument(); |
| 782 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 783 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 784 | if (!Default.isInvalid()) { |
| 785 | // Check only that we have a template template argument. We don't want to |
| 786 | // try to check well-formedness now, because our template template parameter |
| 787 | // might have dependent types in its template parameters, which we wouldn't |
| 788 | // be able to match now. |
| 789 | // |
| 790 | // If none of the template template parameter's template arguments mention |
| 791 | // other template parameters, we could actually perform more checking here. |
| 792 | // However, it isn't worth doing. |
| 793 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 794 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 795 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 796 | << DefaultArg.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 797 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 798 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 799 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 800 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 801 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 802 | DefaultArg.getArgument().getAsTemplate(), |
| 803 | UPPC_DefaultArgument)) |
| 804 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 805 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 806 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 807 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 808 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 809 | return Param; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 812 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 813 | /// contains the template parameters in Params/NumParams. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 814 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 815 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 816 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 817 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 818 | SourceLocation LAngleLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 819 | Decl **Params, unsigned NumParams, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 820 | SourceLocation RAngleLoc) { |
| 821 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 822 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 823 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 824 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 825 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 826 | RAngleLoc); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 827 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 828 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 829 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 830 | if (SS.isSet()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 831 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 832 | } |
| 833 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 834 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 835 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 836 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 837 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 838 | AttributeList *Attr, |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 839 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 840 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 841 | unsigned NumOuterTemplateParamLists, |
| 842 | TemplateParameterList** OuterTemplateParamLists) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 844 | "No template parameters"); |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 845 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 846 | bool Invalid = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 847 | |
| 848 | // Check that we can declare a template here. |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 849 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 850 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 851 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 852 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 853 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 854 | |
| 855 | // There is no such thing as an unnamed class template. |
| 856 | if (!Name) { |
| 857 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 858 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 861 | // Find any previous declaration with this name. For a friend with no |
| 862 | // scope explicitly specified, we only look for tag declarations (per |
| 863 | // C++11 [basic.lookup.elab]p2). |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 864 | DeclContext *SemanticContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 865 | LookupResult Previous(*this, Name, NameLoc, |
| 866 | (SS.isEmpty() && TUK == TUK_Friend) |
| 867 | ? LookupTagName : LookupOrdinaryName, |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 868 | ForRedeclaration); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 869 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 870 | SemanticContext = computeDeclContext(SS, true); |
| 871 | if (!SemanticContext) { |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 872 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 873 | // in the AST, and historically we have just ignored such friend |
| 874 | // class templates, so don't complain here. |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 875 | Diag(NameLoc, TUK == TUK_Friend |
| 876 | ? diag::warn_template_qualified_friend_ignored |
| 877 | : diag::err_template_qualified_declarator_no_match) |
Douglas Gregor | 67daacb | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 878 | << SS.getScopeRep() << SS.getRange(); |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 879 | return TUK != TUK_Friend; |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 880 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 881 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 882 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 883 | return true; |
| 884 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 885 | // If we're adding a template to a dependent context, we may need to |
| 886 | // rebuilding some of the types used within the template parameter list, |
| 887 | // now that we know what the current instantiation is. |
| 888 | if (SemanticContext->isDependentContext()) { |
| 889 | ContextRAII SavedContext(*this, SemanticContext); |
| 890 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 891 | Invalid = true; |
Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 892 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
| 893 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc); |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 894 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 895 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 896 | } else { |
| 897 | SemanticContext = CurContext; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 898 | LookupName(Previous, S); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 899 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 901 | if (Previous.isAmbiguous()) |
| 902 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 903 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 904 | NamedDecl *PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 905 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 906 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 907 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 908 | // If there is a previous declaration with the same name, check |
| 909 | // whether this is a valid redeclaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 910 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 911 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 912 | |
| 913 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 914 | // class template partial specialization, or class template specialization. |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 915 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 916 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 917 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 918 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 919 | PrevClassTemplate |
Douglas Gregor | 7f34bae | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 920 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 921 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 922 | PrevClassTemplate |
| 923 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 924 | ->getSpecializedTemplate(); |
| 925 | } |
| 926 | } |
| 927 | |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 928 | if (TUK == TUK_Friend) { |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 929 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 930 | // [...] When looking for a prior declaration of a class or a function |
| 931 | // 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] | 932 | // function is neither a qualified name nor a template-id, scopes outside |
| 933 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 934 | if (!SS.isSet()) { |
| 935 | DeclContext *OutermostContext = CurContext; |
| 936 | while (!OutermostContext->isFileContext()) |
| 937 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | d43784f | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 938 | |
Richard Smith | 61e582f | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 939 | if (PrevDecl && |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 940 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 941 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 942 | SemanticContext = PrevDecl->getDeclContext(); |
| 943 | } else { |
| 944 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 945 | // context we computed is the semantic context for our new |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 946 | // declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 947 | PrevDecl = PrevClassTemplate = nullptr; |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 948 | SemanticContext = OutermostContext; |
Richard Smith | 6483d22 | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 949 | |
| 950 | // Check that the chosen semantic context doesn't already contain a |
| 951 | // declaration of this name as a non-tag type. |
| 952 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
| 953 | ForRedeclaration); |
| 954 | DeclContext *LookupContext = SemanticContext; |
| 955 | while (LookupContext->isTransparentContext()) |
| 956 | LookupContext = LookupContext->getLookupParent(); |
| 957 | LookupQualifiedName(Previous, LookupContext); |
| 958 | |
| 959 | if (Previous.isAmbiguous()) |
| 960 | return true; |
| 961 | |
| 962 | if (Previous.begin() != Previous.end()) |
| 963 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | b74b103 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 964 | } |
John McCall | 90d3bb9 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 965 | } |
Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 966 | } else if (PrevDecl && |
| 967 | !isDeclInScope(PrevDecl, SemanticContext, S, SS.isValid())) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 968 | PrevDecl = PrevClassTemplate = nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 969 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 970 | if (PrevClassTemplate) { |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 971 | // Ensure that the template parameter lists are compatible. Skip this check |
| 972 | // for a friend in a dependent context: the template parameter list itself |
| 973 | // could be dependent. |
| 974 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 975 | !TemplateParameterListsAreEqual(TemplateParams, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 976 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 977 | /*Complain=*/true, |
| 978 | TPL_TemplateMatch)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 979 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 980 | |
| 981 | // C++ [temp.class]p4: |
| 982 | // In a redeclaration, partial specialization, explicit |
| 983 | // specialization or explicit instantiation of a class template, |
| 984 | // the class-key shall agree in kind with the original class |
| 985 | // template declaration (7.1.5.3). |
| 986 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 987 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
| 988 | TUK == TUK_Definition, KWLoc, *Name)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 990 | << Name |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 991 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 992 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 993 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 996 | // Check for redefinition of this class template. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 997 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 998 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 999 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1000 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1001 | // FIXME: Would it make sense to try to "forget" the previous |
| 1002 | // definition, as part of error recovery? |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1003 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1004 | } |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1005 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1006 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 1007 | // Maybe we will complain about the shadowed template parameter. |
| 1008 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 1009 | // Just pretend that we didn't see the previous declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1010 | PrevDecl = nullptr; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1011 | } else if (PrevDecl) { |
| 1012 | // C++ [temp]p5: |
| 1013 | // A class template shall not have the same name as any other |
| 1014 | // template, class, function, object, enumeration, enumerator, |
| 1015 | // namespace, or type in the same scope (3.3), except as specified |
| 1016 | // in (14.5.4). |
| 1017 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1018 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1019 | return true; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1022 | // Check the template parameter list of this declaration, possibly |
| 1023 | // merging in the template parameter list from the previous class |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1024 | // template declaration. Skip this check for a friend in a dependent |
| 1025 | // context, because the template parameter list might be dependent. |
| 1026 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1027 | CheckTemplateParameterList( |
| 1028 | TemplateParams, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1029 | PrevClassTemplate ? PrevClassTemplate->getTemplateParameters() |
| 1030 | : nullptr, |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1031 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1032 | SemanticContext->isDependentContext()) |
| 1033 | ? TPC_ClassTemplateMember |
| 1034 | : TUK == TUK_Friend ? TPC_FriendClassTemplate |
| 1035 | : TPC_ClassTemplate)) |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1036 | Invalid = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1037 | |
Douglas Gregor | ce40e2e | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1038 | if (SS.isSet()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1039 | // 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] | 1040 | // template out-of-line. |
Richard Smith | e85e176 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1041 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1042 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 1043 | : diag::err_member_decl_does_not_match) |
| 1044 | << Name << SemanticContext << /*IsDefinition*/true << SS.getRange(); |
Douglas Gregor | fe0055e | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1045 | Invalid = true; |
| 1046 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1049 | CXXRecordDecl *NewClass = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1050 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1051 | PrevClassTemplate? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1052 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1053 | /*DelayTypeCreation=*/true); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1054 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1055 | if (NumOuterTemplateParamLists > 0) |
| 1056 | NewClass->setTemplateParameterListsInfo(Context, |
| 1057 | NumOuterTemplateParamLists, |
| 1058 | OuterTemplateParamLists); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1059 | |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1060 | // Add alignment attributes if necessary; these attributes are checked when |
| 1061 | // the ASTContext lays out the structure. |
Eli Friedman | 0415f3e1 | 2012-08-08 21:08:34 +0000 | [diff] [blame] | 1062 | if (TUK == TUK_Definition) { |
| 1063 | AddAlignmentAttributesForRecord(NewClass); |
| 1064 | AddMsStructLayoutForRecord(NewClass); |
| 1065 | } |
Eli Friedman | edb6f5d | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1066 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1067 | ClassTemplateDecl *NewTemplate |
| 1068 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1069 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1070 | NewClass, PrevClassTemplate); |
Douglas Gregor | 97f1f1c | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1071 | NewClass->setDescribedClassTemplate(NewTemplate); |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1072 | |
Douglas Gregor | 21823bf | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1073 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | ef15bdb | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1074 | NewTemplate->setModulePrivate(); |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1075 | |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1076 | // Build the type for the class template declaration now. |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1077 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1078 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1079 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1080 | (void)T; |
| 1081 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1082 | // 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] | 1083 | // class template, make a note of that. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1084 | if (PrevClassTemplate && |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1085 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1086 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1087 | |
Anders Carlsson | 137108d | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1088 | // Set the access specifier. |
Douglas Gregor | 31feb33 | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1089 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1090 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1091 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1092 | // Set the lexical context of these templates |
| 1093 | NewClass->setLexicalDeclContext(CurContext); |
| 1094 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1095 | |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1096 | if (TUK == TUK_Definition) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1097 | NewClass->startDefinition(); |
| 1098 | |
| 1099 | if (Attr) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1100 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1101 | |
Rafael Espindola | 0c6c405 | 2012-08-22 14:52:14 +0000 | [diff] [blame] | 1102 | if (PrevClassTemplate) |
| 1103 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1104 | |
Rafael Espindola | 385c042 | 2012-07-13 18:04:45 +0000 | [diff] [blame] | 1105 | AddPushedVisibilityAttribute(NewClass); |
| 1106 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1107 | if (TUK != TUK_Friend) |
| 1108 | PushOnScopeChains(NewTemplate, S); |
| 1109 | else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1110 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1111 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1112 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1113 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1114 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1115 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1116 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1117 | // Friend templates are visible in fairly strange ways. |
| 1118 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1119 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1120 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1121 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1122 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1123 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1124 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1125 | |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1126 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1127 | NewClass->getLocation(), |
| 1128 | NewTemplate, |
| 1129 | /*FIXME:*/NewClass->getLocation()); |
| 1130 | Friend->setAccess(AS_public); |
| 1131 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1132 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1133 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1134 | if (Invalid) { |
| 1135 | NewTemplate->setInvalidDecl(); |
| 1136 | NewClass->setInvalidDecl(); |
| 1137 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1138 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1139 | ActOnDocumentableDecl(NewTemplate); |
| 1140 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1141 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1144 | /// \brief Diagnose the presence of a default template argument on a |
| 1145 | /// template parameter, which is ill-formed in certain contexts. |
| 1146 | /// |
| 1147 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1148 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1149 | Sema::TemplateParamListContext TPC, |
| 1150 | SourceLocation ParamLoc, |
| 1151 | SourceRange DefArgRange) { |
| 1152 | switch (TPC) { |
| 1153 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1154 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1155 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1156 | return false; |
| 1157 | |
| 1158 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1159 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1160 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1161 | // A default template-argument shall not be specified in a |
| 1162 | // function template declaration or a function template |
| 1163 | // definition [...] |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1164 | // If a friend function template declaration specifies a default |
| 1165 | // template-argument, that declaration shall be a definition and shall be |
| 1166 | // the only declaration of the function template in the translation unit. |
| 1167 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1168 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1169 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1170 | : diag::ext_template_parameter_default_in_function_template) |
| 1171 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1172 | return false; |
| 1173 | |
| 1174 | case Sema::TPC_ClassTemplateMember: |
| 1175 | // C++0x [temp.param]p9: |
| 1176 | // A default template-argument shall not be specified in the |
| 1177 | // template-parameter-lists of the definition of a member of a |
| 1178 | // class template that appears outside of the member's class. |
| 1179 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1180 | << DefArgRange; |
| 1181 | return true; |
| 1182 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1183 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1184 | case Sema::TPC_FriendFunctionTemplate: |
| 1185 | // C++ [temp.param]p9: |
| 1186 | // A default template-argument shall not be specified in a |
| 1187 | // friend template declaration. |
| 1188 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1189 | << DefArgRange; |
| 1190 | return true; |
| 1191 | |
| 1192 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1193 | // for friend function templates if there is only a single |
| 1194 | // declaration (and it is a definition). Strange! |
| 1195 | } |
| 1196 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1197 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1200 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1201 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1202 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1203 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1204 | // A template template parameter which is a parameter pack is also a pack |
| 1205 | // expansion. |
| 1206 | if (TTP->isParameterPack()) |
| 1207 | return false; |
| 1208 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1209 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1210 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1211 | NamedDecl *P = Params->getParam(I); |
| 1212 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1213 | if (!NTTP->isParameterPack() && |
| 1214 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1215 | NTTP->getTypeSourceInfo(), |
| 1216 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1217 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1218 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1219 | continue; |
| 1220 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1221 | |
| 1222 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1223 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1224 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1225 | return true; |
| 1226 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1227 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1228 | return false; |
| 1229 | } |
| 1230 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1231 | /// \brief Checks the validity of a template parameter list, possibly |
| 1232 | /// considering the template parameter list from a previous |
| 1233 | /// declaration. |
| 1234 | /// |
| 1235 | /// If an "old" template parameter list is provided, it must be |
| 1236 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1237 | /// template parameter list. |
| 1238 | /// |
| 1239 | /// \param NewParams Template parameter list for a new template |
| 1240 | /// declaration. This template parameter list will be updated with any |
| 1241 | /// default arguments that are carried through from the previous |
| 1242 | /// template parameter list. |
| 1243 | /// |
| 1244 | /// \param OldParams If provided, template parameter list from a |
| 1245 | /// previous declaration of the same template. Default template |
| 1246 | /// arguments will be merged from the old template parameter list to |
| 1247 | /// the new template parameter list. |
| 1248 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1249 | /// \param TPC Describes the context in which we are checking the given |
| 1250 | /// template parameter list. |
| 1251 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1252 | /// \returns true if an error occurred, false otherwise. |
| 1253 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1254 | TemplateParameterList *OldParams, |
| 1255 | TemplateParamListContext TPC) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1256 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1258 | // C++ [temp.param]p10: |
| 1259 | // The set of default template-arguments available for use with a |
| 1260 | // template declaration or definition is obtained by merging the |
| 1261 | // default arguments from the definition (if in scope) and all |
| 1262 | // declarations in scope in the same way default function |
| 1263 | // arguments are (8.3.6). |
| 1264 | bool SawDefaultArgument = false; |
| 1265 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1266 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1267 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1268 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1269 | if (OldParams) |
| 1270 | OldParam = OldParams->begin(); |
| 1271 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1272 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1273 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1274 | NewParamEnd = NewParams->end(); |
| 1275 | NewParam != NewParamEnd; ++NewParam) { |
| 1276 | // Variables used to diagnose redundant default arguments |
| 1277 | bool RedundantDefaultArg = false; |
| 1278 | SourceLocation OldDefaultLoc; |
| 1279 | SourceLocation NewDefaultLoc; |
| 1280 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1281 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1282 | bool MissingDefaultArg = false; |
| 1283 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1284 | // Variable used to diagnose non-final parameter packs |
| 1285 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1286 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1287 | if (TemplateTypeParmDecl *NewTypeParm |
| 1288 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1289 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1290 | if (NewTypeParm->hasDefaultArgument() && |
| 1291 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1292 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1293 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1294 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1295 | NewTypeParm->removeDefaultArgument(); |
| 1296 | |
| 1297 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1298 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1299 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1300 | |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1301 | if (NewTypeParm->isParameterPack()) { |
| 1302 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1303 | "Parameter packs can't have a default argument!"); |
| 1304 | SawParameterPack = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1305 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1306 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1307 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1308 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1309 | SawDefaultArgument = true; |
| 1310 | RedundantDefaultArg = true; |
| 1311 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1312 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1313 | // Merge the default argument from the old declaration to the |
| 1314 | // new declaration. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1315 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1316 | true); |
| 1317 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1318 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1319 | SawDefaultArgument = true; |
| 1320 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1321 | } else if (SawDefaultArgument) |
| 1322 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1323 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1324 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1325 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1326 | if (!NewNonTypeParm->isParameterPack() && |
| 1327 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1328 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1329 | UPPC_NonTypeTemplateParameterType)) { |
| 1330 | Invalid = true; |
| 1331 | continue; |
| 1332 | } |
| 1333 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1334 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1335 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1336 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1337 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1338 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1339 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1342 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1343 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1344 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1345 | if (NewNonTypeParm->isParameterPack()) { |
| 1346 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1347 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1348 | if (!NewNonTypeParm->isPackExpansion()) |
| 1349 | SawParameterPack = true; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1350 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 1351 | NewNonTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1352 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1353 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1354 | SawDefaultArgument = true; |
| 1355 | RedundantDefaultArg = true; |
| 1356 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1357 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1358 | // Merge the default argument from the old declaration to the |
| 1359 | // new declaration. |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1360 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1361 | // expression that points to a previous non-type template |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1362 | // parameter. |
| 1363 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1364 | OldNonTypeParm->getDefaultArgument(), |
| 1365 | /*Inherited=*/ true); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1366 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1367 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1368 | SawDefaultArgument = true; |
| 1369 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1370 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1372 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1373 | TemplateTemplateParmDecl *NewTemplateParm |
| 1374 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1375 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1376 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1377 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1378 | Invalid = true; |
| 1379 | continue; |
| 1380 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1381 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1382 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1383 | if (NewTemplateParm->hasDefaultArgument() && |
| 1384 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1385 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1386 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1387 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1388 | |
| 1389 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1390 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1391 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1392 | if (NewTemplateParm->isParameterPack()) { |
| 1393 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1394 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1395 | if (!NewTemplateParm->isPackExpansion()) |
| 1396 | SawParameterPack = true; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1397 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1398 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1399 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1400 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1401 | SawDefaultArgument = true; |
| 1402 | RedundantDefaultArg = true; |
| 1403 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1404 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1405 | // Merge the default argument from the old declaration to the |
| 1406 | // new declaration. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1407 | // FIXME: We need to create a new kind of "default argument" expression |
| 1408 | // that points to a previous template template parameter. |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1409 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1410 | OldTemplateParm->getDefaultArgument(), |
| 1411 | /*Inherited=*/ true); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1412 | PreviousDefaultArgLoc |
| 1413 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1414 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1415 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1416 | PreviousDefaultArgLoc |
| 1417 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1418 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1419 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1422 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1423 | // If a template parameter of a primary class template or alias template |
| 1424 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1425 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1426 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 1427 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1428 | Diag((*NewParam)->getLocation(), |
| 1429 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1430 | Invalid = true; |
| 1431 | } |
| 1432 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1433 | if (RedundantDefaultArg) { |
| 1434 | // C++ [temp.param]p12: |
| 1435 | // A template-parameter shall not be given default arguments |
| 1436 | // by two different declarations in the same scope. |
| 1437 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1438 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1439 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1440 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1441 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1442 | // If a template-parameter of a class template has a default |
| 1443 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1444 | // have a default template-argument supplied or be a template parameter |
| 1445 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1446 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1447 | diag::err_template_param_default_arg_missing); |
| 1448 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1449 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1450 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | // If we have an old template parameter list that we're merging |
| 1454 | // in, move on to the next parameter. |
| 1455 | if (OldParams) |
| 1456 | ++OldParam; |
| 1457 | } |
| 1458 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1459 | // We were missing some default arguments at the end of the list, so remove |
| 1460 | // all of the default arguments. |
| 1461 | if (RemoveDefaultArguments) { |
| 1462 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1463 | NewParamEnd = NewParams->end(); |
| 1464 | NewParam != NewParamEnd; ++NewParam) { |
| 1465 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1466 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1467 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1468 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1469 | NTTP->removeDefaultArgument(); |
| 1470 | else |
| 1471 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1472 | } |
| 1473 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1474 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1475 | return Invalid; |
| 1476 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1477 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1478 | namespace { |
| 1479 | |
| 1480 | /// A class which looks for a use of a certain level of template |
| 1481 | /// parameter. |
| 1482 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1483 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1484 | |
| 1485 | unsigned Depth; |
| 1486 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1487 | SourceLocation MatchLoc; |
| 1488 | |
| 1489 | DependencyChecker(unsigned Depth) : Depth(Depth), Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1490 | |
| 1491 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1492 | NamedDecl *ND = Params->getParam(0); |
| 1493 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1494 | Depth = PD->getDepth(); |
| 1495 | } else if (NonTypeTemplateParmDecl *PD = |
| 1496 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1497 | Depth = PD->getDepth(); |
| 1498 | } else { |
| 1499 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1500 | } |
| 1501 | } |
| 1502 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1503 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1504 | if (ParmDepth >= Depth) { |
| 1505 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1506 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1507 | return true; |
| 1508 | } |
| 1509 | return false; |
| 1510 | } |
| 1511 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1512 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1513 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 1514 | } |
| 1515 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1516 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1517 | return !Matches(T->getDepth()); |
| 1518 | } |
| 1519 | |
| 1520 | bool TraverseTemplateName(TemplateName N) { |
| 1521 | if (TemplateTemplateParmDecl *PD = |
| 1522 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1523 | if (Matches(PD->getDepth())) |
| 1524 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1525 | return super::TraverseTemplateName(N); |
| 1526 | } |
| 1527 | |
| 1528 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1529 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1530 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 1531 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1532 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1533 | return super::VisitDeclRefExpr(E); |
| 1534 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1535 | |
| 1536 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 1537 | return TraverseType(T->getReplacementType()); |
| 1538 | } |
| 1539 | |
| 1540 | bool |
| 1541 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 1542 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 1543 | } |
| 1544 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1545 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1546 | return TraverseType(T->getInjectedSpecializationType()); |
| 1547 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1548 | }; |
| 1549 | } |
| 1550 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1551 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1552 | /// list. |
| 1553 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1554 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1555 | DependencyChecker Checker(Params); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1556 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1557 | return Checker.Match; |
| 1558 | } |
| 1559 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1560 | // Find the source range corresponding to the named type in the given |
| 1561 | // nested-name-specifier, if any. |
| 1562 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1563 | QualType T, |
| 1564 | const CXXScopeSpec &SS) { |
| 1565 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1566 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1567 | if (const Type *CurType = NNS->getAsType()) { |
| 1568 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1569 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1570 | } else |
| 1571 | break; |
| 1572 | |
| 1573 | NNSLoc = NNSLoc.getPrefix(); |
| 1574 | } |
| 1575 | |
| 1576 | return SourceRange(); |
| 1577 | } |
| 1578 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1580 | /// specifier, returning the template parameter list that applies to the |
| 1581 | /// name. |
| 1582 | /// |
| 1583 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1584 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1586 | /// \param DeclLoc The location of the declaration itself. |
| 1587 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1588 | /// \param SS the scope specifier that will be matched to the given template |
| 1589 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1590 | /// being declared. |
| 1591 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1592 | /// \param TemplateId The template-id following the scope specifier, if there |
| 1593 | /// is one. Used to check for a missing 'template<>'. |
| 1594 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1595 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1596 | /// innermost template parameter lists. |
| 1597 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1598 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1599 | /// matching template parameters to scope specifiers in friend |
| 1600 | /// declarations. |
| 1601 | /// |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1602 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1603 | /// declared is an explicit specialization, false otherwise. |
| 1604 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1605 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1606 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1607 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1609 | /// 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] | 1610 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1611 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 1612 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1613 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1614 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
| 1615 | bool &IsExplicitSpecialization, bool &Invalid) { |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1616 | IsExplicitSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1617 | Invalid = false; |
| 1618 | |
| 1619 | // The sequence of nested types to which we will match up the template |
| 1620 | // parameter lists. We first build this list by starting with the type named |
| 1621 | // 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] | 1622 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1623 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1624 | if (SS.getScopeRep()) { |
| 1625 | if (CXXRecordDecl *Record |
| 1626 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1627 | T = Context.getTypeDeclType(Record); |
| 1628 | else |
| 1629 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1630 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1631 | |
| 1632 | // If we found an explicit specialization that prevents us from needing |
| 1633 | // 'template<>' headers, this will be set to the location of that |
| 1634 | // explicit specialization. |
| 1635 | SourceLocation ExplicitSpecLoc; |
| 1636 | |
| 1637 | while (!T.isNull()) { |
| 1638 | NestedTypes.push_back(T); |
| 1639 | |
| 1640 | // Retrieve the parent of a record type. |
| 1641 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1642 | // If this type is an explicit specialization, we're done. |
| 1643 | if (ClassTemplateSpecializationDecl *Spec |
| 1644 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1645 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1646 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1647 | ExplicitSpecLoc = Spec->getLocation(); |
| 1648 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1649 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1650 | } else if (Record->getTemplateSpecializationKind() |
| 1651 | == TSK_ExplicitSpecialization) { |
| 1652 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1653 | break; |
| 1654 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1655 | |
| 1656 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1657 | T = Context.getTypeDeclType(Parent); |
| 1658 | else |
| 1659 | T = QualType(); |
| 1660 | continue; |
| 1661 | } |
| 1662 | |
| 1663 | if (const TemplateSpecializationType *TST |
| 1664 | = T->getAs<TemplateSpecializationType>()) { |
| 1665 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1666 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1667 | T = Context.getTypeDeclType(Parent); |
| 1668 | else |
| 1669 | T = QualType(); |
| 1670 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1671 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | // Look one step prior in a dependent template specialization type. |
| 1675 | if (const DependentTemplateSpecializationType *DependentTST |
| 1676 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1677 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1678 | T = QualType(NNS->getAsType(), 0); |
| 1679 | else |
| 1680 | T = QualType(); |
| 1681 | continue; |
| 1682 | } |
| 1683 | |
| 1684 | // Look one step prior in a dependent name type. |
| 1685 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1686 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1687 | T = QualType(NNS->getAsType(), 0); |
| 1688 | else |
| 1689 | T = QualType(); |
| 1690 | continue; |
| 1691 | } |
| 1692 | |
| 1693 | // Retrieve the parent of an enumeration type. |
| 1694 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1695 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1696 | // check here. |
| 1697 | EnumDecl *Enum = EnumT->getDecl(); |
| 1698 | |
| 1699 | // Get to the parent type. |
| 1700 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1701 | T = Context.getTypeDeclType(Parent); |
| 1702 | else |
| 1703 | T = QualType(); |
| 1704 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1705 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1706 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1707 | T = QualType(); |
| 1708 | } |
| 1709 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1710 | // to the innermost while checking template-parameter-lists. |
| 1711 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1712 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1713 | // C++0x [temp.expl.spec]p17: |
| 1714 | // A member or a member template may be nested within many |
| 1715 | // enclosing class templates. In an explicit specialization for |
| 1716 | // such a member, the member declaration shall be preceded by a |
| 1717 | // template<> for each enclosing class template that is |
| 1718 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1719 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1720 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1721 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1722 | if (SawNonEmptyTemplateParameterList) { |
| 1723 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1724 | << !Recovery << Range; |
| 1725 | Invalid = true; |
| 1726 | IsExplicitSpecialization = false; |
| 1727 | return true; |
| 1728 | } |
| 1729 | |
| 1730 | return false; |
| 1731 | }; |
| 1732 | |
| 1733 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 1734 | // Check that we can have an explicit specialization here. |
| 1735 | if (CheckExplicitSpecialization(Range, true)) |
| 1736 | return true; |
| 1737 | |
| 1738 | // We don't have a template header, but we should. |
| 1739 | SourceLocation ExpectedTemplateLoc; |
| 1740 | if (!ParamLists.empty()) |
| 1741 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1742 | else |
| 1743 | ExpectedTemplateLoc = DeclStartLoc; |
| 1744 | |
| 1745 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1746 | << Range |
| 1747 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1748 | return false; |
| 1749 | }; |
| 1750 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1751 | unsigned ParamIdx = 0; |
| 1752 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1753 | ++TypeIdx) { |
| 1754 | T = NestedTypes[TypeIdx]; |
| 1755 | |
| 1756 | // Whether we expect a 'template<>' header. |
| 1757 | bool NeedEmptyTemplateHeader = false; |
| 1758 | |
| 1759 | // Whether we expect a template header with parameters. |
| 1760 | bool NeedNonemptyTemplateHeader = false; |
| 1761 | |
| 1762 | // For a dependent type, the set of template parameters that we |
| 1763 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1764 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1765 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1766 | // C++0x [temp.expl.spec]p15: |
| 1767 | // A member or a member template may be nested within many enclosing |
| 1768 | // class templates. In an explicit specialization for such a member, the |
| 1769 | // member declaration shall be preceded by a template<> for each |
| 1770 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1771 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1772 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1773 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1774 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1775 | NeedNonemptyTemplateHeader = true; |
| 1776 | } else if (Record->isDependentType()) { |
| 1777 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1778 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1779 | ->getTemplateParameters(); |
| 1780 | NeedNonemptyTemplateHeader = true; |
| 1781 | } |
| 1782 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1783 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1784 | // C++0x [temp.expl.spec]p4: |
| 1785 | // Members of an explicitly specialized class template are defined |
| 1786 | // in the same manner as members of normal classes, and not using |
| 1787 | // the template<> syntax. |
| 1788 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1789 | NeedEmptyTemplateHeader = true; |
| 1790 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1791 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1792 | } else if (Record->getTemplateSpecializationKind()) { |
| 1793 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1794 | != TSK_ExplicitSpecialization && |
| 1795 | TypeIdx == NumTypes - 1) |
| 1796 | IsExplicitSpecialization = true; |
| 1797 | |
| 1798 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1799 | } |
| 1800 | } else if (const TemplateSpecializationType *TST |
| 1801 | = T->getAs<TemplateSpecializationType>()) { |
| 1802 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1803 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1804 | NeedNonemptyTemplateHeader = true; |
| 1805 | } |
| 1806 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1807 | // FIXME: We actually could/should check the template arguments here |
| 1808 | // against the corresponding template parameter list. |
| 1809 | NeedNonemptyTemplateHeader = false; |
| 1810 | } |
| 1811 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1812 | // C++ [temp.expl.spec]p16: |
| 1813 | // In an explicit specialization declaration for a member of a class |
| 1814 | // template or a member template that ap- pears in namespace scope, the |
| 1815 | // member template and some of its enclosing class templates may remain |
| 1816 | // unspecialized, except that the declaration shall not explicitly |
| 1817 | // specialize a class member template if its en- closing class templates |
| 1818 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1819 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1820 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1821 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 1822 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1823 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1824 | } else |
| 1825 | SawNonEmptyTemplateParameterList = true; |
| 1826 | } |
| 1827 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1828 | if (NeedEmptyTemplateHeader) { |
| 1829 | // If we're on the last of the types, and we need a 'template<>' header |
| 1830 | // here, then it's an explicit specialization. |
| 1831 | if (TypeIdx == NumTypes - 1) |
| 1832 | IsExplicitSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1833 | |
| 1834 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1835 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1836 | // The header has template parameters when it shouldn't. Complain. |
| 1837 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1838 | diag::err_template_param_list_matches_nontemplate) |
| 1839 | << T |
| 1840 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1841 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1842 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1843 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1844 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1845 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1846 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1847 | // Consume this template header. |
| 1848 | ++ParamIdx; |
| 1849 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1850 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1851 | |
| 1852 | if (!IsFriend) |
| 1853 | if (DiagnoseMissingExplicitSpecialization( |
| 1854 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1855 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1856 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1857 | continue; |
| 1858 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1859 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1860 | if (NeedNonemptyTemplateHeader) { |
| 1861 | // In friend declarations we can have template-ids which don't |
| 1862 | // depend on the corresponding template parameter lists. But |
| 1863 | // assume that empty parameter lists are supposed to match this |
| 1864 | // template-id. |
| 1865 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1866 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1867 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1868 | ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1869 | else |
| 1870 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1871 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1872 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1873 | if (ParamIdx < ParamLists.size()) { |
| 1874 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1875 | if (ExpectedTemplateParams && |
| 1876 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1877 | ExpectedTemplateParams, |
| 1878 | true, TPL_TemplateMatch)) |
| 1879 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1880 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1881 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1882 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1883 | TPC_ClassTemplateMember)) |
| 1884 | Invalid = true; |
| 1885 | |
| 1886 | ++ParamIdx; |
| 1887 | continue; |
| 1888 | } |
| 1889 | |
| 1890 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1891 | << T |
| 1892 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1893 | Invalid = true; |
| 1894 | continue; |
| 1895 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1896 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1897 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1898 | // If there were at least as many template-ids as there were template |
| 1899 | // parameter lists, then there are no template parameter lists remaining for |
| 1900 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1901 | if (ParamIdx >= ParamLists.size()) { |
| 1902 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1903 | // We don't have a template header for the declaration itself, but we |
| 1904 | // should. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1905 | IsExplicitSpecialization = true; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1906 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 1907 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1908 | |
| 1909 | // Fabricate an empty template parameter list for the invented header. |
| 1910 | return TemplateParameterList::Create(Context, SourceLocation(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1911 | SourceLocation(), nullptr, 0, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1912 | SourceLocation()); |
| 1913 | } |
| 1914 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1915 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1916 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1917 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1918 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1919 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1920 | bool HasAnyExplicitSpecHeader = false; |
| 1921 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1922 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1923 | if (ParamLists[I]->size() == 0) |
| 1924 | HasAnyExplicitSpecHeader = true; |
| 1925 | else |
| 1926 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1927 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1928 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1929 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1930 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 1931 | : diag::err_template_spec_extra_headers) |
| 1932 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1933 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1934 | |
| 1935 | // If there was a specialization somewhere, such that 'template<>' is |
| 1936 | // not required, and there were any 'template<>' headers, note where the |
| 1937 | // specialization occurred. |
| 1938 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1939 | Diag(ExplicitSpecLoc, |
| 1940 | diag::note_explicit_template_spec_does_not_need_header) |
| 1941 | << NestedTypes.back(); |
| 1942 | |
| 1943 | // We have a template parameter list with no corresponding scope, which |
| 1944 | // means that the resulting template declaration can't be instantiated |
| 1945 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1946 | if (!AllExplicitSpecHeaders) |
| 1947 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1948 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1949 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1950 | // C++ [temp.expl.spec]p16: |
| 1951 | // In an explicit specialization declaration for a member of a class |
| 1952 | // template or a member template that ap- pears in namespace scope, the |
| 1953 | // member template and some of its enclosing class templates may remain |
| 1954 | // unspecialized, except that the declaration shall not explicitly |
| 1955 | // specialize a class member template if its en- closing class templates |
| 1956 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1957 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1958 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 1959 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1960 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1961 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1962 | // Return the last template parameter list, which corresponds to the |
| 1963 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1964 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1967 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1968 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1969 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1970 | << (isa<FunctionTemplateDecl>(Template) |
| 1971 | ? 0 |
| 1972 | : isa<ClassTemplateDecl>(Template) |
| 1973 | ? 1 |
| 1974 | : isa<VarTemplateDecl>(Template) |
| 1975 | ? 2 |
| 1976 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 1977 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1978 | return; |
| 1979 | } |
| 1980 | |
| 1981 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1982 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1983 | IEnd = OST->end(); |
| 1984 | I != IEnd; ++I) |
| 1985 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1986 | << 0 << (*I)->getDeclName(); |
| 1987 | |
| 1988 | return; |
| 1989 | } |
| 1990 | } |
| 1991 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1992 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1993 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1994 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1995 | DependentTemplateName *DTN |
| 1996 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1997 | if (DTN && DTN->isIdentifier()) |
| 1998 | // When building a template-id where the template-name is dependent, |
| 1999 | // assume the template is a type template. Either our assumption is |
| 2000 | // correct, or the code is ill-formed and will be diagnosed when the |
| 2001 | // dependent name is substituted. |
| 2002 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 2003 | DTN->getQualifier(), |
| 2004 | DTN->getIdentifier(), |
| 2005 | TemplateArgs); |
| 2006 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2007 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 2008 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
| 2009 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2010 | // We might have a substituted template template parameter pack. If so, |
| 2011 | // build a template specialization type for it. |
| 2012 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 2013 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2014 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2015 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 2016 | << Name; |
| 2017 | NoteAllFoundTemplates(Name); |
| 2018 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2019 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2020 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2021 | // Check that the template argument list is well-formed for this |
| 2022 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2023 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2024 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2025 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2026 | return QualType(); |
| 2027 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2028 | QualType CanonType; |
| 2029 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2030 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2031 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 2032 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2033 | // Find the canonical type for this type alias template specialization. |
| 2034 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 2035 | if (Pattern->isInvalidDecl()) |
| 2036 | return QualType(); |
| 2037 | |
| 2038 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2039 | Converted.data(), Converted.size()); |
| 2040 | |
| 2041 | // Only substitute for the innermost template argument list. |
| 2042 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 0c4a34b | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 2043 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 2044 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 2045 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 2046 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2047 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2048 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2049 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2050 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2051 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2052 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2053 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 2054 | TemplateArgLists, AliasTemplate->getLocation(), |
| 2055 | AliasTemplate->getDeclName()); |
| 2056 | if (CanonType.isNull()) |
| 2057 | return QualType(); |
| 2058 | } else if (Name.isDependent() || |
| 2059 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2060 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2061 | // This class template specialization is a dependent |
| 2062 | // type. Therefore, its canonical type is another class template |
| 2063 | // specialization type that contains all of the converted |
| 2064 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 2065 | // A<T, T> have identical types when A is declared as: |
| 2066 | // |
| 2067 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2068 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2069 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2070 | Converted.data(), |
| 2071 | Converted.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2072 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2073 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2074 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2075 | // In the future, we need to teach getTemplateSpecializationType to only |
| 2076 | // build the canonical type and return that to us. |
| 2077 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2078 | |
| 2079 | // This might work out to be a current instantiation, in which |
| 2080 | // case the canonical type needs to be the InjectedClassNameType. |
| 2081 | // |
| 2082 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2083 | // changes to CurContext don't change the set of current |
| 2084 | // instantiations. |
| 2085 | if (isa<ClassTemplateDecl>(Template)) { |
| 2086 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2087 | // If we get out to a namespace, we're done. |
| 2088 | if (Ctx->isFileContext()) break; |
| 2089 | |
| 2090 | // If this isn't a record, keep looking. |
| 2091 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2092 | if (!Record) continue; |
| 2093 | |
| 2094 | // Look for one of the two cases with InjectedClassNameTypes |
| 2095 | // and check whether it's the same template. |
| 2096 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2097 | !Record->getDescribedClassTemplate()) |
| 2098 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2099 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2100 | // Fetch the injected class name type and check whether its |
| 2101 | // injected type is equal to the type we just built. |
| 2102 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2103 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2104 | ->getInjectedSpecializationType(); |
| 2105 | |
| 2106 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2107 | continue; |
| 2108 | |
| 2109 | // If so, the canonical type of this TST is the injected |
| 2110 | // class name type of the record we just found. |
| 2111 | assert(ICNT.isCanonical()); |
| 2112 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2113 | break; |
| 2114 | } |
| 2115 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2116 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2117 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2118 | // Find the class template specialization declaration that |
| 2119 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2120 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2121 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2122 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2123 | InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2124 | if (!Decl) { |
| 2125 | // This is the first time we have referenced this class template |
| 2126 | // specialization. Create the canonical declaration and add it to |
| 2127 | // the set of specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2128 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2129 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2130 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2131 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2132 | ClassTemplate->getLocation(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2133 | ClassTemplate, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2134 | Converted.data(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2135 | Converted.size(), nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2136 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 2137 | if (ClassTemplate->isOutOfLine()) |
| 2138 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 2141 | // Diagnose uses of this specialization. |
| 2142 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 2143 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2144 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2145 | assert(isa<RecordType>(CanonType) && |
| 2146 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2147 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2148 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2149 | // Build the fully-sugared type for this class template |
| 2150 | // specialization, which refers back to the class template |
| 2151 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2152 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2155 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2156 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2157 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2158 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2159 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2160 | SourceLocation RAngleLoc, |
| 2161 | bool IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2162 | if (SS.isInvalid()) |
| 2163 | return true; |
| 2164 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2165 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2166 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2167 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2168 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2169 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2170 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2171 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2172 | QualType T |
| 2173 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2174 | DTN->getQualifier(), |
| 2175 | DTN->getIdentifier(), |
| 2176 | TemplateArgs); |
| 2177 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2178 | TypeLocBuilder TLB; |
| 2179 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2180 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2181 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2182 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2183 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2184 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2185 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2186 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2187 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2188 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2189 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2190 | } |
| 2191 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2192 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2193 | |
| 2194 | if (Result.isNull()) |
| 2195 | return true; |
| 2196 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2197 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2198 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2199 | TemplateSpecializationTypeLoc SpecTL |
| 2200 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2201 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2202 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2203 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2204 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2205 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2206 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2207 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2208 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2209 | // constructor or destructor name (in such a case, the scope specifier |
| 2210 | // will be attached to the enclosing Decl or Expr node). |
| 2211 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2212 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2213 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2214 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2215 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2216 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2217 | } |
| 2218 | |
| 2219 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2220 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2221 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2222 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2223 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2224 | SourceLocation TagLoc, |
| 2225 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2226 | SourceLocation TemplateKWLoc, |
| 2227 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2228 | SourceLocation TemplateLoc, |
| 2229 | SourceLocation LAngleLoc, |
| 2230 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2231 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2232 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2233 | |
| 2234 | // Translate the parser's template argument list in our AST format. |
| 2235 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2236 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2237 | |
| 2238 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2239 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2240 | ElaboratedTypeKeyword Keyword |
| 2241 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2242 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2243 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2244 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2245 | DTN->getQualifier(), |
| 2246 | DTN->getIdentifier(), |
| 2247 | TemplateArgs); |
| 2248 | |
| 2249 | // Build type-source information. |
| 2250 | TypeLocBuilder TLB; |
| 2251 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2252 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2253 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2254 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2255 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2256 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2257 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2258 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2259 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2260 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2261 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2262 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2263 | |
| 2264 | if (TypeAliasTemplateDecl *TAT = |
| 2265 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2266 | // C++0x [dcl.type.elab]p2: |
| 2267 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2268 | // resolves to an alias template specialization, the |
| 2269 | // elaborated-type-specifier is ill-formed. |
| 2270 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2271 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2272 | } |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2273 | |
| 2274 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2275 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2276 | return TypeResult(true); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2277 | |
| 2278 | // Check the tag kind |
| 2279 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2280 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2281 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2282 | IdentifierInfo *Id = D->getIdentifier(); |
| 2283 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2284 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2285 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 2286 | TagLoc, *Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2287 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2288 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2289 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2290 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2291 | } |
| 2292 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2293 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2294 | // Provide source-location information for the template specialization. |
| 2295 | TypeLocBuilder TLB; |
| 2296 | TemplateSpecializationTypeLoc SpecTL |
| 2297 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2298 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2299 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2300 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2301 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2302 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2303 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2304 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2305 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2306 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2307 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2308 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2309 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2310 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2311 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2314 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2315 | Sema &S, SourceLocation NameLoc, TemplateParameterList *TemplateParams, |
| 2316 | unsigned ExplicitArgs, SmallVectorImpl<TemplateArgument> &TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2317 | |
| 2318 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 2319 | NamedDecl *PrevDecl, |
| 2320 | SourceLocation Loc, |
| 2321 | bool IsPartialSpecialization); |
| 2322 | |
| 2323 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2324 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2325 | static bool isTemplateArgumentTemplateParameter( |
| 2326 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 2327 | switch (Arg.getKind()) { |
| 2328 | case TemplateArgument::Null: |
| 2329 | case TemplateArgument::NullPtr: |
| 2330 | case TemplateArgument::Integral: |
| 2331 | case TemplateArgument::Declaration: |
| 2332 | case TemplateArgument::Pack: |
| 2333 | case TemplateArgument::TemplateExpansion: |
| 2334 | return false; |
| 2335 | |
| 2336 | case TemplateArgument::Type: { |
| 2337 | QualType Type = Arg.getAsType(); |
| 2338 | const TemplateTypeParmType *TPT = |
| 2339 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 2340 | return TPT && !Type.hasQualifiers() && |
| 2341 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 2342 | } |
| 2343 | |
| 2344 | case TemplateArgument::Expression: { |
| 2345 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 2346 | if (!DRE || !DRE->getDecl()) |
| 2347 | return false; |
| 2348 | const NonTypeTemplateParmDecl *NTTP = |
| 2349 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 2350 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 2351 | } |
| 2352 | |
| 2353 | case TemplateArgument::Template: |
| 2354 | const TemplateTemplateParmDecl *TTP = |
| 2355 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 2356 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 2357 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 2358 | } |
| 2359 | llvm_unreachable("unexpected kind of template argument"); |
| 2360 | } |
| 2361 | |
| 2362 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 2363 | ArrayRef<TemplateArgument> Args) { |
| 2364 | if (Params->size() != Args.size()) |
| 2365 | return false; |
| 2366 | |
| 2367 | unsigned Depth = Params->getDepth(); |
| 2368 | |
| 2369 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 2370 | TemplateArgument Arg = Args[I]; |
| 2371 | |
| 2372 | // If the parameter is a pack expansion, the argument must be a pack |
| 2373 | // whose only element is a pack expansion. |
| 2374 | if (Params->getParam(I)->isParameterPack()) { |
| 2375 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 2376 | !Arg.pack_begin()->isPackExpansion()) |
| 2377 | return false; |
| 2378 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 2379 | } |
| 2380 | |
| 2381 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 2382 | return false; |
| 2383 | } |
| 2384 | |
| 2385 | return true; |
| 2386 | } |
| 2387 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2388 | /// Convert the parser's template argument list representation into our form. |
| 2389 | static TemplateArgumentListInfo |
| 2390 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 2391 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 2392 | TemplateId.RAngleLoc); |
| 2393 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 2394 | TemplateId.NumArgs); |
| 2395 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 2396 | return TemplateArgs; |
| 2397 | } |
| 2398 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2399 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2400 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
| 2401 | TemplateParameterList *TemplateParams, VarDecl::StorageClass SC, |
| 2402 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2403 | // D must be variable template id. |
| 2404 | assert(D.getName().getKind() == UnqualifiedId::IK_TemplateId && |
| 2405 | "Variable template specialization is declared with a template it."); |
| 2406 | |
| 2407 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2408 | TemplateArgumentListInfo TemplateArgs = |
| 2409 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2410 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 2411 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 2412 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2413 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2414 | TemplateName Name = TemplateId->Template.get(); |
| 2415 | |
| 2416 | // The template-id must name a variable template. |
| 2417 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2418 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 2419 | if (!VarTemplate) { |
| 2420 | NamedDecl *FnTemplate; |
| 2421 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 2422 | FnTemplate = *OTS->begin(); |
| 2423 | else |
| 2424 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 2425 | if (FnTemplate) |
| 2426 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 2427 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2428 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 2429 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2430 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2431 | |
| 2432 | // Check for unexpanded parameter packs in any of the template arguments. |
| 2433 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 2434 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 2435 | UPPC_PartialSpecialization)) |
| 2436 | return true; |
| 2437 | |
| 2438 | // Check that the template argument list is well-formed for this |
| 2439 | // template. |
| 2440 | SmallVector<TemplateArgument, 4> Converted; |
| 2441 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 2442 | false, Converted)) |
| 2443 | return true; |
| 2444 | |
| 2445 | // Check that the type of this variable template specialization |
| 2446 | // matches the expected type. |
| 2447 | TypeSourceInfo *ExpectedDI; |
| 2448 | { |
| 2449 | // Do substitution on the type of the declaration |
| 2450 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
| 2451 | Converted.data(), Converted.size()); |
| 2452 | InstantiatingTemplate Inst(*this, TemplateKWLoc, VarTemplate); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2453 | if (Inst.isInvalid()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2454 | return true; |
| 2455 | VarDecl *Templated = VarTemplate->getTemplatedDecl(); |
| 2456 | ExpectedDI = |
| 2457 | SubstType(Templated->getTypeSourceInfo(), |
| 2458 | MultiLevelTemplateArgumentList(TemplateArgList), |
| 2459 | Templated->getTypeSpecStartLoc(), Templated->getDeclName()); |
| 2460 | } |
| 2461 | if (!ExpectedDI) |
| 2462 | return true; |
| 2463 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2464 | // Find the variable template (partial) specialization declaration that |
| 2465 | // corresponds to these arguments. |
| 2466 | if (IsPartialSpecialization) { |
| 2467 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2468 | *this, TemplateNameLoc, VarTemplate->getTemplateParameters(), |
| 2469 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2470 | return true; |
| 2471 | |
| 2472 | bool InstantiationDependent; |
| 2473 | if (!Name.isDependent() && |
| 2474 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2475 | TemplateArgs.getArgumentArray(), TemplateArgs.size(), |
| 2476 | InstantiationDependent)) { |
| 2477 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 2478 | << VarTemplate->getDeclName(); |
| 2479 | IsPartialSpecialization = false; |
| 2480 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2481 | |
| 2482 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 2483 | Converted)) { |
| 2484 | // C++ [temp.class.spec]p9b3: |
| 2485 | // |
| 2486 | // -- The argument list of the specialization shall not be identical |
| 2487 | // to the implicit argument list of the primary template. |
| 2488 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 2489 | << /*variable template*/ 1 |
| 2490 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 2491 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 2492 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 2493 | // of the primary template. |
| 2494 | return true; |
| 2495 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2498 | void *InsertPos = nullptr; |
| 2499 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2500 | |
| 2501 | if (IsPartialSpecialization) |
| 2502 | // FIXME: Template parameter list matters too |
| 2503 | PrevDecl = VarTemplate->findPartialSpecialization( |
| 2504 | Converted.data(), Converted.size(), InsertPos); |
| 2505 | else |
| 2506 | PrevDecl = VarTemplate->findSpecialization(Converted.data(), |
| 2507 | Converted.size(), InsertPos); |
| 2508 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2509 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2510 | |
| 2511 | // Check whether we can declare a variable template specialization in |
| 2512 | // the current scope. |
| 2513 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 2514 | TemplateNameLoc, |
| 2515 | IsPartialSpecialization)) |
| 2516 | return true; |
| 2517 | |
| 2518 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 2519 | // Since the only prior variable template specialization with these |
| 2520 | // arguments was referenced but not declared, reuse that |
| 2521 | // declaration node as our own, updating its source location and |
| 2522 | // the list of outer template parameters to reflect our new declaration. |
| 2523 | Specialization = PrevDecl; |
| 2524 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2525 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2526 | } else if (IsPartialSpecialization) { |
| 2527 | // Create a new class template partial specialization declaration node. |
| 2528 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 2529 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2530 | VarTemplatePartialSpecializationDecl *Partial = |
| 2531 | VarTemplatePartialSpecializationDecl::Create( |
| 2532 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 2533 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 2534 | Converted.data(), Converted.size(), TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2535 | |
| 2536 | if (!PrevPartial) |
| 2537 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 2538 | Specialization = Partial; |
| 2539 | |
| 2540 | // If we are providing an explicit specialization of a member variable |
| 2541 | // template specialization, make a note of that. |
| 2542 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2543 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2544 | |
| 2545 | // Check that all of the template parameters of the variable template |
| 2546 | // partial specialization are deducible from the template |
| 2547 | // arguments. If not, this variable template partial specialization |
| 2548 | // will never be used. |
| 2549 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 2550 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 2551 | TemplateParams->getDepth(), DeducibleParams); |
| 2552 | |
| 2553 | if (!DeducibleParams.all()) { |
| 2554 | unsigned NumNonDeducible = |
| 2555 | DeducibleParams.size() - DeducibleParams.count(); |
| 2556 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2557 | << /*variable template*/ 1 << (NumNonDeducible > 1) |
| 2558 | << SourceRange(TemplateNameLoc, RAngleLoc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2559 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 2560 | if (!DeducibleParams[I]) { |
| 2561 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 2562 | if (Param->getDeclName()) |
| 2563 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
| 2564 | << Param->getDeclName(); |
| 2565 | else |
| 2566 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 2567 | << "(anonymous)"; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2568 | } |
| 2569 | } |
| 2570 | } |
| 2571 | } else { |
| 2572 | // Create a new class template specialization declaration node for |
| 2573 | // this explicit specialization or friend declaration. |
| 2574 | Specialization = VarTemplateSpecializationDecl::Create( |
| 2575 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
| 2576 | VarTemplate, DI->getType(), DI, SC, Converted.data(), Converted.size()); |
| 2577 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 2578 | |
| 2579 | if (!PrevDecl) |
| 2580 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 2581 | } |
| 2582 | |
| 2583 | // C++ [temp.expl.spec]p6: |
| 2584 | // If a template, a member template or the member of a class template is |
| 2585 | // explicitly specialized then that specialization shall be declared |
| 2586 | // before the first use of that specialization that would cause an implicit |
| 2587 | // instantiation to take place, in every translation unit in which such a |
| 2588 | // use occurs; no diagnostic is required. |
| 2589 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 2590 | bool Okay = false; |
| 2591 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 2592 | // Is there any previous explicit specialization declaration? |
| 2593 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 2594 | Okay = true; |
| 2595 | break; |
| 2596 | } |
| 2597 | } |
| 2598 | |
| 2599 | if (!Okay) { |
| 2600 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 2601 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 2602 | << Name << Range; |
| 2603 | |
| 2604 | Diag(PrevDecl->getPointOfInstantiation(), |
| 2605 | diag::note_instantiation_required_here) |
| 2606 | << (PrevDecl->getTemplateSpecializationKind() != |
| 2607 | TSK_ImplicitInstantiation); |
| 2608 | return true; |
| 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 2613 | Specialization->setLexicalDeclContext(CurContext); |
| 2614 | |
| 2615 | // Add the specialization into its lexical context, so that it can |
| 2616 | // be seen when iterating through the list of declarations in that |
| 2617 | // context. However, specializations are not found by name lookup. |
| 2618 | CurContext->addDecl(Specialization); |
| 2619 | |
| 2620 | // Note that this is an explicit specialization. |
| 2621 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 2622 | |
| 2623 | if (PrevDecl) { |
| 2624 | // Check that this isn't a redefinition of this specialization, |
| 2625 | // merging with previous declarations. |
| 2626 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
| 2627 | ForRedeclaration); |
| 2628 | PrevSpec.addDecl(PrevDecl); |
| 2629 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2630 | } else if (Specialization->isStaticDataMember() && |
| 2631 | Specialization->isOutOfLine()) { |
| 2632 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
| 2635 | // Link instantiations of static data members back to the template from |
| 2636 | // which they were instantiated. |
| 2637 | if (Specialization->isStaticDataMember()) |
| 2638 | Specialization->setInstantiationOfStaticDataMember( |
| 2639 | VarTemplate->getTemplatedDecl(), |
| 2640 | Specialization->getSpecializationKind()); |
| 2641 | |
| 2642 | return Specialization; |
| 2643 | } |
| 2644 | |
| 2645 | namespace { |
| 2646 | /// \brief A partial specialization whose template arguments have matched |
| 2647 | /// a given template-id. |
| 2648 | struct PartialSpecMatchResult { |
| 2649 | VarTemplatePartialSpecializationDecl *Partial; |
| 2650 | TemplateArgumentList *Args; |
| 2651 | }; |
| 2652 | } |
| 2653 | |
| 2654 | DeclResult |
| 2655 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2656 | SourceLocation TemplateNameLoc, |
| 2657 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2658 | assert(Template && "A variable template id without template?"); |
| 2659 | |
| 2660 | // Check that the template argument list is well-formed for this template. |
| 2661 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2662 | if (CheckTemplateArgumentList( |
| 2663 | Template, TemplateNameLoc, |
| 2664 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2665 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2666 | return true; |
| 2667 | |
| 2668 | // Find the variable template specialization declaration that |
| 2669 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2670 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2671 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
| 2672 | Converted.data(), Converted.size(), InsertPos)) |
| 2673 | // If we already have a variable template specialization, return it. |
| 2674 | return Spec; |
| 2675 | |
| 2676 | // This is the first time we have referenced this variable template |
| 2677 | // specialization. Create the canonical declaration and add it to |
| 2678 | // the set of specializations, based on the closest partial specialization |
| 2679 | // that it represents. That is, |
| 2680 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 2681 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
| 2682 | Converted.data(), Converted.size()); |
| 2683 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 2684 | bool AmbiguousPartialSpec = false; |
| 2685 | typedef PartialSpecMatchResult MatchResult; |
| 2686 | SmallVector<MatchResult, 4> Matched; |
| 2687 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
| 2688 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation); |
| 2689 | |
| 2690 | // 1. Attempt to find the closest partial specialization that this |
| 2691 | // specializes, if any. |
| 2692 | // If any of the template arguments is dependent, then this is probably |
| 2693 | // a placeholder for an incomplete declarative context; which must be |
| 2694 | // complete by instantiation time. Thus, do not search through the partial |
| 2695 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2696 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 2697 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 2698 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2699 | bool InstantiationDependent = false; |
| 2700 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 2701 | TemplateArgs, InstantiationDependent)) { |
| 2702 | |
| 2703 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 2704 | Template->getPartialSpecializations(PartialSpecs); |
| 2705 | |
| 2706 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 2707 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 2708 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 2709 | |
| 2710 | if (TemplateDeductionResult Result = |
| 2711 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 2712 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2713 | // TODO: Actually use the failed-deduction info? |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2714 | FailedCandidates.addCandidate() |
| 2715 | .set(Partial, MakeDeductionFailureInfo(Context, Result, Info)); |
| 2716 | (void)Result; |
| 2717 | } else { |
| 2718 | Matched.push_back(PartialSpecMatchResult()); |
| 2719 | Matched.back().Partial = Partial; |
| 2720 | Matched.back().Args = Info.take(); |
| 2721 | } |
| 2722 | } |
| 2723 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2724 | if (Matched.size() >= 1) { |
| 2725 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 2726 | if (Matched.size() == 1) { |
| 2727 | // -- If exactly one matching specialization is found, the |
| 2728 | // instantiation is generated from that specialization. |
| 2729 | // We don't need to do anything for this. |
| 2730 | } else { |
| 2731 | // -- If more than one matching specialization is found, the |
| 2732 | // partial order rules (14.5.4.2) are used to determine |
| 2733 | // whether one of the specializations is more specialized |
| 2734 | // than the others. If none of the specializations is more |
| 2735 | // specialized than all of the other matching |
| 2736 | // specializations, then the use of the variable template is |
| 2737 | // ambiguous and the program is ill-formed. |
| 2738 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 2739 | PEnd = Matched.end(); |
| 2740 | P != PEnd; ++P) { |
| 2741 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 2742 | PointOfInstantiation) == |
| 2743 | P->Partial) |
| 2744 | Best = P; |
| 2745 | } |
| 2746 | |
| 2747 | // Determine if the best partial specialization is more specialized than |
| 2748 | // the others. |
| 2749 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2750 | PEnd = Matched.end(); |
| 2751 | P != PEnd; ++P) { |
| 2752 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 2753 | P->Partial, Best->Partial, |
| 2754 | PointOfInstantiation) != Best->Partial) { |
| 2755 | AmbiguousPartialSpec = true; |
| 2756 | break; |
| 2757 | } |
| 2758 | } |
| 2759 | } |
| 2760 | |
| 2761 | // Instantiate using the best variable template partial specialization. |
| 2762 | InstantiationPattern = Best->Partial; |
| 2763 | InstantiationArgs = Best->Args; |
| 2764 | } else { |
| 2765 | // -- If no match is found, the instantiation is generated |
| 2766 | // from the primary template. |
| 2767 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 2768 | } |
| 2769 | } |
| 2770 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2771 | // 2. Create the canonical declaration. |
| 2772 | // Note that we do not instantiate the variable just yet, since |
| 2773 | // instantiation is handled in DoMarkVarDeclReferenced(). |
| 2774 | // FIXME: LateAttrs et al.? |
| 2775 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 2776 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 2777 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 2778 | if (!Decl) |
| 2779 | return true; |
| 2780 | |
| 2781 | if (AmbiguousPartialSpec) { |
| 2782 | // Partial ordering did not produce a clear winner. Complain. |
| 2783 | Decl->setInvalidDecl(); |
| 2784 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 2785 | << Decl; |
| 2786 | |
| 2787 | // Print the matching partial specializations. |
| 2788 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2789 | PEnd = Matched.end(); |
| 2790 | P != PEnd; ++P) |
| 2791 | Diag(P->Partial->getLocation(), diag::note_partial_spec_match) |
| 2792 | << getTemplateArgumentBindingsText( |
| 2793 | P->Partial->getTemplateParameters(), *P->Args); |
| 2794 | return true; |
| 2795 | } |
| 2796 | |
| 2797 | if (VarTemplatePartialSpecializationDecl *D = |
| 2798 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 2799 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 2800 | |
| 2801 | assert(Decl && "No variable template specialization?"); |
| 2802 | return Decl; |
| 2803 | } |
| 2804 | |
| 2805 | ExprResult |
| 2806 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 2807 | const DeclarationNameInfo &NameInfo, |
| 2808 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2809 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2810 | |
| 2811 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 2812 | *TemplateArgs); |
| 2813 | if (Decl.isInvalid()) |
| 2814 | return ExprError(); |
| 2815 | |
| 2816 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 2817 | if (!Var->getTemplateSpecializationKind()) |
| 2818 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 2819 | NameInfo.getLoc()); |
| 2820 | |
| 2821 | // Build an ordinary singleton decl ref. |
| 2822 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2823 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2826 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2827 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2828 | LookupResult &R, |
| 2829 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2830 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2831 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2832 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2833 | // 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] | 2834 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2835 | // foo<int> could identify a single function unambiguously |
| 2836 | // This approach does NOT work, since f<int>(1); |
| 2837 | // gets resolved prior to resorting to overload resolution |
| 2838 | // i.e., template<class T> void f(double); |
| 2839 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2840 | |
| 2841 | // These should be filtered out by our callers. |
| 2842 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2843 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2844 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2845 | // In C++1y, check variable template ids. |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 2846 | bool InstantiationDependent; |
| 2847 | if (R.getAsSingle<VarTemplateDecl>() && |
| 2848 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2849 | *TemplateArgs, InstantiationDependent)) { |
| 2850 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 2851 | R.getAsSingle<VarTemplateDecl>(), |
| 2852 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2855 | // We don't want lookup warnings at this point. |
| 2856 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2857 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2858 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2859 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2860 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2861 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2862 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2863 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2864 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2865 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2866 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2869 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2870 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2871 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2872 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2873 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2874 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2875 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2876 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2877 | DeclContext *DC; |
| 2878 | if (!(DC = computeDeclContext(SS, false)) || |
| 2879 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2880 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2881 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2882 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2883 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2884 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2885 | LookupTemplateName(R, (Scope*)nullptr, SS, QualType(), /*Entering*/ false, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2886 | MemberOfUnknownSpecialization); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2887 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2888 | if (R.isAmbiguous()) |
| 2889 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2890 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2891 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2892 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2893 | << NameInfo.getName() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2894 | return ExprError(); |
| 2895 | } |
| 2896 | |
| 2897 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2898 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 2899 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame^] | 2900 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2901 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2902 | return ExprError(); |
| 2903 | } |
| 2904 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2905 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2908 | /// \brief Form a dependent template name. |
| 2909 | /// |
| 2910 | /// This action forms a dependent template name given the template |
| 2911 | /// name and its (presumably dependent) scope specifier. For |
| 2912 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2913 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2914 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2915 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2916 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2917 | SourceLocation TemplateKWLoc, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2918 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2919 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2920 | bool EnteringContext, |
| 2921 | TemplateTy &Result) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2922 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 2923 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2924 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2925 | diag::warn_cxx98_compat_template_outside_of_template : |
| 2926 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2927 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2928 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2929 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2930 | if (SS.isSet()) |
| 2931 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2932 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2933 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2934 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2935 | // C++0x [temp.names]p5: |
| 2936 | // If a name prefixed by the keyword template is not the name of |
| 2937 | // a template, the program is ill-formed. [Note: the keyword |
| 2938 | // template may not be applied to non-template members of class |
| 2939 | // templates. -end note ] [ Note: as is the case with the |
| 2940 | // typename prefix, the template prefix is allowed in cases |
| 2941 | // where it is not strictly necessary; i.e., when the |
| 2942 | // nested-name-specifier or the expression on the left of the -> |
| 2943 | // or . is not dependent on a template-parameter, or the use |
| 2944 | // does not appear in the scope of a template. -end note] |
| 2945 | // |
| 2946 | // Note: C++03 was more strict here, because it banned the use of |
| 2947 | // the "template" keyword prior to a template-name that was not a |
| 2948 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2949 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2950 | // 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] | 2951 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 2952 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2953 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2954 | MemberOfUnknownSpecialization); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2955 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2956 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | 5ecbb1b | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2957 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2958 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2959 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2960 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2961 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2962 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2963 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2964 | << Name.getSourceRange() |
| 2965 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2966 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2967 | } else { |
| 2968 | // We found something; return it. |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2969 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2970 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 2973 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2974 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2975 | switch (Name.getKind()) { |
| 2976 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2977 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2978 | Name.Identifier)); |
| 2979 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2980 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2981 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2982 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2983 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 2984 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2985 | |
| 2986 | case UnqualifiedId::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 2987 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2988 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2989 | default: |
| 2990 | break; |
| 2991 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2992 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2993 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2994 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2995 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2996 | << Name.getSourceRange() |
| 2997 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2998 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3001 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3002 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3003 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3004 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3005 | QualType ArgType; |
| 3006 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3007 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3008 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3009 | switch(Arg.getKind()) { |
| 3010 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3011 | // C++ [temp.arg.type]p1: |
| 3012 | // A template-argument for a template-parameter which is a |
| 3013 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3014 | ArgType = Arg.getAsType(); |
| 3015 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3016 | break; |
| 3017 | case TemplateArgument::Template: { |
| 3018 | // We have a template type parameter but the template argument |
| 3019 | // is a template without any arguments. |
| 3020 | SourceRange SR = AL.getSourceRange(); |
| 3021 | TemplateName Name = Arg.getAsTemplate(); |
| 3022 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 3023 | << Name << SR; |
| 3024 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 3025 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3026 | |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3027 | return true; |
| 3028 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3029 | case TemplateArgument::Expression: { |
| 3030 | // We have a template type parameter but the template argument is an |
| 3031 | // expression; see if maybe it is missing the "typename" keyword. |
| 3032 | CXXScopeSpec SS; |
| 3033 | DeclarationNameInfo NameInfo; |
| 3034 | |
| 3035 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 3036 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3037 | NameInfo = ArgExpr->getNameInfo(); |
| 3038 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 3039 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 3040 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3041 | NameInfo = ArgExpr->getNameInfo(); |
| 3042 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 3043 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3044 | if (ArgExpr->isImplicitAccess()) { |
| 3045 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3046 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 3047 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3050 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3051 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 3052 | LookupParsedName(Result, CurScope, &SS); |
| 3053 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3054 | if (Result.getAsSingle<TypeDecl>() || |
| 3055 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3056 | LookupResult::NotFoundInCurrentInstantiation) { |
| 3057 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3058 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3059 | Diag(Loc, getLangOpts().MSVCCompat |
| 3060 | ? diag::ext_ms_template_type_arg_missing_typename |
| 3061 | : diag::err_template_arg_must_be_type_suggest) |
| 3062 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3063 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3064 | |
| 3065 | // Recover by synthesizing a type using the location information that we |
| 3066 | // already have. |
| 3067 | ArgType = |
| 3068 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 3069 | TypeLocBuilder TLB; |
| 3070 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 3071 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 3072 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3073 | TL.setNameLoc(NameInfo.getLoc()); |
| 3074 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 3075 | |
| 3076 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 3077 | // properly. |
| 3078 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 3079 | TemplateArgumentLocInfo(TSI)); |
| 3080 | |
| 3081 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3082 | } |
| 3083 | } |
| 3084 | // fallthrough |
| 3085 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3086 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3087 | // We have a template type parameter but the template argument |
| 3088 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 3089 | SourceRange SR = AL.getSourceRange(); |
| 3090 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3091 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3092 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3093 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3094 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3095 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3096 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3097 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3098 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3099 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3100 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3101 | ArgType = Context.getCanonicalType(ArgType); |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3102 | |
| 3103 | // Objective-C ARC: |
| 3104 | // If an explicitly-specified template argument type is a lifetime type |
| 3105 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3106 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3107 | ArgType->isObjCLifetimeType() && |
| 3108 | !ArgType.getObjCLifetime()) { |
| 3109 | Qualifiers Qs; |
| 3110 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 3111 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 3112 | } |
| 3113 | |
| 3114 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3115 | return false; |
| 3116 | } |
| 3117 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3118 | /// \brief Substitute template arguments into the default template argument for |
| 3119 | /// the given template type parameter. |
| 3120 | /// |
| 3121 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3122 | /// the substitution. |
| 3123 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3124 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3125 | /// for. |
| 3126 | /// |
| 3127 | /// \param TemplateLoc the location of the template name that started the |
| 3128 | /// template-id we are checking. |
| 3129 | /// |
| 3130 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3131 | /// terminates the template-id. |
| 3132 | /// |
| 3133 | /// \param Param the template template parameter whose default we are |
| 3134 | /// substituting into. |
| 3135 | /// |
| 3136 | /// \param Converted the list of template arguments provided for template |
| 3137 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3138 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3139 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3140 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3141 | TemplateDecl *Template, |
| 3142 | SourceLocation TemplateLoc, |
| 3143 | SourceLocation RAngleLoc, |
| 3144 | TemplateTypeParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3145 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3146 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3147 | |
| 3148 | // If the argument type is dependent, instantiate it now based |
| 3149 | // on the previously-computed template arguments. |
| 3150 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3151 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3152 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3153 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3154 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3155 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3156 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3157 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3158 | Converted.data(), Converted.size()); |
| 3159 | |
| 3160 | // Only substitute for the innermost template argument list. |
| 3161 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3162 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3163 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3164 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3165 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3166 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3167 | ArgType = |
| 3168 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 3169 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
| 3172 | return ArgType; |
| 3173 | } |
| 3174 | |
| 3175 | /// \brief Substitute template arguments into the default template argument for |
| 3176 | /// the given non-type template parameter. |
| 3177 | /// |
| 3178 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3179 | /// the substitution. |
| 3180 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3181 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3182 | /// for. |
| 3183 | /// |
| 3184 | /// \param TemplateLoc the location of the template name that started the |
| 3185 | /// template-id we are checking. |
| 3186 | /// |
| 3187 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3188 | /// terminates the template-id. |
| 3189 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3190 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3191 | /// substituting into. |
| 3192 | /// |
| 3193 | /// \param Converted the list of template arguments provided for template |
| 3194 | /// parameters that precede \p Param in the template parameter list. |
| 3195 | /// |
| 3196 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3197 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3198 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3199 | TemplateDecl *Template, |
| 3200 | SourceLocation TemplateLoc, |
| 3201 | SourceLocation RAngleLoc, |
| 3202 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3203 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3204 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3205 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3206 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3207 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3208 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3209 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3210 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3211 | Converted.data(), Converted.size()); |
| 3212 | |
| 3213 | // Only substitute for the innermost template argument list. |
| 3214 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3215 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3216 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3217 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3218 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3219 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
Eli Friedman | c25372b | 2012-04-26 22:43:24 +0000 | [diff] [blame] | 3220 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3221 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3222 | } |
| 3223 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3224 | /// \brief Substitute template arguments into the default template argument for |
| 3225 | /// the given template template parameter. |
| 3226 | /// |
| 3227 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3228 | /// the substitution. |
| 3229 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3230 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3231 | /// for. |
| 3232 | /// |
| 3233 | /// \param TemplateLoc the location of the template name that started the |
| 3234 | /// template-id we are checking. |
| 3235 | /// |
| 3236 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3237 | /// terminates the template-id. |
| 3238 | /// |
| 3239 | /// \param Param the template template parameter whose default we are |
| 3240 | /// substituting into. |
| 3241 | /// |
| 3242 | /// \param Converted the list of template arguments provided for template |
| 3243 | /// parameters that precede \p Param in the template parameter list. |
| 3244 | /// |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3245 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 3246 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3247 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3248 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 3249 | static TemplateName |
| 3250 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3251 | TemplateDecl *Template, |
| 3252 | SourceLocation TemplateLoc, |
| 3253 | SourceLocation RAngleLoc, |
| 3254 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3255 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3256 | NestedNameSpecifierLoc &QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3257 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, Template, Converted, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3258 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3259 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3260 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3261 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3262 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3263 | Converted.data(), Converted.size()); |
| 3264 | |
| 3265 | // Only substitute for the innermost template argument list. |
| 3266 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3267 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3268 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3269 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3270 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3271 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3272 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3273 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3274 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3275 | QualifierLoc = |
| 3276 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3277 | if (!QualifierLoc) |
| 3278 | return TemplateName(); |
| 3279 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3280 | |
| 3281 | return SemaRef.SubstTemplateName( |
| 3282 | QualifierLoc, |
| 3283 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 3284 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 3285 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3288 | /// \brief If the given template parameter has a default template |
| 3289 | /// argument, substitute into that default template argument and |
| 3290 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3291 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3292 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 3293 | SourceLocation TemplateLoc, |
| 3294 | SourceLocation RAngleLoc, |
| 3295 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3296 | SmallVectorImpl<TemplateArgument> |
| 3297 | &Converted, |
| 3298 | bool &HasDefaultArg) { |
| 3299 | HasDefaultArg = false; |
| 3300 | |
| 3301 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3302 | if (!TypeParm->hasDefaultArgument()) |
| 3303 | return TemplateArgumentLoc(); |
| 3304 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3305 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3306 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3307 | TemplateLoc, |
| 3308 | RAngleLoc, |
| 3309 | TypeParm, |
| 3310 | Converted); |
| 3311 | if (DI) |
| 3312 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 3313 | |
| 3314 | return TemplateArgumentLoc(); |
| 3315 | } |
| 3316 | |
| 3317 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 3318 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3319 | if (!NonTypeParm->hasDefaultArgument()) |
| 3320 | return TemplateArgumentLoc(); |
| 3321 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3322 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3323 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3324 | TemplateLoc, |
| 3325 | RAngleLoc, |
| 3326 | NonTypeParm, |
| 3327 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3328 | if (Arg.isInvalid()) |
| 3329 | return TemplateArgumentLoc(); |
| 3330 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3331 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3332 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 3333 | } |
| 3334 | |
| 3335 | TemplateTemplateParmDecl *TempTempParm |
| 3336 | = cast<TemplateTemplateParmDecl>(Param); |
| 3337 | if (!TempTempParm->hasDefaultArgument()) |
| 3338 | return TemplateArgumentLoc(); |
| 3339 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3340 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3341 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3342 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3343 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3344 | RAngleLoc, |
| 3345 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3346 | Converted, |
| 3347 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3348 | if (TName.isNull()) |
| 3349 | return TemplateArgumentLoc(); |
| 3350 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3351 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3352 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3353 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 3354 | } |
| 3355 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3356 | /// \brief Check that the given template argument corresponds to the given |
| 3357 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3358 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3359 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3360 | /// checked. |
| 3361 | /// |
| 3362 | /// \param Arg The template argument. |
| 3363 | /// |
| 3364 | /// \param Template The template in which the template argument resides. |
| 3365 | /// |
| 3366 | /// \param TemplateLoc The location of the template name for the template |
| 3367 | /// whose argument list we're matching. |
| 3368 | /// |
| 3369 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 3370 | /// the template argument list. |
| 3371 | /// |
| 3372 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 3373 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 3374 | /// |
| 3375 | /// \param Converted The checked, converted argument will be added to the |
| 3376 | /// end of this small vector. |
| 3377 | /// |
| 3378 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 3379 | /// explicitly written, deduced, etc. |
| 3380 | /// |
| 3381 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3382 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3383 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 3384 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3385 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3386 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3387 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3388 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3389 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3390 | // Check template type parameters. |
| 3391 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3392 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3393 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3394 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3395 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3396 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3397 | // with the template arguments we've seen thus far. But if the |
| 3398 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3399 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3400 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 3401 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3402 | |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3403 | if (NTTPType->isDependentType() && |
| 3404 | !isa<TemplateTemplateParmDecl>(Template) && |
| 3405 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3406 | // Do substitution on the type of the non-type template parameter. |
| 3407 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3408 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3409 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3410 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3411 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3412 | |
| 3413 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3414 | Converted.data(), Converted.size()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3415 | NTTPType = SubstType(NTTPType, |
| 3416 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 3417 | NTTP->getLocation(), |
| 3418 | NTTP->getDeclName()); |
| 3419 | // If that worked, check the non-type template parameter type |
| 3420 | // for validity. |
| 3421 | if (!NTTPType.isNull()) |
| 3422 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 3423 | NTTP->getLocation()); |
| 3424 | if (NTTPType.isNull()) |
| 3425 | return true; |
| 3426 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3427 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3428 | switch (Arg.getArgument().getKind()) { |
| 3429 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3430 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3431 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3432 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3433 | TemplateArgument Result; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3434 | ExprResult Res = |
| 3435 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 3436 | Result, CTAK); |
| 3437 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3438 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3439 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3440 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3441 | break; |
| 3442 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3443 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3444 | case TemplateArgument::Declaration: |
| 3445 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3446 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3447 | // We've already checked this template argument, so just copy |
| 3448 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3449 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3450 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3451 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3452 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3453 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3454 | // We were given a template template argument. It may not be ill-formed; |
| 3455 | // see below. |
| 3456 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3457 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 3458 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3459 | // We have a template argument such as \c T::template X, which we |
| 3460 | // parsed as a template template argument. However, since we now |
| 3461 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3462 | // template name into an expression. |
| 3463 | |
| 3464 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 3465 | Arg.getTemplateNameLoc()); |
| 3466 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 3467 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3468 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3469 | // FIXME: the template-template arg was a DependentTemplateName, |
| 3470 | // so it was provided with a template keyword. However, its source |
| 3471 | // location is not stored in the template argument structure. |
| 3472 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3473 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 3474 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 3475 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3476 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3477 | // If we parsed the template argument as a pack expansion, create a |
| 3478 | // pack expansion expression. |
| 3479 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3480 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3481 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3482 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3483 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3484 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3485 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3486 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3487 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3488 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3489 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3490 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3491 | break; |
| 3492 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3493 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3494 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3495 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3496 | // therefore cannot be a non-type template argument. |
| 3497 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 3498 | << Arg.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3499 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3500 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3501 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3502 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3503 | case TemplateArgument::Type: { |
| 3504 | // We have a non-type template parameter but the template |
| 3505 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3506 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3507 | // C++ [temp.arg]p2: |
| 3508 | // In a template-argument, an ambiguity between a type-id and |
| 3509 | // an expression is resolved to a type-id, regardless of the |
| 3510 | // form of the corresponding template-parameter. |
| 3511 | // |
| 3512 | // We warn specifically about this case, since it can be rather |
| 3513 | // confusing for users. |
| 3514 | QualType T = Arg.getArgument().getAsType(); |
| 3515 | SourceRange SR = Arg.getSourceRange(); |
| 3516 | if (T->isFunctionType()) |
| 3517 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 3518 | else |
| 3519 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 3520 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3521 | return true; |
| 3522 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3523 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3524 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3525 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3526 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3527 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3528 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3532 | // Check template template parameters. |
| 3533 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3534 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3535 | // Substitute into the template parameter list of the template |
| 3536 | // template parameter, since previously-supplied template arguments |
| 3537 | // may appear within the template template parameter. |
| 3538 | { |
| 3539 | // Set up a template instantiation context. |
| 3540 | LocalInstantiationScope Scope(*this); |
| 3541 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3542 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3543 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3544 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3545 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3546 | |
| 3547 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3548 | Converted.data(), Converted.size()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3549 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3550 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3551 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 3552 | if (!TempParm) |
| 3553 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3554 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3555 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3556 | switch (Arg.getArgument().getKind()) { |
| 3557 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3558 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3559 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3560 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3561 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3562 | if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3563 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3564 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3565 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3566 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3567 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3568 | case TemplateArgument::Expression: |
| 3569 | case TemplateArgument::Type: |
| 3570 | // We have a template template parameter but the template |
| 3571 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3572 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3573 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3574 | return true; |
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 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3577 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3578 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3579 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3580 | case TemplateArgument::NullPtr: |
| 3581 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3582 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3583 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3584 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3585 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3586 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3587 | return false; |
| 3588 | } |
| 3589 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3590 | /// \brief Diagnose an arity mismatch in the |
| 3591 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 3592 | SourceLocation TemplateLoc, |
| 3593 | TemplateArgumentListInfo &TemplateArgs) { |
| 3594 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 3595 | unsigned NumParams = Params->size(); |
| 3596 | unsigned NumArgs = TemplateArgs.size(); |
| 3597 | |
| 3598 | SourceRange Range; |
| 3599 | if (NumArgs > NumParams) |
| 3600 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
| 3601 | TemplateArgs.getRAngleLoc()); |
| 3602 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3603 | << (NumArgs > NumParams) |
| 3604 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3605 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3606 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3607 | << Template << Range; |
| 3608 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3609 | << Params->getSourceRange(); |
| 3610 | return true; |
| 3611 | } |
| 3612 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3613 | /// \brief Check whether the template parameter is a pack expansion, and if so, |
| 3614 | /// determine the number of parameters produced by that expansion. For instance: |
| 3615 | /// |
| 3616 | /// \code |
| 3617 | /// template<typename ...Ts> struct A { |
| 3618 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 3619 | /// }; |
| 3620 | /// \endcode |
| 3621 | /// |
| 3622 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 3623 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3624 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3625 | if (NonTypeTemplateParmDecl *NTTP |
| 3626 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3627 | if (NTTP->isExpandedParameterPack()) |
| 3628 | return NTTP->getNumExpansionTypes(); |
| 3629 | } |
| 3630 | |
| 3631 | if (TemplateTemplateParmDecl *TTP |
| 3632 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 3633 | if (TTP->isExpandedParameterPack()) |
| 3634 | return TTP->getNumExpansionTemplateParameters(); |
| 3635 | } |
| 3636 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 3637 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3640 | /// \brief Check that the given template argument list is well-formed |
| 3641 | /// for specializing the given template. |
| 3642 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 3643 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3644 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 3645 | bool PartialTemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3646 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3647 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3648 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3649 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 3650 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3651 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3652 | // [...] The type and form of each template-argument specified in |
| 3653 | // a template-id shall match the type and form specified for the |
| 3654 | // corresponding parameter declared by the template in its |
| 3655 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3656 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3657 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3658 | unsigned ArgIdx = 0, NumArgs = TemplateArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3659 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3660 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 3661 | ParamEnd = Params->end(); |
| 3662 | Param != ParamEnd; /* increment in loop */) { |
| 3663 | // If we have an expanded parameter pack, make sure we don't have too |
| 3664 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3665 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3666 | if (*Expansions == ArgumentPack.size()) { |
| 3667 | // We're done with this parameter pack. Pack up its arguments and add |
| 3668 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3669 | Converted.push_back( |
| 3670 | TemplateArgument::CreatePackCopy(Context, |
| 3671 | ArgumentPack.data(), |
| 3672 | ArgumentPack.size())); |
| 3673 | ArgumentPack.clear(); |
| 3674 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3675 | // This argument is assigned to the next parameter. |
| 3676 | ++Param; |
| 3677 | continue; |
| 3678 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 3679 | // Not enough arguments for this parameter pack. |
| 3680 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3681 | << false |
| 3682 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3683 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3684 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3685 | << Template; |
| 3686 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3687 | << Params->getSourceRange(); |
| 3688 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3689 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3690 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3691 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3692 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3693 | // Check the template argument we were given. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3694 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 3695 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3696 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3697 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3698 | |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3699 | if (TemplateArgs[ArgIdx].getArgument().isPackExpansion() && |
| 3700 | isa<TypeAliasTemplateDecl>(Template) && |
| 3701 | !(Param + 1 == ParamEnd && (*Param)->isTemplateParameterPack() && |
| 3702 | !getExpandedPackSize(*Param))) { |
| 3703 | // Core issue 1430: we have a pack expansion as an argument to an |
| 3704 | // alias template, and it's not part of a final parameter pack. This |
| 3705 | // can't be canonicalized, so reject it now. |
| 3706 | Diag(TemplateArgs[ArgIdx].getLocation(), |
| 3707 | diag::err_alias_template_expansion_into_fixed_list) |
| 3708 | << TemplateArgs[ArgIdx].getSourceRange(); |
| 3709 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 3710 | return true; |
| 3711 | } |
| 3712 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3713 | // We're now done with this argument. |
| 3714 | ++ArgIdx; |
| 3715 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3716 | if ((*Param)->isTemplateParameterPack()) { |
| 3717 | // The template parameter was a template parameter pack, so take the |
| 3718 | // deduced argument and place it on the argument pack. Note that we |
| 3719 | // stay on the same template parameter so that we can deduce more |
| 3720 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 3721 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3722 | } else { |
| 3723 | // Move to the next template parameter. |
| 3724 | ++Param; |
| 3725 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3726 | |
| 3727 | // If we just saw a pack expansion, then directly convert the remaining |
| 3728 | // arguments, because we don't know what parameters they'll match up |
| 3729 | // with. |
| 3730 | if (TemplateArgs[ArgIdx-1].getArgument().isPackExpansion()) { |
| 3731 | bool InFinalParameterPack = Param != ParamEnd && |
| 3732 | Param + 1 == ParamEnd && |
| 3733 | (*Param)->isTemplateParameterPack() && |
| 3734 | !getExpandedPackSize(*Param); |
| 3735 | |
| 3736 | if (!InFinalParameterPack && !ArgumentPack.empty()) { |
| 3737 | // If we were part way through filling in an expanded parameter pack, |
| 3738 | // fall back to just producing individual arguments. |
| 3739 | Converted.insert(Converted.end(), |
| 3740 | ArgumentPack.begin(), ArgumentPack.end()); |
| 3741 | ArgumentPack.clear(); |
| 3742 | } |
| 3743 | |
| 3744 | while (ArgIdx < NumArgs) { |
| 3745 | if (InFinalParameterPack) |
| 3746 | ArgumentPack.push_back(TemplateArgs[ArgIdx].getArgument()); |
| 3747 | else |
| 3748 | Converted.push_back(TemplateArgs[ArgIdx].getArgument()); |
| 3749 | ++ArgIdx; |
| 3750 | } |
| 3751 | |
| 3752 | // Push the argument pack onto the list of converted arguments. |
| 3753 | if (InFinalParameterPack) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3754 | Converted.push_back( |
| 3755 | TemplateArgument::CreatePackCopy(Context, |
| 3756 | ArgumentPack.data(), |
| 3757 | ArgumentPack.size())); |
| 3758 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3759 | } |
| 3760 | |
| 3761 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3762 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3763 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3764 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3765 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3766 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3767 | // If we're checking a partial template argument list, we're done. |
| 3768 | if (PartialTemplateArgs) { |
| 3769 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 3770 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3771 | ArgumentPack.data(), |
| 3772 | ArgumentPack.size())); |
| 3773 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3774 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3777 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3778 | // 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] | 3779 | if ((*Param)->isTemplateParameterPack()) { |
| 3780 | assert(!getExpandedPackSize(*Param) && |
| 3781 | "Should have dealt with this already"); |
| 3782 | |
| 3783 | // A non-expanded parameter pack before the end of the parameter list |
| 3784 | // only occurs for an ill-formed template parameter list, unless we've |
| 3785 | // got a partial argument list for a function template, so just bail out. |
| 3786 | if (Param + 1 != ParamEnd) |
| 3787 | return true; |
| 3788 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3789 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3790 | ArgumentPack.data(), |
| 3791 | ArgumentPack.size())); |
| 3792 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3793 | |
| 3794 | ++Param; |
| 3795 | continue; |
| 3796 | } |
| 3797 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3798 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3799 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3800 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3801 | // Retrieve the default template argument from the template |
| 3802 | // parameter. For each kind of template parameter, we substitute the |
| 3803 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3804 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3805 | // the default argument. |
| 3806 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3807 | if (!TTP->hasDefaultArgument()) |
| 3808 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3809 | TemplateArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3810 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3811 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3812 | Template, |
| 3813 | TemplateLoc, |
| 3814 | RAngleLoc, |
| 3815 | TTP, |
| 3816 | Converted); |
| 3817 | if (!ArgType) |
| 3818 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3819 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3820 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 3821 | ArgType); |
| 3822 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3823 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3824 | if (!NTTP->hasDefaultArgument()) |
| 3825 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3826 | TemplateArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3827 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3828 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3829 | TemplateLoc, |
| 3830 | RAngleLoc, |
| 3831 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3832 | Converted); |
| 3833 | if (E.isInvalid()) |
| 3834 | return true; |
| 3835 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3836 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3837 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 3838 | } else { |
| 3839 | TemplateTemplateParmDecl *TempParm |
| 3840 | = cast<TemplateTemplateParmDecl>(*Param); |
| 3841 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3842 | if (!TempParm->hasDefaultArgument()) |
| 3843 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3844 | TemplateArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3845 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3846 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3847 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3848 | TemplateLoc, |
| 3849 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3850 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3851 | Converted, |
| 3852 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3853 | if (Name.isNull()) |
| 3854 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3855 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3856 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3857 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3858 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3859 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3860 | // Introduce an instantiation record that describes where we are using |
| 3861 | // the default template argument. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3862 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 3863 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3864 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3865 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3866 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3867 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3868 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3869 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3870 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3871 | |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3872 | // Core issue 150 (assumed resolution): if this is a template template |
| 3873 | // parameter, keep track of the default template arguments from the |
| 3874 | // template definition. |
| 3875 | if (isTemplateTemplateParameter) |
| 3876 | TemplateArgs.addArgument(Arg); |
| 3877 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3878 | // Move to the next template parameter and argument. |
| 3879 | ++Param; |
| 3880 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3881 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3882 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 3883 | // If we're performing a partial argument substitution, allow any trailing |
| 3884 | // pack expansions; they might be empty. This can happen even if |
| 3885 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 3886 | // still dependent). |
| 3887 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 3888 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
| 3889 | while (ArgIdx < NumArgs && |
| 3890 | TemplateArgs[ArgIdx].getArgument().isPackExpansion()) |
| 3891 | Converted.push_back(TemplateArgs[ArgIdx++].getArgument()); |
| 3892 | } |
| 3893 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3894 | // If we have any leftover arguments, then there were too many arguments. |
| 3895 | // Complain and fail. |
| 3896 | if (ArgIdx < NumArgs) |
| 3897 | return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3898 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3899 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3902 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3903 | class UnnamedLocalNoLinkageFinder |
| 3904 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3905 | { |
| 3906 | Sema &S; |
| 3907 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3908 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3909 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3910 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3911 | public: |
| 3912 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3913 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3914 | bool Visit(QualType T) { |
| 3915 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3916 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3917 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3918 | #define TYPE(Class, Parent) \ |
| 3919 | bool Visit##Class##Type(const Class##Type *); |
| 3920 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 3921 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3922 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 3923 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3924 | #include "clang/AST/TypeNodes.def" |
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 | bool VisitTagDecl(const TagDecl *Tag); |
| 3927 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3928 | }; |
| 3929 | } |
| 3930 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3931 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3932 | return false; |
| 3933 | } |
| 3934 | |
| 3935 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3936 | return Visit(T->getElementType()); |
| 3937 | } |
| 3938 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3939 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3940 | return Visit(T->getPointeeType()); |
| 3941 | } |
| 3942 | |
| 3943 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3944 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3945 | return Visit(T->getPointeeType()); |
| 3946 | } |
| 3947 | |
| 3948 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3949 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3950 | return Visit(T->getPointeeType()); |
| 3951 | } |
| 3952 | |
| 3953 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3954 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3955 | return Visit(T->getPointeeType()); |
| 3956 | } |
| 3957 | |
| 3958 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3959 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3960 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3961 | } |
| 3962 | |
| 3963 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3964 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3965 | return Visit(T->getElementType()); |
| 3966 | } |
| 3967 | |
| 3968 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3969 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3970 | return Visit(T->getElementType()); |
| 3971 | } |
| 3972 | |
| 3973 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3974 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3975 | return Visit(T->getElementType()); |
| 3976 | } |
| 3977 | |
| 3978 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3979 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3980 | return Visit(T->getElementType()); |
| 3981 | } |
| 3982 | |
| 3983 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3984 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3985 | return Visit(T->getElementType()); |
| 3986 | } |
| 3987 | |
| 3988 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3989 | return Visit(T->getElementType()); |
| 3990 | } |
| 3991 | |
| 3992 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 3993 | return Visit(T->getElementType()); |
| 3994 | } |
| 3995 | |
| 3996 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 3997 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 3998 | for (const auto &A : T->param_types()) { |
| 3999 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4000 | return true; |
| 4001 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4002 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4003 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4004 | } |
| 4005 | |
| 4006 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 4007 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4008 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4009 | } |
| 4010 | |
| 4011 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 4012 | const UnresolvedUsingType*) { |
| 4013 | return false; |
| 4014 | } |
| 4015 | |
| 4016 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 4017 | return false; |
| 4018 | } |
| 4019 | |
| 4020 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 4021 | return Visit(T->getUnderlyingType()); |
| 4022 | } |
| 4023 | |
| 4024 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 4025 | return false; |
| 4026 | } |
| 4027 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4028 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 4029 | const UnaryTransformType*) { |
| 4030 | return false; |
| 4031 | } |
| 4032 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4033 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 4034 | return Visit(T->getDeducedType()); |
| 4035 | } |
| 4036 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4037 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 4038 | return VisitTagDecl(T->getDecl()); |
| 4039 | } |
| 4040 | |
| 4041 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 4042 | return VisitTagDecl(T->getDecl()); |
| 4043 | } |
| 4044 | |
| 4045 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 4046 | const TemplateTypeParmType*) { |
| 4047 | return false; |
| 4048 | } |
| 4049 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4050 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 4051 | const SubstTemplateTypeParmPackType *) { |
| 4052 | return false; |
| 4053 | } |
| 4054 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4055 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 4056 | const TemplateSpecializationType*) { |
| 4057 | return false; |
| 4058 | } |
| 4059 | |
| 4060 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 4061 | const InjectedClassNameType* T) { |
| 4062 | return VisitTagDecl(T->getDecl()); |
| 4063 | } |
| 4064 | |
| 4065 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 4066 | const DependentNameType* T) { |
| 4067 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4068 | } |
| 4069 | |
| 4070 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 4071 | const DependentTemplateSpecializationType* T) { |
| 4072 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4073 | } |
| 4074 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4075 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 4076 | const PackExpansionType* T) { |
| 4077 | return Visit(T->getPattern()); |
| 4078 | } |
| 4079 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4080 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 4081 | return false; |
| 4082 | } |
| 4083 | |
| 4084 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 4085 | const ObjCInterfaceType *) { |
| 4086 | return false; |
| 4087 | } |
| 4088 | |
| 4089 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 4090 | const ObjCObjectPointerType *) { |
| 4091 | return false; |
| 4092 | } |
| 4093 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4094 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 4095 | return Visit(T->getValueType()); |
| 4096 | } |
| 4097 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4098 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 4099 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4100 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4101 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4102 | diag::warn_cxx98_compat_template_arg_local_type : |
| 4103 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4104 | << S.Context.getTypeDeclType(Tag) << SR; |
| 4105 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 4108 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4109 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4110 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4111 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 4112 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4113 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 4114 | return true; |
| 4115 | } |
| 4116 | |
| 4117 | return false; |
| 4118 | } |
| 4119 | |
| 4120 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 4121 | NestedNameSpecifier *NNS) { |
| 4122 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 4123 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4124 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4125 | switch (NNS->getKind()) { |
| 4126 | case NestedNameSpecifier::Identifier: |
| 4127 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4128 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4129 | case NestedNameSpecifier::Global: |
| 4130 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4131 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4132 | case NestedNameSpecifier::TypeSpec: |
| 4133 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 4134 | return Visit(QualType(NNS->getAsType(), 0)); |
| 4135 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4136 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4137 | } |
| 4138 | |
| 4139 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4140 | /// \brief Check a template argument against its corresponding |
| 4141 | /// template type parameter. |
| 4142 | /// |
| 4143 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 4144 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4145 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4146 | TypeSourceInfo *ArgInfo) { |
| 4147 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4148 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 4149 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 4150 | |
| 4151 | if (Arg->isVariablyModifiedType()) { |
| 4152 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4153 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4154 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4155 | } |
| 4156 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4157 | // C++03 [temp.arg.type]p2: |
| 4158 | // A local type, a type with no linkage, an unnamed type or a type |
| 4159 | // compounded from any of these types shall not be used as a |
| 4160 | // template-argument for a template type-parameter. |
| 4161 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4162 | // 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] | 4163 | // a warning. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4164 | if (LangOpts.CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4165 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 4166 | SR.getBegin()) != DiagnosticsEngine::Ignored || |
| 4167 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type, |
| 4168 | SR.getBegin()) != DiagnosticsEngine::Ignored : |
| 4169 | Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4170 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 4171 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 4172 | } |
| 4173 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4174 | return false; |
| 4175 | } |
| 4176 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4177 | enum NullPointerValueKind { |
| 4178 | NPV_NotNullPointer, |
| 4179 | NPV_NullPointer, |
| 4180 | NPV_Error |
| 4181 | }; |
| 4182 | |
| 4183 | /// \brief Determine whether the given template argument is a null pointer |
| 4184 | /// value of the appropriate type. |
| 4185 | static NullPointerValueKind |
| 4186 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 4187 | QualType ParamType, Expr *Arg) { |
| 4188 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 4189 | return NPV_NotNullPointer; |
| 4190 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4191 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4192 | return NPV_NotNullPointer; |
| 4193 | |
| 4194 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4195 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 4196 | if (ArgRV.isInvalid()) |
| 4197 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4198 | Arg = ArgRV.get(); |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4199 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4200 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4201 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4202 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4203 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4204 | EvalResult.HasSideEffects) { |
| 4205 | SourceLocation DiagLoc = Arg->getExprLoc(); |
| 4206 | |
| 4207 | // If our only note is the usual "invalid subexpression" note, just point |
| 4208 | // the caret at its location rather than producing an essentially |
| 4209 | // redundant note. |
| 4210 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 4211 | diag::note_invalid_subexpr_in_const_expr) { |
| 4212 | DiagLoc = Notes[0].first; |
| 4213 | Notes.clear(); |
| 4214 | } |
| 4215 | |
| 4216 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 4217 | << Arg->getType() << Arg->getSourceRange(); |
| 4218 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 4219 | S.Diag(Notes[I].first, Notes[I].second); |
| 4220 | |
| 4221 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4222 | return NPV_Error; |
| 4223 | } |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4224 | |
| 4225 | // C++11 [temp.arg.nontype]p1: |
| 4226 | // - an address constant expression of type std::nullptr_t |
| 4227 | if (Arg->getType()->isNullPtrType()) |
| 4228 | return NPV_NullPointer; |
| 4229 | |
| 4230 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 4231 | // - a constant expression that evaluates to a null member pointer value |
| 4232 | // (4.11); or |
| 4233 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 4234 | (EvalResult.Val.isMemberPointer() && |
| 4235 | !EvalResult.Val.getMemberPointerDecl())) { |
| 4236 | // If our expression has an appropriate type, we've succeeded. |
| 4237 | bool ObjCLifetimeConversion; |
| 4238 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 4239 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 4240 | ObjCLifetimeConversion)) |
| 4241 | return NPV_NullPointer; |
| 4242 | |
| 4243 | // The types didn't match, but we know we got a null pointer; complain, |
| 4244 | // then recover as if the types were correct. |
| 4245 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 4246 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4247 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4248 | return NPV_NullPointer; |
| 4249 | } |
| 4250 | |
| 4251 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 4252 | // constant, suggest a cast to the appropriate type. |
| 4253 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 4254 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 4255 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4256 | << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 4257 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()), |
| 4258 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4259 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4260 | return NPV_NullPointer; |
| 4261 | } |
| 4262 | |
| 4263 | // FIXME: If we ever want to support general, address-constant expressions |
| 4264 | // as non-type template arguments, we should return the ExprResult here to |
| 4265 | // be interpreted by the caller. |
| 4266 | return NPV_NotNullPointer; |
| 4267 | } |
| 4268 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4269 | /// \brief Checks whether the given template argument is compatible with its |
| 4270 | /// template parameter. |
| 4271 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 4272 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 4273 | Expr *Arg, QualType ArgType) { |
| 4274 | bool ObjCLifetimeConversion; |
| 4275 | if (ParamType->isPointerType() && |
| 4276 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 4277 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 4278 | ObjCLifetimeConversion)) { |
| 4279 | // For pointer-to-object types, qualification conversions are |
| 4280 | // permitted. |
| 4281 | } else { |
| 4282 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 4283 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 4284 | // C++ [temp.arg.nontype]p5b3: |
| 4285 | // For a non-type template-parameter of type reference to |
| 4286 | // object, no conversions apply. The type referred to by the |
| 4287 | // reference may be more cv-qualified than the (otherwise |
| 4288 | // identical) type of the template- argument. The |
| 4289 | // template-parameter is bound directly to the |
| 4290 | // template-argument, which shall be an lvalue. |
| 4291 | |
| 4292 | // FIXME: Other qualifiers? |
| 4293 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 4294 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 4295 | |
| 4296 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 4297 | S.Diag(Arg->getLocStart(), |
| 4298 | diag::err_template_arg_ref_bind_ignores_quals) |
| 4299 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 4300 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4301 | return true; |
| 4302 | } |
| 4303 | } |
| 4304 | } |
| 4305 | |
| 4306 | // At this point, the template argument refers to an object or |
| 4307 | // function with external linkage. We now need to check whether the |
| 4308 | // argument and parameter types are compatible. |
| 4309 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 4310 | ParamType.getNonReferenceType())) { |
| 4311 | // We can't perform this conversion or binding. |
| 4312 | if (ParamType->isReferenceType()) |
| 4313 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 4314 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
| 4315 | else |
| 4316 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4317 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
| 4318 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4319 | return true; |
| 4320 | } |
| 4321 | } |
| 4322 | |
| 4323 | return false; |
| 4324 | } |
| 4325 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4326 | /// \brief Checks whether the given template argument is the address |
| 4327 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4328 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4329 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 4330 | NonTypeTemplateParmDecl *Param, |
| 4331 | QualType ParamType, |
| 4332 | Expr *ArgIn, |
| 4333 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4334 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4335 | Expr *Arg = ArgIn; |
| 4336 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4337 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4338 | // If our parameter has pointer type, check for a null template value. |
| 4339 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 4340 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 4341 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 4342 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4343 | Converted = TemplateArgument(ParamType, /*isNullPtr*/true); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4344 | return false; |
| 4345 | |
| 4346 | case NPV_Error: |
| 4347 | return true; |
| 4348 | |
| 4349 | case NPV_NotNullPointer: |
| 4350 | break; |
| 4351 | } |
| 4352 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4353 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4354 | bool AddressTaken = false; |
| 4355 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4356 | if (S.getLangOpts().MicrosoftExt) { |
| 4357 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 4358 | // dereference and address-of operators. |
| 4359 | Arg = Arg->IgnoreParenCasts(); |
| 4360 | |
| 4361 | bool ExtWarnMSTemplateArg = false; |
| 4362 | UnaryOperatorKind FirstOpKind; |
| 4363 | SourceLocation FirstOpLoc; |
| 4364 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4365 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 4366 | if (UnOpKind == UO_Deref) |
| 4367 | ExtWarnMSTemplateArg = true; |
| 4368 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 4369 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 4370 | if (!AddrOpLoc.isValid()) { |
| 4371 | FirstOpKind = UnOpKind; |
| 4372 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 4373 | } |
| 4374 | } else |
| 4375 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4376 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4377 | if (FirstOpLoc.isValid()) { |
| 4378 | if (ExtWarnMSTemplateArg) |
| 4379 | S.Diag(ArgIn->getLocStart(), diag::ext_ms_deref_template_argument) |
| 4380 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4381 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4382 | if (FirstOpKind == UO_AddrOf) |
| 4383 | AddressTaken = true; |
| 4384 | else if (Arg->getType()->isPointerType()) { |
| 4385 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 4386 | // constant expression. |
| 4387 | assert(FirstOpKind == UO_Deref); |
| 4388 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4389 | << Arg->getSourceRange(); |
| 4390 | } |
| 4391 | } |
| 4392 | } else { |
| 4393 | // See through any implicit casts we added to fix the type. |
| 4394 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4395 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4396 | // C++ [temp.arg.nontype]p1: |
| 4397 | // |
| 4398 | // A template-argument for a non-type, non-template |
| 4399 | // template-parameter shall be one of: [...] |
| 4400 | // |
| 4401 | // -- the address of an object or function with external |
| 4402 | // linkage, including function templates and function |
| 4403 | // template-ids but excluding non-static class members, |
| 4404 | // expressed as & id-expression where the & is optional if |
| 4405 | // the name refers to a function or array, or if the |
| 4406 | // corresponding template-parameter is a reference; or |
| 4407 | |
| 4408 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4409 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4410 | bool ExtraParens = false; |
| 4411 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 4412 | if (!Invalid && !ExtraParens) { |
| 4413 | S.Diag(Arg->getLocStart(), |
| 4414 | S.getLangOpts().CPlusPlus11 |
| 4415 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 4416 | : diag::ext_template_arg_extra_parens) |
| 4417 | << Arg->getSourceRange(); |
| 4418 | ExtraParens = true; |
| 4419 | } |
| 4420 | |
| 4421 | Arg = Parens->getSubExpr(); |
| 4422 | } |
| 4423 | |
| 4424 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4425 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4426 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4427 | |
| 4428 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4429 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 4430 | Arg = UnOp->getSubExpr(); |
| 4431 | AddressTaken = true; |
| 4432 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 4433 | } |
| 4434 | } |
| 4435 | |
| 4436 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4437 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4438 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4439 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4440 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4441 | // Stop checking the precise nature of the argument if it is value dependent, |
| 4442 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4443 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4444 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4445 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4446 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4447 | |
| 4448 | if (isa<CXXUuidofExpr>(Arg)) { |
| 4449 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 4450 | ArgIn, Arg, ArgType)) |
| 4451 | return true; |
| 4452 | |
| 4453 | Converted = TemplateArgument(ArgIn); |
| 4454 | return false; |
| 4455 | } |
| 4456 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4457 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 4458 | if (!DRE) { |
| 4459 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4460 | << Arg->getSourceRange(); |
| 4461 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4462 | return true; |
| 4463 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4464 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4465 | ValueDecl *Entity = DRE->getDecl(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4466 | |
| 4467 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4468 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4469 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4470 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4471 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4472 | return true; |
| 4473 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4474 | |
| 4475 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4476 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4477 | if (!Method->isStatic()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4478 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4479 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4480 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4481 | return true; |
| 4482 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4483 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4484 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4485 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 4486 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4487 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4488 | // A non-type template argument must refer to an object or function. |
| 4489 | if (!Func && !Var) { |
| 4490 | // We found something, but we don't know specifically what it is. |
| 4491 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 4492 | << Arg->getSourceRange(); |
| 4493 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 4494 | return true; |
| 4495 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4496 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4497 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4498 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4499 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4500 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 4501 | diag::ext_template_arg_object_internal) |
| 4502 | << !Func << Entity << Arg->getSourceRange(); |
| 4503 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4504 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4505 | } else if (!Entity->hasLinkage()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4506 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 4507 | << !Func << Entity << Arg->getSourceRange(); |
| 4508 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4509 | << !Func; |
| 4510 | return true; |
| 4511 | } |
| 4512 | |
| 4513 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4514 | // If the template parameter has pointer type, the function decays. |
| 4515 | if (ParamType->isPointerType() && !AddressTaken) |
| 4516 | ArgType = S.Context.getPointerType(Func->getType()); |
| 4517 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 4518 | // If we originally had an address-of operator, but the |
| 4519 | // parameter has reference type, complain and (if things look |
| 4520 | // like they will work) drop the address-of operator. |
| 4521 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 4522 | ParamType.getNonReferenceType())) { |
| 4523 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4524 | << ParamType; |
| 4525 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4526 | return true; |
| 4527 | } |
| 4528 | |
| 4529 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4530 | << ParamType |
| 4531 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4532 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4533 | |
| 4534 | ArgType = Func->getType(); |
| 4535 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4536 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4537 | // A value of reference type is not an object. |
| 4538 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4539 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4540 | diag::err_template_arg_reference_var) |
| 4541 | << Var->getType() << Arg->getSourceRange(); |
| 4542 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4543 | return true; |
| 4544 | } |
| 4545 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4546 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 4547 | if (Var->getTLSKind()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4548 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 4549 | << Arg->getSourceRange(); |
| 4550 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 4551 | return true; |
| 4552 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4553 | |
| 4554 | // If the template parameter has pointer type, we must have taken |
| 4555 | // the address of this object. |
| 4556 | if (ParamType->isReferenceType()) { |
| 4557 | if (AddressTaken) { |
| 4558 | // If we originally had an address-of operator, but the |
| 4559 | // parameter has reference type, complain and (if things look |
| 4560 | // like they will work) drop the address-of operator. |
| 4561 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 4562 | ParamType.getNonReferenceType())) { |
| 4563 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4564 | << ParamType; |
| 4565 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4566 | return true; |
| 4567 | } |
| 4568 | |
| 4569 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4570 | << ParamType |
| 4571 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4572 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4573 | |
| 4574 | ArgType = Var->getType(); |
| 4575 | } |
| 4576 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 4577 | if (Var->getType()->isArrayType()) { |
| 4578 | // Array-to-pointer decay. |
| 4579 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 4580 | } else { |
| 4581 | // If the template parameter has pointer type but the address of |
| 4582 | // this object was not taken, complain and (possibly) recover by |
| 4583 | // taking the address of the entity. |
| 4584 | ArgType = S.Context.getPointerType(Var->getType()); |
| 4585 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 4586 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4587 | << ParamType; |
| 4588 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4589 | return true; |
| 4590 | } |
| 4591 | |
| 4592 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4593 | << ParamType |
| 4594 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 4595 | |
| 4596 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4597 | } |
| 4598 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4599 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4600 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4601 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 4602 | Arg, ArgType)) |
| 4603 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4604 | |
| 4605 | // Create the template argument. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4606 | Converted = TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), |
| 4607 | ParamType->isReferenceType()); |
Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 4608 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4609 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4610 | } |
| 4611 | |
| 4612 | /// \brief Checks whether the given template argument is a pointer to |
| 4613 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4614 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 4615 | NonTypeTemplateParmDecl *Param, |
| 4616 | QualType ParamType, |
| 4617 | Expr *&ResultArg, |
| 4618 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4619 | bool Invalid = false; |
| 4620 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4621 | // Check for a null pointer value. |
| 4622 | Expr *Arg = ResultArg; |
| 4623 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 4624 | case NPV_Error: |
| 4625 | return true; |
| 4626 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 4627 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4628 | Converted = TemplateArgument(ParamType, /*isNullPtr*/true); |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 4629 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 4630 | S.RequireCompleteType(Arg->getExprLoc(), ParamType, 0); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4631 | return false; |
| 4632 | case NPV_NotNullPointer: |
| 4633 | break; |
| 4634 | } |
| 4635 | |
| 4636 | bool ObjCLifetimeConversion; |
| 4637 | if (S.IsQualificationConversion(Arg->getType(), |
| 4638 | ParamType.getNonReferenceType(), |
| 4639 | false, ObjCLifetimeConversion)) { |
| 4640 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4641 | Arg->getValueKind()).get(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4642 | ResultArg = Arg; |
| 4643 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), |
| 4644 | ParamType.getNonReferenceType())) { |
| 4645 | // We can't perform this conversion. |
| 4646 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4647 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4648 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4649 | return true; |
| 4650 | } |
| 4651 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4652 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4653 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4654 | Arg = Cast->getSubExpr(); |
| 4655 | |
| 4656 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4657 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4658 | // A template-argument for a non-type, non-template |
| 4659 | // template-parameter shall be one of: [...] |
| 4660 | // |
| 4661 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4662 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4663 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4664 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4665 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4666 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4667 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4668 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4669 | S.Diag(Arg->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4670 | S.getLangOpts().CPlusPlus11 ? |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4671 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 4672 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4673 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4674 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4675 | } |
| 4676 | |
| 4677 | Arg = Parens->getSubExpr(); |
| 4678 | } |
| 4679 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4680 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4681 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4682 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4683 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4684 | // A pointer-to-member constant written &Class::member. |
| 4685 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4686 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4687 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 4688 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4689 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4690 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4691 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4692 | // A constant of pointer-to-member type. |
| 4693 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 4694 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 4695 | if (VD->getType()->isMemberPointerType()) { |
David Majnemer | cd053cd | 2013-12-10 00:40:58 +0000 | [diff] [blame] | 4696 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4697 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4698 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4699 | } else { |
| 4700 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
| 4701 | Converted = TemplateArgument(VD, /*isReferenceParam*/false); |
| 4702 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4703 | return Invalid; |
| 4704 | } |
| 4705 | } |
| 4706 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4707 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4708 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4709 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4710 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4711 | if (!DRE) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4712 | return S.Diag(Arg->getLocStart(), |
| 4713 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4714 | << Arg->getSourceRange(); |
| 4715 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4716 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 4717 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 4718 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4719 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4720 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4721 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 4722 | "Only non-static member pointers can make it here"); |
| 4723 | |
| 4724 | // Okay: this is the address of a non-static member, and therefore |
| 4725 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4726 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4727 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4728 | } else { |
| 4729 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
| 4730 | Converted = TemplateArgument(D, /*isReferenceParam*/false); |
| 4731 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4732 | return Invalid; |
| 4733 | } |
| 4734 | |
| 4735 | // 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] | 4736 | S.Diag(Arg->getLocStart(), |
| 4737 | diag::err_template_arg_not_pointer_to_member_form) |
| 4738 | << Arg->getSourceRange(); |
| 4739 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4740 | return true; |
| 4741 | } |
| 4742 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4743 | /// \brief Check a template argument against its corresponding |
| 4744 | /// non-type template parameter. |
| 4745 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4746 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4747 | /// If an error occurred, it returns ExprError(); otherwise, it |
| 4748 | /// returns the converted template argument. \p |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4749 | /// InstantiatedParamType is the type of the non-type template |
| 4750 | /// parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4751 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 4752 | QualType InstantiatedParamType, Expr *Arg, |
| 4753 | TemplateArgument &Converted, |
| 4754 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4755 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4756 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4757 | // If either the parameter has a dependent type or the argument is |
| 4758 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4759 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 4760 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 4761 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4762 | return Arg; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4763 | } |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4764 | |
| 4765 | // C++ [temp.arg.nontype]p5: |
| 4766 | // The following conversions are performed on each expression used |
| 4767 | // as a non-type template-argument. If a non-type |
| 4768 | // template-argument cannot be converted to the type of the |
| 4769 | // corresponding template-parameter then the program is |
| 4770 | // ill-formed. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4771 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4772 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4773 | // C++11: |
| 4774 | // -- for a non-type template-parameter of integral or |
| 4775 | // enumeration type, conversions permitted in a converted |
| 4776 | // constant expression are applied. |
| 4777 | // |
| 4778 | // C++98: |
| 4779 | // -- for a non-type template-parameter of integral or |
| 4780 | // enumeration type, integral promotions (4.5) and integral |
| 4781 | // conversions (4.7) are applied. |
| 4782 | |
| 4783 | if (CTAK == CTAK_Deduced && |
| 4784 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 4785 | // C++ [temp.deduct.type]p17: |
| 4786 | // If, in the declaration of a function template with a non-type |
| 4787 | // template-parameter, the non-type template-parameter is used |
| 4788 | // in an expression in the function parameter-list and, if the |
| 4789 | // corresponding template-argument is deduced, the |
| 4790 | // template-argument type shall match the type of the |
| 4791 | // template-parameter exactly, except that a template-argument |
| 4792 | // deduced from an array bound may be of any integral type. |
| 4793 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 4794 | << Arg->getType().getUnqualifiedType() |
| 4795 | << ParamType.getUnqualifiedType(); |
| 4796 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4797 | return ExprError(); |
| 4798 | } |
| 4799 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4800 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4801 | // We can't check arbitrary value-dependent arguments. |
| 4802 | // FIXME: If there's no viable conversion to the template parameter type, |
| 4803 | // we should be able to diagnose that prior to instantiation. |
| 4804 | if (Arg->isValueDependent()) { |
| 4805 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4806 | return Arg; |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
| 4809 | // C++ [temp.arg.nontype]p1: |
| 4810 | // A template-argument for a non-type, non-template template-parameter |
| 4811 | // shall be one of: |
| 4812 | // |
| 4813 | // -- for a non-type template-parameter of integral or enumeration |
| 4814 | // type, a converted constant expression of the type of the |
| 4815 | // template-parameter; or |
| 4816 | llvm::APSInt Value; |
| 4817 | ExprResult ArgResult = |
| 4818 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 4819 | CCEK_TemplateArg); |
| 4820 | if (ArgResult.isInvalid()) |
| 4821 | return ExprError(); |
| 4822 | |
| 4823 | // Widen the argument value to sizeof(parameter type). This is almost |
| 4824 | // always a no-op, except when the parameter type is bool. In |
| 4825 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 4826 | QualType IntegerType = ParamType; |
| 4827 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 4828 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 4829 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 4830 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4831 | Converted = TemplateArgument(Context, Value, |
| 4832 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4833 | return ArgResult; |
| 4834 | } |
| 4835 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4836 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 4837 | if (ArgResult.isInvalid()) |
| 4838 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4839 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4840 | |
| 4841 | QualType ArgType = Arg->getType(); |
| 4842 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4843 | // C++ [temp.arg.nontype]p1: |
| 4844 | // A template-argument for a non-type, non-template |
| 4845 | // template-parameter shall be one of: |
| 4846 | // |
| 4847 | // -- an integral constant-expression of integral or enumeration |
| 4848 | // type; or |
| 4849 | // -- the name of a non-type template-parameter; or |
| 4850 | SourceLocation NonConstantLoc; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4851 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4852 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4853 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4854 | diag::err_template_arg_not_integral_or_enumeral) |
| 4855 | << ArgType << Arg->getSourceRange(); |
| 4856 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4857 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4858 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 4859 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 4860 | QualType T; |
| 4861 | |
| 4862 | public: |
| 4863 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 4864 | |
| 4865 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 4866 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 4867 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 4868 | } |
| 4869 | } Diagnoser(ArgType); |
| 4870 | |
| 4871 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4872 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4873 | if (!Arg) |
| 4874 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4875 | } |
| 4876 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4877 | // From here on out, all we care about are the unqualified forms |
| 4878 | // of the parameter and argument types. |
| 4879 | ParamType = ParamType.getUnqualifiedType(); |
| 4880 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4881 | |
| 4882 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 4883 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4884 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4885 | } else if (ParamType->isBooleanType()) { |
| 4886 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4887 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4888 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 4889 | !ParamType->isEnumeralType()) { |
| 4890 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4891 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4892 | } else { |
| 4893 | // We can't perform this conversion. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4894 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4895 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4896 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4897 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4898 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4899 | } |
| 4900 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4901 | // Add the value of this argument to the list of converted |
| 4902 | // arguments. We use the bitwidth and signedness of the template |
| 4903 | // parameter. |
| 4904 | if (Arg->isValueDependent()) { |
| 4905 | // The argument is value-dependent. Create a new |
| 4906 | // TemplateArgument with the converted expression. |
| 4907 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4908 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4909 | } |
| 4910 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4911 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4912 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 4913 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4914 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4915 | if (ParamType->isBooleanType()) { |
| 4916 | // Value must be zero or one. |
| 4917 | Value = Value != 0; |
| 4918 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 4919 | if (Value.getBitWidth() != AllowedBits) |
| 4920 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4921 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4922 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4923 | llvm::APSInt OldValue = Value; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4924 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4925 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4926 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 4927 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 4928 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4929 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4930 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4931 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4932 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4933 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4934 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4935 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4936 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 4937 | << Arg->getSourceRange(); |
| 4938 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4939 | } |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4940 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4941 | // Complain if we overflowed the template parameter's type. |
| 4942 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4943 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4944 | RequiredBits = OldValue.getActiveBits(); |
| 4945 | else if (OldValue.isUnsigned()) |
| 4946 | RequiredBits = OldValue.getActiveBits() + 1; |
| 4947 | else |
| 4948 | RequiredBits = OldValue.getMinSignedBits(); |
| 4949 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4950 | Diag(Arg->getLocStart(), |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4951 | diag::warn_template_arg_too_large) |
| 4952 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 4953 | << Arg->getSourceRange(); |
| 4954 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4955 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4956 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4957 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4958 | Converted = TemplateArgument(Context, Value, |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 4959 | ParamType->isEnumeralType() |
| 4960 | ? Context.getCanonicalType(ParamType) |
| 4961 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4962 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4963 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4964 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4965 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4966 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 4967 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4968 | // Handle pointer-to-function, reference-to-function, and |
| 4969 | // pointer-to-member-function all in (roughly) the same way. |
| 4970 | if (// -- For a non-type template-parameter of type pointer to |
| 4971 | // function, only the function-to-pointer conversion (4.3) is |
| 4972 | // applied. If the template-argument represents a set of |
| 4973 | // overloaded functions (or a pointer to such), the matching |
| 4974 | // function is selected from the set (13.4). |
| 4975 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4976 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4977 | // -- For a non-type template-parameter of type reference to |
| 4978 | // function, no conversions apply. If the template-argument |
| 4979 | // represents a set of overloaded functions, the matching |
| 4980 | // function is selected from the set (13.4). |
| 4981 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4982 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4983 | // -- For a non-type template-parameter of type pointer to |
| 4984 | // member function, no conversions apply. If the |
| 4985 | // template-argument represents a set of overloaded member |
| 4986 | // functions, the matching member function is selected from |
| 4987 | // the set (13.4). |
| 4988 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4989 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4990 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4991 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4992 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4993 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4994 | true, |
| 4995 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4996 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4997 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4998 | |
| 4999 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5000 | ArgType = Arg->getType(); |
| 5001 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5002 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5003 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5004 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5005 | if (!ParamType->isMemberPointerType()) { |
| 5006 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5007 | ParamType, |
| 5008 | Arg, Converted)) |
| 5009 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5010 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5011 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5012 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5013 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5014 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5015 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5016 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 5019 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5020 | // -- for a non-type template-parameter of type pointer to |
| 5021 | // object, qualification conversions (4.4) and the |
| 5022 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5023 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5024 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5025 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5026 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5027 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5028 | ParamType, |
| 5029 | Arg, Converted)) |
| 5030 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5031 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5032 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5033 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5034 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5035 | // -- For a non-type template-parameter of type reference to |
| 5036 | // object, no conversions apply. The type referred to by the |
| 5037 | // reference may be more cv-qualified than the (otherwise |
| 5038 | // identical) type of the template-argument. The |
| 5039 | // template-parameter is bound directly to the |
| 5040 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5041 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5042 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5043 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5044 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5045 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 5046 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5047 | true, |
| 5048 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5049 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5050 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5051 | |
| 5052 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5053 | ArgType = Arg->getType(); |
| 5054 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5055 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5056 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5057 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5058 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5059 | ParamType, |
| 5060 | Arg, Converted)) |
| 5061 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5062 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5063 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5064 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5065 | // Deal with parameters of type std::nullptr_t. |
| 5066 | if (ParamType->isNullPtrType()) { |
| 5067 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 5068 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5069 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5070 | } |
| 5071 | |
| 5072 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 5073 | case NPV_NotNullPointer: |
| 5074 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 5075 | << Arg->getType() << ParamType; |
| 5076 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5077 | return ExprError(); |
| 5078 | |
| 5079 | case NPV_Error: |
| 5080 | return ExprError(); |
| 5081 | |
| 5082 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 5083 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5084 | Converted = TemplateArgument(ParamType, /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5085 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5086 | } |
| 5087 | } |
| 5088 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5089 | // -- For a non-type template-parameter of type pointer to data |
| 5090 | // member, qualification conversions (4.4) are applied. |
| 5091 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 5092 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5093 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5094 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5095 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5096 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5097 | } |
| 5098 | |
| 5099 | /// \brief Check a template argument against its corresponding |
| 5100 | /// template template parameter. |
| 5101 | /// |
| 5102 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 5103 | /// It returns true if an error occurred, and false otherwise. |
| 5104 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 5105 | TemplateArgumentLoc &Arg, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5106 | unsigned ArgumentPackIndex) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5107 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5108 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 5109 | if (!Template) { |
| 5110 | // Any dependent template name is fine. |
| 5111 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 5112 | return false; |
| 5113 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5114 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5115 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5116 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5117 | // the name of a class template or an alias template, expressed as an |
| 5118 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5119 | // primary class templates are considered when matching the |
| 5120 | // template template argument with the corresponding parameter; |
| 5121 | // partial specializations are not considered even if their |
| 5122 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5123 | // |
| 5124 | // Note that we also allow template template parameters here, which |
| 5125 | // will happen when we are dealing with, e.g., class template |
| 5126 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5127 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5128 | !isa<TemplateTemplateParmDecl>(Template) && |
| 5129 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5130 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5131 | "Only function templates are possible here"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5132 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5133 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5134 | << Template; |
| 5135 | } |
| 5136 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5137 | TemplateParameterList *Params = Param->getTemplateParameters(); |
| 5138 | if (Param->isExpandedParameterPack()) |
| 5139 | Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); |
| 5140 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5141 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5142 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5143 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5144 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5145 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5146 | } |
| 5147 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5148 | /// \brief Given a non-type template argument that refers to a |
| 5149 | /// declaration and the type of its corresponding non-type template |
| 5150 | /// parameter, produce an expression that properly refers to that |
| 5151 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5152 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5153 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 5154 | QualType ParamType, |
| 5155 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 5156 | // C++ [temp.param]p8: |
| 5157 | // |
| 5158 | // A non-type template-parameter of type "array of T" or |
| 5159 | // "function returning T" is adjusted to be of type "pointer to |
| 5160 | // T" or "pointer to function returning T", respectively. |
| 5161 | if (ParamType->isArrayType()) |
| 5162 | ParamType = Context.getArrayDecayedType(ParamType); |
| 5163 | else if (ParamType->isFunctionType()) |
| 5164 | ParamType = Context.getPointerType(ParamType); |
| 5165 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5166 | // For a NULL non-type template argument, return nullptr casted to the |
| 5167 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5168 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5169 | return ImpCastExprToType( |
| 5170 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 5171 | ParamType, |
| 5172 | ParamType->getAs<MemberPointerType>() |
| 5173 | ? CK_NullToMemberPointer |
| 5174 | : CK_NullToPointer); |
| 5175 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5176 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 5177 | "Only declaration template arguments permitted here"); |
| 5178 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5179 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 5180 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5181 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 5182 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 5183 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5184 | // If the value is a class member, we might have a pointer-to-member. |
| 5185 | // Determine whether the non-type template template parameter is of |
| 5186 | // pointer-to-member type. If so, we need to build an appropriate |
| 5187 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 5188 | // would refer to the member itself. |
| 5189 | if (ParamType->isMemberPointerType()) { |
| 5190 | QualType ClassType |
| 5191 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 5192 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5193 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5194 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5195 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 5196 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5197 | |
| 5198 | // The actual value-ness of this is unimportant, but for |
| 5199 | // internal consistency's sake, references to instance methods |
| 5200 | // are r-values. |
| 5201 | ExprValueKind VK = VK_LValue; |
| 5202 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 5203 | VK = VK_RValue; |
| 5204 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5205 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5206 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5207 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5208 | Loc, |
| 5209 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5210 | if (RefExpr.isInvalid()) |
| 5211 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5212 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5213 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5214 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5215 | // We might need to perform a trailing qualification conversion, since |
| 5216 | // the element type on the parameter could be more qualified than the |
| 5217 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5218 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5219 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5220 | ParamType.getUnqualifiedType(), false, |
| 5221 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5222 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5223 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5224 | assert(!RefExpr.isInvalid() && |
| 5225 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5226 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5227 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5228 | } |
| 5229 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5230 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5231 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5232 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5233 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5234 | // When the non-type template parameter is a pointer, take the |
| 5235 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5236 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5237 | if (RefExpr.isInvalid()) |
| 5238 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5239 | |
| 5240 | if (T->isFunctionType() || T->isArrayType()) { |
| 5241 | // Decay functions and arrays. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5242 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5243 | if (RefExpr.isInvalid()) |
| 5244 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5245 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5246 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5247 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5248 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5249 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5250 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5251 | } |
| 5252 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5253 | ExprValueKind VK = VK_RValue; |
| 5254 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5255 | // If the non-type template parameter has reference type, qualify the |
| 5256 | // resulting declaration reference with the extra qualifiers on the |
| 5257 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5258 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 5259 | VK = VK_LValue; |
| 5260 | T = Context.getQualifiedType(T, |
| 5261 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5262 | } else if (isa<FunctionDecl>(VD)) { |
| 5263 | // References to functions are always lvalues. |
| 5264 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5265 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5266 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5267 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5268 | } |
| 5269 | |
| 5270 | /// \brief Construct a new expression that refers to the given |
| 5271 | /// integral template argument with the given source-location |
| 5272 | /// information. |
| 5273 | /// |
| 5274 | /// This routine takes care of the mapping from an integral template |
| 5275 | /// argument (which may have any integral type) to the appropriate |
| 5276 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5277 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5278 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 5279 | SourceLocation Loc) { |
| 5280 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 5281 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5282 | QualType OrigT = Arg.getIntegralType(); |
| 5283 | |
| 5284 | // If this is an enum type that we're instantiating, we need to use an integer |
| 5285 | // type the same size as the enumerator. We don't want to build an |
| 5286 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 5287 | // any integral type with C++11 enum classes, make sure we create the right |
| 5288 | // type of literal for it. |
| 5289 | QualType T = OrigT; |
| 5290 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 5291 | T = ET->getDecl()->getIntegerType(); |
| 5292 | |
| 5293 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5294 | if (T->isAnyCharacterType()) { |
| 5295 | CharacterLiteral::CharacterKind Kind; |
| 5296 | if (T->isWideCharType()) |
| 5297 | Kind = CharacterLiteral::Wide; |
| 5298 | else if (T->isChar16Type()) |
| 5299 | Kind = CharacterLiteral::UTF16; |
| 5300 | else if (T->isChar32Type()) |
| 5301 | Kind = CharacterLiteral::UTF32; |
| 5302 | else |
| 5303 | Kind = CharacterLiteral::Ascii; |
| 5304 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5305 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 5306 | Kind, T, Loc); |
| 5307 | } else if (T->isBooleanType()) { |
| 5308 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 5309 | T, Loc); |
| 5310 | } else if (T->isNullPtrType()) { |
| 5311 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 5312 | } else { |
| 5313 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5314 | } |
| 5315 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5316 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5317 | // FIXME: This is a hack. We need a better way to handle substituted |
| 5318 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5319 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 5320 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5321 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5322 | Loc, Loc); |
| 5323 | } |
| 5324 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5325 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5326 | } |
| 5327 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5328 | /// \brief Match two template parameters within template parameter lists. |
| 5329 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 5330 | bool Complain, |
| 5331 | Sema::TemplateParameterListEqualKind Kind, |
| 5332 | SourceLocation TemplateArgLoc) { |
| 5333 | // Check the actual kind (type, non-type, template). |
| 5334 | if (Old->getKind() != New->getKind()) { |
| 5335 | if (Complain) { |
| 5336 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 5337 | if (TemplateArgLoc.isValid()) { |
| 5338 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5339 | NextDiag = diag::note_template_param_different_kind; |
| 5340 | } |
| 5341 | S.Diag(New->getLocation(), NextDiag) |
| 5342 | << (Kind != Sema::TPL_TemplateMatch); |
| 5343 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 5344 | << (Kind != Sema::TPL_TemplateMatch); |
| 5345 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5346 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5347 | return false; |
| 5348 | } |
| 5349 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5350 | // Check that both are parameter packs are neither are parameter packs. |
| 5351 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5352 | // template template parameter, the template template parameter can have |
| 5353 | // a parameter pack where the template template argument does not. |
| 5354 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 5355 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5356 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5357 | if (Complain) { |
| 5358 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 5359 | if (TemplateArgLoc.isValid()) { |
| 5360 | S.Diag(TemplateArgLoc, |
| 5361 | diag::err_template_arg_template_params_mismatch); |
| 5362 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 5363 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5364 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5365 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 5366 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 5367 | : 2; |
| 5368 | S.Diag(New->getLocation(), NextDiag) |
| 5369 | << ParamKind << New->isParameterPack(); |
| 5370 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 5371 | << ParamKind << Old->isParameterPack(); |
| 5372 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5373 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5374 | return false; |
| 5375 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5376 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5377 | // For non-type template parameters, check the type of the parameter. |
| 5378 | if (NonTypeTemplateParmDecl *OldNTTP |
| 5379 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 5380 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5381 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5382 | // If we are matching a template template argument to a template |
| 5383 | // template parameter and one of the non-type template parameter types |
| 5384 | // is dependent, then we must wait until template instantiation time |
| 5385 | // to actually compare the arguments. |
| 5386 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5387 | (OldNTTP->getType()->isDependentType() || |
| 5388 | NewNTTP->getType()->isDependentType())) |
| 5389 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5390 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5391 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 5392 | if (Complain) { |
| 5393 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 5394 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5395 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5396 | diag::err_template_arg_template_params_mismatch); |
| 5397 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 5398 | } |
| 5399 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 5400 | << NewNTTP->getType() |
| 5401 | << (Kind != Sema::TPL_TemplateMatch); |
| 5402 | S.Diag(OldNTTP->getLocation(), |
| 5403 | diag::note_template_nontype_parm_prev_declaration) |
| 5404 | << OldNTTP->getType(); |
| 5405 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5406 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5407 | return false; |
| 5408 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5409 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5410 | return true; |
| 5411 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5412 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5413 | // For template template parameters, check the template parameter types. |
| 5414 | // The template parameter lists of template template |
| 5415 | // parameters must agree. |
| 5416 | if (TemplateTemplateParmDecl *OldTTP |
| 5417 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5418 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5419 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 5420 | OldTTP->getTemplateParameters(), |
| 5421 | Complain, |
| 5422 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5423 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5424 | : Kind), |
| 5425 | TemplateArgLoc); |
| 5426 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5427 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5428 | return true; |
| 5429 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5430 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5431 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 5432 | /// lists. |
| 5433 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5434 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5435 | TemplateParameterList *New, |
| 5436 | TemplateParameterList *Old, |
| 5437 | Sema::TemplateParameterListEqualKind Kind, |
| 5438 | SourceLocation TemplateArgLoc) { |
| 5439 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 5440 | if (TemplateArgLoc.isValid()) { |
| 5441 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5442 | NextDiag = diag::note_template_param_list_different_arity; |
| 5443 | } |
| 5444 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 5445 | << (New->size() > Old->size()) |
| 5446 | << (Kind != Sema::TPL_TemplateMatch) |
| 5447 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 5448 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 5449 | << (Kind != Sema::TPL_TemplateMatch) |
| 5450 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 5451 | } |
| 5452 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5453 | /// \brief Determine whether the given template parameter lists are |
| 5454 | /// equivalent. |
| 5455 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5456 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5457 | /// source code as part of a new template declaration. |
| 5458 | /// |
| 5459 | /// \param Old The old template parameter list, typically found via |
| 5460 | /// name lookup of the template declared with this template parameter |
| 5461 | /// list. |
| 5462 | /// |
| 5463 | /// \param Complain If true, this routine will produce a diagnostic if |
| 5464 | /// the template parameter lists are not equivalent. |
| 5465 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5466 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5467 | /// |
| 5468 | /// \param TemplateArgLoc If this source location is valid, then we |
| 5469 | /// are actually checking the template parameter list of a template |
| 5470 | /// argument (New) against the template parameter list of its |
| 5471 | /// corresponding template template parameter (Old). We produce |
| 5472 | /// slightly different diagnostics in this scenario. |
| 5473 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5474 | /// \returns True if the template parameter lists are equal, false |
| 5475 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5476 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5477 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 5478 | TemplateParameterList *Old, |
| 5479 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5480 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5481 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5482 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 5483 | if (Complain) |
| 5484 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5485 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5486 | |
| 5487 | return false; |
| 5488 | } |
| 5489 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5490 | // C++0x [temp.arg.template]p3: |
| 5491 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5492 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5493 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5494 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5495 | // template-parameter-list of P. [...] |
| 5496 | TemplateParameterList::iterator NewParm = New->begin(); |
| 5497 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5498 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5499 | OldParmEnd = Old->end(); |
| 5500 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 5501 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 5502 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5503 | if (NewParm == NewParmEnd) { |
| 5504 | if (Complain) |
| 5505 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5506 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5507 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5508 | return false; |
| 5509 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5510 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5511 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5512 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5513 | return false; |
| 5514 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5515 | ++NewParm; |
| 5516 | continue; |
| 5517 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5518 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5519 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5520 | // [...] When P's template- parameter-list contains a template parameter |
| 5521 | // pack (14.5.3), the template parameter pack will match zero or more |
| 5522 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5523 | // template-parameter-list of A with the same type and form as the |
| 5524 | // template parameter pack in P (ignoring whether those template |
| 5525 | // parameters are template parameter packs). |
| 5526 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 5527 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5528 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5529 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5530 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5531 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5532 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5533 | // Make sure we exhausted all of the arguments. |
| 5534 | if (NewParm != NewParmEnd) { |
| 5535 | if (Complain) |
| 5536 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5537 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5538 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5539 | return false; |
| 5540 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5541 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5542 | return true; |
| 5543 | } |
| 5544 | |
| 5545 | /// \brief Check whether a template can be declared within this scope. |
| 5546 | /// |
| 5547 | /// If the template declaration is valid in this scope, returns |
| 5548 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5549 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5550 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 5551 | if (!S) |
| 5552 | return false; |
| 5553 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5554 | // Find the nearest enclosing declaration scope. |
| 5555 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5556 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5557 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5558 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5559 | // C++ [temp]p4: |
| 5560 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 5561 | DeclContext *Ctx = S->getEntity(); |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5562 | if (Ctx && Ctx->isExternCContext()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5563 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5564 | << TemplateParams->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5565 | |
Eli Friedman | dfbd0c4 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 5566 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5567 | Ctx = Ctx->getParent(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5568 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5569 | // C++ [temp]p2: |
| 5570 | // A template-declaration can appear only as a namespace scope or |
| 5571 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 5572 | if (Ctx) { |
| 5573 | if (Ctx->isFileContext()) |
| 5574 | return false; |
| 5575 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 5576 | // C++ [temp.mem]p2: |
| 5577 | // A local class shall not have member templates. |
| 5578 | if (RD->isLocalClass()) |
| 5579 | return Diag(TemplateParams->getTemplateLoc(), |
| 5580 | diag::err_template_inside_local_class) |
| 5581 | << TemplateParams->getSourceRange(); |
| 5582 | else |
| 5583 | return false; |
| 5584 | } |
| 5585 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5586 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5587 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5588 | diag::err_template_outside_namespace_or_class_scope) |
| 5589 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5590 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5591 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5592 | /// \brief Determine what kind of template specialization the given declaration |
| 5593 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5594 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5595 | if (!D) |
| 5596 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5597 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5598 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 5599 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5600 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 5601 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5602 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 5603 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5604 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5605 | return TSK_Undeclared; |
| 5606 | } |
| 5607 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5608 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5609 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5610 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5611 | /// This routine determines whether a template specialization can be declared |
| 5612 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5613 | /// |
| 5614 | /// \param S the semantic analysis object for which this check is being |
| 5615 | /// performed. |
| 5616 | /// |
| 5617 | /// \param Specialized the entity being specialized or instantiated, which |
| 5618 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5619 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5620 | /// member class). |
| 5621 | /// |
| 5622 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 5623 | /// |
| 5624 | /// \param Loc the location of the explicit specialization or instantiation of |
| 5625 | /// this entity. |
| 5626 | /// |
| 5627 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 5628 | /// a class template. |
| 5629 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5630 | /// \returns true if there was an error that we cannot recover from, false |
| 5631 | /// otherwise. |
| 5632 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 5633 | NamedDecl *Specialized, |
| 5634 | NamedDecl *PrevDecl, |
| 5635 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5636 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5637 | // Keep these "kind" numbers in sync with the %select statements in the |
| 5638 | // various diagnostics emitted by this routine. |
| 5639 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5640 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5641 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5642 | else if (isa<VarTemplateDecl>(Specialized)) |
| 5643 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5644 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5645 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5646 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5647 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5648 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5649 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5650 | else if (isa<RecordDecl>(Specialized)) |
| 5651 | EntityKind = 7; |
| 5652 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 5653 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5654 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5655 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5656 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5657 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5658 | return true; |
| 5659 | } |
| 5660 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5661 | // C++ [temp.expl.spec]p2: |
| 5662 | // An explicit specialization shall be declared in the namespace |
| 5663 | // of which the template is a member, or, for member templates, in |
| 5664 | // the namespace of which the enclosing class or enclosing class |
| 5665 | // template is a member. An explicit specialization of a member |
| 5666 | // function, member class or static data member of a class |
| 5667 | // template shall be declared in the namespace of which the class |
| 5668 | // template is a member. Such a declaration may also be a |
| 5669 | // definition. If the declaration is not a definition, the |
| 5670 | // specialization may be defined later in the name- space in which |
| 5671 | // the explicit specialization was declared, or in a namespace |
| 5672 | // that encloses the one in which the explicit specialization was |
| 5673 | // declared. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5674 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5675 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5676 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5677 | return true; |
| 5678 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5679 | |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5680 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5681 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 5682 | // Do not warn for class scope explicit specialization during |
| 5683 | // instantiation, warning was already emitted during pattern |
| 5684 | // semantic analysis. |
| 5685 | if (!S.ActiveTemplateInstantiations.size()) |
| 5686 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 5687 | << Specialized; |
| 5688 | } else { |
| 5689 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5690 | << Specialized; |
| 5691 | return true; |
| 5692 | } |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5693 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5694 | |
Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 5695 | if (S.CurContext->isRecord() && |
| 5696 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 5697 | // Make sure that we're specializing in the right record context. |
| 5698 | // Otherwise, things can go horribly wrong. |
| 5699 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5700 | << Specialized; |
| 5701 | return true; |
| 5702 | } |
| 5703 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5704 | // C++ [temp.class.spec]p6: |
| 5705 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5706 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 5707 | // and 14.5.2). |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5708 | DeclContext *SpecializedContext |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5709 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5710 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5711 | |
| 5712 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
| 5713 | // namespace. |
| 5714 | // Note that HandleDeclarator() performs this check for explicit |
| 5715 | // specializations of function templates, static data members, and member |
| 5716 | // functions, so we skip the check here for those kinds of entities. |
| 5717 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
| 5718 | // Should we refactor that check, so that it occurs later? |
| 5719 | if (!DC->Encloses(SpecializedContext) && |
| 5720 | !(isa<FunctionTemplateDecl>(Specialized) || |
| 5721 | isa<FunctionDecl>(Specialized) || |
| 5722 | isa<VarTemplateDecl>(Specialized) || |
| 5723 | isa<VarDecl>(Specialized))) { |
| 5724 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 5725 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 5726 | << EntityKind << Specialized; |
| 5727 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 5728 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 5729 | << EntityKind << Specialized |
| 5730 | << cast<NamedDecl>(SpecializedContext); |
| 5731 | else |
| 5732 | llvm_unreachable("unexpected namespace context for specialization"); |
| 5733 | |
| 5734 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 5735 | } else if ((!PrevDecl || |
| 5736 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 5737 | getTemplateSpecializationKind(PrevDecl) == |
| 5738 | TSK_ImplicitInstantiation)) { |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5739 | // C++ [temp.exp.spec]p2: |
| 5740 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5741 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5742 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5743 | // An explicit specialization of a member function, member class or |
| 5744 | // static data member of a class template shall be declared in the |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5745 | // namespace of which the class template is a member. |
| 5746 | // |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5747 | // C++11 [temp.expl.spec]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5748 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5749 | // the specialized template. |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5750 | // C++11 [temp.explicit]p3: |
| 5751 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 5752 | // template. |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5753 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5754 | bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext); |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5755 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5756 | assert(!IsCPlusPlus11Extension && |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5757 | "DC encloses TU but isn't in enclosing namespace set"); |
| 5758 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | 8ce6315 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 5759 | << EntityKind << Specialized; |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5760 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 5761 | int Diag; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5762 | if (!IsCPlusPlus11Extension) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5763 | Diag = diag::err_template_spec_decl_out_of_scope; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5764 | else if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5765 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 5766 | else |
| 5767 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 5768 | S.Diag(Loc, Diag) |
| 5769 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 5770 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5771 | |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5772 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5773 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5774 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5775 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5776 | return false; |
| 5777 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5778 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5779 | static SourceRange findTemplateParameter(unsigned Depth, Expr *E) { |
| 5780 | if (!E->isInstantiationDependent()) |
| 5781 | return SourceLocation(); |
| 5782 | DependencyChecker Checker(Depth); |
| 5783 | Checker.TraverseStmt(E); |
| 5784 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 5785 | return E->getSourceRange(); |
| 5786 | return Checker.MatchLoc; |
| 5787 | } |
| 5788 | |
| 5789 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 5790 | if (!TL.getType()->isDependentType()) |
| 5791 | return SourceLocation(); |
| 5792 | DependencyChecker Checker(Depth); |
| 5793 | Checker.TraverseTypeLoc(TL); |
| 5794 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 5795 | return TL.getSourceRange(); |
| 5796 | return Checker.MatchLoc; |
| 5797 | } |
| 5798 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5799 | /// \brief Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5800 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5801 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5802 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 5803 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5804 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 5805 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5806 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5807 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 5808 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5809 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5810 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5811 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5812 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5813 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5814 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5815 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5816 | |
| 5817 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5818 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 5819 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5820 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 5821 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 5822 | |
| 5823 | // Strip off any implicit casts we added as part of type checking. |
| 5824 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 5825 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5826 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5827 | // C++ [temp.class.spec]p8: |
| 5828 | // A non-type argument is non-specialized if it is the name of a |
| 5829 | // non-type parameter. All other non-type arguments are |
| 5830 | // specialized. |
| 5831 | // |
| 5832 | // Below, we check the two conditions that only apply to |
| 5833 | // specialized non-type arguments, so skip any non-specialized |
| 5834 | // arguments. |
| 5835 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 5836 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5837 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5838 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5839 | // C++ [temp.class.spec]p9: |
| 5840 | // Within the argument list of a class template partial |
| 5841 | // specialization, the following restrictions apply: |
| 5842 | // -- A partially specialized non-type argument expression |
| 5843 | // shall not involve a template parameter of the partial |
| 5844 | // specialization except when the argument expression is a |
| 5845 | // simple identifier. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5846 | SourceRange ParamUseRange = |
| 5847 | findTemplateParameter(Param->getDepth(), ArgExpr); |
| 5848 | if (ParamUseRange.isValid()) { |
| 5849 | if (IsDefaultArgument) { |
| 5850 | S.Diag(TemplateNameLoc, |
| 5851 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 5852 | S.Diag(ParamUseRange.getBegin(), |
| 5853 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 5854 | << ParamUseRange; |
| 5855 | } else { |
| 5856 | S.Diag(ParamUseRange.getBegin(), |
| 5857 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 5858 | << ParamUseRange; |
| 5859 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5860 | return true; |
| 5861 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5862 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5863 | // -- The type of a template parameter corresponding to a |
| 5864 | // specialized non-type argument shall not be dependent on a |
| 5865 | // parameter of the specialization. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5866 | // |
| 5867 | // FIXME: We need to delay this check until instantiation in some cases: |
| 5868 | // |
| 5869 | // template<template<typename> class X> struct A { |
| 5870 | // template<typename T, X<T> N> struct B; |
| 5871 | // template<typename T> struct B<T, 0>; |
| 5872 | // }; |
| 5873 | // template<typename> using X = int; |
| 5874 | // A<X>::B<int, 0> b; |
| 5875 | ParamUseRange = findTemplateParameter( |
| 5876 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
| 5877 | if (ParamUseRange.isValid()) { |
| 5878 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getLocStart(), |
| 5879 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 5880 | << Param->getType() << ParamUseRange; |
| 5881 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
| 5882 | << (IsDefaultArgument ? ParamUseRange : SourceRange()); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5883 | return true; |
| 5884 | } |
| 5885 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5886 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5887 | return false; |
| 5888 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5889 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5890 | /// \brief Check the non-type template arguments of a class template |
| 5891 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 5892 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5893 | /// \param TemplateNameLoc the location of the template name. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5894 | /// \param TemplateParams the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5895 | /// template. |
| 5896 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5897 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5898 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5899 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5900 | /// \returns \c true if there was an error, \c false otherwise. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5901 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5902 | Sema &S, SourceLocation TemplateNameLoc, |
| 5903 | TemplateParameterList *TemplateParams, unsigned NumExplicit, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5904 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5905 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 5906 | |
| 5907 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 5908 | NonTypeTemplateParmDecl *Param |
| 5909 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 5910 | if (!Param) |
| 5911 | continue; |
| 5912 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5913 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
| 5914 | S, TemplateNameLoc, Param, &ArgList[I], 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5915 | return true; |
| 5916 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5917 | |
| 5918 | return false; |
| 5919 | } |
| 5920 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5921 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5922 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 5923 | TagUseKind TUK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5924 | SourceLocation KWLoc, |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5925 | SourceLocation ModulePrivateLoc, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 5926 | TemplateIdAnnotation &TemplateId, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5927 | AttributeList *Attr, |
| 5928 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5929 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 5930 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 5931 | CXXScopeSpec &SS = TemplateId.SS; |
| 5932 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5933 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 5934 | // store the location of the outermost template keyword in the declaration. |
| 5935 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 5936 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 5937 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 5938 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 5939 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5940 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5941 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 5942 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5943 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 5944 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5945 | |
| 5946 | if (!ClassTemplate) { |
| 5947 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5948 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 5949 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 5950 | return true; |
| 5951 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5952 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5953 | bool isExplicitSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5954 | bool isPartialSpecialization = false; |
| 5955 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5956 | // Check the validity of the template headers that introduce this |
| 5957 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5958 | // FIXME: We probably shouldn't complain about these headers for |
| 5959 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 5960 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 5961 | TemplateParameterList *TemplateParams = |
| 5962 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 5963 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
| 5964 | TemplateParameterLists, TUK == TUK_Friend, isExplicitSpecialization, |
| 5965 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 5966 | if (Invalid) |
| 5967 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5968 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5969 | if (TemplateParams && TemplateParams->size() > 0) { |
| 5970 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5971 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 5972 | if (TUK == TUK_Friend) { |
| 5973 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 5974 | << SourceRange(LAngleLoc, RAngleLoc); |
| 5975 | return true; |
| 5976 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5977 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5978 | // C++ [temp.class.spec]p10: |
| 5979 | // The template parameter list of a specialization shall not |
| 5980 | // contain default template argument values. |
| 5981 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 5982 | Decl *Param = TemplateParams->getParam(I); |
| 5983 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 5984 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5985 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5986 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5987 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5988 | } |
| 5989 | } else if (NonTypeTemplateParmDecl *NTTP |
| 5990 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 5991 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5992 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5993 | diag::err_default_arg_in_partial_spec) |
| 5994 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 5995 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5996 | } |
| 5997 | } else { |
| 5998 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5999 | if (TTP->hasDefaultArgument()) { |
| 6000 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 6001 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6002 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6003 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6004 | } |
| 6005 | } |
| 6006 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6007 | } else if (TemplateParams) { |
| 6008 | if (TUK == TUK_Friend) |
| 6009 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6010 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6011 | SourceRange(TemplateParams->getTemplateLoc(), |
| 6012 | TemplateParams->getRAngleLoc())) |
| 6013 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6014 | else |
| 6015 | isExplicitSpecialization = true; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6016 | } else { |
| 6017 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6018 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6019 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6020 | // Check that the specialization uses the same tag kind as the |
| 6021 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6022 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6023 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6024 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6025 | Kind, TUK == TUK_Definition, KWLoc, |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6026 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6027 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6028 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6029 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6030 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6031 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6032 | diag::note_previous_use); |
| 6033 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6034 | } |
| 6035 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6036 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6037 | TemplateArgumentListInfo TemplateArgs = |
| 6038 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6039 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6040 | // Check for unexpanded parameter packs in any of the template arguments. |
| 6041 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6042 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6043 | UPPC_PartialSpecialization)) |
| 6044 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6045 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6046 | // Check that the template argument list is well-formed for this |
| 6047 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6048 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6049 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6050 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6051 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6052 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6053 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6054 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6055 | if (isPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6056 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6057 | *this, TemplateNameLoc, ClassTemplate->getTemplateParameters(), |
| 6058 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6059 | return true; |
| 6060 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6061 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6062 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6063 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6064 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6065 | TemplateArgs.size(), |
| 6066 | InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6067 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 6068 | << ClassTemplate->getDeclName(); |
| 6069 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6070 | } |
| 6071 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6072 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6073 | void *InsertPos = nullptr; |
| 6074 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6075 | |
| 6076 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6077 | // FIXME: Template parameter list matters, too |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6078 | PrevDecl |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6079 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 6080 | Converted.size(), |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6081 | InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6082 | else |
| 6083 | PrevDecl |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6084 | = ClassTemplate->findSpecialization(Converted.data(), |
| 6085 | Converted.size(), InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6086 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6087 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6088 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6089 | // Check whether we can declare a class template specialization in |
| 6090 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6091 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6092 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 6093 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6094 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6095 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6096 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6097 | // The canonical type |
| 6098 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 6099 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6100 | // Build the canonical type that describes the converted template |
| 6101 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6102 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6103 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6104 | Converted.data(), |
| 6105 | Converted.size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6106 | |
| 6107 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6108 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 6109 | // C++ [temp.class.spec]p9b3: |
| 6110 | // |
| 6111 | // -- The argument list of the specialization shall not be identical |
| 6112 | // to the implicit argument list of the primary template. |
| 6113 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6114 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6115 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6116 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 6117 | ClassTemplate->getIdentifier(), |
| 6118 | TemplateNameLoc, |
| 6119 | Attr, |
| 6120 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6121 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6122 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6123 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6124 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6125 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6126 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6127 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 6128 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6129 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6130 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6131 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6132 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 6133 | TemplateParams, |
| 6134 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6135 | Converted.data(), |
| 6136 | Converted.size(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6137 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6138 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 6139 | PrevPartial); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6140 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6141 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6142 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6143 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6144 | TemplateParameterLists.data()); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6145 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6146 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6147 | if (!PrevPartial) |
| 6148 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6149 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6150 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6151 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6152 | // template specialization, make a note of that. |
| 6153 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 6154 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6155 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6156 | // Check that all of the template parameters of the class template |
| 6157 | // partial specialization are deducible from the template |
| 6158 | // arguments. If not, this class template partial specialization |
| 6159 | // will never be used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6160 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6161 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6162 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 6163 | DeducibleParams); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6164 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6165 | if (!DeducibleParams.all()) { |
| 6166 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6167 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6168 | << /*class template*/0 << (NumNonDeducible > 1) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6169 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 6170 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 6171 | if (!DeducibleParams[I]) { |
| 6172 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 6173 | if (Param->getDeclName()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6174 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6175 | diag::note_partial_spec_unused_parameter) |
| 6176 | << Param->getDeclName(); |
| 6177 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6178 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6179 | diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 6180 | << "(anonymous)"; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6181 | } |
| 6182 | } |
| 6183 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6184 | } else { |
| 6185 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6186 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6187 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6188 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6189 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6190 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6191 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6192 | Converted.data(), |
| 6193 | Converted.size(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6194 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6195 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6196 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6197 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6198 | TemplateParameterLists.size(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6199 | TemplateParameterLists.data()); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6200 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6201 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6202 | if (!PrevDecl) |
| 6203 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6204 | |
| 6205 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6206 | } |
| 6207 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6208 | // C++ [temp.expl.spec]p6: |
| 6209 | // 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] | 6210 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6211 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6212 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6213 | // use occurs; no diagnostic is required. |
| 6214 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6215 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6216 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6217 | // Is there any previous explicit specialization declaration? |
| 6218 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6219 | Okay = true; |
| 6220 | break; |
| 6221 | } |
| 6222 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6223 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6224 | if (!Okay) { |
| 6225 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 6226 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 6227 | << Context.getTypeDeclType(Specialization) << Range; |
| 6228 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6229 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6230 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6231 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6232 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6233 | return true; |
| 6234 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6235 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6236 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6237 | // If this is not a friend, note that this is an explicit specialization. |
| 6238 | if (TUK != TUK_Friend) |
| 6239 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6240 | |
| 6241 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6242 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6243 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6244 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6245 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6246 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6247 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 6248 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6249 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6250 | } |
| 6251 | } |
| 6252 | |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 6253 | if (Attr) |
| 6254 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6255 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 6256 | // Add alignment attributes if necessary; these attributes are checked when |
| 6257 | // the ASTContext lays out the structure. |
| 6258 | if (TUK == TUK_Definition) { |
| 6259 | AddAlignmentAttributesForRecord(Specialization); |
| 6260 | AddMsStructLayoutForRecord(Specialization); |
| 6261 | } |
| 6262 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6263 | if (ModulePrivateLoc.isValid()) |
| 6264 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 6265 | << (isPartialSpecialization? 1 : 0) |
| 6266 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 6267 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 6268 | // Build the fully-sugared type for this class template |
| 6269 | // specialization as the user wrote in the specialization |
| 6270 | // itself. This means that we'll pretty-print the type retrieved |
| 6271 | // from the specialization's declaration the way that the user |
| 6272 | // actually wrote the specialization, rather than formatting the |
| 6273 | // name based on the "canonical" representation used to store the |
| 6274 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6275 | TypeSourceInfo *WrittenTy |
| 6276 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6277 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6278 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6279 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6280 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6281 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6282 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 6283 | // C++ [temp.expl.spec]p9: |
| 6284 | // A template explicit specialization is in the scope of the |
| 6285 | // namespace in which the template was defined. |
| 6286 | // |
| 6287 | // We actually implement this paragraph where we set the semantic |
| 6288 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 6289 | // but we also maintain the lexical context where the actual |
| 6290 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6291 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6292 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6293 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6294 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6295 | Specialization->startDefinition(); |
| 6296 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6297 | if (TUK == TUK_Friend) { |
| 6298 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 6299 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6300 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6301 | /*FIXME:*/KWLoc); |
| 6302 | Friend->setAccess(AS_public); |
| 6303 | CurContext->addDecl(Friend); |
| 6304 | } else { |
| 6305 | // Add the specialization into its lexical context, so that it can |
| 6306 | // be seen when iterating through the list of declarations in that |
| 6307 | // context. However, specializations are not found by name lookup. |
| 6308 | CurContext->addDecl(Specialization); |
| 6309 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6310 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6311 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6312 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6313 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6314 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6315 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6316 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 6317 | ActOnDocumentableDecl(NewDecl); |
| 6318 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6319 | } |
| 6320 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6321 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6322 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6323 | Declarator &D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6324 | assert(getCurFunctionDecl() == nullptr && "Function parsing confused"); |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6325 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6326 | |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6327 | if (FTI.hasPrototype) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6328 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6329 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6330 | |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6331 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6332 | |
Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 6333 | D.setFunctionDefinitionKind(FDK_Definition); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6334 | Decl *DP = HandleDeclarator(ParentScope, D, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6335 | TemplateParameterLists); |
Argyrios Kyrtzidis | 6fada2d | 2012-12-14 06:53:58 +0000 | [diff] [blame] | 6336 | return ActOnStartOfFunctionDef(FnBodyScope, DP); |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6337 | } |
| 6338 | |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6339 | /// \brief Strips various properties off an implicit instantiation |
| 6340 | /// that has just been explicitly specialized. |
| 6341 | static void StripImplicitInstantiation(NamedDecl *D) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 6342 | D->dropAttrs(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6343 | |
| 6344 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 6345 | FD->setInlineSpecified(false); |
Jordan Rose | a0a86be | 2013-03-08 22:25:36 +0000 | [diff] [blame] | 6346 | |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 6347 | for (auto I : FD->params()) |
| 6348 | I->dropAttrs(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6349 | } |
| 6350 | } |
| 6351 | |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6352 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 6353 | // declaration or definition. |
| 6354 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6355 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6356 | // Explicit instantiations following a specialization have no effect and |
| 6357 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 6358 | // until a valid name loc is found. |
| 6359 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6360 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 6361 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6362 | PrevDiagLoc = Prev->getLocation(); |
| 6363 | } |
| 6364 | assert(PrevDiagLoc.isValid() && |
| 6365 | "Explicit instantiation without point of instantiation?"); |
| 6366 | return PrevDiagLoc; |
| 6367 | } |
| 6368 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6369 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6370 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6371 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6372 | /// new specialization/instantiation will have any effect. |
| 6373 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6374 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6375 | /// instantiation. |
| 6376 | /// |
| 6377 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 6378 | /// |
| 6379 | /// \param PrevDecl the previous declaration of the entity. |
| 6380 | /// |
| 6381 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 6382 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6383 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6384 | /// declaration was instantiated (either implicitly or explicitly). |
| 6385 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6386 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6387 | /// specialization or instantiation has no effect and should be ignored. |
| 6388 | /// |
| 6389 | /// \returns true if there was an error that should prevent the introduction of |
| 6390 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6391 | bool |
| 6392 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 6393 | TemplateSpecializationKind NewTSK, |
| 6394 | NamedDecl *PrevDecl, |
| 6395 | TemplateSpecializationKind PrevTSK, |
| 6396 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6397 | bool &HasNoEffect) { |
| 6398 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6399 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6400 | switch (NewTSK) { |
| 6401 | case TSK_Undeclared: |
| 6402 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 6403 | assert( |
| 6404 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 6405 | "previous declaration must be implicit!"); |
| 6406 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6407 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6408 | case TSK_ExplicitSpecialization: |
| 6409 | switch (PrevTSK) { |
| 6410 | case TSK_Undeclared: |
| 6411 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6412 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6413 | // explicitly specialized or has merely been mentioned without any |
| 6414 | // instantiation. |
| 6415 | return false; |
| 6416 | |
| 6417 | case TSK_ImplicitInstantiation: |
| 6418 | if (PrevPointOfInstantiation.isInvalid()) { |
| 6419 | // The declaration itself has not actually been instantiated, so it is |
| 6420 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6421 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6422 | return false; |
| 6423 | } |
| 6424 | // Fall through |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6425 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6426 | case TSK_ExplicitInstantiationDeclaration: |
| 6427 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6428 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 6429 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6430 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6431 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6432 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6433 | // 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] | 6434 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6435 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6436 | // implicit instantiation to take place, in every translation unit in |
| 6437 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6438 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6439 | // Is there any previous explicit specialization declaration? |
| 6440 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 6441 | return false; |
| 6442 | } |
| 6443 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6444 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6445 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6446 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6447 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6448 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6449 | return true; |
| 6450 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6451 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6452 | case TSK_ExplicitInstantiationDeclaration: |
| 6453 | switch (PrevTSK) { |
| 6454 | case TSK_ExplicitInstantiationDeclaration: |
| 6455 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6456 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6457 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6458 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6459 | case TSK_Undeclared: |
| 6460 | case TSK_ImplicitInstantiation: |
| 6461 | // We're explicitly instantiating something that may have already been |
| 6462 | // implicitly instantiated; that's fine. |
| 6463 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6464 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6465 | case TSK_ExplicitSpecialization: |
| 6466 | // C++0x [temp.explicit]p4: |
| 6467 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6468 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6469 | // specialization for that template, the explicit instantiation has no |
| 6470 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6471 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6472 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6473 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6474 | case TSK_ExplicitInstantiationDefinition: |
| 6475 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6476 | // If an entity is the subject of both an explicit instantiation |
| 6477 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6478 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6479 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6480 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6481 | |
| 6482 | // Explicit instantiations following a specialization have no effect and |
| 6483 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 6484 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6485 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 6486 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6487 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6488 | return false; |
| 6489 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6490 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6491 | case TSK_ExplicitInstantiationDefinition: |
| 6492 | switch (PrevTSK) { |
| 6493 | case TSK_Undeclared: |
| 6494 | case TSK_ImplicitInstantiation: |
| 6495 | // We're explicitly instantiating something that may have already been |
| 6496 | // implicitly instantiated; that's fine. |
| 6497 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6498 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6499 | case TSK_ExplicitSpecialization: |
| 6500 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 6501 | // For a given set of template parameters, if an explicit |
| 6502 | // instantiation of a template appears after a declaration of |
| 6503 | // an explicit specialization for that template, the explicit |
| 6504 | // instantiation has no effect. |
| 6505 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6506 | // 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] | 6507 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6508 | // has been explicitly specialized. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6509 | Diag(NewLoc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6510 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 6511 | diag::ext_explicit_instantiation_after_specialization) |
| 6512 | << PrevDecl; |
| 6513 | Diag(PrevDecl->getLocation(), |
| 6514 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6515 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6516 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6517 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6518 | case TSK_ExplicitInstantiationDeclaration: |
| 6519 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6520 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6521 | |
| 6522 | // C++0x [temp.explicit]p4: |
| 6523 | // For a given set of template parameters, if an explicit instantiation |
| 6524 | // of a template appears after a declaration of an explicit |
| 6525 | // specialization for that template, the explicit instantiation has no |
| 6526 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6527 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6528 | // Is there any previous explicit specialization declaration? |
| 6529 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6530 | HasNoEffect = true; |
| 6531 | break; |
| 6532 | } |
| 6533 | } |
| 6534 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6535 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6536 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6537 | case TSK_ExplicitInstantiationDefinition: |
| 6538 | // C++0x [temp.spec]p5: |
| 6539 | // For a given template and a given set of template-arguments, |
| 6540 | // - an explicit instantiation definition shall appear at most once |
| 6541 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6542 | |
| 6543 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 6544 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
| 6545 | ? diag::warn_explicit_instantiation_duplicate |
| 6546 | : diag::err_explicit_instantiation_duplicate) |
| 6547 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6548 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6549 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6550 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6551 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6552 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6553 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6554 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6555 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6556 | } |
| 6557 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6558 | /// \brief Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6559 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6560 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6561 | /// The only possible way to get a dependent function template specialization |
| 6562 | /// is with a friend declaration, like so: |
| 6563 | /// |
| 6564 | /// \code |
| 6565 | /// template \<class T> void foo(T); |
| 6566 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6567 | /// friend void foo<>(T); |
| 6568 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6569 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6570 | /// |
| 6571 | /// There really isn't any useful analysis we can do here, so we |
| 6572 | /// just store the information. |
| 6573 | bool |
| 6574 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 6575 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 6576 | LookupResult &Previous) { |
| 6577 | // Remove anything from Previous that isn't a function template in |
| 6578 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6579 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6580 | LookupResult::Filter F = Previous.makeFilter(); |
| 6581 | while (F.hasNext()) { |
| 6582 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 6583 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6584 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 6585 | D->getDeclContext()->getRedeclContext())) |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6586 | F.erase(); |
| 6587 | } |
| 6588 | F.done(); |
| 6589 | |
| 6590 | // Should this be diagnosed here? |
| 6591 | if (Previous.empty()) return true; |
| 6592 | |
| 6593 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 6594 | ExplicitTemplateArgs); |
| 6595 | return false; |
| 6596 | } |
| 6597 | |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6598 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6599 | /// specialization. |
| 6600 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6601 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6602 | /// explicit function template specialization. On successful completion, |
| 6603 | /// the function declaration \p FD will become a function template |
| 6604 | /// specialization. |
| 6605 | /// |
| 6606 | /// \param FD the function declaration, which will be updated to become a |
| 6607 | /// function template specialization. |
| 6608 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6609 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 6610 | /// if any. Note that this may be valid info even when 0 arguments are |
| 6611 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 6612 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6613 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6614 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6615 | /// this function specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6616 | bool Sema::CheckFunctionTemplateSpecialization( |
| 6617 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 6618 | LookupResult &Previous) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6619 | // The set of function template specializations that could match this |
| 6620 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6621 | UnresolvedSet<8> Candidates; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6622 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6623 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6624 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6625 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6626 | I != E; ++I) { |
| 6627 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 6628 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6629 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6630 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6631 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 6632 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6633 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6634 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6635 | // When matching a constexpr member function template specialization |
| 6636 | // against the primary template, we don't yet know whether the |
| 6637 | // specialization has an implicit 'const' (because we don't know whether |
| 6638 | // it will be a static member function until we know which template it |
| 6639 | // specializes), so adjust it now assuming it specializes this template. |
| 6640 | QualType FT = FD->getType(); |
| 6641 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6642 | CXXMethodDecl *OldMD = |
| 6643 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6644 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6645 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6646 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6647 | EPI.TypeQuals |= Qualifiers::Const; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6648 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6649 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6650 | } |
| 6651 | } |
| 6652 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6653 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6654 | // A trailing template-argument can be left unspecified in the |
| 6655 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6656 | // provided it can be deduced from the function argument type. |
| 6657 | // Perform template argument deduction to determine whether we may be |
| 6658 | // specializing this template. |
| 6659 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6660 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6661 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 6662 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 6663 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
| 6664 | ExplicitTemplateArgs, FT, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6665 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6666 | // that we can provide nifty diagnostics. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6667 | FailedCandidates.addCandidate() |
| 6668 | .set(FunTmpl->getTemplatedDecl(), |
| 6669 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6670 | (void)TDK; |
| 6671 | continue; |
| 6672 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6673 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6674 | // Record this candidate. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6675 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6676 | } |
| 6677 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6678 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 6679 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6680 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 6681 | Candidates.begin(), Candidates.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6682 | FD->getLocation(), |
| 6683 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 6684 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6685 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6686 | PDiag(diag::note_function_template_spec_matched)); |
| 6687 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6688 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6689 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6690 | |
| 6691 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 6692 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 6693 | |
| 6694 | FunctionTemplateSpecializationInfo *SpecInfo |
| 6695 | = Specialization->getTemplateSpecializationInfo(); |
| 6696 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6697 | |
| 6698 | // Note: do not overwrite location info if previous template |
| 6699 | // specialization kind was explicit. |
| 6700 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6701 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6702 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6703 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 6704 | // function can differ from the template declaration with respect to |
| 6705 | // the constexpr specifier. |
| 6706 | Specialization->setConstexpr(FD->isConstexpr()); |
| 6707 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6708 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6709 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6710 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6711 | |
| 6712 | // If this is a friend declaration, then we're not really declaring |
| 6713 | // an explicit specialization. |
| 6714 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6715 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6716 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6717 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6718 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6719 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6720 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6721 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6722 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6723 | |
| 6724 | // C++ [temp.expl.spec]p6: |
| 6725 | // 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] | 6726 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6727 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6728 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6729 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6730 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6731 | if (!isFriend && |
| 6732 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6733 | TSK_ExplicitSpecialization, |
| 6734 | Specialization, |
| 6735 | SpecInfo->getTemplateSpecializationKind(), |
| 6736 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6737 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6738 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6739 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6740 | // Mark the prior declaration as an explicit specialization, so that later |
| 6741 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6742 | if (!isFriend) { |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6743 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6744 | MarkUnusedFileScopedDecl(Specialization); |
| 6745 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6746 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6747 | // Turn the given function declaration into a function template |
| 6748 | // specialization, with the template arguments from the previous |
| 6749 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6750 | // Take copies of (semantic and syntactic) template argument lists. |
| 6751 | const TemplateArgumentList* TemplArgs = new (Context) |
| 6752 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 6753 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6754 | TemplArgs, /*InsertPos=*/nullptr, |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6755 | SpecInfo->getTemplateSpecializationKind(), |
Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 6756 | ExplicitTemplateArgs); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6757 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6758 | // The "previous declaration" for this function template specialization is |
| 6759 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6760 | Previous.clear(); |
| 6761 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6762 | return false; |
| 6763 | } |
| 6764 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6765 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6766 | /// specialization. |
| 6767 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6768 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6769 | /// explicit member function specialization. On successful completion, |
| 6770 | /// the function declaration \p FD will become a member function |
| 6771 | /// specialization. |
| 6772 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6773 | /// \param Member the member declaration, which will be updated to become a |
| 6774 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6775 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6776 | /// \param Previous the set of declarations, one of which may be specialized |
| 6777 | /// by this function specialization; the set will be modified to contain the |
| 6778 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6779 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6780 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6781 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 6782 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6783 | // Try to find the member we are instantiating. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6784 | NamedDecl *Instantiation = nullptr; |
| 6785 | NamedDecl *InstantiatedFrom = nullptr; |
| 6786 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6787 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6788 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6789 | // Nowhere to look anyway. |
| 6790 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6791 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6792 | I != E; ++I) { |
| 6793 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 6794 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 6795 | QualType Adjusted = Function->getType(); |
| 6796 | if (!hasExplicitCallingConv(Adjusted)) |
| 6797 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
| 6798 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6799 | Instantiation = Method; |
| 6800 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6801 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6802 | break; |
| 6803 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6804 | } |
| 6805 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6806 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6807 | VarDecl *PrevVar; |
| 6808 | if (Previous.isSingleResult() && |
| 6809 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6810 | if (PrevVar->isStaticDataMember()) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6811 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6812 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6813 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6814 | } |
| 6815 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6816 | CXXRecordDecl *PrevRecord; |
| 6817 | if (Previous.isSingleResult() && |
| 6818 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 6819 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6820 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6821 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6822 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6823 | } else if (isa<EnumDecl>(Member)) { |
| 6824 | EnumDecl *PrevEnum; |
| 6825 | if (Previous.isSingleResult() && |
| 6826 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
| 6827 | Instantiation = PrevEnum; |
| 6828 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 6829 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 6830 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6831 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6832 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6833 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6834 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6835 | // specializations are always out-of-line, the caller will complain about |
| 6836 | // this mismatch later. |
| 6837 | return false; |
| 6838 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 6839 | |
| 6840 | // If this is a friend, just bail out here before we start turning |
| 6841 | // things into explicit specializations. |
| 6842 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 6843 | // Preserve instantiation information. |
| 6844 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 6845 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 6846 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 6847 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 6848 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 6849 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 6850 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 6851 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 6852 | } |
| 6853 | |
| 6854 | Previous.clear(); |
| 6855 | Previous.addDecl(Instantiation); |
| 6856 | return false; |
| 6857 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6858 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6859 | // Make sure that this is a specialization of a member. |
| 6860 | if (!InstantiatedFrom) { |
| 6861 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 6862 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6863 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 6864 | return true; |
| 6865 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6866 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6867 | // C++ [temp.expl.spec]p6: |
| 6868 | // 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] | 6869 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6870 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6871 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6872 | // use occurs; no diagnostic is required. |
| 6873 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6874 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6875 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6876 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 6877 | TSK_ExplicitSpecialization, |
| 6878 | Instantiation, |
| 6879 | MSInfo->getTemplateSpecializationKind(), |
| 6880 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6881 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6882 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6883 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6884 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6885 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6886 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6887 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6888 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6889 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 6890 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6891 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6892 | // the original declaration to note that it is an explicit specialization |
| 6893 | // (if it was previously an implicit instantiation). This latter step |
| 6894 | // makes bookkeeping easier. |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6895 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6896 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 6897 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 6898 | TSK_ImplicitInstantiation) { |
| 6899 | InstantiationFunction->setTemplateSpecializationKind( |
| 6900 | TSK_ExplicitSpecialization); |
| 6901 | InstantiationFunction->setLocation(Member->getLocation()); |
| 6902 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6903 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6904 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 6905 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 6906 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6907 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6908 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6909 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 6910 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 6911 | TSK_ImplicitInstantiation) { |
| 6912 | InstantiationVar->setTemplateSpecializationKind( |
| 6913 | TSK_ExplicitSpecialization); |
| 6914 | InstantiationVar->setLocation(Member->getLocation()); |
| 6915 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6916 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6917 | cast<VarDecl>(Member)->setInstantiationOfStaticDataMember( |
| 6918 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6919 | MarkUnusedFileScopedDecl(InstantiationVar); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6920 | } else if (isa<CXXRecordDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6921 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 6922 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 6923 | TSK_ImplicitInstantiation) { |
| 6924 | InstantiationClass->setTemplateSpecializationKind( |
| 6925 | TSK_ExplicitSpecialization); |
| 6926 | InstantiationClass->setLocation(Member->getLocation()); |
| 6927 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6928 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6929 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6930 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 6931 | TSK_ExplicitSpecialization); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6932 | } else { |
| 6933 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); |
| 6934 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); |
| 6935 | if (InstantiationEnum->getTemplateSpecializationKind() == |
| 6936 | TSK_ImplicitInstantiation) { |
| 6937 | InstantiationEnum->setTemplateSpecializationKind( |
| 6938 | TSK_ExplicitSpecialization); |
| 6939 | InstantiationEnum->setLocation(Member->getLocation()); |
| 6940 | } |
| 6941 | |
| 6942 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( |
| 6943 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6944 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6945 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6946 | // Save the caller the trouble of having to figure out which declaration |
| 6947 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6948 | Previous.clear(); |
| 6949 | Previous.addDecl(Instantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6950 | return false; |
| 6951 | } |
| 6952 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6953 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6954 | /// |
| 6955 | /// \returns true if a serious error occurs, false otherwise. |
| 6956 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6957 | SourceLocation InstLoc, |
| 6958 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6959 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 6960 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6961 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6962 | if (CurContext->isRecord()) { |
| 6963 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 6964 | << D; |
| 6965 | return true; |
| 6966 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6967 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6968 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6969 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6970 | // template. If the name declared in the explicit instantiation is an |
| 6971 | // unqualified name, the explicit instantiation shall appear in the |
| 6972 | // namespace where its template is declared or, if that namespace is inline |
| 6973 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6974 | // |
| 6975 | // 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] | 6976 | if (WasQualifiedName) { |
| 6977 | if (CurContext->Encloses(OrigContext)) |
| 6978 | return false; |
| 6979 | } else { |
| 6980 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 6981 | return false; |
| 6982 | } |
| 6983 | |
| 6984 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 6985 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6986 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6987 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6988 | diag::err_explicit_instantiation_out_of_scope : |
| 6989 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6990 | << D << NS; |
| 6991 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6992 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6993 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6994 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 6995 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 6996 | << D << NS; |
| 6997 | } else |
| 6998 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6999 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7000 | diag::err_explicit_instantiation_must_be_global : |
| 7001 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 7002 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7003 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7004 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7005 | } |
| 7006 | |
| 7007 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 7008 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 7009 | if (!SS.isSet()) |
| 7010 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7011 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7012 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7013 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7014 | // or a static data member of a class template specialization, the name of |
| 7015 | // the class template specialization in the qualified-id for the member |
| 7016 | // name shall be a simple-template-id. |
| 7017 | // |
| 7018 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7019 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 7020 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7021 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7022 | if (isa<TemplateSpecializationType>(T)) |
| 7023 | return true; |
| 7024 | |
| 7025 | return false; |
| 7026 | } |
| 7027 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7028 | // Explicit instantiation of a class template specialization |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7029 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7030 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7031 | SourceLocation ExternLoc, |
| 7032 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7033 | unsigned TagSpec, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7034 | SourceLocation KWLoc, |
| 7035 | const CXXScopeSpec &SS, |
| 7036 | TemplateTy TemplateD, |
| 7037 | SourceLocation TemplateNameLoc, |
| 7038 | SourceLocation LAngleLoc, |
| 7039 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7040 | SourceLocation RAngleLoc, |
| 7041 | AttributeList *Attr) { |
| 7042 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 7043 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7044 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7045 | // Check that the specialization uses the same tag kind as the |
| 7046 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7047 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7048 | assert(Kind != TTK_Enum && |
| 7049 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7050 | |
| 7051 | if (isa<TypeAliasTemplateDecl>(TD)) { |
| 7052 | Diag(KWLoc, diag::err_tag_reference_non_tag) << Kind; |
| 7053 | Diag(TD->getTemplatedDecl()->getLocation(), |
| 7054 | diag::note_previous_use); |
| 7055 | return true; |
| 7056 | } |
| 7057 | |
| 7058 | ClassTemplateDecl *ClassTemplate = cast<ClassTemplateDecl>(TD); |
| 7059 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7060 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7061 | Kind, /*isDefinition*/false, KWLoc, |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7062 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7063 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7064 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7065 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7066 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7067 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7068 | diag::note_previous_use); |
| 7069 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7070 | } |
| 7071 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7072 | // C++0x [temp.explicit]p2: |
| 7073 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7074 | // definition and an explicit instantiation declaration. An explicit |
| 7075 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7076 | TemplateSpecializationKind TSK |
| 7077 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7078 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7079 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7080 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7081 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 7082 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7083 | |
| 7084 | // Check that the template argument list is well-formed for this |
| 7085 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7086 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7087 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7088 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7089 | return true; |
| 7090 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7091 | // Find the class template specialization declaration that |
| 7092 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7093 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7094 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7095 | = ClassTemplate->findSpecialization(Converted.data(), |
| 7096 | Converted.size(), InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7097 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7098 | TemplateSpecializationKind PrevDecl_TSK |
| 7099 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 7100 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7101 | // C++0x [temp.explicit]p2: |
| 7102 | // [...] An explicit instantiation shall appear in an enclosing |
| 7103 | // namespace of its template. [...] |
| 7104 | // |
| 7105 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7106 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 7107 | SS.isSet())) |
| 7108 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7109 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7110 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7111 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7112 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7113 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7114 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7115 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7116 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7117 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7118 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7119 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7120 | // Even though HasNoEffect == true means that this explicit instantiation |
| 7121 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 7122 | |
| 7123 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 7124 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7125 | // Since the only prior class template specialization with these |
| 7126 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7127 | // declaration node as our own, updating the source location |
| 7128 | // for the template name to reflect our new declaration. |
| 7129 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7130 | Specialization = PrevDecl; |
| 7131 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7132 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7133 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7134 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7135 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7136 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7137 | // Create a new class template specialization declaration node for |
| 7138 | // this explicit specialization. |
| 7139 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7140 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7141 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7142 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7143 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7144 | Converted.data(), |
| 7145 | Converted.size(), |
| 7146 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7147 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7148 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7149 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7150 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7151 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7152 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7153 | } |
| 7154 | |
| 7155 | // Build the fully-sugared type for this explicit instantiation as |
| 7156 | // the user wrote in the explicit instantiation itself. This means |
| 7157 | // that we'll pretty-print the type retrieved from the |
| 7158 | // specialization's declaration the way that the user actually wrote |
| 7159 | // the explicit instantiation, rather than formatting the name based |
| 7160 | // on the "canonical" representation used to store the template |
| 7161 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7162 | TypeSourceInfo *WrittenTy |
| 7163 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7164 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7165 | Context.getTypeDeclType(Specialization)); |
| 7166 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7167 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7168 | // Set source locations for keywords. |
| 7169 | Specialization->setExternLoc(ExternLoc); |
| 7170 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | 40bcfd7 | 2013-04-22 23:23:42 +0000 | [diff] [blame] | 7171 | Specialization->setRBraceLoc(SourceLocation()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7172 | |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 7173 | if (Attr) |
| 7174 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 7175 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7176 | // Add the explicit instantiation into its lexical context. However, |
| 7177 | // since explicit instantiations are never found by name lookup, we |
| 7178 | // just put it into the declaration context directly. |
| 7179 | Specialization->setLexicalDeclContext(CurContext); |
| 7180 | CurContext->addDecl(Specialization); |
| 7181 | |
| 7182 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 7183 | if (HasNoEffect) { |
| 7184 | // Set the template specialization kind. |
| 7185 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7186 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 7187 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7188 | |
| 7189 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7190 | // A definition of a class template or class member template |
| 7191 | // shall be in scope at the point of the explicit instantiation of |
| 7192 | // the class template or class member template. |
| 7193 | // |
| 7194 | // This check comes when we actually try to perform the |
| 7195 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7196 | ClassTemplateSpecializationDecl *Def |
| 7197 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7198 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7199 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7200 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7201 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7202 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7203 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 7204 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7205 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7206 | // Instantiate the members of this class template specialization. |
| 7207 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7208 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7209 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7210 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 7211 | |
| 7212 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 7213 | // TSK_ExplicitInstantiationDefinition |
| 7214 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 7215 | TSK == TSK_ExplicitInstantiationDefinition) |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7216 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7217 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7218 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7219 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7220 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7221 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7222 | // Set the template specialization kind. |
| 7223 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7224 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7225 | } |
| 7226 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7227 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7228 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7229 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7230 | SourceLocation ExternLoc, |
| 7231 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7232 | unsigned TagSpec, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7233 | SourceLocation KWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7234 | CXXScopeSpec &SS, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7235 | IdentifierInfo *Name, |
| 7236 | SourceLocation NameLoc, |
| 7237 | AttributeList *Attr) { |
| 7238 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 7239 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7240 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7241 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7242 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7243 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7244 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 7245 | SourceLocation(), false, TypeResult(), |
| 7246 | /*IsTypeSpecifier*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7247 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 7248 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7249 | if (!TagD) |
| 7250 | return true; |
| 7251 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7252 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7253 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7254 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 7255 | if (Tag->isInvalidDecl()) |
| 7256 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7257 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7258 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 7259 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 7260 | if (!Pattern) { |
| 7261 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 7262 | << Context.getTypeDeclType(Record); |
| 7263 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 7264 | return true; |
| 7265 | } |
| 7266 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7267 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7268 | // If the explicit instantiation is for a class or member class, the |
| 7269 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7270 | // simple-template-id. |
| 7271 | // |
| 7272 | // C++98 has the same restriction, just worded differently. |
| 7273 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7274 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7275 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7276 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7277 | // C++0x [temp.explicit]p2: |
| 7278 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7279 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7280 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 7281 | TemplateSpecializationKind TSK |
| 7282 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7283 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7284 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7285 | // C++0x [temp.explicit]p2: |
| 7286 | // [...] An explicit instantiation shall appear in an enclosing |
| 7287 | // namespace of its template. [...] |
| 7288 | // |
| 7289 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7290 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7291 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7292 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7293 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7294 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7295 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7296 | PrevDecl = Record; |
| 7297 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7298 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7299 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7300 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7301 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7302 | PrevDecl, |
| 7303 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7304 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7305 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7306 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7307 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7308 | return TagD; |
| 7309 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7310 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7311 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7312 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7313 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7314 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7315 | // 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] | 7316 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7317 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7318 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7319 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 7320 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 7321 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7322 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 7323 | << Pattern; |
| 7324 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7325 | } else { |
| 7326 | if (InstantiateClass(NameLoc, Record, Def, |
| 7327 | getTemplateInstantiationArgs(Record), |
| 7328 | TSK)) |
| 7329 | return true; |
| 7330 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7331 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7332 | if (!RecordDef) |
| 7333 | return true; |
| 7334 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7335 | } |
| 7336 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7337 | // Instantiate all of the members of the class. |
| 7338 | InstantiateClassMembers(NameLoc, RecordDef, |
| 7339 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7340 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7341 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7342 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 7343 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7344 | // FIXME: We don't have any representation for explicit instantiations of |
| 7345 | // member classes. Such a representation is not needed for compilation, but it |
| 7346 | // should be available for clients that want to see all of the declarations in |
| 7347 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7348 | return TagD; |
| 7349 | } |
| 7350 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7351 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 7352 | SourceLocation ExternLoc, |
| 7353 | SourceLocation TemplateLoc, |
| 7354 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7355 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7356 | // TODO: check if/when DNInfo should replace Name. |
| 7357 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 7358 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7359 | if (!Name) { |
| 7360 | if (!D.isInvalidType()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7361 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7362 | diag::err_explicit_instantiation_requires_name) |
| 7363 | << D.getDeclSpec().getSourceRange() |
| 7364 | << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7365 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7366 | return true; |
| 7367 | } |
| 7368 | |
| 7369 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 7370 | // we find one that is. |
| 7371 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 7372 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 7373 | S = S->getParent(); |
| 7374 | |
| 7375 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7376 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 7377 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7378 | if (R.isNull()) |
| 7379 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7380 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7381 | // C++ [dcl.stc]p1: |
| 7382 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 7383 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7384 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7385 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 7386 | << Name; |
| 7387 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7388 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 7389 | != DeclSpec::SCS_unspecified) { |
| 7390 | // Complain about then remove the storage class specifier. |
| 7391 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 7392 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 7393 | |
| 7394 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7395 | } |
| 7396 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 7397 | // C++0x [temp.explicit]p1: |
| 7398 | // [...] An explicit instantiation of a function template shall not use the |
| 7399 | // inline or constexpr specifiers. |
| 7400 | // Presumably, this also applies to member functions of class templates as |
| 7401 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7402 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7403 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7404 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7405 | diag::err_explicit_instantiation_inline : |
| 7406 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7407 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7408 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7409 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 7410 | // not already specified. |
| 7411 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 7412 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7413 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7414 | // C++0x [temp.explicit]p2: |
| 7415 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7416 | // definition and an explicit instantiation declaration. An explicit |
| 7417 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7418 | TemplateSpecializationKind TSK |
| 7419 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7420 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7421 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7422 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7423 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7424 | |
| 7425 | if (!R->isFunctionType()) { |
| 7426 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7427 | // A [...] static data member of a class template can be explicitly |
| 7428 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7429 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7430 | // C++1y [temp.explicit]p1: |
| 7431 | // A [...] variable [...] template specialization can be explicitly |
| 7432 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7433 | if (Previous.isAmbiguous()) |
| 7434 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7435 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 7436 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7437 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7438 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7439 | if (!PrevTemplate) { |
| 7440 | if (!Prev || !Prev->isStaticDataMember()) { |
| 7441 | // We expect to see a data data member here. |
| 7442 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 7443 | << Name; |
| 7444 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7445 | P != PEnd; ++P) |
| 7446 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 7447 | return true; |
| 7448 | } |
| 7449 | |
| 7450 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 7451 | // FIXME: Check for explicit specialization? |
| 7452 | Diag(D.getIdentifierLoc(), |
| 7453 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 7454 | << Prev; |
| 7455 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 7456 | // FIXME: Can we provide a note showing where this was declared? |
| 7457 | return true; |
| 7458 | } |
| 7459 | } else { |
| 7460 | // Explicitly instantiate a variable template. |
| 7461 | |
| 7462 | // C++1y [dcl.spec.auto]p6: |
| 7463 | // ... A program that uses auto or decltype(auto) in a context not |
| 7464 | // explicitly allowed in this section is ill-formed. |
| 7465 | // |
| 7466 | // This includes auto-typed variable template instantiations. |
| 7467 | if (R->isUndeducedType()) { |
| 7468 | Diag(T->getTypeLoc().getLocStart(), |
| 7469 | diag::err_auto_not_allowed_var_inst); |
| 7470 | return true; |
| 7471 | } |
| 7472 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7473 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 7474 | // C++1y [temp.explicit]p3: |
| 7475 | // If the explicit instantiation is for a variable, the unqualified-id |
| 7476 | // in the declaration shall be a template-id. |
| 7477 | Diag(D.getIdentifierLoc(), |
| 7478 | diag::err_explicit_instantiation_without_template_id) |
| 7479 | << PrevTemplate; |
| 7480 | Diag(PrevTemplate->getLocation(), |
| 7481 | diag::note_explicit_instantiation_here); |
| 7482 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7483 | } |
| 7484 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7485 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7486 | TemplateArgumentListInfo TemplateArgs = |
| 7487 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7488 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7489 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 7490 | D.getIdentifierLoc(), TemplateArgs); |
| 7491 | if (Res.isInvalid()) |
| 7492 | return true; |
| 7493 | |
| 7494 | // Ignore access control bits, we don't need them for redeclaration |
| 7495 | // checking. |
| 7496 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7497 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7498 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7499 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7500 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7501 | // or a static data member of a class template specialization, the name of |
| 7502 | // the class template specialization in the qualified-id for the member |
| 7503 | // name shall be a simple-template-id. |
| 7504 | // |
| 7505 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7506 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 7507 | // This does not apply to variable template specializations, where the |
| 7508 | // template-id is in the unqualified-id instead. |
| 7509 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7510 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7511 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7512 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7513 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7514 | // Check the scope of this explicit instantiation. |
| 7515 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7516 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7517 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 7518 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 7519 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7520 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7521 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7522 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7523 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7524 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7525 | if (!HasNoEffect) { |
| 7526 | // Instantiate static data member or variable template. |
| 7527 | |
| 7528 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 7529 | if (PrevTemplate) { |
| 7530 | // Merge attributes. |
| 7531 | if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList()) |
| 7532 | ProcessDeclAttributeList(S, Prev, Attr); |
| 7533 | } |
| 7534 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7535 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 7536 | } |
| 7537 | |
| 7538 | // Check the new variable specialization against the parsed input. |
| 7539 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
| 7540 | Diag(T->getTypeLoc().getLocStart(), |
| 7541 | diag::err_invalid_var_template_spec_type) |
| 7542 | << 0 << PrevTemplate << R << Prev->getType(); |
| 7543 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 7544 | << 2 << PrevTemplate->getDeclName(); |
| 7545 | return true; |
| 7546 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7547 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7548 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7549 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7550 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7551 | |
| 7552 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 7553 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7554 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7555 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7556 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7557 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7558 | HasExplicitTemplateArgs = true; |
| 7559 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7560 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7561 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7562 | // A [...] function [...] can be explicitly instantiated from its template. |
| 7563 | // A member function [...] of a class template can be explicitly |
| 7564 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7565 | // template. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7566 | UnresolvedSet<8> Matches; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7567 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7568 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7569 | P != PEnd; ++P) { |
| 7570 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7571 | if (!HasExplicitTemplateArgs) { |
| 7572 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 7573 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType()); |
| 7574 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7575 | Matches.clear(); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7576 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7577 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7578 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 7579 | break; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7580 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7581 | } |
| 7582 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7583 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7584 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 7585 | if (!FunTmpl) |
| 7586 | continue; |
| 7587 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7588 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7589 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7590 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7591 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7592 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 7593 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7594 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7595 | // Keep track of almost-matches. |
| 7596 | FailedCandidates.addCandidate() |
| 7597 | .set(FunTmpl->getTemplatedDecl(), |
| 7598 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7599 | (void)TDK; |
| 7600 | continue; |
| 7601 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7602 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7603 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7604 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7605 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7606 | // Find the most specialized function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7607 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 7608 | Matches.begin(), Matches.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7609 | D.getIdentifierLoc(), |
| 7610 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 7611 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 7612 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7613 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7614 | if (Result == Matches.end()) |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7615 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7616 | |
| 7617 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 7618 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7619 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7620 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7621 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7622 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 7623 | << Specialization |
| 7624 | << (Specialization->getTemplateSpecializationKind() == |
| 7625 | TSK_ExplicitSpecialization); |
| 7626 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 7627 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7628 | } |
| 7629 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7630 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7631 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 7632 | PrevDecl = Specialization; |
| 7633 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7634 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7635 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7636 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7637 | PrevDecl, |
| 7638 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7639 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7640 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7641 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7642 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7643 | // FIXME: We may still want to build some representation of this |
| 7644 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7645 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7646 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7647 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 7648 | |
| 7649 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 2aa7acf | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 7650 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 7651 | if (Attr) |
| 7652 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7653 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7654 | if (Specialization->isDefined()) { |
| 7655 | // Let the ASTConsumer know that this function has been explicitly |
| 7656 | // instantiated now, and its linkage might have changed. |
| 7657 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 7658 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 7659 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7660 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7661 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7662 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7663 | // or a static data member of a class template specialization, the name of |
| 7664 | // the class template specialization in the qualified-id for the member |
| 7665 | // name shall be a simple-template-id. |
| 7666 | // |
| 7667 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7668 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7669 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7670 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7671 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7672 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7673 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7674 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7675 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7676 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7677 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7678 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7679 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7680 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7681 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7682 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7683 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7684 | } |
| 7685 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7686 | TypeResult |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7687 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 7688 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 7689 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 7690 | // This has to hold, because SS is expected to be defined. |
| 7691 | assert(Name && "Expected a name in a dependent tag"); |
| 7692 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7693 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7694 | if (!NNS) |
| 7695 | return true; |
| 7696 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7697 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 7698 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 7699 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 7700 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7701 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 7702 | return true; |
| 7703 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7704 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7705 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7706 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7707 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 7708 | |
| 7709 | // Create type-source location information for this type. |
| 7710 | TypeLocBuilder TLB; |
| 7711 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7712 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7713 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 7714 | TL.setNameLoc(NameLoc); |
| 7715 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7716 | } |
| 7717 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7718 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7719 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 7720 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 7721 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7722 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7723 | return true; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7724 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7725 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 7726 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7727 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7728 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 7729 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7730 | << FixItHint::CreateRemoval(TypenameLoc); |
| 7731 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 7732 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 7733 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 7734 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 7735 | if (T.isNull()) |
| 7736 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7737 | |
| 7738 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 7739 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7740 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7741 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 7742 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 7743 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7744 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7745 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7746 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 7747 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7748 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7749 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7750 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7751 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7752 | } |
| 7753 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7754 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7755 | Sema::ActOnTypenameType(Scope *S, |
| 7756 | SourceLocation TypenameLoc, |
| 7757 | const CXXScopeSpec &SS, |
| 7758 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7759 | TemplateTy TemplateIn, |
| 7760 | SourceLocation TemplateNameLoc, |
| 7761 | SourceLocation LAngleLoc, |
| 7762 | ASTTemplateArgsPtr TemplateArgsIn, |
| 7763 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7764 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 7765 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7766 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7767 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 7768 | diag::ext_typename_outside_of_template) |
| 7769 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7770 | |
| 7771 | // Translate the parser's template argument list in our AST format. |
| 7772 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 7773 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 7774 | |
| 7775 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7776 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 7777 | // Construct a dependent template specialization type. |
| 7778 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7779 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7780 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 7781 | DTN->getQualifier(), |
| 7782 | DTN->getIdentifier(), |
| 7783 | TemplateArgs); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7784 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7785 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 7786 | TypeLocBuilder Builder; |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7787 | DependentTemplateSpecializationTypeLoc SpecTL |
| 7788 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7789 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 7790 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 7791 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7792 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7793 | SpecTL.setLAngleLoc(LAngleLoc); |
| 7794 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7795 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 7796 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7797 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 7798 | } |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7799 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7800 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 7801 | if (T.isNull()) |
| 7802 | return true; |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7803 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7804 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7805 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7806 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7807 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7808 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 7809 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7810 | SpecTL.setLAngleLoc(LAngleLoc); |
| 7811 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7812 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 7813 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 7814 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7815 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 7816 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7817 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 7818 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 7819 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7820 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 7821 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 7822 | } |
| 7823 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7824 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7825 | /// Determine whether this failed name lookup should be treated as being |
| 7826 | /// disabled by a usage of std::enable_if. |
| 7827 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
| 7828 | SourceRange &CondRange) { |
| 7829 | // We must be looking for a ::type... |
| 7830 | if (!II.isStr("type")) |
| 7831 | return false; |
| 7832 | |
| 7833 | // ... within an explicitly-written template specialization... |
| 7834 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 7835 | return false; |
| 7836 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7837 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 7838 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 7839 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7840 | return false; |
| 7841 | const TemplateSpecializationType *EnableIfTST = |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7842 | cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr()); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7843 | |
| 7844 | // ... which names a complete class template declaration... |
| 7845 | const TemplateDecl *EnableIfDecl = |
| 7846 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 7847 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 7848 | return false; |
| 7849 | |
| 7850 | // ... called "enable_if". |
| 7851 | const IdentifierInfo *EnableIfII = |
| 7852 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 7853 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 7854 | return false; |
| 7855 | |
| 7856 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7857 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7858 | return true; |
| 7859 | } |
| 7860 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7861 | /// \brief Build the type that describes a C++ typename specifier, |
| 7862 | /// e.g., "typename T::type". |
| 7863 | QualType |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7864 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 7865 | SourceLocation KeywordLoc, |
| 7866 | NestedNameSpecifierLoc QualifierLoc, |
| 7867 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 7868 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7869 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7870 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7871 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7872 | DeclContext *Ctx = computeDeclContext(SS); |
| 7873 | if (!Ctx) { |
| 7874 | // If the nested-name-specifier is dependent and couldn't be |
| 7875 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7876 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 7877 | return Context.getDependentNameType(Keyword, |
| 7878 | QualifierLoc.getNestedNameSpecifier(), |
| 7879 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 7880 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7881 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7882 | // If the nested-name-specifier refers to the current instantiation, |
| 7883 | // the "typename" keyword itself is superfluous. In C++03, the |
| 7884 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 7885 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 7886 | // 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] | 7887 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7888 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 7889 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7890 | |
| 7891 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 7892 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7893 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7894 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7895 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7896 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7897 | case LookupResult::NotFound: { |
| 7898 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 7899 | // a more specific diagnostic. |
| 7900 | SourceRange CondRange; |
| 7901 | if (isEnableIf(QualifierLoc, II, CondRange)) { |
| 7902 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
| 7903 | << Ctx << CondRange; |
| 7904 | return QualType(); |
| 7905 | } |
| 7906 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 7907 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7908 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7909 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 7910 | |
| 7911 | case LookupResult::FoundUnresolvedValue: { |
| 7912 | // We found a using declaration that is a value. Most likely, the using |
| 7913 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7914 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 7915 | IILoc); |
| 7916 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 7917 | << Name << Ctx << FullRange; |
| 7918 | if (UnresolvedUsingValueDecl *Using |
| 7919 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7920 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 7921 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 7922 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 7923 | } |
| 7924 | } |
| 7925 | // Fall through to create a dependent typename type, from which we can recover |
| 7926 | // better. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7927 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 7928 | case LookupResult::NotFoundInCurrentInstantiation: |
| 7929 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7930 | return Context.getDependentNameType(Keyword, |
| 7931 | QualifierLoc.getNestedNameSpecifier(), |
| 7932 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7933 | |
| 7934 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7935 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7936 | // We found a type. Build an ElaboratedType, since the |
| 7937 | // typename-specifier was just sugar. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7938 | return Context.getElaboratedType(ETK_Typename, |
| 7939 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7940 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7941 | } |
| 7942 | |
| 7943 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7944 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7945 | break; |
| 7946 | |
| 7947 | case LookupResult::FoundOverloaded: |
| 7948 | DiagID = diag::err_typename_nested_not_type; |
| 7949 | Referenced = *Result.begin(); |
| 7950 | break; |
| 7951 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 7952 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7953 | return QualType(); |
| 7954 | } |
| 7955 | |
| 7956 | // If we get here, it's because name lookup did not find a |
| 7957 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7958 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 7959 | IILoc); |
| 7960 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7961 | if (Referenced) |
| 7962 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 7963 | << Name; |
| 7964 | return QualType(); |
| 7965 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7966 | |
| 7967 | namespace { |
| 7968 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 7969 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7970 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7971 | SourceLocation Loc; |
| 7972 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7973 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7974 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 7975 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7976 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7977 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7978 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7979 | DeclarationName Entity) |
| 7980 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7981 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7982 | |
| 7983 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7984 | /// transformed. |
| 7985 | /// |
| 7986 | /// For the purposes of type reconstruction, a type has already been |
| 7987 | /// transformed if it is NULL or if it is not dependent. |
| 7988 | bool AlreadyTransformed(QualType T) { |
| 7989 | return T.isNull() || !T->isDependentType(); |
| 7990 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7991 | |
| 7992 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7993 | /// rebuilt. |
| 7994 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7995 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7996 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 7997 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7998 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7999 | /// \brief Sets the "base" location and entity when that |
| 8000 | /// information is known based on another transformation. |
| 8001 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 8002 | this->Loc = Loc; |
| 8003 | this->Entity = Entity; |
| 8004 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 8005 | |
| 8006 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 8007 | // Lambdas never need to be transformed. |
| 8008 | return E; |
| 8009 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8010 | }; |
| 8011 | } |
| 8012 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8013 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 8014 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8015 | /// 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] | 8016 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8017 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8018 | /// partial specialization thereof). This routine will rebuild that type now |
| 8019 | /// that we have entered the declarator's scope, which may produce different |
| 8020 | /// canonical types, e.g., |
| 8021 | /// |
| 8022 | /// \code |
| 8023 | /// template<typename T> |
| 8024 | /// struct X { |
| 8025 | /// typedef T* pointer; |
| 8026 | /// pointer data(); |
| 8027 | /// }; |
| 8028 | /// |
| 8029 | /// template<typename T> |
| 8030 | /// typename X<T>::pointer X<T>::data() { ... } |
| 8031 | /// \endcode |
| 8032 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 8033 | /// 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] | 8034 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 8035 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8036 | /// 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] | 8037 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 8038 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8039 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 8040 | SourceLocation Loc, |
| 8041 | DeclarationName Name) { |
| 8042 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8043 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8044 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8045 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 8046 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 8047 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8048 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8049 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8050 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 8051 | DeclarationName()); |
| 8052 | return Rebuilder.TransformExpr(E); |
| 8053 | } |
| 8054 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8055 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8056 | if (SS.isInvalid()) |
| 8057 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8058 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8059 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8060 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 8061 | DeclarationName()); |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8062 | NestedNameSpecifierLoc Rebuilt |
| 8063 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 8064 | if (!Rebuilt) |
| 8065 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8066 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8067 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8068 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8069 | } |
| 8070 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8071 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 8072 | /// instantiation. |
| 8073 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 8074 | TemplateParameterList *Params) { |
| 8075 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 8076 | Decl *Param = Params->getParam(I); |
| 8077 | |
| 8078 | // There is nothing to rebuild in a type parameter. |
| 8079 | if (isa<TemplateTypeParmDecl>(Param)) |
| 8080 | continue; |
| 8081 | |
| 8082 | // Rebuild the template parameter list of a template template parameter. |
| 8083 | if (TemplateTemplateParmDecl *TTP |
| 8084 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 8085 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 8086 | TTP->getTemplateParameters())) |
| 8087 | return true; |
| 8088 | |
| 8089 | continue; |
| 8090 | } |
| 8091 | |
| 8092 | // Rebuild the type of a non-type template parameter. |
| 8093 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 8094 | TypeSourceInfo *NewTSI |
| 8095 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 8096 | NTTP->getLocation(), |
| 8097 | NTTP->getDeclName()); |
| 8098 | if (!NewTSI) |
| 8099 | return true; |
| 8100 | |
| 8101 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 8102 | NTTP->setTypeSourceInfo(NewTSI); |
| 8103 | NTTP->setType(NewTSI->getType()); |
| 8104 | } |
| 8105 | } |
| 8106 | |
| 8107 | return false; |
| 8108 | } |
| 8109 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8110 | /// \brief Produces a formatted string that describes the binding of |
| 8111 | /// template parameters to template arguments. |
| 8112 | std::string |
| 8113 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8114 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8115 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8116 | } |
| 8117 | |
| 8118 | std::string |
| 8119 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8120 | const TemplateArgument *Args, |
| 8121 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8122 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8123 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8124 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8125 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8126 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8127 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8128 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8129 | if (I >= NumArgs) |
| 8130 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8131 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8132 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8133 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8134 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8135 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8136 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8137 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8138 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8139 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8140 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8141 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8142 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8143 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 8144 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8145 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8146 | |
| 8147 | Out << ']'; |
| 8148 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8149 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8150 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8151 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 8152 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8153 | if (!FD) |
| 8154 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8155 | |
| 8156 | LateParsedTemplate *LPT = new LateParsedTemplate; |
| 8157 | |
| 8158 | // Take tokens to avoid allocations |
| 8159 | LPT->Toks.swap(Toks); |
| 8160 | LPT->D = FnD; |
| 8161 | LateParsedTemplateMap[FD] = LPT; |
| 8162 | |
| 8163 | FD->setLateTemplateParsed(true); |
| 8164 | } |
| 8165 | |
| 8166 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 8167 | if (!FD) |
| 8168 | return; |
| 8169 | FD->setLateTemplateParsed(false); |
| 8170 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8171 | |
| 8172 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 8173 | DeclContext *DC = CurContext; |
| 8174 | |
| 8175 | while (DC) { |
| 8176 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 8177 | const FunctionDecl *FD = RD->isLocalClass(); |
| 8178 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 8179 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 8180 | return false; |
| 8181 | |
| 8182 | DC = DC->getParent(); |
| 8183 | } |
| 8184 | return false; |
| 8185 | } |