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 |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 321 | auto FilterCCC = llvm::make_unique<CorrectionCandidateCallback>(); |
| 322 | FilterCCC->WantTypeSpecifiers = false; |
| 323 | FilterCCC->WantExpressionKeywords = false; |
| 324 | FilterCCC->WantRemainingKeywords = false; |
| 325 | FilterCCC->WantCXXNamedCasts = true; |
| 326 | if (TypoCorrection Corrected = CorrectTypo( |
| 327 | Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS, |
| 328 | std::move(FilterCCC), CTK_ErrorRecovery, LookupCtx)) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 329 | Found.setLookupName(Corrected.getCorrection()); |
| 330 | if (Corrected.getCorrectionDecl()) |
| 331 | Found.addDecl(Corrected.getCorrectionDecl()); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 332 | FilterAcceptableTemplateNames(Found); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 333 | if (!Found.empty()) { |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 334 | if (LookupCtx) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 335 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 336 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 337 | Name.getAsString() == CorrectedStr; |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 338 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
| 339 | << Name << LookupCtx << DroppedSpecifier |
| 340 | << SS.getRange()); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 341 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 342 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 343 | } |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 344 | } |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 345 | } else { |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 346 | Found.setLookupName(Name); |
Douglas Gregor | ff18cc1 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 350 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 351 | if (Found.empty()) { |
| 352 | if (isDependent) |
| 353 | MemberOfUnknownSpecialization = true; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 354 | return; |
Douglas Gregor | fc6c3e7 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 355 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 356 | |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 357 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && |
Richard Smith | e7d67f2 | 2013-09-03 21:22:41 +0000 | [diff] [blame] | 358 | !getLangOpts().CPlusPlus11) { |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 359 | // C++03 [basic.lookup.classref]p1: |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 360 | // [...] If the lookup in the class of the object expression finds a |
| 361 | // template, the name is also looked up in the context of the entire |
| 362 | // postfix-expression and [...] |
| 363 | // |
Douglas Gregor | 1b02e4a | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 364 | // Note: C++11 does not perform this second lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 365 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 366 | LookupOrdinaryName); |
| 367 | LookupName(FoundOuter, S); |
Douglas Gregor | 50a3cdd | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 368 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 369 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 370 | if (FoundOuter.empty()) { |
| 371 | // - if the name is not found, the name found in the class of the |
| 372 | // object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 373 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 374 | FoundOuter.isAmbiguous()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 375 | // - if the name is found in the context of the entire |
| 376 | // postfix-expression and does not name a class template, the name |
| 377 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | de0a43f | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 378 | FoundOuter.clear(); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 379 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 380 | // - if the name found is a class template, it must refer to the same |
| 381 | // entity as the one found in the class of the object expression, |
| 382 | // otherwise the program is ill-formed. |
| 383 | if (!Found.isSingleResult() || |
| 384 | Found.getFoundDecl()->getCanonicalDecl() |
| 385 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 386 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 2f96e9f | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 387 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 388 | << Found.getLookupName() |
| 389 | << ObjectType; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 390 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 391 | diag::note_ambig_member_ref_object_type) |
| 392 | << ObjectType; |
| 393 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 394 | diag::note_ambig_member_ref_scope); |
| 395 | |
| 396 | // Recover by taking the template that we found in the object |
| 397 | // expression's type. |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 403 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 404 | /// was just parsed. This is only possible with an explicit scope |
| 405 | /// specifier naming a dependent type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 406 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 407 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 408 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 409 | const DeclarationNameInfo &NameInfo, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 410 | bool isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 411 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 412 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 413 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 414 | if (!isAddressOfOperand && |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 415 | isa<CXXMethodDecl>(DC) && |
| 416 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 417 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 418 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 419 | // Since the 'this' expression is synthesized, we don't need to |
| 420 | // perform the double-lookup check. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 421 | NamedDecl *FirstQualifierInScope = nullptr; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 422 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 423 | return CXXDependentScopeMemberExpr::Create( |
| 424 | Context, /*This*/ nullptr, ThisType, /*IsArrow*/ true, |
| 425 | /*Op*/ SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
| 426 | FirstQualifierInScope, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 429 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 430 | } |
| 431 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 432 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 433 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 434 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 435 | const DeclarationNameInfo &NameInfo, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 436 | const TemplateArgumentListInfo *TemplateArgs) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 437 | return DependentScopeDeclRefExpr::Create( |
| 438 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 439 | TemplateArgs); |
Douglas Gregor | 55ad91f | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 442 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 443 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 444 | /// declaration at location Loc. Returns true to indicate that this is |
| 445 | /// an error, and false otherwise. |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 446 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 447 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 448 | |
| 449 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 450 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 451 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 452 | |
| 453 | // C++ [temp.local]p4: |
| 454 | // A template-parameter shall not be redeclared within its |
| 455 | // scope (including nested scopes). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 457 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 458 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 459 | return; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 462 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 463 | /// the parameter D to reference the templated declaration and return a pointer |
| 464 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 465 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 466 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 467 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 468 | return Temp; |
| 469 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 470 | return nullptr; |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 473 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 474 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 475 | assert(Kind == Template && |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 476 | "Only template template arguments can be pack expansions here"); |
| 477 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 478 | "Template template argument pack expansion without packs"); |
| 479 | ParsedTemplateArgument Result(*this); |
| 480 | Result.EllipsisLoc = EllipsisLoc; |
| 481 | return Result; |
| 482 | } |
| 483 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 484 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 485 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 486 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 487 | switch (Arg.getKind()) { |
| 488 | case ParsedTemplateArgument::Type: { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 489 | TypeSourceInfo *DI; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 490 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 491 | if (!DI) |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 492 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 493 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 494 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 495 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 496 | case ParsedTemplateArgument::NonType: { |
| 497 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 498 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 499 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 500 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 501 | case ParsedTemplateArgument::Template: { |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 502 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 503 | TemplateArgument TArg; |
| 504 | if (Arg.getEllipsisLoc().isValid()) |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 505 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 506 | else |
| 507 | TArg = Template; |
| 508 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 509 | Arg.getScopeSpec().getWithLocInContext( |
| 510 | SemaRef.Context), |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 511 | Arg.getLocation(), |
| 512 | Arg.getEllipsisLoc()); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 515 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 516 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 517 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 518 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 519 | /// \brief Translates template arguments as provided by the parser |
| 520 | /// into template arguments used by semantic analysis. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 521 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 522 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 523 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 524 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 525 | TemplateArgsIn[I])); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 526 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 527 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 528 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 529 | SourceLocation Loc, |
| 530 | IdentifierInfo *Name) { |
| 531 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
| 532 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 533 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 534 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 535 | } |
| 536 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 537 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 538 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 539 | /// the keyword "typename" was used to declare the type parameter |
| 540 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 541 | /// "class" or "typename" keyword. ParamName is the name of the |
| 542 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 543 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 544 | /// If the type parameter has a default argument, it will be added |
| 545 | /// later via ActOnTypeParameterDefault. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 546 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 547 | SourceLocation EllipsisLoc, |
| 548 | SourceLocation KeyLoc, |
| 549 | IdentifierInfo *ParamName, |
| 550 | SourceLocation ParamNameLoc, |
| 551 | unsigned Depth, unsigned Position, |
| 552 | SourceLocation EqualLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 553 | ParsedType DefaultArg) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | assert(S->isTemplateParamScope() && |
| 555 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 556 | bool Invalid = false; |
| 557 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 558 | SourceLocation Loc = ParamNameLoc; |
| 559 | if (!ParamName) |
| 560 | Loc = KeyLoc; |
| 561 | |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 562 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 563 | TemplateTypeParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 564 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 565 | KeyLoc, Loc, Depth, Position, ParamName, |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 566 | Typename, IsParameterPack); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 567 | Param->setAccess(AS_public); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 568 | if (Invalid) |
| 569 | Param->setInvalidDecl(); |
| 570 | |
| 571 | if (ParamName) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 572 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 573 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 574 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 575 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 576 | IdResolver.AddDecl(Param); |
| 577 | } |
| 578 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 579 | // C++0x [temp.param]p9: |
| 580 | // A default template-argument may be specified for any kind of |
| 581 | // template-parameter that is not a template parameter pack. |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 582 | if (DefaultArg && IsParameterPack) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 583 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 584 | DefaultArg = ParsedType(); |
| 585 | } |
| 586 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 587 | // Handle the default argument, if provided. |
| 588 | if (DefaultArg) { |
| 589 | TypeSourceInfo *DefaultTInfo; |
| 590 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 591 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 592 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 593 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 594 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 595 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 596 | UPPC_DefaultArgument)) |
| 597 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 598 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 599 | // Check the template argument itself. |
| 600 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 601 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 602 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 603 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 604 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 605 | Param->setDefaultArgument(DefaultTInfo, false); |
| 606 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 607 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 608 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 611 | /// \brief Check that the type of a non-type template parameter is |
| 612 | /// well-formed. |
| 613 | /// |
| 614 | /// \returns the (possibly-promoted) parameter type if valid; |
| 615 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 616 | QualType |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 617 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a09387d | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 618 | // We don't allow variably-modified types as the type of non-type template |
| 619 | // parameters. |
| 620 | if (T->isVariablyModifiedType()) { |
| 621 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 622 | << T; |
| 623 | return QualType(); |
| 624 | } |
| 625 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 626 | // C++ [temp.param]p4: |
| 627 | // |
| 628 | // A non-type template-parameter shall have one of the following |
| 629 | // (optionally cv-qualified) types: |
| 630 | // |
| 631 | // -- integral or enumeration type, |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 632 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | // -- pointer to object or pointer to function, |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 634 | T->isPointerType() || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | // -- reference to object or reference to function, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 636 | T->isReferenceType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 637 | // -- pointer to member, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 638 | T->isMemberPointerType() || |
Douglas Gregor | 80af313 | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 639 | // -- std::nullptr_t. |
| 640 | T->isNullPtrType() || |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 641 | // If T is a dependent type, we can't do the check now, so we |
| 642 | // assume that it is well-formed. |
Richard Smith | d0e1c95 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 643 | T->isDependentType()) { |
| 644 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 645 | // are ignored when determining its type. |
| 646 | return T.getUnqualifiedType(); |
| 647 | } |
| 648 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 649 | // C++ [temp.param]p8: |
| 650 | // |
| 651 | // A non-type template-parameter of type "array of T" or |
| 652 | // "function returning T" is adjusted to be of type "pointer to |
| 653 | // T" or "pointer to function returning T", respectively. |
| 654 | else if (T->isArrayType()) |
| 655 | // FIXME: Keep the type prior to promotion? |
| 656 | return Context.getArrayDecayedType(T); |
| 657 | else if (T->isFunctionType()) |
| 658 | // FIXME: Keep the type prior to promotion? |
| 659 | return Context.getPointerType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 660 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 661 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 662 | << T; |
| 663 | |
| 664 | return QualType(); |
| 665 | } |
| 666 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 667 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 668 | unsigned Depth, |
| 669 | unsigned Position, |
| 670 | SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 671 | Expr *Default) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 672 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 673 | QualType T = TInfo->getType(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 674 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 675 | assert(S->isTemplateParamScope() && |
| 676 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 677 | bool Invalid = false; |
| 678 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 679 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 680 | if (T.isNull()) { |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 681 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ce0fc86f | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 682 | Invalid = true; |
| 683 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 684 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 685 | IdentifierInfo *ParamName = D.getIdentifier(); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 686 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 687 | NonTypeTemplateParmDecl *Param |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 688 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 689 | D.getLocStart(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 690 | D.getIdentifierLoc(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 691 | Depth, Position, ParamName, T, |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 692 | IsParameterPack, TInfo); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 693 | Param->setAccess(AS_public); |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 694 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 695 | if (Invalid) |
| 696 | Param->setInvalidDecl(); |
| 697 | |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 698 | if (ParamName) { |
| 699 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 700 | ParamName); |
| 701 | |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 702 | // Add the template parameter into the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 703 | S->AddDecl(Param); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 704 | IdResolver.AddDecl(Param); |
| 705 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 706 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 707 | // C++0x [temp.param]p9: |
| 708 | // A default template-argument may be specified for any kind of |
| 709 | // template-parameter that is not a template parameter pack. |
| 710 | if (Default && IsParameterPack) { |
| 711 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 712 | Default = nullptr; |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 715 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 716 | if (Default) { |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 717 | // Check for unexpanded parameter packs. |
| 718 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 719 | return Param; |
| 720 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 721 | TemplateArgument Converted; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 722 | ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
| 723 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 724 | Param->setInvalidDecl(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 725 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 726 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 727 | Default = DefaultRes.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 728 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 729 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 730 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 731 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 732 | return Param; |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 733 | } |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 734 | |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 735 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 736 | /// parameter (e.g. T in template <template \<typename> class T> class array) |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 737 | /// has been parsed. S is the current scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 738 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 739 | SourceLocation TmpLoc, |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 740 | TemplateParameterList *Params, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 741 | SourceLocation EllipsisLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 742 | IdentifierInfo *Name, |
| 743 | SourceLocation NameLoc, |
| 744 | unsigned Depth, |
| 745 | unsigned Position, |
| 746 | SourceLocation EqualLoc, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 747 | ParsedTemplateArgument Default) { |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 748 | assert(S->isTemplateParamScope() && |
| 749 | "Template template parameter not in template parameter scope!"); |
| 750 | |
| 751 | // Construct the parameter object. |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 752 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 753 | TemplateTemplateParmDecl *Param = |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 754 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 755 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 756 | Depth, Position, IsParameterPack, |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 757 | Name, Params); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 758 | Param->setAccess(AS_public); |
| 759 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 760 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 761 | // into the scope and lookup mechanisms. |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 762 | if (Name) { |
Richard Smith | b80d540 | 2013-06-25 22:21:36 +0000 | [diff] [blame] | 763 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 764 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 765 | S->AddDecl(Param); |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 766 | IdResolver.AddDecl(Param); |
| 767 | } |
| 768 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 769 | if (Params->size() == 0) { |
| 770 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 771 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 772 | Param->setInvalidDecl(); |
| 773 | } |
| 774 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 775 | // C++0x [temp.param]p9: |
| 776 | // A default template-argument may be specified for any kind of |
| 777 | // template-parameter that is not a template parameter pack. |
| 778 | if (IsParameterPack && !Default.isInvalid()) { |
| 779 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 780 | Default = ParsedTemplateArgument(); |
| 781 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 782 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 783 | if (!Default.isInvalid()) { |
| 784 | // Check only that we have a template template argument. We don't want to |
| 785 | // try to check well-formedness now, because our template template parameter |
| 786 | // might have dependent types in its template parameters, which we wouldn't |
| 787 | // be able to match now. |
| 788 | // |
| 789 | // If none of the template template parameter's template arguments mention |
| 790 | // other template parameters, we could actually perform more checking here. |
| 791 | // However, it isn't worth doing. |
| 792 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 793 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 794 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 795 | << DefaultArg.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 796 | return Param; |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 797 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 798 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 799 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 800 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 801 | DefaultArg.getArgument().getAsTemplate(), |
| 802 | UPPC_DefaultArgument)) |
| 803 | return Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 804 | |
Douglas Gregor | dc13ded | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 805 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 806 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 807 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 808 | return Param; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 811 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 812 | /// contains the template parameters in Params/NumParams. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 813 | TemplateParameterList * |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 814 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 815 | SourceLocation ExportLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | SourceLocation TemplateLoc, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 817 | SourceLocation LAngleLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 818 | Decl **Params, unsigned NumParams, |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 819 | SourceLocation RAngleLoc) { |
| 820 | if (ExportLoc.isValid()) |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 821 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 822 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 823 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 824 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 825 | RAngleLoc); |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 826 | } |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 827 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 828 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 829 | if (SS.isSet()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 830 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 831 | } |
| 832 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 833 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 834 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 835 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 836 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 837 | AttributeList *Attr, |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 838 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 839 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 840 | SourceLocation FriendLoc, |
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 | |
Richard Smith | 234ff47 | 2014-08-23 00:49:01 +0000 | [diff] [blame] | 1107 | if (TUK != TUK_Friend) { |
| 1108 | // Per C++ [basic.scope.temp]p2, skip the template parameter scopes. |
| 1109 | Scope *Outer = S; |
| 1110 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1111 | Outer = Outer->getParent(); |
| 1112 | PushOnScopeChains(NewTemplate, Outer); |
| 1113 | } else { |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1114 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1115 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1116 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1117 | } |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1118 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1119 | NewTemplate->setObjectOfFriendDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1120 | |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1121 | // Friend templates are visible in fairly strange ways. |
| 1122 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1123 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1124 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1125 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1126 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1127 | /* AddToContext = */ false); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1128 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1129 | |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 1130 | FriendDecl *Friend = FriendDecl::Create( |
| 1131 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
Douglas Gregor | 3dad842 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1132 | Friend->setAccess(AS_public); |
| 1133 | CurContext->addDecl(Friend); |
John McCall | 27b5c25 | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1134 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1135 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1136 | if (Invalid) { |
| 1137 | NewTemplate->setInvalidDecl(); |
| 1138 | NewClass->setInvalidDecl(); |
| 1139 | } |
Rafael Espindola | eca5cd2 | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 1140 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1141 | ActOnDocumentableDecl(NewTemplate); |
| 1142 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1143 | return NewTemplate; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1146 | /// \brief Diagnose the presence of a default template argument on a |
| 1147 | /// template parameter, which is ill-formed in certain contexts. |
| 1148 | /// |
| 1149 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1150 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1151 | Sema::TemplateParamListContext TPC, |
| 1152 | SourceLocation ParamLoc, |
| 1153 | SourceRange DefArgRange) { |
| 1154 | switch (TPC) { |
| 1155 | case Sema::TPC_ClassTemplate: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1156 | case Sema::TPC_VarTemplate: |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1157 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1158 | return false; |
| 1159 | |
| 1160 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1161 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1162 | // C++ [temp.param]p9: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1163 | // A default template-argument shall not be specified in a |
| 1164 | // function template declaration or a function template |
| 1165 | // definition [...] |
Douglas Gregor | a99fb4c | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1166 | // If a friend function template declaration specifies a default |
| 1167 | // template-argument, that declaration shall be a definition and shall be |
| 1168 | // the only declaration of the function template in the translation unit. |
| 1169 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1170 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1171 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1172 | : diag::ext_template_parameter_default_in_function_template) |
| 1173 | << DefArgRange; |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1174 | return false; |
| 1175 | |
| 1176 | case Sema::TPC_ClassTemplateMember: |
| 1177 | // C++0x [temp.param]p9: |
| 1178 | // A default template-argument shall not be specified in the |
| 1179 | // template-parameter-lists of the definition of a member of a |
| 1180 | // class template that appears outside of the member's class. |
| 1181 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1182 | << DefArgRange; |
| 1183 | return true; |
| 1184 | |
David Majnemer | ba8f17a | 2013-06-25 22:08:55 +0000 | [diff] [blame] | 1185 | case Sema::TPC_FriendClassTemplate: |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1186 | case Sema::TPC_FriendFunctionTemplate: |
| 1187 | // C++ [temp.param]p9: |
| 1188 | // A default template-argument shall not be specified in a |
| 1189 | // friend template declaration. |
| 1190 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1191 | << DefArgRange; |
| 1192 | return true; |
| 1193 | |
| 1194 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1195 | // for friend function templates if there is only a single |
| 1196 | // declaration (and it is a definition). Strange! |
| 1197 | } |
| 1198 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1199 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1202 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1203 | /// of a template template parameter, recursively. |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1204 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1205 | TemplateTemplateParmDecl *TTP) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1206 | // A template template parameter which is a parameter pack is also a pack |
| 1207 | // expansion. |
| 1208 | if (TTP->isParameterPack()) |
| 1209 | return false; |
| 1210 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1211 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1212 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1213 | NamedDecl *P = Params->getParam(I); |
| 1214 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1215 | if (!NTTP->isParameterPack() && |
| 1216 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1217 | NTTP->getTypeSourceInfo(), |
| 1218 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1219 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1220 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1221 | continue; |
| 1222 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1223 | |
| 1224 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1225 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1226 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1227 | return true; |
| 1228 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1229 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1230 | return false; |
| 1231 | } |
| 1232 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1233 | /// \brief Checks the validity of a template parameter list, possibly |
| 1234 | /// considering the template parameter list from a previous |
| 1235 | /// declaration. |
| 1236 | /// |
| 1237 | /// If an "old" template parameter list is provided, it must be |
| 1238 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1239 | /// template parameter list. |
| 1240 | /// |
| 1241 | /// \param NewParams Template parameter list for a new template |
| 1242 | /// declaration. This template parameter list will be updated with any |
| 1243 | /// default arguments that are carried through from the previous |
| 1244 | /// template parameter list. |
| 1245 | /// |
| 1246 | /// \param OldParams If provided, template parameter list from a |
| 1247 | /// previous declaration of the same template. Default template |
| 1248 | /// arguments will be merged from the old template parameter list to |
| 1249 | /// the new template parameter list. |
| 1250 | /// |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1251 | /// \param TPC Describes the context in which we are checking the given |
| 1252 | /// template parameter list. |
| 1253 | /// |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1254 | /// \returns true if an error occurred, false otherwise. |
| 1255 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1256 | TemplateParameterList *OldParams, |
| 1257 | TemplateParamListContext TPC) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1258 | bool Invalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1260 | // C++ [temp.param]p10: |
| 1261 | // The set of default template-arguments available for use with a |
| 1262 | // template declaration or definition is obtained by merging the |
| 1263 | // default arguments from the definition (if in scope) and all |
| 1264 | // declarations in scope in the same way default function |
| 1265 | // arguments are (8.3.6). |
| 1266 | bool SawDefaultArgument = false; |
| 1267 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1268 | |
Mike Stump | c89c8e3 | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1269 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 5bd22da | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1270 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1271 | if (OldParams) |
| 1272 | OldParam = OldParams->begin(); |
| 1273 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1274 | bool RemoveDefaultArguments = false; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1275 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1276 | NewParamEnd = NewParams->end(); |
| 1277 | NewParam != NewParamEnd; ++NewParam) { |
| 1278 | // Variables used to diagnose redundant default arguments |
| 1279 | bool RedundantDefaultArg = false; |
| 1280 | SourceLocation OldDefaultLoc; |
| 1281 | SourceLocation NewDefaultLoc; |
| 1282 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1283 | // Variable used to diagnose missing default arguments |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1284 | bool MissingDefaultArg = false; |
| 1285 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1286 | // Variable used to diagnose non-final parameter packs |
| 1287 | bool SawParameterPack = false; |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1288 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1289 | if (TemplateTypeParmDecl *NewTypeParm |
| 1290 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1291 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1292 | if (NewTypeParm->hasDefaultArgument() && |
| 1293 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1294 | NewTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1295 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1296 | .getSourceRange())) |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1297 | NewTypeParm->removeDefaultArgument(); |
| 1298 | |
| 1299 | // Merge default arguments for template type parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1300 | TemplateTypeParmDecl *OldTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1301 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1302 | |
Anders Carlsson | 327865d | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1303 | if (NewTypeParm->isParameterPack()) { |
| 1304 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1305 | "Parameter packs can't have a default argument!"); |
| 1306 | SawParameterPack = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1307 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1308 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1309 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1310 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1311 | SawDefaultArgument = true; |
| 1312 | RedundantDefaultArg = true; |
| 1313 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1314 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1315 | // Merge the default argument from the old declaration to the |
| 1316 | // new declaration. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1317 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1318 | true); |
| 1319 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1320 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1321 | SawDefaultArgument = true; |
| 1322 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1323 | } else if (SawDefaultArgument) |
| 1324 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1325 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1326 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1327 | // Check for unexpanded parameter packs. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1328 | if (!NewNonTypeParm->isParameterPack() && |
| 1329 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1330 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1331 | UPPC_NonTypeTemplateParameterType)) { |
| 1332 | Invalid = true; |
| 1333 | continue; |
| 1334 | } |
| 1335 | |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1336 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1337 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1338 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1339 | NewNonTypeParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1340 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1341 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1344 | // Merge default arguments for non-type template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1345 | NonTypeTemplateParmDecl *OldNonTypeParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1346 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1347 | if (NewNonTypeParm->isParameterPack()) { |
| 1348 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1349 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1350 | if (!NewNonTypeParm->isPackExpansion()) |
| 1351 | SawParameterPack = true; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1352 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Richard Smith | 35828f1 | 2013-07-22 03:31:14 +0000 | [diff] [blame] | 1353 | NewNonTypeParm->hasDefaultArgument()) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1354 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1355 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1356 | SawDefaultArgument = true; |
| 1357 | RedundantDefaultArg = true; |
| 1358 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1359 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1360 | // Merge the default argument from the old declaration to the |
| 1361 | // new declaration. |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1362 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1363 | // expression that points to a previous non-type template |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1364 | // parameter. |
| 1365 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1366 | OldNonTypeParm->getDefaultArgument(), |
| 1367 | /*Inherited=*/ true); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1368 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1369 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1370 | SawDefaultArgument = true; |
| 1371 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1372 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1373 | MissingDefaultArg = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1374 | } else { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1375 | TemplateTemplateParmDecl *NewTemplateParm |
| 1376 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1377 | |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1378 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1379 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 38ee75e | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1380 | Invalid = true; |
| 1381 | continue; |
| 1382 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1383 | |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1384 | // Check the presence of a default argument here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1385 | if (NewTemplateParm->hasDefaultArgument() && |
| 1386 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1387 | NewTemplateParm->getLocation(), |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1388 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1389 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1390 | |
| 1391 | // Merge default arguments for template template parameters |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1392 | TemplateTemplateParmDecl *OldTemplateParm |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1393 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1394 | if (NewTemplateParm->isParameterPack()) { |
| 1395 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1396 | "Parameter packs can't have a default argument!"); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1397 | if (!NewTemplateParm->isPackExpansion()) |
| 1398 | SawParameterPack = true; |
Douglas Gregor | 0018cdc | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1399 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1400 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1401 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1402 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1403 | SawDefaultArgument = true; |
| 1404 | RedundantDefaultArg = true; |
| 1405 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1406 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1407 | // Merge the default argument from the old declaration to the |
| 1408 | // new declaration. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1409 | // FIXME: We need to create a new kind of "default argument" expression |
| 1410 | // that points to a previous template template parameter. |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1411 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1412 | OldTemplateParm->getDefaultArgument(), |
| 1413 | /*Inherited=*/ true); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1414 | PreviousDefaultArgLoc |
| 1415 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1416 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1417 | SawDefaultArgument = true; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1418 | PreviousDefaultArgLoc |
| 1419 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1420 | } else if (SawDefaultArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1421 | MissingDefaultArg = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1424 | // C++11 [temp.param]p11: |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1425 | // If a template parameter of a primary class template or alias template |
| 1426 | // is a template parameter pack, it shall be the last template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1427 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1428 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 1429 | TPC == TPC_TypeAliasTemplate)) { |
David Blaikie | 651c73c | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1430 | Diag((*NewParam)->getLocation(), |
| 1431 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1432 | Invalid = true; |
| 1433 | } |
| 1434 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1435 | if (RedundantDefaultArg) { |
| 1436 | // C++ [temp.param]p12: |
| 1437 | // A template-parameter shall not be given default arguments |
| 1438 | // by two different declarations in the same scope. |
| 1439 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1440 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1441 | Invalid = true; |
Douglas Gregor | 8b481d8 | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1442 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1443 | // C++ [temp.param]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1444 | // If a template-parameter of a class template has a default |
| 1445 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | 7dba51f | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1446 | // have a default template-argument supplied or be a template parameter |
| 1447 | // pack. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1449 | diag::err_template_param_default_arg_missing); |
| 1450 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1451 | Invalid = true; |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1452 | RemoveDefaultArguments = true; |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | // If we have an old template parameter list that we're merging |
| 1456 | // in, move on to the next parameter. |
| 1457 | if (OldParams) |
| 1458 | ++OldParam; |
| 1459 | } |
| 1460 | |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1461 | // We were missing some default arguments at the end of the list, so remove |
| 1462 | // all of the default arguments. |
| 1463 | if (RemoveDefaultArguments) { |
| 1464 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1465 | NewParamEnd = NewParams->end(); |
| 1466 | NewParam != NewParamEnd; ++NewParam) { |
| 1467 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1468 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1469 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0693def | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1470 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1471 | NTTP->removeDefaultArgument(); |
| 1472 | else |
| 1473 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1474 | } |
| 1475 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1476 | |
Douglas Gregor | dba3263 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1477 | return Invalid; |
| 1478 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1479 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1480 | namespace { |
| 1481 | |
| 1482 | /// A class which looks for a use of a certain level of template |
| 1483 | /// parameter. |
| 1484 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1485 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1486 | |
| 1487 | unsigned Depth; |
| 1488 | bool Match; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1489 | SourceLocation MatchLoc; |
| 1490 | |
| 1491 | DependencyChecker(unsigned Depth) : Depth(Depth), Match(false) {} |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1492 | |
| 1493 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1494 | NamedDecl *ND = Params->getParam(0); |
| 1495 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1496 | Depth = PD->getDepth(); |
| 1497 | } else if (NonTypeTemplateParmDecl *PD = |
| 1498 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1499 | Depth = PD->getDepth(); |
| 1500 | } else { |
| 1501 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1502 | } |
| 1503 | } |
| 1504 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1505 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1506 | if (ParmDepth >= Depth) { |
| 1507 | Match = true; |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1508 | MatchLoc = Loc; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1509 | return true; |
| 1510 | } |
| 1511 | return false; |
| 1512 | } |
| 1513 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1514 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1515 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 1516 | } |
| 1517 | |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1518 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1519 | return !Matches(T->getDepth()); |
| 1520 | } |
| 1521 | |
| 1522 | bool TraverseTemplateName(TemplateName N) { |
| 1523 | if (TemplateTemplateParmDecl *PD = |
| 1524 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1525 | if (Matches(PD->getDepth())) |
| 1526 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1527 | return super::TraverseTemplateName(N); |
| 1528 | } |
| 1529 | |
| 1530 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1531 | if (NonTypeTemplateParmDecl *PD = |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1532 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 1533 | if (Matches(PD->getDepth(), E->getExprLoc())) |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1534 | return false; |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1535 | return super::VisitDeclRefExpr(E); |
| 1536 | } |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 1537 | |
| 1538 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 1539 | return TraverseType(T->getReplacementType()); |
| 1540 | } |
| 1541 | |
| 1542 | bool |
| 1543 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 1544 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 1545 | } |
| 1546 | |
Douglas Gregor | a6a7e3c | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1547 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1548 | return TraverseType(T->getInjectedSpecializationType()); |
| 1549 | } |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1550 | }; |
| 1551 | } |
| 1552 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1553 | /// Determines whether a given type depends on the given parameter |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1554 | /// list. |
| 1555 | static bool |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1556 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1557 | DependencyChecker Checker(Params); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1558 | Checker.TraverseType(T); |
John McCall | a020a01 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1559 | return Checker.Match; |
| 1560 | } |
| 1561 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1562 | // Find the source range corresponding to the named type in the given |
| 1563 | // nested-name-specifier, if any. |
| 1564 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1565 | QualType T, |
| 1566 | const CXXScopeSpec &SS) { |
| 1567 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1568 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1569 | if (const Type *CurType = NNS->getAsType()) { |
| 1570 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1571 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1572 | } else |
| 1573 | break; |
| 1574 | |
| 1575 | NNSLoc = NNSLoc.getPrefix(); |
| 1576 | } |
| 1577 | |
| 1578 | return SourceRange(); |
| 1579 | } |
| 1580 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1581 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1582 | /// specifier, returning the template parameter list that applies to the |
| 1583 | /// name. |
| 1584 | /// |
| 1585 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1586 | /// specifier or a template parameter list. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1587 | /// |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1588 | /// \param DeclLoc The location of the declaration itself. |
| 1589 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1590 | /// \param SS the scope specifier that will be matched to the given template |
| 1591 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1592 | /// being declared. |
| 1593 | /// |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1594 | /// \param TemplateId The template-id following the scope specifier, if there |
| 1595 | /// is one. Used to check for a missing 'template<>'. |
| 1596 | /// |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1597 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1598 | /// innermost template parameter lists. |
| 1599 | /// |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1600 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1601 | /// matching template parameters to scope specifiers in friend |
| 1602 | /// declarations. |
| 1603 | /// |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1604 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1605 | /// declared is an explicit specialization, false otherwise. |
| 1606 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1608 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1609 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1610 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1611 | /// 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] | 1612 | /// itself a template). |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1613 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 1614 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1615 | TemplateIdAnnotation *TemplateId, |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1616 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
| 1617 | bool &IsExplicitSpecialization, bool &Invalid) { |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1618 | IsExplicitSpecialization = false; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1619 | Invalid = false; |
| 1620 | |
| 1621 | // The sequence of nested types to which we will match up the template |
| 1622 | // parameter lists. We first build this list by starting with the type named |
| 1623 | // 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] | 1624 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1625 | QualType T; |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1626 | if (SS.getScopeRep()) { |
| 1627 | if (CXXRecordDecl *Record |
| 1628 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1629 | T = Context.getTypeDeclType(Record); |
| 1630 | else |
| 1631 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1632 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1633 | |
| 1634 | // If we found an explicit specialization that prevents us from needing |
| 1635 | // 'template<>' headers, this will be set to the location of that |
| 1636 | // explicit specialization. |
| 1637 | SourceLocation ExplicitSpecLoc; |
| 1638 | |
| 1639 | while (!T.isNull()) { |
| 1640 | NestedTypes.push_back(T); |
| 1641 | |
| 1642 | // Retrieve the parent of a record type. |
| 1643 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1644 | // If this type is an explicit specialization, we're done. |
| 1645 | if (ClassTemplateSpecializationDecl *Spec |
| 1646 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1647 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1648 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1649 | ExplicitSpecLoc = Spec->getLocation(); |
| 1650 | break; |
Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1651 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1652 | } else if (Record->getTemplateSpecializationKind() |
| 1653 | == TSK_ExplicitSpecialization) { |
| 1654 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1655 | break; |
| 1656 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1657 | |
| 1658 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1659 | T = Context.getTypeDeclType(Parent); |
| 1660 | else |
| 1661 | T = QualType(); |
| 1662 | continue; |
| 1663 | } |
| 1664 | |
| 1665 | if (const TemplateSpecializationType *TST |
| 1666 | = T->getAs<TemplateSpecializationType>()) { |
| 1667 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1668 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1669 | T = Context.getTypeDeclType(Parent); |
| 1670 | else |
| 1671 | T = QualType(); |
| 1672 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1673 | } |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | // Look one step prior in a dependent template specialization type. |
| 1677 | if (const DependentTemplateSpecializationType *DependentTST |
| 1678 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1679 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1680 | T = QualType(NNS->getAsType(), 0); |
| 1681 | else |
| 1682 | T = QualType(); |
| 1683 | continue; |
| 1684 | } |
| 1685 | |
| 1686 | // Look one step prior in a dependent name type. |
| 1687 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1688 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1689 | T = QualType(NNS->getAsType(), 0); |
| 1690 | else |
| 1691 | T = QualType(); |
| 1692 | continue; |
| 1693 | } |
| 1694 | |
| 1695 | // Retrieve the parent of an enumeration type. |
| 1696 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1697 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1698 | // check here. |
| 1699 | EnumDecl *Enum = EnumT->getDecl(); |
| 1700 | |
| 1701 | // Get to the parent type. |
| 1702 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1703 | T = Context.getTypeDeclType(Parent); |
| 1704 | else |
| 1705 | T = QualType(); |
| 1706 | continue; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1707 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1708 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1709 | T = QualType(); |
| 1710 | } |
| 1711 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1712 | // to the innermost while checking template-parameter-lists. |
| 1713 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1714 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1715 | // C++0x [temp.expl.spec]p17: |
| 1716 | // A member or a member template may be nested within many |
| 1717 | // enclosing class templates. In an explicit specialization for |
| 1718 | // such a member, the member declaration shall be preceded by a |
| 1719 | // template<> for each enclosing class template that is |
| 1720 | // explicitly specialized. |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1721 | bool SawNonEmptyTemplateParameterList = false; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1722 | |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1723 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1724 | if (SawNonEmptyTemplateParameterList) { |
| 1725 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1726 | << !Recovery << Range; |
| 1727 | Invalid = true; |
| 1728 | IsExplicitSpecialization = false; |
| 1729 | return true; |
| 1730 | } |
| 1731 | |
| 1732 | return false; |
| 1733 | }; |
| 1734 | |
| 1735 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 1736 | // Check that we can have an explicit specialization here. |
| 1737 | if (CheckExplicitSpecialization(Range, true)) |
| 1738 | return true; |
| 1739 | |
| 1740 | // We don't have a template header, but we should. |
| 1741 | SourceLocation ExpectedTemplateLoc; |
| 1742 | if (!ParamLists.empty()) |
| 1743 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1744 | else |
| 1745 | ExpectedTemplateLoc = DeclStartLoc; |
| 1746 | |
| 1747 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1748 | << Range |
| 1749 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1750 | return false; |
| 1751 | }; |
| 1752 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1753 | unsigned ParamIdx = 0; |
| 1754 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1755 | ++TypeIdx) { |
| 1756 | T = NestedTypes[TypeIdx]; |
| 1757 | |
| 1758 | // Whether we expect a 'template<>' header. |
| 1759 | bool NeedEmptyTemplateHeader = false; |
| 1760 | |
| 1761 | // Whether we expect a template header with parameters. |
| 1762 | bool NeedNonemptyTemplateHeader = false; |
| 1763 | |
| 1764 | // For a dependent type, the set of template parameters that we |
| 1765 | // expect to see. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1766 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1767 | |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1768 | // C++0x [temp.expl.spec]p15: |
| 1769 | // A member or a member template may be nested within many enclosing |
| 1770 | // class templates. In an explicit specialization for such a member, the |
| 1771 | // member declaration shall be preceded by a template<> for each |
| 1772 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1773 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1774 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1775 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1776 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1777 | NeedNonemptyTemplateHeader = true; |
| 1778 | } else if (Record->isDependentType()) { |
| 1779 | if (Record->getDescribedClassTemplate()) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1780 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1781 | ->getTemplateParameters(); |
| 1782 | NeedNonemptyTemplateHeader = true; |
| 1783 | } |
| 1784 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1785 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1786 | // C++0x [temp.expl.spec]p4: |
| 1787 | // Members of an explicitly specialized class template are defined |
| 1788 | // in the same manner as members of normal classes, and not using |
| 1789 | // the template<> syntax. |
| 1790 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1791 | NeedEmptyTemplateHeader = true; |
| 1792 | else |
Douglas Gregor | b32e825 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1793 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1794 | } else if (Record->getTemplateSpecializationKind()) { |
| 1795 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 373af9b | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1796 | != TSK_ExplicitSpecialization && |
| 1797 | TypeIdx == NumTypes - 1) |
| 1798 | IsExplicitSpecialization = true; |
| 1799 | |
| 1800 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1801 | } |
| 1802 | } else if (const TemplateSpecializationType *TST |
| 1803 | = T->getAs<TemplateSpecializationType>()) { |
| 1804 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1805 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1806 | NeedNonemptyTemplateHeader = true; |
| 1807 | } |
| 1808 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1809 | // FIXME: We actually could/should check the template arguments here |
| 1810 | // against the corresponding template parameter list. |
| 1811 | NeedNonemptyTemplateHeader = false; |
| 1812 | } |
| 1813 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1814 | // C++ [temp.expl.spec]p16: |
| 1815 | // In an explicit specialization declaration for a member of a class |
| 1816 | // template or a member template that ap- pears in namespace scope, the |
| 1817 | // member template and some of its enclosing class templates may remain |
| 1818 | // unspecialized, except that the declaration shall not explicitly |
| 1819 | // specialize a class member template if its en- closing class templates |
| 1820 | // are not explicitly specialized as well. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1821 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1822 | if (ParamLists[ParamIdx]->size() == 0) { |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1823 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 1824 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1825 | return nullptr; |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1826 | } else |
| 1827 | SawNonEmptyTemplateParameterList = true; |
| 1828 | } |
| 1829 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1830 | if (NeedEmptyTemplateHeader) { |
| 1831 | // If we're on the last of the types, and we need a 'template<>' header |
| 1832 | // here, then it's an explicit specialization. |
| 1833 | if (TypeIdx == NumTypes - 1) |
| 1834 | IsExplicitSpecialization = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1835 | |
| 1836 | if (ParamIdx < ParamLists.size()) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1837 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1838 | // The header has template parameters when it shouldn't. Complain. |
| 1839 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1840 | diag::err_template_param_list_matches_nontemplate) |
| 1841 | << T |
| 1842 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1843 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1844 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1845 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1846 | return nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1847 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1848 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1849 | // Consume this template header. |
| 1850 | ++ParamIdx; |
| 1851 | continue; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1852 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1853 | |
| 1854 | if (!IsFriend) |
| 1855 | if (DiagnoseMissingExplicitSpecialization( |
| 1856 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1857 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1858 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1859 | continue; |
| 1860 | } |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1861 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1862 | if (NeedNonemptyTemplateHeader) { |
| 1863 | // In friend declarations we can have template-ids which don't |
| 1864 | // depend on the corresponding template parameter lists. But |
| 1865 | // assume that empty parameter lists are supposed to match this |
| 1866 | // template-id. |
| 1867 | if (IsFriend && T->isDependentType()) { |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1868 | if (ParamIdx < ParamLists.size() && |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1869 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1870 | ExpectedTemplateParams = nullptr; |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1871 | else |
| 1872 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1873 | } |
Douglas Gregor | ed5731f | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1874 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1875 | if (ParamIdx < ParamLists.size()) { |
| 1876 | // Check the template parameter list, if we can. |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1877 | if (ExpectedTemplateParams && |
| 1878 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1879 | ExpectedTemplateParams, |
| 1880 | true, TPL_TemplateMatch)) |
| 1881 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1882 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1883 | if (!Invalid && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1884 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1885 | TPC_ClassTemplateMember)) |
| 1886 | Invalid = true; |
| 1887 | |
| 1888 | ++ParamIdx; |
| 1889 | continue; |
| 1890 | } |
| 1891 | |
| 1892 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1893 | << T |
| 1894 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1895 | Invalid = true; |
| 1896 | continue; |
| 1897 | } |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1898 | } |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1899 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1900 | // If there were at least as many template-ids as there were template |
| 1901 | // parameter lists, then there are no template parameter lists remaining for |
| 1902 | // the declaration itself. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1903 | if (ParamIdx >= ParamLists.size()) { |
| 1904 | if (TemplateId && !IsFriend) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1905 | // We don't have a template header for the declaration itself, but we |
| 1906 | // should. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1907 | IsExplicitSpecialization = true; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1908 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 1909 | TemplateId->RAngleLoc)); |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1910 | |
| 1911 | // Fabricate an empty template parameter list for the invented header. |
| 1912 | return TemplateParameterList::Create(Context, SourceLocation(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1913 | SourceLocation(), nullptr, 0, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1914 | SourceLocation()); |
| 1915 | } |
| 1916 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1917 | return nullptr; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 1918 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1919 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1920 | // If there were too many template parameter lists, complain about that now. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1921 | if (ParamIdx < ParamLists.size() - 1) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1922 | bool HasAnyExplicitSpecHeader = false; |
| 1923 | bool AllExplicitSpecHeaders = true; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1924 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1925 | if (ParamLists[I]->size() == 0) |
| 1926 | HasAnyExplicitSpecHeader = true; |
| 1927 | else |
| 1928 | AllExplicitSpecHeaders = false; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1929 | } |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1930 | |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1931 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1932 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 1933 | : diag::err_template_spec_extra_headers) |
| 1934 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1935 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
Douglas Gregor | 972fe53 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1936 | |
| 1937 | // If there was a specialization somewhere, such that 'template<>' is |
| 1938 | // not required, and there were any 'template<>' headers, note where the |
| 1939 | // specialization occurred. |
| 1940 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1941 | Diag(ExplicitSpecLoc, |
| 1942 | diag::note_explicit_template_spec_does_not_need_header) |
| 1943 | << NestedTypes.back(); |
| 1944 | |
| 1945 | // We have a template parameter list with no corresponding scope, which |
| 1946 | // means that the resulting template declaration can't be instantiated |
| 1947 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1948 | if (!AllExplicitSpecHeaders) |
| 1949 | Invalid = true; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1950 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1951 | |
Douglas Gregor | 522d5eb | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1952 | // C++ [temp.expl.spec]p16: |
| 1953 | // In an explicit specialization declaration for a member of a class |
| 1954 | // template or a member template that ap- pears in namespace scope, the |
| 1955 | // member template and some of its enclosing class templates may remain |
| 1956 | // unspecialized, except that the declaration shall not explicitly |
| 1957 | // specialize a class member template if its en- closing class templates |
| 1958 | // are not explicitly specialized as well. |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1959 | if (ParamLists.back()->size() == 0 && |
NAKAMURA Takumi | de4077a | 2014-04-17 08:57:09 +0000 | [diff] [blame] | 1960 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 1961 | false)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1962 | return nullptr; |
Richard Smith | 11a80dc | 2014-04-17 03:52:20 +0000 | [diff] [blame] | 1963 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1964 | // Return the last template parameter list, which corresponds to the |
| 1965 | // entity being declared. |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 1966 | return ParamLists.back(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1969 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1970 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1971 | Diag(Template->getLocation(), diag::note_template_declared_here) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1972 | << (isa<FunctionTemplateDecl>(Template) |
| 1973 | ? 0 |
| 1974 | : isa<ClassTemplateDecl>(Template) |
| 1975 | ? 1 |
| 1976 | : isa<VarTemplateDecl>(Template) |
| 1977 | ? 2 |
| 1978 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 1979 | << Template->getDeclName(); |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1980 | return; |
| 1981 | } |
| 1982 | |
| 1983 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1984 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1985 | IEnd = OST->end(); |
| 1986 | I != IEnd; ++I) |
| 1987 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1988 | << 0 << (*I)->getDeclName(); |
| 1989 | |
| 1990 | return; |
| 1991 | } |
| 1992 | } |
| 1993 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1994 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1995 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1996 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1997 | DependentTemplateName *DTN |
| 1998 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1999 | if (DTN && DTN->isIdentifier()) |
| 2000 | // When building a template-id where the template-name is dependent, |
| 2001 | // assume the template is a type template. Either our assumption is |
| 2002 | // correct, or the code is ill-formed and will be diagnosed when the |
| 2003 | // dependent name is substituted. |
| 2004 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 2005 | DTN->getQualifier(), |
| 2006 | DTN->getIdentifier(), |
| 2007 | TemplateArgs); |
| 2008 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2009 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Richard Smith | 8f65806 | 2013-12-04 00:56:29 +0000 | [diff] [blame] | 2010 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
| 2011 | isa<VarTemplateDecl>(Template)) { |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2012 | // We might have a substituted template template parameter pack. If so, |
| 2013 | // build a template specialization type for it. |
| 2014 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 2015 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2016 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 2017 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 2018 | << Name; |
| 2019 | NoteAllFoundTemplates(Name); |
| 2020 | return QualType(); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2021 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2022 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2023 | // Check that the template argument list is well-formed for this |
| 2024 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2025 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2026 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2027 | false, Converted)) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2028 | return QualType(); |
| 2029 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2030 | QualType CanonType; |
| 2031 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2032 | bool InstantiationDependent = false; |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2033 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 2034 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2035 | // Find the canonical type for this type alias template specialization. |
| 2036 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 2037 | if (Pattern->isInvalidDecl()) |
| 2038 | return QualType(); |
| 2039 | |
| 2040 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2041 | Converted.data(), Converted.size()); |
| 2042 | |
| 2043 | // Only substitute for the innermost template argument list. |
| 2044 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 0c4a34b | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 2045 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | 5e96d83 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 2046 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 2047 | for (unsigned I = 0; I < Depth; ++I) |
Richard Smith | 841d8b2 | 2013-05-17 03:04:50 +0000 | [diff] [blame] | 2048 | TemplateArgLists.addOuterTemplateArguments(None); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2049 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2050 | LocalInstantiationScope Scope(*this); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2051 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2052 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 2053 | return QualType(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2054 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2055 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 2056 | TemplateArgLists, AliasTemplate->getLocation(), |
| 2057 | AliasTemplate->getDeclName()); |
| 2058 | if (CanonType.isNull()) |
| 2059 | return QualType(); |
| 2060 | } else if (Name.isDependent() || |
| 2061 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2062 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2063 | // This class template specialization is a dependent |
| 2064 | // type. Therefore, its canonical type is another class template |
| 2065 | // specialization type that contains all of the converted |
| 2066 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 2067 | // A<T, T> have identical types when A is declared as: |
| 2068 | // |
| 2069 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2070 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2071 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2072 | Converted.data(), |
| 2073 | Converted.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2074 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2075 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2076 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2077 | // In the future, we need to teach getTemplateSpecializationType to only |
| 2078 | // build the canonical type and return that to us. |
| 2079 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2080 | |
| 2081 | // This might work out to be a current instantiation, in which |
| 2082 | // case the canonical type needs to be the InjectedClassNameType. |
| 2083 | // |
| 2084 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2085 | // changes to CurContext don't change the set of current |
| 2086 | // instantiations. |
| 2087 | if (isa<ClassTemplateDecl>(Template)) { |
| 2088 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2089 | // If we get out to a namespace, we're done. |
| 2090 | if (Ctx->isFileContext()) break; |
| 2091 | |
| 2092 | // If this isn't a record, keep looking. |
| 2093 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2094 | if (!Record) continue; |
| 2095 | |
| 2096 | // Look for one of the two cases with InjectedClassNameTypes |
| 2097 | // and check whether it's the same template. |
| 2098 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2099 | !Record->getDescribedClassTemplate()) |
| 2100 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2101 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2102 | // Fetch the injected class name type and check whether its |
| 2103 | // injected type is equal to the type we just built. |
| 2104 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2105 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2106 | ->getInjectedSpecializationType(); |
| 2107 | |
| 2108 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2109 | continue; |
| 2110 | |
| 2111 | // If so, the canonical type of this TST is the injected |
| 2112 | // class name type of the record we just found. |
| 2113 | assert(ICNT.isCanonical()); |
| 2114 | CanonType = ICNT; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2115 | break; |
| 2116 | } |
| 2117 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2118 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2119 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2120 | // Find the class template specialization declaration that |
| 2121 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2122 | void *InsertPos = nullptr; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2123 | ClassTemplateSpecializationDecl *Decl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2124 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2125 | if (!Decl) { |
| 2126 | // This is the first time we have referenced this class template |
| 2127 | // specialization. Create the canonical declaration and add it to |
| 2128 | // the set of specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2129 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2130 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2131 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | fd3a455 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2132 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2133 | ClassTemplate->getLocation(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2134 | ClassTemplate, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2135 | Converted.data(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2136 | Converted.size(), nullptr); |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2137 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Abramo Bagnara | 02b9553 | 2012-09-05 09:05:18 +0000 | [diff] [blame] | 2138 | if (ClassTemplate->isOutOfLine()) |
| 2139 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Chandler Carruth | 2acfb22 | 2013-09-27 22:14:40 +0000 | [diff] [blame] | 2142 | // Diagnose uses of this specialization. |
| 2143 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 2144 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2145 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2146 | assert(isa<RecordType>(CanonType) && |
| 2147 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2148 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2149 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2150 | // Build the fully-sugared type for this class template |
| 2151 | // specialization, which refers back to the class template |
| 2152 | // specialization we created or found. |
John McCall | 30576cd | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2153 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2156 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2157 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2158 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2159 | SourceLocation LAngleLoc, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2160 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2161 | SourceLocation RAngleLoc, |
| 2162 | bool IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2163 | if (SS.isInvalid()) |
| 2164 | return true; |
| 2165 | |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2166 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2167 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2168 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2169 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2170 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2171 | |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2172 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2173 | QualType T |
| 2174 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2175 | DTN->getQualifier(), |
| 2176 | DTN->getIdentifier(), |
| 2177 | TemplateArgs); |
| 2178 | // Build type-source information. |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2179 | TypeLocBuilder TLB; |
| 2180 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2181 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2182 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2183 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2184 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2185 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2186 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2187 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 5a06472 | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2188 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2189 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2190 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2191 | } |
| 2192 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2193 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2194 | |
| 2195 | if (Result.isNull()) |
| 2196 | return true; |
| 2197 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2198 | // Build type-source information. |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2199 | TypeLocBuilder TLB; |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2200 | TemplateSpecializationTypeLoc SpecTL |
| 2201 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2202 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2203 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2204 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2205 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2206 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2207 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2208 | |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2209 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2210 | // constructor or destructor name (in such a case, the scope specifier |
| 2211 | // will be attached to the enclosing Decl or Expr node). |
| 2212 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2213 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2214 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2215 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2216 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2217 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2218 | } |
| 2219 | |
| 2220 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2221 | } |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2222 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2223 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2224 | TypeSpecifierType TagSpec, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2225 | SourceLocation TagLoc, |
| 2226 | CXXScopeSpec &SS, |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2227 | SourceLocation TemplateKWLoc, |
| 2228 | TemplateTy TemplateD, |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2229 | SourceLocation TemplateLoc, |
| 2230 | SourceLocation LAngleLoc, |
| 2231 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2232 | SourceLocation RAngleLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2233 | TemplateName Template = TemplateD.get(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2234 | |
| 2235 | // Translate the parser's template argument list in our AST format. |
| 2236 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2237 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2238 | |
| 2239 | // Determine the tag kind |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2240 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2241 | ElaboratedTypeKeyword Keyword |
| 2242 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2243 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2244 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2245 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2246 | DTN->getQualifier(), |
| 2247 | DTN->getIdentifier(), |
| 2248 | TemplateArgs); |
| 2249 | |
| 2250 | // Build type-source information. |
| 2251 | TypeLocBuilder TLB; |
| 2252 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2253 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2254 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2255 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2256 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2257 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2258 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2259 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2260 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2261 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2262 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2263 | } |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2264 | |
| 2265 | if (TypeAliasTemplateDecl *TAT = |
| 2266 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2267 | // C++0x [dcl.type.elab]p2: |
| 2268 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2269 | // resolves to an alias template specialization, the |
| 2270 | // elaborated-type-specifier is ill-formed. |
| 2271 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2272 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2273 | } |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2274 | |
| 2275 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2276 | if (Result.isNull()) |
Matt Beaumont-Gay | 045bde4 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2277 | return TypeResult(true); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2278 | |
| 2279 | // Check the tag kind |
| 2280 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2281 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2282 | |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2283 | IdentifierInfo *Id = D->getIdentifier(); |
| 2284 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2285 | |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2286 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 2287 | TagLoc, *Id)) { |
John McCall | d8fe9af | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2288 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2289 | << Result |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2290 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2291 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2292 | } |
| 2293 | } |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2294 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2295 | // Provide source-location information for the template specialization. |
| 2296 | TypeLocBuilder TLB; |
| 2297 | TemplateSpecializationTypeLoc SpecTL |
| 2298 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2299 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2300 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2301 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2302 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2303 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2304 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2305 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2306 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2307 | // and tag keyword. |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2308 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2309 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2310 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2311 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2312 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2315 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2316 | Sema &S, SourceLocation NameLoc, TemplateParameterList *TemplateParams, |
| 2317 | unsigned ExplicitArgs, SmallVectorImpl<TemplateArgument> &TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2318 | |
| 2319 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 2320 | NamedDecl *PrevDecl, |
| 2321 | SourceLocation Loc, |
| 2322 | bool IsPartialSpecialization); |
| 2323 | |
| 2324 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2325 | |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2326 | static bool isTemplateArgumentTemplateParameter( |
| 2327 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 2328 | switch (Arg.getKind()) { |
| 2329 | case TemplateArgument::Null: |
| 2330 | case TemplateArgument::NullPtr: |
| 2331 | case TemplateArgument::Integral: |
| 2332 | case TemplateArgument::Declaration: |
| 2333 | case TemplateArgument::Pack: |
| 2334 | case TemplateArgument::TemplateExpansion: |
| 2335 | return false; |
| 2336 | |
| 2337 | case TemplateArgument::Type: { |
| 2338 | QualType Type = Arg.getAsType(); |
| 2339 | const TemplateTypeParmType *TPT = |
| 2340 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 2341 | return TPT && !Type.hasQualifiers() && |
| 2342 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 2343 | } |
| 2344 | |
| 2345 | case TemplateArgument::Expression: { |
| 2346 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 2347 | if (!DRE || !DRE->getDecl()) |
| 2348 | return false; |
| 2349 | const NonTypeTemplateParmDecl *NTTP = |
| 2350 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 2351 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 2352 | } |
| 2353 | |
| 2354 | case TemplateArgument::Template: |
| 2355 | const TemplateTemplateParmDecl *TTP = |
| 2356 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 2357 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 2358 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 2359 | } |
| 2360 | llvm_unreachable("unexpected kind of template argument"); |
| 2361 | } |
| 2362 | |
| 2363 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 2364 | ArrayRef<TemplateArgument> Args) { |
| 2365 | if (Params->size() != Args.size()) |
| 2366 | return false; |
| 2367 | |
| 2368 | unsigned Depth = Params->getDepth(); |
| 2369 | |
| 2370 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 2371 | TemplateArgument Arg = Args[I]; |
| 2372 | |
| 2373 | // If the parameter is a pack expansion, the argument must be a pack |
| 2374 | // whose only element is a pack expansion. |
| 2375 | if (Params->getParam(I)->isParameterPack()) { |
| 2376 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 2377 | !Arg.pack_begin()->isPackExpansion()) |
| 2378 | return false; |
| 2379 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 2380 | } |
| 2381 | |
| 2382 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 2383 | return false; |
| 2384 | } |
| 2385 | |
| 2386 | return true; |
| 2387 | } |
| 2388 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2389 | /// Convert the parser's template argument list representation into our form. |
| 2390 | static TemplateArgumentListInfo |
| 2391 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 2392 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 2393 | TemplateId.RAngleLoc); |
| 2394 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 2395 | TemplateId.NumArgs); |
| 2396 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 2397 | return TemplateArgs; |
| 2398 | } |
| 2399 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2400 | DeclResult Sema::ActOnVarTemplateSpecialization( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2401 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
Craig Topper | c79e5e3 | 2014-10-31 06:57:13 +0000 | [diff] [blame] | 2402 | TemplateParameterList *TemplateParams, StorageClass SC, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2403 | bool IsPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2404 | // D must be variable template id. |
| 2405 | assert(D.getName().getKind() == UnqualifiedId::IK_TemplateId && |
| 2406 | "Variable template specialization is declared with a template it."); |
| 2407 | |
| 2408 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2409 | TemplateArgumentListInfo TemplateArgs = |
| 2410 | makeTemplateArgumentListInfo(*this, *TemplateId); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2411 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 2412 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 2413 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 2414 | |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2415 | TemplateName Name = TemplateId->Template.get(); |
| 2416 | |
| 2417 | // The template-id must name a variable template. |
| 2418 | VarTemplateDecl *VarTemplate = |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2419 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 2420 | if (!VarTemplate) { |
| 2421 | NamedDecl *FnTemplate; |
| 2422 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 2423 | FnTemplate = *OTS->begin(); |
| 2424 | else |
| 2425 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 2426 | if (FnTemplate) |
| 2427 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 2428 | << FnTemplate->getDeclName(); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 2429 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 2430 | << IsPartialSpecialization; |
Karthik Bhat | 967c13d | 2014-05-08 13:16:20 +0000 | [diff] [blame] | 2431 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2432 | |
| 2433 | // Check for unexpanded parameter packs in any of the template arguments. |
| 2434 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 2435 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 2436 | UPPC_PartialSpecialization)) |
| 2437 | return true; |
| 2438 | |
| 2439 | // Check that the template argument list is well-formed for this |
| 2440 | // template. |
| 2441 | SmallVector<TemplateArgument, 4> Converted; |
| 2442 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 2443 | false, Converted)) |
| 2444 | return true; |
| 2445 | |
| 2446 | // Check that the type of this variable template specialization |
| 2447 | // matches the expected type. |
| 2448 | TypeSourceInfo *ExpectedDI; |
| 2449 | { |
| 2450 | // Do substitution on the type of the declaration |
| 2451 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
| 2452 | Converted.data(), Converted.size()); |
| 2453 | InstantiatingTemplate Inst(*this, TemplateKWLoc, VarTemplate); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2454 | if (Inst.isInvalid()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2455 | return true; |
| 2456 | VarDecl *Templated = VarTemplate->getTemplatedDecl(); |
| 2457 | ExpectedDI = |
| 2458 | SubstType(Templated->getTypeSourceInfo(), |
| 2459 | MultiLevelTemplateArgumentList(TemplateArgList), |
| 2460 | Templated->getTypeSpecStartLoc(), Templated->getDeclName()); |
| 2461 | } |
| 2462 | if (!ExpectedDI) |
| 2463 | return true; |
| 2464 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2465 | // Find the variable template (partial) specialization declaration that |
| 2466 | // corresponds to these arguments. |
| 2467 | if (IsPartialSpecialization) { |
| 2468 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 2469 | *this, TemplateNameLoc, VarTemplate->getTemplateParameters(), |
| 2470 | TemplateArgs.size(), Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2471 | return true; |
| 2472 | |
| 2473 | bool InstantiationDependent; |
| 2474 | if (!Name.isDependent() && |
| 2475 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2476 | TemplateArgs.getArgumentArray(), TemplateArgs.size(), |
| 2477 | InstantiationDependent)) { |
| 2478 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 2479 | << VarTemplate->getDeclName(); |
| 2480 | IsPartialSpecialization = false; |
| 2481 | } |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2482 | |
| 2483 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 2484 | Converted)) { |
| 2485 | // C++ [temp.class.spec]p9b3: |
| 2486 | // |
| 2487 | // -- The argument list of the specialization shall not be identical |
| 2488 | // to the implicit argument list of the primary template. |
| 2489 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 2490 | << /*variable template*/ 1 |
| 2491 | << /*is definition*/(SC != SC_Extern && !CurContext->isRecord()) |
| 2492 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 2493 | // FIXME: Recover from this by treating the declaration as a redeclaration |
| 2494 | // of the primary template. |
| 2495 | return true; |
| 2496 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2499 | void *InsertPos = nullptr; |
| 2500 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2501 | |
| 2502 | if (IsPartialSpecialization) |
| 2503 | // FIXME: Template parameter list matters too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2504 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2505 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2506 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2507 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2508 | VarTemplateSpecializationDecl *Specialization = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2509 | |
| 2510 | // Check whether we can declare a variable template specialization in |
| 2511 | // the current scope. |
| 2512 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 2513 | TemplateNameLoc, |
| 2514 | IsPartialSpecialization)) |
| 2515 | return true; |
| 2516 | |
| 2517 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 2518 | // Since the only prior variable template specialization with these |
| 2519 | // arguments was referenced but not declared, reuse that |
| 2520 | // declaration node as our own, updating its source location and |
| 2521 | // the list of outer template parameters to reflect our new declaration. |
| 2522 | Specialization = PrevDecl; |
| 2523 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2524 | PrevDecl = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2525 | } else if (IsPartialSpecialization) { |
| 2526 | // Create a new class template partial specialization declaration node. |
| 2527 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 2528 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2529 | VarTemplatePartialSpecializationDecl *Partial = |
| 2530 | VarTemplatePartialSpecializationDecl::Create( |
| 2531 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 2532 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 2533 | Converted.data(), Converted.size(), TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2534 | |
| 2535 | if (!PrevPartial) |
| 2536 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 2537 | Specialization = Partial; |
| 2538 | |
| 2539 | // If we are providing an explicit specialization of a member variable |
| 2540 | // template specialization, make a note of that. |
| 2541 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2542 | PrevPartial->setMemberSpecialization(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2543 | |
| 2544 | // Check that all of the template parameters of the variable template |
| 2545 | // partial specialization are deducible from the template |
| 2546 | // arguments. If not, this variable template partial specialization |
| 2547 | // will never be used. |
| 2548 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 2549 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 2550 | TemplateParams->getDepth(), DeducibleParams); |
| 2551 | |
| 2552 | if (!DeducibleParams.all()) { |
| 2553 | unsigned NumNonDeducible = |
| 2554 | DeducibleParams.size() - DeducibleParams.count(); |
| 2555 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 2556 | << /*variable template*/ 1 << (NumNonDeducible > 1) |
| 2557 | << SourceRange(TemplateNameLoc, RAngleLoc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2558 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 2559 | if (!DeducibleParams[I]) { |
| 2560 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 2561 | if (Param->getDeclName()) |
| 2562 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
| 2563 | << Param->getDeclName(); |
| 2564 | else |
| 2565 | Diag(Param->getLocation(), diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 2566 | << "(anonymous)"; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2567 | } |
| 2568 | } |
| 2569 | } |
| 2570 | } else { |
| 2571 | // Create a new class template specialization declaration node for |
| 2572 | // this explicit specialization or friend declaration. |
| 2573 | Specialization = VarTemplateSpecializationDecl::Create( |
| 2574 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
| 2575 | VarTemplate, DI->getType(), DI, SC, Converted.data(), Converted.size()); |
| 2576 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 2577 | |
| 2578 | if (!PrevDecl) |
| 2579 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 2580 | } |
| 2581 | |
| 2582 | // C++ [temp.expl.spec]p6: |
| 2583 | // If a template, a member template or the member of a class template is |
| 2584 | // explicitly specialized then that specialization shall be declared |
| 2585 | // before the first use of that specialization that would cause an implicit |
| 2586 | // instantiation to take place, in every translation unit in which such a |
| 2587 | // use occurs; no diagnostic is required. |
| 2588 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 2589 | bool Okay = false; |
| 2590 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 2591 | // Is there any previous explicit specialization declaration? |
| 2592 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 2593 | Okay = true; |
| 2594 | break; |
| 2595 | } |
| 2596 | } |
| 2597 | |
| 2598 | if (!Okay) { |
| 2599 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 2600 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 2601 | << Name << Range; |
| 2602 | |
| 2603 | Diag(PrevDecl->getPointOfInstantiation(), |
| 2604 | diag::note_instantiation_required_here) |
| 2605 | << (PrevDecl->getTemplateSpecializationKind() != |
| 2606 | TSK_ImplicitInstantiation); |
| 2607 | return true; |
| 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 2612 | Specialization->setLexicalDeclContext(CurContext); |
| 2613 | |
| 2614 | // Add the specialization into its lexical context, so that it can |
| 2615 | // be seen when iterating through the list of declarations in that |
| 2616 | // context. However, specializations are not found by name lookup. |
| 2617 | CurContext->addDecl(Specialization); |
| 2618 | |
| 2619 | // Note that this is an explicit specialization. |
| 2620 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 2621 | |
| 2622 | if (PrevDecl) { |
| 2623 | // Check that this isn't a redefinition of this specialization, |
| 2624 | // merging with previous declarations. |
| 2625 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
| 2626 | ForRedeclaration); |
| 2627 | PrevSpec.addDecl(PrevDecl); |
| 2628 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2629 | } else if (Specialization->isStaticDataMember() && |
| 2630 | Specialization->isOutOfLine()) { |
| 2631 | Specialization->setAccess(VarTemplate->getAccess()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | // Link instantiations of static data members back to the template from |
| 2635 | // which they were instantiated. |
| 2636 | if (Specialization->isStaticDataMember()) |
| 2637 | Specialization->setInstantiationOfStaticDataMember( |
| 2638 | VarTemplate->getTemplatedDecl(), |
| 2639 | Specialization->getSpecializationKind()); |
| 2640 | |
| 2641 | return Specialization; |
| 2642 | } |
| 2643 | |
| 2644 | namespace { |
| 2645 | /// \brief A partial specialization whose template arguments have matched |
| 2646 | /// a given template-id. |
| 2647 | struct PartialSpecMatchResult { |
| 2648 | VarTemplatePartialSpecializationDecl *Partial; |
| 2649 | TemplateArgumentList *Args; |
| 2650 | }; |
| 2651 | } |
| 2652 | |
| 2653 | DeclResult |
| 2654 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2655 | SourceLocation TemplateNameLoc, |
| 2656 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2657 | assert(Template && "A variable template id without template?"); |
| 2658 | |
| 2659 | // Check that the template argument list is well-formed for this template. |
| 2660 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2661 | if (CheckTemplateArgumentList( |
| 2662 | Template, TemplateNameLoc, |
| 2663 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2664 | Converted)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2665 | return true; |
| 2666 | |
| 2667 | // Find the variable template specialization declaration that |
| 2668 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2669 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2670 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2671 | Converted, InsertPos)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2672 | // If we already have a variable template specialization, return it. |
| 2673 | return Spec; |
| 2674 | |
| 2675 | // This is the first time we have referenced this variable template |
| 2676 | // specialization. Create the canonical declaration and add it to |
| 2677 | // the set of specializations, based on the closest partial specialization |
| 2678 | // that it represents. That is, |
| 2679 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 2680 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
| 2681 | Converted.data(), Converted.size()); |
| 2682 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 2683 | bool AmbiguousPartialSpec = false; |
| 2684 | typedef PartialSpecMatchResult MatchResult; |
| 2685 | SmallVector<MatchResult, 4> Matched; |
| 2686 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
| 2687 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation); |
| 2688 | |
| 2689 | // 1. Attempt to find the closest partial specialization that this |
| 2690 | // specializes, if any. |
| 2691 | // If any of the template arguments is dependent, then this is probably |
| 2692 | // a placeholder for an incomplete declarative context; which must be |
| 2693 | // complete by instantiation time. Thus, do not search through the partial |
| 2694 | // specializations yet. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2695 | // TODO: Unify with InstantiateClassTemplateSpecialization()? |
| 2696 | // Perhaps better after unification of DeduceTemplateArguments() and |
| 2697 | // getMoreSpecializedPartialSpecialization(). |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2698 | bool InstantiationDependent = false; |
| 2699 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 2700 | TemplateArgs, InstantiationDependent)) { |
| 2701 | |
| 2702 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 2703 | Template->getPartialSpecializations(PartialSpecs); |
| 2704 | |
| 2705 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 2706 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 2707 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 2708 | |
| 2709 | if (TemplateDeductionResult Result = |
| 2710 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 2711 | // Store the failed-deduction information for use in diagnostics, later. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2712 | // TODO: Actually use the failed-deduction info? |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2713 | FailedCandidates.addCandidate() |
| 2714 | .set(Partial, MakeDeductionFailureInfo(Context, Result, Info)); |
| 2715 | (void)Result; |
| 2716 | } else { |
| 2717 | Matched.push_back(PartialSpecMatchResult()); |
| 2718 | Matched.back().Partial = Partial; |
| 2719 | Matched.back().Args = Info.take(); |
| 2720 | } |
| 2721 | } |
| 2722 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2723 | if (Matched.size() >= 1) { |
| 2724 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 2725 | if (Matched.size() == 1) { |
| 2726 | // -- If exactly one matching specialization is found, the |
| 2727 | // instantiation is generated from that specialization. |
| 2728 | // We don't need to do anything for this. |
| 2729 | } else { |
| 2730 | // -- If more than one matching specialization is found, the |
| 2731 | // partial order rules (14.5.4.2) are used to determine |
| 2732 | // whether one of the specializations is more specialized |
| 2733 | // than the others. If none of the specializations is more |
| 2734 | // specialized than all of the other matching |
| 2735 | // specializations, then the use of the variable template is |
| 2736 | // ambiguous and the program is ill-formed. |
| 2737 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 2738 | PEnd = Matched.end(); |
| 2739 | P != PEnd; ++P) { |
| 2740 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 2741 | PointOfInstantiation) == |
| 2742 | P->Partial) |
| 2743 | Best = P; |
| 2744 | } |
| 2745 | |
| 2746 | // Determine if the best partial specialization is more specialized than |
| 2747 | // the others. |
| 2748 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2749 | PEnd = Matched.end(); |
| 2750 | P != PEnd; ++P) { |
| 2751 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 2752 | P->Partial, Best->Partial, |
| 2753 | PointOfInstantiation) != Best->Partial) { |
| 2754 | AmbiguousPartialSpec = true; |
| 2755 | break; |
| 2756 | } |
| 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | // Instantiate using the best variable template partial specialization. |
| 2761 | InstantiationPattern = Best->Partial; |
| 2762 | InstantiationArgs = Best->Args; |
| 2763 | } else { |
| 2764 | // -- If no match is found, the instantiation is generated |
| 2765 | // from the primary template. |
| 2766 | // InstantiationPattern = Template->getTemplatedDecl(); |
| 2767 | } |
| 2768 | } |
| 2769 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2770 | // 2. Create the canonical declaration. |
| 2771 | // Note that we do not instantiate the variable just yet, since |
| 2772 | // instantiation is handled in DoMarkVarDeclReferenced(). |
| 2773 | // FIXME: LateAttrs et al.? |
| 2774 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 2775 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 2776 | Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); |
| 2777 | if (!Decl) |
| 2778 | return true; |
| 2779 | |
| 2780 | if (AmbiguousPartialSpec) { |
| 2781 | // Partial ordering did not produce a clear winner. Complain. |
| 2782 | Decl->setInvalidDecl(); |
| 2783 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 2784 | << Decl; |
| 2785 | |
| 2786 | // Print the matching partial specializations. |
| 2787 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 2788 | PEnd = Matched.end(); |
| 2789 | P != PEnd; ++P) |
| 2790 | Diag(P->Partial->getLocation(), diag::note_partial_spec_match) |
| 2791 | << getTemplateArgumentBindingsText( |
| 2792 | P->Partial->getTemplateParameters(), *P->Args); |
| 2793 | return true; |
| 2794 | } |
| 2795 | |
| 2796 | if (VarTemplatePartialSpecializationDecl *D = |
| 2797 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 2798 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 2799 | |
| 2800 | assert(Decl && "No variable template specialization?"); |
| 2801 | return Decl; |
| 2802 | } |
| 2803 | |
| 2804 | ExprResult |
| 2805 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 2806 | const DeclarationNameInfo &NameInfo, |
| 2807 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 2808 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2809 | |
| 2810 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 2811 | *TemplateArgs); |
| 2812 | if (Decl.isInvalid()) |
| 2813 | return ExprError(); |
| 2814 | |
| 2815 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 2816 | if (!Var->getTemplateSpecializationKind()) |
| 2817 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 2818 | NameInfo.getLoc()); |
| 2819 | |
| 2820 | // Build an ordinary singleton decl ref. |
| 2821 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2822 | /*FoundD=*/nullptr, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2825 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2826 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2827 | LookupResult &R, |
| 2828 | bool RequiresADL, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2829 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2830 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2831 | // template arguments that we have against the template name, if the template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2832 | // 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] | 2833 | // though. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2834 | // foo<int> could identify a single function unambiguously |
| 2835 | // This approach does NOT work, since f<int>(1); |
| 2836 | // gets resolved prior to resorting to overload resolution |
| 2837 | // i.e., template<class T> void f(double); |
| 2838 | // vs template<class T, class U> void f(U); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2839 | |
| 2840 | // These should be filtered out by our callers. |
| 2841 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2842 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2843 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2844 | // In C++1y, check variable template ids. |
Richard Smith | d7d11ef | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 2845 | bool InstantiationDependent; |
| 2846 | if (R.getAsSingle<VarTemplateDecl>() && |
| 2847 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 2848 | *TemplateArgs, InstantiationDependent)) { |
| 2849 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 2850 | R.getAsSingle<VarTemplateDecl>(), |
| 2851 | TemplateKWLoc, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2854 | // We don't want lookup warnings at this point. |
| 2855 | R.suppressDiagnostics(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2856 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2857 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2858 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2859 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2860 | TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2861 | R.getLookupNameInfo(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2862 | RequiresADL, TemplateArgs, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2863 | R.begin(), R.end()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2864 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2865 | return ULE; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2868 | // We actually only call this from template instantiation. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2869 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2870 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2871 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2872 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2873 | const TemplateArgumentListInfo *TemplateArgs) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2874 | |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2875 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2876 | DeclContext *DC; |
| 2877 | if (!(DC = computeDeclContext(SS, false)) || |
| 2878 | DC->isDependentContext() || |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2879 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 65f7c3d | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2880 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2881 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2882 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2883 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2884 | LookupTemplateName(R, (Scope*)nullptr, SS, QualType(), /*Entering*/ false, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2885 | MemberOfUnknownSpecialization); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2886 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2887 | if (R.isAmbiguous()) |
| 2888 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2889 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2890 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2891 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2892 | << NameInfo.getName() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2893 | return ExprError(); |
| 2894 | } |
| 2895 | |
| 2896 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2897 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 2898 | << SS.getScopeRep() |
Reid Kleckner | 32506ed | 2014-06-12 23:03:48 +0000 | [diff] [blame] | 2899 | << NameInfo.getName().getAsString() << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2900 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2901 | return ExprError(); |
| 2902 | } |
| 2903 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2904 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2907 | /// \brief Form a dependent template name. |
| 2908 | /// |
| 2909 | /// This action forms a dependent template name given the template |
| 2910 | /// name and its (presumably dependent) scope specifier. For |
| 2911 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2912 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2913 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2914 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2915 | CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2916 | SourceLocation TemplateKWLoc, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2917 | UnqualifiedId &Name, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2918 | ParsedType ObjectType, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2919 | bool EnteringContext, |
| 2920 | TemplateTy &Result) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2921 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 2922 | Diag(TemplateKWLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2923 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2924 | diag::warn_cxx98_compat_template_outside_of_template : |
| 2925 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2926 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2927 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2928 | DeclContext *LookupCtx = nullptr; |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2929 | if (SS.isSet()) |
| 2930 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2931 | if (!LookupCtx && ObjectType) |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2932 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2933 | if (LookupCtx) { |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2934 | // C++0x [temp.names]p5: |
| 2935 | // If a name prefixed by the keyword template is not the name of |
| 2936 | // a template, the program is ill-formed. [Note: the keyword |
| 2937 | // template may not be applied to non-template members of class |
| 2938 | // templates. -end note ] [ Note: as is the case with the |
| 2939 | // typename prefix, the template prefix is allowed in cases |
| 2940 | // where it is not strictly necessary; i.e., when the |
| 2941 | // nested-name-specifier or the expression on the left of the -> |
| 2942 | // or . is not dependent on a template-parameter, or the use |
| 2943 | // does not appear in the scope of a template. -end note] |
| 2944 | // |
| 2945 | // Note: C++03 was more strict here, because it banned the use of |
| 2946 | // the "template" keyword prior to a template-name that was not a |
| 2947 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2948 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2949 | // 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] | 2950 | bool MemberOfUnknownSpecialization; |
Richard Smith | af41696 | 2012-11-15 00:31:27 +0000 | [diff] [blame] | 2951 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2952 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2953 | MemberOfUnknownSpecialization); |
Douglas Gregor | 9abe237 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2954 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2955 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | 5ecbb1b | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2956 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2957 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2958 | // This is a dependent template. Handle it below. |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2959 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2960 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2961 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2962 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2963 | << Name.getSourceRange() |
| 2964 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2965 | return TNK_Non_template; |
Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2966 | } else { |
| 2967 | // We found something; return it. |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2968 | return TNK; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2969 | } |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 2972 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2973 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2974 | switch (Name.getKind()) { |
| 2975 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2976 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2977 | Name.Identifier)); |
| 2978 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2979 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2980 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2981 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2982 | Name.OperatorFunctionId.Operator)); |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 2983 | return TNK_Function_template; |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2984 | |
| 2985 | case UnqualifiedId::IK_LiteralOperatorId: |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 2986 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2987 | |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2988 | default: |
| 2989 | break; |
| 2990 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2991 | |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2992 | Diag(Name.getLocStart(), |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2993 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2994 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2995 | << Name.getSourceRange() |
| 2996 | << TemplateKWLoc; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2997 | return TNK_Non_template; |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3000 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3001 | TemplateArgumentLoc &AL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3002 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3003 | const TemplateArgument &Arg = AL.getArgument(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3004 | QualType ArgType; |
| 3005 | TypeSourceInfo *TSI = nullptr; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3006 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3007 | // Check template type parameter. |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3008 | switch(Arg.getKind()) { |
| 3009 | case TemplateArgument::Type: |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3010 | // C++ [temp.arg.type]p1: |
| 3011 | // A template-argument for a template-parameter which is a |
| 3012 | // type shall be a type-id. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3013 | ArgType = Arg.getAsType(); |
| 3014 | TSI = AL.getTypeSourceInfo(); |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3015 | break; |
| 3016 | case TemplateArgument::Template: { |
| 3017 | // We have a template type parameter but the template argument |
| 3018 | // is a template without any arguments. |
| 3019 | SourceRange SR = AL.getSourceRange(); |
| 3020 | TemplateName Name = Arg.getAsTemplate(); |
| 3021 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 3022 | << Name << SR; |
| 3023 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 3024 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3025 | |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3026 | return true; |
| 3027 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3028 | case TemplateArgument::Expression: { |
| 3029 | // We have a template type parameter but the template argument is an |
| 3030 | // expression; see if maybe it is missing the "typename" keyword. |
| 3031 | CXXScopeSpec SS; |
| 3032 | DeclarationNameInfo NameInfo; |
| 3033 | |
| 3034 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 3035 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3036 | NameInfo = ArgExpr->getNameInfo(); |
| 3037 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 3038 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 3039 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3040 | NameInfo = ArgExpr->getNameInfo(); |
| 3041 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 3042 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3043 | if (ArgExpr->isImplicitAccess()) { |
| 3044 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 3045 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 3046 | } |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3047 | } |
| 3048 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3049 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3050 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 3051 | LookupParsedName(Result, CurScope, &SS); |
| 3052 | |
Kaelyn Uhrain | 055e947 | 2012-06-08 01:07:26 +0000 | [diff] [blame] | 3053 | if (Result.getAsSingle<TypeDecl>() || |
| 3054 | Result.getResultKind() == |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3055 | LookupResult::NotFoundInCurrentInstantiation) { |
| 3056 | // Suggest that the user add 'typename' before the NNS. |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3057 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3058 | Diag(Loc, getLangOpts().MSVCCompat |
| 3059 | ? diag::ext_ms_template_type_arg_missing_typename |
| 3060 | : diag::err_template_arg_must_be_type_suggest) |
| 3061 | << FixItHint::CreateInsertion(Loc, "typename "); |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3062 | Diag(Param->getLocation(), diag::note_template_param_here); |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3063 | |
| 3064 | // Recover by synthesizing a type using the location information that we |
| 3065 | // already have. |
| 3066 | ArgType = |
| 3067 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 3068 | TypeLocBuilder TLB; |
| 3069 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 3070 | TL.setElaboratedKeywordLoc(SourceLocation(/*synthesized*/)); |
| 3071 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3072 | TL.setNameLoc(NameInfo.getLoc()); |
| 3073 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 3074 | |
| 3075 | // Overwrite our input TemplateArgumentLoc so that we can recover |
| 3076 | // properly. |
| 3077 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 3078 | TemplateArgumentLocInfo(TSI)); |
| 3079 | |
| 3080 | break; |
Kaelyn Uhrain | 864d0b0 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 3081 | } |
| 3082 | } |
| 3083 | // fallthrough |
| 3084 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3085 | default: { |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3086 | // We have a template type parameter but the template argument |
| 3087 | // is not a type. |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 3088 | SourceRange SR = AL.getSourceRange(); |
| 3089 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3090 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3091 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3092 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3093 | } |
Jeffrey Yasskin | 823015d | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 3094 | } |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3095 | |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3096 | if (CheckTemplateArgument(Param, TSI)) |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3097 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3098 | |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3099 | // Add the converted template type argument. |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3100 | ArgType = Context.getCanonicalType(ArgType); |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3101 | |
| 3102 | // Objective-C ARC: |
| 3103 | // If an explicitly-specified template argument type is a lifetime type |
| 3104 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3105 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 3106 | ArgType->isObjCLifetimeType() && |
| 3107 | !ArgType.getObjCLifetime()) { |
| 3108 | Qualifiers Qs; |
| 3109 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 3110 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 3111 | } |
| 3112 | |
| 3113 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | c8cbb2d | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 3114 | return false; |
| 3115 | } |
| 3116 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3117 | /// \brief Substitute template arguments into the default template argument for |
| 3118 | /// the given template type parameter. |
| 3119 | /// |
| 3120 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3121 | /// the substitution. |
| 3122 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3123 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3124 | /// for. |
| 3125 | /// |
| 3126 | /// \param TemplateLoc the location of the template name that started the |
| 3127 | /// template-id we are checking. |
| 3128 | /// |
| 3129 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3130 | /// terminates the template-id. |
| 3131 | /// |
| 3132 | /// \param Param the template template parameter whose default we are |
| 3133 | /// substituting into. |
| 3134 | /// |
| 3135 | /// \param Converted the list of template arguments provided for template |
| 3136 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3137 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3138 | static TypeSourceInfo * |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3139 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3140 | TemplateDecl *Template, |
| 3141 | SourceLocation TemplateLoc, |
| 3142 | SourceLocation RAngleLoc, |
| 3143 | TemplateTypeParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3144 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3145 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3146 | |
| 3147 | // If the argument type is dependent, instantiate it now based |
| 3148 | // on the previously-computed template arguments. |
| 3149 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3150 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3151 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3152 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3153 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3154 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3155 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3156 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3157 | Converted.data(), Converted.size()); |
| 3158 | |
| 3159 | // Only substitute for the innermost template argument list. |
| 3160 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3161 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3162 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3163 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3164 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3165 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3166 | ArgType = |
| 3167 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 3168 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3169 | } |
| 3170 | |
| 3171 | return ArgType; |
| 3172 | } |
| 3173 | |
| 3174 | /// \brief Substitute template arguments into the default template argument for |
| 3175 | /// the given non-type template parameter. |
| 3176 | /// |
| 3177 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3178 | /// the substitution. |
| 3179 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3180 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3181 | /// for. |
| 3182 | /// |
| 3183 | /// \param TemplateLoc the location of the template name that started the |
| 3184 | /// template-id we are checking. |
| 3185 | /// |
| 3186 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3187 | /// terminates the template-id. |
| 3188 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3189 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3190 | /// substituting into. |
| 3191 | /// |
| 3192 | /// \param Converted the list of template arguments provided for template |
| 3193 | /// parameters that precede \p Param in the template parameter list. |
| 3194 | /// |
| 3195 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3196 | static ExprResult |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3197 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3198 | TemplateDecl *Template, |
| 3199 | SourceLocation TemplateLoc, |
| 3200 | SourceLocation RAngleLoc, |
| 3201 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3202 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3203 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3204 | Template, Converted, |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3205 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3206 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3207 | return ExprError(); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3208 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3209 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3210 | Converted.data(), Converted.size()); |
| 3211 | |
| 3212 | // Only substitute for the innermost template argument list. |
| 3213 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3214 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3215 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3216 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3217 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3218 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
Eli Friedman | c25372b | 2012-04-26 22:43:24 +0000 | [diff] [blame] | 3219 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3220 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3223 | /// \brief Substitute template arguments into the default template argument for |
| 3224 | /// the given template template parameter. |
| 3225 | /// |
| 3226 | /// \param SemaRef the semantic analysis object for which we are performing |
| 3227 | /// the substitution. |
| 3228 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3229 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3230 | /// for. |
| 3231 | /// |
| 3232 | /// \param TemplateLoc the location of the template name that started the |
| 3233 | /// template-id we are checking. |
| 3234 | /// |
| 3235 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 3236 | /// terminates the template-id. |
| 3237 | /// |
| 3238 | /// \param Param the template template parameter whose default we are |
| 3239 | /// substituting into. |
| 3240 | /// |
| 3241 | /// \param Converted the list of template arguments provided for template |
| 3242 | /// parameters that precede \p Param in the template parameter list. |
| 3243 | /// |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3244 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 3245 | /// source-location information) that precedes the template name. |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3246 | /// |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3247 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 3248 | static TemplateName |
| 3249 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 3250 | TemplateDecl *Template, |
| 3251 | SourceLocation TemplateLoc, |
| 3252 | SourceLocation RAngleLoc, |
| 3253 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3254 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3255 | NestedNameSpecifierLoc &QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3256 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, Template, Converted, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3257 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3258 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3259 | return TemplateName(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3260 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3261 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 3262 | Converted.data(), Converted.size()); |
| 3263 | |
| 3264 | // Only substitute for the innermost template argument list. |
| 3265 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3266 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 3267 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 3268 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3269 | |
Argyrios Kyrtzidis | 6fe744c | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 3270 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3271 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3272 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3273 | if (QualifierLoc) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3274 | QualifierLoc = |
| 3275 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3276 | if (!QualifierLoc) |
| 3277 | return TemplateName(); |
| 3278 | } |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 3279 | |
| 3280 | return SemaRef.SubstTemplateName( |
| 3281 | QualifierLoc, |
| 3282 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 3283 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 3284 | TemplateArgLists); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3287 | /// \brief If the given template parameter has a default template |
| 3288 | /// argument, substitute into that default template argument and |
| 3289 | /// return the corresponding template argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3290 | TemplateArgumentLoc |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3291 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 3292 | SourceLocation TemplateLoc, |
| 3293 | SourceLocation RAngleLoc, |
| 3294 | Decl *Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3295 | SmallVectorImpl<TemplateArgument> |
| 3296 | &Converted, |
| 3297 | bool &HasDefaultArg) { |
| 3298 | HasDefaultArg = false; |
| 3299 | |
| 3300 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3301 | if (!TypeParm->hasDefaultArgument()) |
| 3302 | return TemplateArgumentLoc(); |
| 3303 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3304 | HasDefaultArg = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3305 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3306 | TemplateLoc, |
| 3307 | RAngleLoc, |
| 3308 | TypeParm, |
| 3309 | Converted); |
| 3310 | if (DI) |
| 3311 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 3312 | |
| 3313 | return TemplateArgumentLoc(); |
| 3314 | } |
| 3315 | |
| 3316 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 3317 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3318 | if (!NonTypeParm->hasDefaultArgument()) |
| 3319 | return TemplateArgumentLoc(); |
| 3320 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3321 | HasDefaultArg = true; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3322 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3323 | TemplateLoc, |
| 3324 | RAngleLoc, |
| 3325 | NonTypeParm, |
| 3326 | Converted); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3327 | if (Arg.isInvalid()) |
| 3328 | return TemplateArgumentLoc(); |
| 3329 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3330 | Expr *ArgE = Arg.getAs<Expr>(); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3331 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 3332 | } |
| 3333 | |
| 3334 | TemplateTemplateParmDecl *TempTempParm |
| 3335 | = cast<TemplateTemplateParmDecl>(Param); |
| 3336 | if (!TempTempParm->hasDefaultArgument()) |
| 3337 | return TemplateArgumentLoc(); |
| 3338 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 3339 | HasDefaultArg = true; |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3340 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3341 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3342 | TemplateLoc, |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3343 | RAngleLoc, |
| 3344 | TempTempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3345 | Converted, |
| 3346 | QualifierLoc); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3347 | if (TName.isNull()) |
| 3348 | return TemplateArgumentLoc(); |
| 3349 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3350 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3351 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3352 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 3353 | } |
| 3354 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3355 | /// \brief Check that the given template argument corresponds to the given |
| 3356 | /// template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3357 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3358 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3359 | /// checked. |
| 3360 | /// |
| 3361 | /// \param Arg The template argument. |
| 3362 | /// |
| 3363 | /// \param Template The template in which the template argument resides. |
| 3364 | /// |
| 3365 | /// \param TemplateLoc The location of the template name for the template |
| 3366 | /// whose argument list we're matching. |
| 3367 | /// |
| 3368 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 3369 | /// the template argument list. |
| 3370 | /// |
| 3371 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 3372 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 3373 | /// |
| 3374 | /// \param Converted The checked, converted argument will be added to the |
| 3375 | /// end of this small vector. |
| 3376 | /// |
| 3377 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 3378 | /// explicitly written, deduced, etc. |
| 3379 | /// |
| 3380 | /// \returns true on error, false otherwise. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3381 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 3382 | TemplateArgumentLoc &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 3383 | NamedDecl *Template, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3384 | SourceLocation TemplateLoc, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3385 | SourceLocation RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3386 | unsigned ArgumentPackIndex, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3387 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3388 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3389 | // Check template type parameters. |
| 3390 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3391 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3392 | |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3393 | // Check non-type template parameters. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3394 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3395 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3396 | // with the template arguments we've seen thus far. But if the |
| 3397 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3398 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3399 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 3400 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3401 | |
Peter Collingbourne | 0168763 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 3402 | if (NTTPType->isDependentType() && |
| 3403 | !isa<TemplateTemplateParmDecl>(Template) && |
| 3404 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3405 | // Do substitution on the type of the non-type template parameter. |
| 3406 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3407 | NTTP, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3408 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3409 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3410 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3411 | |
| 3412 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3413 | Converted.data(), Converted.size()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3414 | NTTPType = SubstType(NTTPType, |
| 3415 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 3416 | NTTP->getLocation(), |
| 3417 | NTTP->getDeclName()); |
| 3418 | // If that worked, check the non-type template parameter type |
| 3419 | // for validity. |
| 3420 | if (!NTTPType.isNull()) |
| 3421 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 3422 | NTTP->getLocation()); |
| 3423 | if (NTTPType.isNull()) |
| 3424 | return true; |
| 3425 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3426 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3427 | switch (Arg.getArgument().getKind()) { |
| 3428 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3429 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3430 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3431 | case TemplateArgument::Expression: { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3432 | TemplateArgument Result; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3433 | ExprResult Res = |
| 3434 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 3435 | Result, CTAK); |
| 3436 | if (Res.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3437 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3438 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3439 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3440 | break; |
| 3441 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3442 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3443 | case TemplateArgument::Declaration: |
| 3444 | case TemplateArgument::Integral: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3445 | case TemplateArgument::NullPtr: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3446 | // We've already checked this template argument, so just copy |
| 3447 | // it to the list of converted arguments. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3448 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3449 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3450 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3451 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3452 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3453 | // We were given a template template argument. It may not be ill-formed; |
| 3454 | // see below. |
| 3455 | if (DependentTemplateName *DTN |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3456 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 3457 | .getAsDependentTemplateName()) { |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3458 | // We have a template argument such as \c T::template X, which we |
| 3459 | // parsed as a template template argument. However, since we now |
| 3460 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3461 | // template name into an expression. |
| 3462 | |
| 3463 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 3464 | Arg.getTemplateNameLoc()); |
| 3465 | |
Douglas Gregor | 3a43fd6 | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 3466 | CXXScopeSpec SS; |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3467 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3468 | // FIXME: the template-template arg was a DependentTemplateName, |
| 3469 | // so it was provided with a template keyword. However, its source |
| 3470 | // location is not stored in the template argument structure. |
| 3471 | SourceLocation TemplateKWLoc; |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3472 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 3473 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 3474 | nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3475 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3476 | // If we parsed the template argument as a pack expansion, create a |
| 3477 | // pack expansion expression. |
| 3478 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3479 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3480 | if (E.isInvalid()) |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3481 | return true; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3482 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3483 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3484 | TemplateArgument Result; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3485 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3486 | if (E.isInvalid()) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3487 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3488 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3489 | Converted.push_back(Result); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3490 | break; |
| 3491 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3492 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3493 | // We have a template argument that actually does refer to a class |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3494 | // template, alias template, or template template parameter, and |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3495 | // therefore cannot be a non-type template argument. |
| 3496 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 3497 | << Arg.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3498 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3499 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3500 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3501 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3502 | case TemplateArgument::Type: { |
| 3503 | // We have a non-type template parameter but the template |
| 3504 | // argument is a type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3505 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3506 | // C++ [temp.arg]p2: |
| 3507 | // In a template-argument, an ambiguity between a type-id and |
| 3508 | // an expression is resolved to a type-id, regardless of the |
| 3509 | // form of the corresponding template-parameter. |
| 3510 | // |
| 3511 | // We warn specifically about this case, since it can be rather |
| 3512 | // confusing for users. |
| 3513 | QualType T = Arg.getArgument().getAsType(); |
| 3514 | SourceRange SR = Arg.getSourceRange(); |
| 3515 | if (T->isFunctionType()) |
| 3516 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 3517 | else |
| 3518 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 3519 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3520 | return true; |
| 3521 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3522 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3523 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3524 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3525 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3526 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3527 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3531 | // Check template template parameters. |
| 3532 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3533 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3534 | // Substitute into the template parameter list of the template |
| 3535 | // template parameter, since previously-supplied template arguments |
| 3536 | // may appear within the template template parameter. |
| 3537 | { |
| 3538 | // Set up a template instantiation context. |
| 3539 | LocalInstantiationScope Scope(*this); |
| 3540 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3541 | TempParm, Converted, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3542 | SourceRange(TemplateLoc, RAngleLoc)); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3543 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3544 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3545 | |
| 3546 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3547 | Converted.data(), Converted.size()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3548 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3549 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3550 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 3551 | if (!TempParm) |
| 3552 | return true; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3553 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3554 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3555 | switch (Arg.getArgument().getKind()) { |
| 3556 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3557 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3558 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3559 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3560 | case TemplateArgument::TemplateExpansion: |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3561 | if (CheckTemplateArgument(TempParm, Arg, ArgumentPackIndex)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3562 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3563 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3564 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3565 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3566 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3567 | case TemplateArgument::Expression: |
| 3568 | case TemplateArgument::Type: |
| 3569 | // We have a template template parameter but the template |
| 3570 | // argument does not refer to a template. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3571 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3572 | << getLangOpts().CPlusPlus11; |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3573 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3574 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3575 | case TemplateArgument::Declaration: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3576 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3577 | case TemplateArgument::Integral: |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3578 | llvm_unreachable("Integral argument with template template parameter"); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3579 | case TemplateArgument::NullPtr: |
| 3580 | llvm_unreachable("Null pointer argument with template template parameter"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3581 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3582 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3583 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3584 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3585 | |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3586 | return false; |
| 3587 | } |
| 3588 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3589 | /// \brief Diagnose an arity mismatch in the |
| 3590 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 3591 | SourceLocation TemplateLoc, |
| 3592 | TemplateArgumentListInfo &TemplateArgs) { |
| 3593 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 3594 | unsigned NumParams = Params->size(); |
| 3595 | unsigned NumArgs = TemplateArgs.size(); |
| 3596 | |
| 3597 | SourceRange Range; |
| 3598 | if (NumArgs > NumParams) |
| 3599 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
| 3600 | TemplateArgs.getRAngleLoc()); |
| 3601 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3602 | << (NumArgs > NumParams) |
| 3603 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3604 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3605 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3606 | << Template << Range; |
| 3607 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3608 | << Params->getSourceRange(); |
| 3609 | return true; |
| 3610 | } |
| 3611 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3612 | /// \brief Check whether the template parameter is a pack expansion, and if so, |
| 3613 | /// determine the number of parameters produced by that expansion. For instance: |
| 3614 | /// |
| 3615 | /// \code |
| 3616 | /// template<typename ...Ts> struct A { |
| 3617 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
| 3618 | /// }; |
| 3619 | /// \endcode |
| 3620 | /// |
| 3621 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
| 3622 | /// is not a pack expansion, so returns an empty Optional. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3623 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3624 | if (NonTypeTemplateParmDecl *NTTP |
| 3625 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3626 | if (NTTP->isExpandedParameterPack()) |
| 3627 | return NTTP->getNumExpansionTypes(); |
| 3628 | } |
| 3629 | |
| 3630 | if (TemplateTemplateParmDecl *TTP |
| 3631 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 3632 | if (TTP->isExpandedParameterPack()) |
| 3633 | return TTP->getNumExpansionTemplateParameters(); |
| 3634 | } |
| 3635 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 3636 | return None; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3639 | /// \brief Check that the given template argument list is well-formed |
| 3640 | /// for specializing the given template. |
| 3641 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 3642 | SourceLocation TemplateLoc, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3643 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 3644 | bool PartialTemplateArgs, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3645 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3646 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3647 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3648 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 3649 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3650 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3651 | // [...] The type and form of each template-argument specified in |
| 3652 | // a template-id shall match the type and form specified for the |
| 3653 | // corresponding parameter declared by the template in its |
| 3654 | // template-parameter-list. |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3655 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3656 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3657 | unsigned ArgIdx = 0, NumArgs = TemplateArgs.size(); |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3658 | LocalInstantiationScope InstScope(*this, true); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3659 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 3660 | ParamEnd = Params->end(); |
| 3661 | Param != ParamEnd; /* increment in loop */) { |
| 3662 | // If we have an expanded parameter pack, make sure we don't have too |
| 3663 | // many arguments. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3664 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3665 | if (*Expansions == ArgumentPack.size()) { |
| 3666 | // We're done with this parameter pack. Pack up its arguments and add |
| 3667 | // them to the list. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3668 | Converted.push_back( |
| 3669 | TemplateArgument::CreatePackCopy(Context, |
| 3670 | ArgumentPack.data(), |
| 3671 | ArgumentPack.size())); |
| 3672 | ArgumentPack.clear(); |
| 3673 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3674 | // This argument is assigned to the next parameter. |
| 3675 | ++Param; |
| 3676 | continue; |
| 3677 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 3678 | // Not enough arguments for this parameter pack. |
| 3679 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3680 | << false |
| 3681 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3682 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3683 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3684 | << Template; |
| 3685 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3686 | << Params->getSourceRange(); |
| 3687 | return true; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3688 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3689 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3690 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3691 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3692 | // Check the template argument we were given. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3693 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 3694 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3695 | ArgumentPack.size(), Converted)) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3696 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3697 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame^] | 3698 | bool PackExpansionIntoNonPack = |
| 3699 | TemplateArgs[ArgIdx].getArgument().isPackExpansion() && |
| 3700 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 3701 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3702 | // Core issue 1430: we have a pack expansion as an argument to an |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame^] | 3703 | // alias template, and it's not part of a parameter pack. This |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3704 | // can't be canonicalized, so reject it now. |
| 3705 | Diag(TemplateArgs[ArgIdx].getLocation(), |
| 3706 | diag::err_alias_template_expansion_into_fixed_list) |
| 3707 | << TemplateArgs[ArgIdx].getSourceRange(); |
| 3708 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 3709 | return true; |
| 3710 | } |
| 3711 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3712 | // We're now done with this argument. |
| 3713 | ++ArgIdx; |
| 3714 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3715 | if ((*Param)->isTemplateParameterPack()) { |
| 3716 | // The template parameter was a template parameter pack, so take the |
| 3717 | // deduced argument and place it on the argument pack. Note that we |
| 3718 | // stay on the same template parameter so that we can deduce more |
| 3719 | // arguments. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 3720 | ArgumentPack.push_back(Converted.pop_back_val()); |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3721 | } else { |
| 3722 | // Move to the next template parameter. |
| 3723 | ++Param; |
| 3724 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3725 | |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame^] | 3726 | // If we just saw a pack expansion into a non-pack, then directly convert |
| 3727 | // the remaining arguments, because we don't know what parameters they'll |
| 3728 | // match up with. |
| 3729 | if (PackExpansionIntoNonPack) { |
| 3730 | if (!ArgumentPack.empty()) { |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3731 | // If we were part way through filling in an expanded parameter pack, |
| 3732 | // fall back to just producing individual arguments. |
| 3733 | Converted.insert(Converted.end(), |
| 3734 | ArgumentPack.begin(), ArgumentPack.end()); |
| 3735 | ArgumentPack.clear(); |
| 3736 | } |
| 3737 | |
| 3738 | while (ArgIdx < NumArgs) { |
Richard Smith | 96d71c3 | 2014-11-12 23:38:38 +0000 | [diff] [blame^] | 3739 | Converted.push_back(TemplateArgs[ArgIdx].getArgument()); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3740 | ++ArgIdx; |
| 3741 | } |
| 3742 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3743 | return false; |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3744 | } |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3745 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3746 | continue; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3747 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3748 | |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3749 | // If we're checking a partial template argument list, we're done. |
| 3750 | if (PartialTemplateArgs) { |
| 3751 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 3752 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3753 | ArgumentPack.data(), |
| 3754 | ArgumentPack.size())); |
| 3755 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3756 | return false; |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3757 | } |
| 3758 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3759 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3760 | // 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] | 3761 | if ((*Param)->isTemplateParameterPack()) { |
| 3762 | assert(!getExpandedPackSize(*Param) && |
| 3763 | "Should have dealt with this already"); |
| 3764 | |
| 3765 | // A non-expanded parameter pack before the end of the parameter list |
| 3766 | // only occurs for an ill-formed template parameter list, unless we've |
| 3767 | // got a partial argument list for a function template, so just bail out. |
| 3768 | if (Param + 1 != ParamEnd) |
| 3769 | return true; |
| 3770 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 3771 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3772 | ArgumentPack.data(), |
| 3773 | ArgumentPack.size())); |
| 3774 | ArgumentPack.clear(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3775 | |
| 3776 | ++Param; |
| 3777 | continue; |
| 3778 | } |
| 3779 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3780 | // Check whether we have a default argument. |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3781 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3782 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3783 | // Retrieve the default template argument from the template |
| 3784 | // parameter. For each kind of template parameter, we substitute the |
| 3785 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3786 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3787 | // the default argument. |
| 3788 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3789 | if (!TTP->hasDefaultArgument()) |
| 3790 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3791 | TemplateArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3792 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3793 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3794 | Template, |
| 3795 | TemplateLoc, |
| 3796 | RAngleLoc, |
| 3797 | TTP, |
| 3798 | Converted); |
| 3799 | if (!ArgType) |
| 3800 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3801 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3802 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 3803 | ArgType); |
| 3804 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3805 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3806 | if (!NTTP->hasDefaultArgument()) |
| 3807 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3808 | TemplateArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3809 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3810 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3811 | TemplateLoc, |
| 3812 | RAngleLoc, |
| 3813 | NTTP, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3814 | Converted); |
| 3815 | if (E.isInvalid()) |
| 3816 | return true; |
| 3817 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3818 | Expr *Ex = E.getAs<Expr>(); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3819 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 3820 | } else { |
| 3821 | TemplateTemplateParmDecl *TempParm |
| 3822 | = cast<TemplateTemplateParmDecl>(*Param); |
| 3823 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3824 | if (!TempParm->hasDefaultArgument()) |
| 3825 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3826 | TemplateArgs); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3827 | |
Douglas Gregor | df846d1 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3828 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3829 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3830 | TemplateLoc, |
| 3831 | RAngleLoc, |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3832 | TempParm, |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3833 | Converted, |
| 3834 | QualifierLoc); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3835 | if (Name.isNull()) |
| 3836 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3837 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3838 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3839 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3840 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3841 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3842 | // Introduce an instantiation record that describes where we are using |
| 3843 | // the default template argument. |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3844 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 3845 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3846 | if (Inst.isInvalid()) |
Richard Smith | 8a874c9 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3847 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3848 | |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3849 | // Check the default template argument. |
Douglas Gregor | eebed72 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3850 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3851 | RAngleLoc, 0, Converted)) |
Douglas Gregor | da0fb53 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3852 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3853 | |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3854 | // Core issue 150 (assumed resolution): if this is a template template |
| 3855 | // parameter, keep track of the default template arguments from the |
| 3856 | // template definition. |
| 3857 | if (isTemplateTemplateParameter) |
| 3858 | TemplateArgs.addArgument(Arg); |
| 3859 | |
Douglas Gregor | 9abeaf5 | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3860 | // Move to the next template parameter and argument. |
| 3861 | ++Param; |
| 3862 | ++ArgIdx; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3863 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3864 | |
Richard Smith | 07f7991 | 2014-06-06 16:00:50 +0000 | [diff] [blame] | 3865 | // If we're performing a partial argument substitution, allow any trailing |
| 3866 | // pack expansions; they might be empty. This can happen even if |
| 3867 | // PartialTemplateArgs is false (the list of arguments is complete but |
| 3868 | // still dependent). |
| 3869 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 3870 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
| 3871 | while (ArgIdx < NumArgs && |
| 3872 | TemplateArgs[ArgIdx].getArgument().isPackExpansion()) |
| 3873 | Converted.push_back(TemplateArgs[ArgIdx++].getArgument()); |
| 3874 | } |
| 3875 | |
Douglas Gregor | 8e07261 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3876 | // If we have any leftover arguments, then there were too many arguments. |
| 3877 | // Complain and fail. |
| 3878 | if (ArgIdx < NumArgs) |
| 3879 | return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3880 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 3881 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3884 | namespace { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3885 | class UnnamedLocalNoLinkageFinder |
| 3886 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3887 | { |
| 3888 | Sema &S; |
| 3889 | SourceRange SR; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3890 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3891 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3892 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3893 | public: |
| 3894 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3895 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3896 | bool Visit(QualType T) { |
| 3897 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3898 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3899 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3900 | #define TYPE(Class, Parent) \ |
| 3901 | bool Visit##Class##Type(const Class##Type *); |
| 3902 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 3903 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3904 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 3905 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3906 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3907 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3908 | bool VisitTagDecl(const TagDecl *Tag); |
| 3909 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3910 | }; |
| 3911 | } |
| 3912 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3913 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3914 | return false; |
| 3915 | } |
| 3916 | |
| 3917 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3918 | return Visit(T->getElementType()); |
| 3919 | } |
| 3920 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3921 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3922 | return Visit(T->getPointeeType()); |
| 3923 | } |
| 3924 | |
| 3925 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3926 | const BlockPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3927 | return Visit(T->getPointeeType()); |
| 3928 | } |
| 3929 | |
| 3930 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3931 | const LValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3932 | return Visit(T->getPointeeType()); |
| 3933 | } |
| 3934 | |
| 3935 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3936 | const RValueReferenceType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3937 | return Visit(T->getPointeeType()); |
| 3938 | } |
| 3939 | |
| 3940 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3941 | const MemberPointerType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3942 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3943 | } |
| 3944 | |
| 3945 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3946 | const ConstantArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3947 | return Visit(T->getElementType()); |
| 3948 | } |
| 3949 | |
| 3950 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3951 | const IncompleteArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3952 | return Visit(T->getElementType()); |
| 3953 | } |
| 3954 | |
| 3955 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3956 | const VariableArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3957 | return Visit(T->getElementType()); |
| 3958 | } |
| 3959 | |
| 3960 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3961 | const DependentSizedArrayType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3962 | return Visit(T->getElementType()); |
| 3963 | } |
| 3964 | |
| 3965 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3966 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3967 | return Visit(T->getElementType()); |
| 3968 | } |
| 3969 | |
| 3970 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3971 | return Visit(T->getElementType()); |
| 3972 | } |
| 3973 | |
| 3974 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 3975 | return Visit(T->getElementType()); |
| 3976 | } |
| 3977 | |
| 3978 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 3979 | const FunctionProtoType* T) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 3980 | for (const auto &A : T->param_types()) { |
| 3981 | if (Visit(A)) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3982 | return true; |
| 3983 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3984 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3985 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3986 | } |
| 3987 | |
| 3988 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 3989 | const FunctionNoProtoType* T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3990 | return Visit(T->getReturnType()); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3991 | } |
| 3992 | |
| 3993 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 3994 | const UnresolvedUsingType*) { |
| 3995 | return false; |
| 3996 | } |
| 3997 | |
| 3998 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 3999 | return false; |
| 4000 | } |
| 4001 | |
| 4002 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 4003 | return Visit(T->getUnderlyingType()); |
| 4004 | } |
| 4005 | |
| 4006 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 4007 | return false; |
| 4008 | } |
| 4009 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4010 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 4011 | const UnaryTransformType*) { |
| 4012 | return false; |
| 4013 | } |
| 4014 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4015 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 4016 | return Visit(T->getDeducedType()); |
| 4017 | } |
| 4018 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4019 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 4020 | return VisitTagDecl(T->getDecl()); |
| 4021 | } |
| 4022 | |
| 4023 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 4024 | return VisitTagDecl(T->getDecl()); |
| 4025 | } |
| 4026 | |
| 4027 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 4028 | const TemplateTypeParmType*) { |
| 4029 | return false; |
| 4030 | } |
| 4031 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4032 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 4033 | const SubstTemplateTypeParmPackType *) { |
| 4034 | return false; |
| 4035 | } |
| 4036 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4037 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 4038 | const TemplateSpecializationType*) { |
| 4039 | return false; |
| 4040 | } |
| 4041 | |
| 4042 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 4043 | const InjectedClassNameType* T) { |
| 4044 | return VisitTagDecl(T->getDecl()); |
| 4045 | } |
| 4046 | |
| 4047 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 4048 | const DependentNameType* T) { |
| 4049 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4050 | } |
| 4051 | |
| 4052 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 4053 | const DependentTemplateSpecializationType* T) { |
| 4054 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 4055 | } |
| 4056 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4057 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 4058 | const PackExpansionType* T) { |
| 4059 | return Visit(T->getPattern()); |
| 4060 | } |
| 4061 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4062 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 4063 | return false; |
| 4064 | } |
| 4065 | |
| 4066 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 4067 | const ObjCInterfaceType *) { |
| 4068 | return false; |
| 4069 | } |
| 4070 | |
| 4071 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 4072 | const ObjCObjectPointerType *) { |
| 4073 | return false; |
| 4074 | } |
| 4075 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4076 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 4077 | return Visit(T->getValueType()); |
| 4078 | } |
| 4079 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4080 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 4081 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4082 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4083 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4084 | diag::warn_cxx98_compat_template_arg_local_type : |
| 4085 | diag::ext_template_arg_local_type) |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4086 | << S.Context.getTypeDeclType(Tag) << SR; |
| 4087 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4088 | } |
| 4089 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 4090 | if (!Tag->hasNameForLinkage()) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4091 | S.Diag(SR.getBegin(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4092 | S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4093 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 4094 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4095 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 4096 | return true; |
| 4097 | } |
| 4098 | |
| 4099 | return false; |
| 4100 | } |
| 4101 | |
| 4102 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 4103 | NestedNameSpecifier *NNS) { |
| 4104 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 4105 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4106 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4107 | switch (NNS->getKind()) { |
| 4108 | case NestedNameSpecifier::Identifier: |
| 4109 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4110 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4111 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 4112 | case NestedNameSpecifier::Super: |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4113 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4114 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4115 | case NestedNameSpecifier::TypeSpec: |
| 4116 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 4117 | return Visit(QualType(NNS->getAsType(), 0)); |
| 4118 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4119 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
| 4122 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4123 | /// \brief Check a template argument against its corresponding |
| 4124 | /// template type parameter. |
| 4125 | /// |
| 4126 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 4127 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4128 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4129 | TypeSourceInfo *ArgInfo) { |
| 4130 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4131 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 959d5a0 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 4132 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 9bb67f4 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 4133 | |
| 4134 | if (Arg->isVariablyModifiedType()) { |
| 4135 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4136 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4137 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4138 | } |
| 4139 | |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4140 | // C++03 [temp.arg.type]p2: |
| 4141 | // A local type, a type with no linkage, an unnamed type or a type |
| 4142 | // compounded from any of these types shall not be used as a |
| 4143 | // template-argument for a template type-parameter. |
| 4144 | // |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4145 | // 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] | 4146 | // a warning. |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 4147 | bool NeedsCheck; |
| 4148 | if (LangOpts.CPlusPlus11) |
| 4149 | NeedsCheck = |
| 4150 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 4151 | SR.getBegin()) || |
| 4152 | !Diags.isIgnored(diag::warn_cxx98_compat_template_arg_local_type, |
| 4153 | SR.getBegin()); |
| 4154 | else |
| 4155 | NeedsCheck = Arg->hasUnnamedOrLocalType(); |
| 4156 | |
| 4157 | if (NeedsCheck) { |
Douglas Gregor | 7731d3f | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 4158 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 4159 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 4160 | } |
| 4161 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4162 | return false; |
| 4163 | } |
| 4164 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4165 | enum NullPointerValueKind { |
| 4166 | NPV_NotNullPointer, |
| 4167 | NPV_NullPointer, |
| 4168 | NPV_Error |
| 4169 | }; |
| 4170 | |
| 4171 | /// \brief Determine whether the given template argument is a null pointer |
| 4172 | /// value of the appropriate type. |
| 4173 | static NullPointerValueKind |
| 4174 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 4175 | QualType ParamType, Expr *Arg) { |
| 4176 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 4177 | return NPV_NotNullPointer; |
| 4178 | |
David Majnemer | 5c734ad | 2014-08-14 00:49:23 +0000 | [diff] [blame] | 4179 | if (!S.getLangOpts().CPlusPlus11) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4180 | return NPV_NotNullPointer; |
| 4181 | |
| 4182 | // Determine whether we have a constant expression. |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4183 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 4184 | if (ArgRV.isInvalid()) |
| 4185 | return NPV_Error; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4186 | Arg = ArgRV.get(); |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4187 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4188 | Expr::EvalResult EvalResult; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4189 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4190 | EvalResult.Diag = &Notes; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4191 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 350880c | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 4192 | EvalResult.HasSideEffects) { |
| 4193 | SourceLocation DiagLoc = Arg->getExprLoc(); |
| 4194 | |
| 4195 | // If our only note is the usual "invalid subexpression" note, just point |
| 4196 | // the caret at its location rather than producing an essentially |
| 4197 | // redundant note. |
| 4198 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 4199 | diag::note_invalid_subexpr_in_const_expr) { |
| 4200 | DiagLoc = Notes[0].first; |
| 4201 | Notes.clear(); |
| 4202 | } |
| 4203 | |
| 4204 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 4205 | << Arg->getType() << Arg->getSourceRange(); |
| 4206 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 4207 | S.Diag(Notes[I].first, Notes[I].second); |
| 4208 | |
| 4209 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4210 | return NPV_Error; |
| 4211 | } |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4212 | |
| 4213 | // C++11 [temp.arg.nontype]p1: |
| 4214 | // - an address constant expression of type std::nullptr_t |
| 4215 | if (Arg->getType()->isNullPtrType()) |
| 4216 | return NPV_NullPointer; |
| 4217 | |
| 4218 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 4219 | // - a constant expression that evaluates to a null member pointer value |
| 4220 | // (4.11); or |
| 4221 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 4222 | (EvalResult.Val.isMemberPointer() && |
| 4223 | !EvalResult.Val.getMemberPointerDecl())) { |
| 4224 | // If our expression has an appropriate type, we've succeeded. |
| 4225 | bool ObjCLifetimeConversion; |
| 4226 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 4227 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 4228 | ObjCLifetimeConversion)) |
| 4229 | return NPV_NullPointer; |
| 4230 | |
| 4231 | // The types didn't match, but we know we got a null pointer; complain, |
| 4232 | // then recover as if the types were correct. |
| 4233 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 4234 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4235 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4236 | return NPV_NullPointer; |
| 4237 | } |
| 4238 | |
| 4239 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 4240 | // constant, suggest a cast to the appropriate type. |
| 4241 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 4242 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 4243 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4244 | << ParamType << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 4245 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getLocEnd()), |
| 4246 | ")"); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4247 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4248 | return NPV_NullPointer; |
| 4249 | } |
| 4250 | |
| 4251 | // FIXME: If we ever want to support general, address-constant expressions |
| 4252 | // as non-type template arguments, we should return the ExprResult here to |
| 4253 | // be interpreted by the caller. |
| 4254 | return NPV_NotNullPointer; |
| 4255 | } |
| 4256 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4257 | /// \brief Checks whether the given template argument is compatible with its |
| 4258 | /// template parameter. |
| 4259 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 4260 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 4261 | Expr *Arg, QualType ArgType) { |
| 4262 | bool ObjCLifetimeConversion; |
| 4263 | if (ParamType->isPointerType() && |
| 4264 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 4265 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 4266 | ObjCLifetimeConversion)) { |
| 4267 | // For pointer-to-object types, qualification conversions are |
| 4268 | // permitted. |
| 4269 | } else { |
| 4270 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 4271 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 4272 | // C++ [temp.arg.nontype]p5b3: |
| 4273 | // For a non-type template-parameter of type reference to |
| 4274 | // object, no conversions apply. The type referred to by the |
| 4275 | // reference may be more cv-qualified than the (otherwise |
| 4276 | // identical) type of the template- argument. The |
| 4277 | // template-parameter is bound directly to the |
| 4278 | // template-argument, which shall be an lvalue. |
| 4279 | |
| 4280 | // FIXME: Other qualifiers? |
| 4281 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 4282 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 4283 | |
| 4284 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 4285 | S.Diag(Arg->getLocStart(), |
| 4286 | diag::err_template_arg_ref_bind_ignores_quals) |
| 4287 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 4288 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4289 | return true; |
| 4290 | } |
| 4291 | } |
| 4292 | } |
| 4293 | |
| 4294 | // At this point, the template argument refers to an object or |
| 4295 | // function with external linkage. We now need to check whether the |
| 4296 | // argument and parameter types are compatible. |
| 4297 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 4298 | ParamType.getNonReferenceType())) { |
| 4299 | // We can't perform this conversion or binding. |
| 4300 | if (ParamType->isReferenceType()) |
| 4301 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 4302 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
| 4303 | else |
| 4304 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4305 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
| 4306 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4307 | return true; |
| 4308 | } |
| 4309 | } |
| 4310 | |
| 4311 | return false; |
| 4312 | } |
| 4313 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4314 | /// \brief Checks whether the given template argument is the address |
| 4315 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4316 | static bool |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4317 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 4318 | NonTypeTemplateParmDecl *Param, |
| 4319 | QualType ParamType, |
| 4320 | Expr *ArgIn, |
| 4321 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4322 | bool Invalid = false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4323 | Expr *Arg = ArgIn; |
| 4324 | QualType ArgType = Arg->getType(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4325 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4326 | bool AddressTaken = false; |
| 4327 | SourceLocation AddrOpLoc; |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4328 | if (S.getLangOpts().MicrosoftExt) { |
| 4329 | // Microsoft Visual C++ strips all casts, allows an arbitrary number of |
| 4330 | // dereference and address-of operators. |
| 4331 | Arg = Arg->IgnoreParenCasts(); |
| 4332 | |
| 4333 | bool ExtWarnMSTemplateArg = false; |
| 4334 | UnaryOperatorKind FirstOpKind; |
| 4335 | SourceLocation FirstOpLoc; |
| 4336 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4337 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 4338 | if (UnOpKind == UO_Deref) |
| 4339 | ExtWarnMSTemplateArg = true; |
| 4340 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 4341 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 4342 | if (!AddrOpLoc.isValid()) { |
| 4343 | FirstOpKind = UnOpKind; |
| 4344 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 4345 | } |
| 4346 | } else |
| 4347 | break; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4348 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4349 | if (FirstOpLoc.isValid()) { |
| 4350 | if (ExtWarnMSTemplateArg) |
| 4351 | S.Diag(ArgIn->getLocStart(), diag::ext_ms_deref_template_argument) |
| 4352 | << ArgIn->getSourceRange(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4353 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4354 | if (FirstOpKind == UO_AddrOf) |
| 4355 | AddressTaken = true; |
| 4356 | else if (Arg->getType()->isPointerType()) { |
| 4357 | // We cannot let pointers get dereferenced here, that is obviously not a |
| 4358 | // constant expression. |
| 4359 | assert(FirstOpKind == UO_Deref); |
| 4360 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4361 | << Arg->getSourceRange(); |
| 4362 | } |
| 4363 | } |
| 4364 | } else { |
| 4365 | // See through any implicit casts we added to fix the type. |
| 4366 | Arg = Arg->IgnoreImpCasts(); |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4367 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4368 | // C++ [temp.arg.nontype]p1: |
| 4369 | // |
| 4370 | // A template-argument for a non-type, non-template |
| 4371 | // template-parameter shall be one of: [...] |
| 4372 | // |
| 4373 | // -- the address of an object or function with external |
| 4374 | // linkage, including function templates and function |
| 4375 | // template-ids but excluding non-static class members, |
| 4376 | // expressed as & id-expression where the & is optional if |
| 4377 | // the name refers to a function or array, or if the |
| 4378 | // corresponding template-parameter is a reference; or |
| 4379 | |
| 4380 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4381 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4382 | bool ExtraParens = false; |
| 4383 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 4384 | if (!Invalid && !ExtraParens) { |
| 4385 | S.Diag(Arg->getLocStart(), |
| 4386 | S.getLangOpts().CPlusPlus11 |
| 4387 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 4388 | : diag::ext_template_arg_extra_parens) |
| 4389 | << Arg->getSourceRange(); |
| 4390 | ExtraParens = true; |
| 4391 | } |
| 4392 | |
| 4393 | Arg = Parens->getSubExpr(); |
| 4394 | } |
| 4395 | |
| 4396 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4397 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4398 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4399 | |
| 4400 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 4401 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 4402 | Arg = UnOp->getSubExpr(); |
| 4403 | AddressTaken = true; |
| 4404 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 4405 | } |
| 4406 | } |
| 4407 | |
| 4408 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4409 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4410 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4411 | } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4412 | |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4413 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 4414 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 4415 | |
| 4416 | // If our parameter has pointer type, check for a null template value. |
| 4417 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 4418 | NullPointerValueKind NPV; |
| 4419 | // dllimport'd entities aren't constant but are available inside of template |
| 4420 | // arguments. |
| 4421 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 4422 | NPV = NPV_NotNullPointer; |
| 4423 | else |
| 4424 | NPV = isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn); |
| 4425 | switch (NPV) { |
| 4426 | case NPV_NullPointer: |
| 4427 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4428 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4429 | /*isNullPtr=*/true); |
David Majnemer | 07910d6 | 2014-06-26 07:48:46 +0000 | [diff] [blame] | 4430 | return false; |
| 4431 | |
| 4432 | case NPV_Error: |
| 4433 | return true; |
| 4434 | |
| 4435 | case NPV_NotNullPointer: |
| 4436 | break; |
| 4437 | } |
| 4438 | } |
| 4439 | |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4440 | // Stop checking the precise nature of the argument if it is value dependent, |
| 4441 | // it should be checked when instantiated. |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4442 | if (Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4443 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4444 | return false; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4445 | } |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4446 | |
| 4447 | if (isa<CXXUuidofExpr>(Arg)) { |
| 4448 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 4449 | ArgIn, Arg, ArgType)) |
| 4450 | return true; |
| 4451 | |
| 4452 | Converted = TemplateArgument(ArgIn); |
| 4453 | return false; |
| 4454 | } |
| 4455 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4456 | if (!DRE) { |
| 4457 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 4458 | << Arg->getSourceRange(); |
| 4459 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4460 | return true; |
| 4461 | } |
Chandler Carruth | 724a8a1 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 4462 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4463 | // Cannot refer to non-static data members |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4464 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4465 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
David Majnemer | 6bedcfa | 2013-10-26 06:12:44 +0000 | [diff] [blame] | 4466 | << Entity << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4467 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4468 | return true; |
| 4469 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4470 | |
| 4471 | // Cannot refer to non-static member functions |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4472 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4473 | if (!Method->isStatic()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4474 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4475 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4476 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4477 | return true; |
| 4478 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4479 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4480 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4481 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 4482 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4483 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4484 | // A non-type template argument must refer to an object or function. |
| 4485 | if (!Func && !Var) { |
| 4486 | // We found something, but we don't know specifically what it is. |
| 4487 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 4488 | << Arg->getSourceRange(); |
| 4489 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 4490 | return true; |
| 4491 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4492 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4493 | // Address / reference template args must have external linkage in C++98. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4494 | if (Entity->getFormalLinkage() == InternalLinkage) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4495 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus11 ? |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4496 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 4497 | diag::ext_template_arg_object_internal) |
| 4498 | << !Func << Entity << Arg->getSourceRange(); |
| 4499 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4500 | << !Func; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 4501 | } else if (!Entity->hasLinkage()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4502 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 4503 | << !Func << Entity << Arg->getSourceRange(); |
| 4504 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 4505 | << !Func; |
| 4506 | return true; |
| 4507 | } |
| 4508 | |
| 4509 | if (Func) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4510 | // If the template parameter has pointer type, the function decays. |
| 4511 | if (ParamType->isPointerType() && !AddressTaken) |
| 4512 | ArgType = S.Context.getPointerType(Func->getType()); |
| 4513 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 4514 | // If we originally had an address-of operator, but the |
| 4515 | // parameter has reference type, complain and (if things look |
| 4516 | // like they will work) drop the address-of operator. |
| 4517 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 4518 | ParamType.getNonReferenceType())) { |
| 4519 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4520 | << ParamType; |
| 4521 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4522 | return true; |
| 4523 | } |
| 4524 | |
| 4525 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4526 | << ParamType |
| 4527 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4528 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4529 | |
| 4530 | ArgType = Func->getType(); |
| 4531 | } |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4532 | } else { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4533 | // A value of reference type is not an object. |
| 4534 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4535 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4536 | diag::err_template_arg_reference_var) |
| 4537 | << Var->getType() << Arg->getSourceRange(); |
| 4538 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4539 | return true; |
| 4540 | } |
| 4541 | |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4542 | // A template argument must have static storage duration. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 4543 | if (Var->getTLSKind()) { |
Richard Smith | 9380e0e | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 4544 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 4545 | << Arg->getSourceRange(); |
| 4546 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 4547 | return true; |
| 4548 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4549 | |
| 4550 | // If the template parameter has pointer type, we must have taken |
| 4551 | // the address of this object. |
| 4552 | if (ParamType->isReferenceType()) { |
| 4553 | if (AddressTaken) { |
| 4554 | // If we originally had an address-of operator, but the |
| 4555 | // parameter has reference type, complain and (if things look |
| 4556 | // like they will work) drop the address-of operator. |
| 4557 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 4558 | ParamType.getNonReferenceType())) { |
| 4559 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4560 | << ParamType; |
| 4561 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4562 | return true; |
| 4563 | } |
| 4564 | |
| 4565 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 4566 | << ParamType |
| 4567 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 4568 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4569 | |
| 4570 | ArgType = Var->getType(); |
| 4571 | } |
| 4572 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 4573 | if (Var->getType()->isArrayType()) { |
| 4574 | // Array-to-pointer decay. |
| 4575 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 4576 | } else { |
| 4577 | // If the template parameter has pointer type but the address of |
| 4578 | // this object was not taken, complain and (possibly) recover by |
| 4579 | // taking the address of the entity. |
| 4580 | ArgType = S.Context.getPointerType(Var->getType()); |
| 4581 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 4582 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4583 | << ParamType; |
| 4584 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4585 | return true; |
| 4586 | } |
| 4587 | |
| 4588 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 4589 | << ParamType |
| 4590 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 4591 | |
| 4592 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4593 | } |
| 4594 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4595 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4596 | |
David Majnemer | 61c39a1 | 2013-08-23 05:39:39 +0000 | [diff] [blame] | 4597 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 4598 | Arg, ArgType)) |
| 4599 | return true; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4600 | |
| 4601 | // Create the template argument. |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4602 | Converted = |
| 4603 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 4604 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity, false); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4605 | return false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4606 | } |
| 4607 | |
| 4608 | /// \brief Checks whether the given template argument is a pointer to |
| 4609 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4610 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 4611 | NonTypeTemplateParmDecl *Param, |
| 4612 | QualType ParamType, |
| 4613 | Expr *&ResultArg, |
| 4614 | TemplateArgument &Converted) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4615 | bool Invalid = false; |
| 4616 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4617 | // Check for a null pointer value. |
| 4618 | Expr *Arg = ResultArg; |
| 4619 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 4620 | case NPV_Error: |
| 4621 | return true; |
| 4622 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 4623 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 4624 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 4625 | /*isNullPtr*/true); |
David Majnemer | 763584d | 2014-02-06 10:59:19 +0000 | [diff] [blame] | 4626 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 4627 | S.RequireCompleteType(Arg->getExprLoc(), ParamType, 0); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4628 | return false; |
| 4629 | case NPV_NotNullPointer: |
| 4630 | break; |
| 4631 | } |
| 4632 | |
| 4633 | bool ObjCLifetimeConversion; |
| 4634 | if (S.IsQualificationConversion(Arg->getType(), |
| 4635 | ParamType.getNonReferenceType(), |
| 4636 | false, ObjCLifetimeConversion)) { |
| 4637 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4638 | Arg->getValueKind()).get(); |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4639 | ResultArg = Arg; |
| 4640 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), |
| 4641 | ParamType.getNonReferenceType())) { |
| 4642 | // We can't perform this conversion. |
| 4643 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 4644 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 4645 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 4646 | return true; |
| 4647 | } |
| 4648 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4649 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4650 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4651 | Arg = Cast->getSubExpr(); |
| 4652 | |
| 4653 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4654 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4655 | // A template-argument for a non-type, non-template |
| 4656 | // template-parameter shall be one of: [...] |
| 4657 | // |
| 4658 | // -- a pointer to member expressed as described in 5.3.1. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4659 | DeclRefExpr *DRE = nullptr; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4660 | |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4661 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 4662 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 4663 | bool ExtraParens = false; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4664 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4665 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4666 | S.Diag(Arg->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4667 | S.getLangOpts().CPlusPlus11 ? |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4668 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 4669 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4670 | << Arg->getSourceRange(); |
Abramo Bagnara | 6a0c409 | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 4671 | ExtraParens = true; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4672 | } |
| 4673 | |
| 4674 | Arg = Parens->getSubExpr(); |
| 4675 | } |
| 4676 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4677 | while (SubstNonTypeTemplateParmExpr *subst = |
| 4678 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 4679 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 4680 | |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4681 | // A pointer-to-member constant written &Class::member. |
| 4682 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4683 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4684 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 4685 | if (DRE && !DRE->getQualifier()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4686 | DRE = nullptr; |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 4687 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4688 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4689 | // A constant of pointer-to-member type. |
| 4690 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 4691 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 4692 | if (VD->getType()->isMemberPointerType()) { |
David Majnemer | cd053cd | 2013-12-10 00:40:58 +0000 | [diff] [blame] | 4693 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4694 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4695 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4696 | } else { |
| 4697 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4698 | Converted = TemplateArgument(VD, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4699 | } |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4700 | return Invalid; |
| 4701 | } |
| 4702 | } |
| 4703 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4704 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4705 | DRE = nullptr; |
Douglas Gregor | 49ba3ca | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4706 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4707 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4708 | if (!DRE) |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4709 | return S.Diag(Arg->getLocStart(), |
| 4710 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4711 | << Arg->getSourceRange(); |
| 4712 | |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4713 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 4714 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 4715 | isa<CXXMethodDecl>(DRE->getDecl())) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4716 | assert((isa<FieldDecl>(DRE->getDecl()) || |
David Majnemer | 3ac84e6 | 2013-10-22 21:56:38 +0000 | [diff] [blame] | 4717 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4718 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 4719 | "Only non-static member pointers can make it here"); |
| 4720 | |
| 4721 | // Okay: this is the address of a non-static member, and therefore |
| 4722 | // a member pointer constant. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4723 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4724 | Converted = TemplateArgument(Arg); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4725 | } else { |
| 4726 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 4727 | Converted = TemplateArgument(D, ParamType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4728 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4729 | return Invalid; |
| 4730 | } |
| 4731 | |
| 4732 | // 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] | 4733 | S.Diag(Arg->getLocStart(), |
| 4734 | diag::err_template_arg_not_pointer_to_member_form) |
| 4735 | << Arg->getSourceRange(); |
| 4736 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4737 | return true; |
| 4738 | } |
| 4739 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4740 | /// \brief Check a template argument against its corresponding |
| 4741 | /// non-type template parameter. |
| 4742 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4743 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4744 | /// If an error occurred, it returns ExprError(); otherwise, it |
| 4745 | /// returns the converted template argument. \p |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4746 | /// InstantiatedParamType is the type of the non-type template |
| 4747 | /// parameter after it has been instantiated. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4748 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 4749 | QualType InstantiatedParamType, Expr *Arg, |
| 4750 | TemplateArgument &Converted, |
| 4751 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4752 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4753 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4754 | // If either the parameter has a dependent type or the argument is |
| 4755 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4756 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 4757 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 4758 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4759 | return Arg; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4760 | } |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4761 | |
| 4762 | // C++ [temp.arg.nontype]p5: |
| 4763 | // The following conversions are performed on each expression used |
| 4764 | // as a non-type template-argument. If a non-type |
| 4765 | // template-argument cannot be converted to the type of the |
| 4766 | // corresponding template-parameter then the program is |
| 4767 | // ill-formed. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4768 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4769 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4770 | // C++11: |
| 4771 | // -- for a non-type template-parameter of integral or |
| 4772 | // enumeration type, conversions permitted in a converted |
| 4773 | // constant expression are applied. |
| 4774 | // |
| 4775 | // C++98: |
| 4776 | // -- for a non-type template-parameter of integral or |
| 4777 | // enumeration type, integral promotions (4.5) and integral |
| 4778 | // conversions (4.7) are applied. |
| 4779 | |
| 4780 | if (CTAK == CTAK_Deduced && |
| 4781 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 4782 | // C++ [temp.deduct.type]p17: |
| 4783 | // If, in the declaration of a function template with a non-type |
| 4784 | // template-parameter, the non-type template-parameter is used |
| 4785 | // in an expression in the function parameter-list and, if the |
| 4786 | // corresponding template-argument is deduced, the |
| 4787 | // template-argument type shall match the type of the |
| 4788 | // template-parameter exactly, except that a template-argument |
| 4789 | // deduced from an array bound may be of any integral type. |
| 4790 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 4791 | << Arg->getType().getUnqualifiedType() |
| 4792 | << ParamType.getUnqualifiedType(); |
| 4793 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4794 | return ExprError(); |
| 4795 | } |
| 4796 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4797 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4798 | // We can't check arbitrary value-dependent arguments. |
| 4799 | // FIXME: If there's no viable conversion to the template parameter type, |
| 4800 | // we should be able to diagnose that prior to instantiation. |
| 4801 | if (Arg->isValueDependent()) { |
| 4802 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4803 | return Arg; |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4804 | } |
| 4805 | |
| 4806 | // C++ [temp.arg.nontype]p1: |
| 4807 | // A template-argument for a non-type, non-template template-parameter |
| 4808 | // shall be one of: |
| 4809 | // |
| 4810 | // -- for a non-type template-parameter of integral or enumeration |
| 4811 | // type, a converted constant expression of the type of the |
| 4812 | // template-parameter; or |
| 4813 | llvm::APSInt Value; |
| 4814 | ExprResult ArgResult = |
| 4815 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 4816 | CCEK_TemplateArg); |
| 4817 | if (ArgResult.isInvalid()) |
| 4818 | return ExprError(); |
| 4819 | |
| 4820 | // Widen the argument value to sizeof(parameter type). This is almost |
| 4821 | // always a no-op, except when the parameter type is bool. In |
| 4822 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 4823 | QualType IntegerType = ParamType; |
| 4824 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 4825 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 4826 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 4827 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4828 | Converted = TemplateArgument(Context, Value, |
| 4829 | Context.getCanonicalType(ParamType)); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4830 | return ArgResult; |
| 4831 | } |
| 4832 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4833 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 4834 | if (ArgResult.isInvalid()) |
| 4835 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4836 | Arg = ArgResult.get(); |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4837 | |
| 4838 | QualType ArgType = Arg->getType(); |
| 4839 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4840 | // C++ [temp.arg.nontype]p1: |
| 4841 | // A template-argument for a non-type, non-template |
| 4842 | // template-parameter shall be one of: |
| 4843 | // |
| 4844 | // -- an integral constant-expression of integral or enumeration |
| 4845 | // type; or |
| 4846 | // -- the name of a non-type template-parameter; or |
| 4847 | SourceLocation NonConstantLoc; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4848 | llvm::APSInt Value; |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4849 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4850 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4851 | diag::err_template_arg_not_integral_or_enumeral) |
| 4852 | << ArgType << Arg->getSourceRange(); |
| 4853 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4854 | return ExprError(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4855 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 4856 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 4857 | QualType T; |
| 4858 | |
| 4859 | public: |
| 4860 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 4861 | |
| 4862 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 4863 | SourceRange SR) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 4864 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 4865 | } |
| 4866 | } Diagnoser(ArgType); |
| 4867 | |
| 4868 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4869 | false).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4870 | if (!Arg) |
| 4871 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4872 | } |
| 4873 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4874 | // From here on out, all we care about are the unqualified forms |
| 4875 | // of the parameter and argument types. |
| 4876 | ParamType = ParamType.getUnqualifiedType(); |
| 4877 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4878 | |
| 4879 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 4880 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4881 | // Okay: no conversion necessary |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4882 | } else if (ParamType->isBooleanType()) { |
| 4883 | // This is an integral-to-boolean conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4884 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4885 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 4886 | !ParamType->isEnumeralType()) { |
| 4887 | // This is an integral promotion or conversion. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4888 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4889 | } else { |
| 4890 | // We can't perform this conversion. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4891 | Diag(Arg->getLocStart(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4892 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4893 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4894 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4895 | return ExprError(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4896 | } |
| 4897 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4898 | // Add the value of this argument to the list of converted |
| 4899 | // arguments. We use the bitwidth and signedness of the template |
| 4900 | // parameter. |
| 4901 | if (Arg->isValueDependent()) { |
| 4902 | // The argument is value-dependent. Create a new |
| 4903 | // TemplateArgument with the converted expression. |
| 4904 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4905 | return Arg; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4906 | } |
| 4907 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4908 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4909 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 4910 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4911 | |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4912 | if (ParamType->isBooleanType()) { |
| 4913 | // Value must be zero or one. |
| 4914 | Value = Value != 0; |
| 4915 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 4916 | if (Value.getBitWidth() != AllowedBits) |
| 4917 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4918 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4919 | } else { |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4920 | llvm::APSInt OldValue = Value; |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4921 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4922 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4923 | // based on the template parameter's type. |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 4924 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | a14cb9f | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 4925 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4926 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4927 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4928 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4929 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4930 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4931 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4932 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4933 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 4934 | << Arg->getSourceRange(); |
| 4935 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4936 | } |
Douglas Gregor | b4f4d51 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4937 | |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4938 | // Complain if we overflowed the template parameter's type. |
| 4939 | unsigned RequiredBits; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4940 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4941 | RequiredBits = OldValue.getActiveBits(); |
| 4942 | else if (OldValue.isUnsigned()) |
| 4943 | RequiredBits = OldValue.getActiveBits() + 1; |
| 4944 | else |
| 4945 | RequiredBits = OldValue.getMinSignedBits(); |
| 4946 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4947 | Diag(Arg->getLocStart(), |
Douglas Gregor | bb3d786 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4948 | diag::warn_template_arg_too_large) |
| 4949 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 4950 | << Arg->getSourceRange(); |
| 4951 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4952 | } |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4953 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4954 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4955 | Converted = TemplateArgument(Context, Value, |
Douglas Gregor | 3d63a9e | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 4956 | ParamType->isEnumeralType() |
| 4957 | ? Context.getCanonicalType(ParamType) |
| 4958 | : IntegerType); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4959 | return Arg; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4960 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4961 | |
Richard Smith | 08b12f1 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4962 | QualType ArgType = Arg->getType(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4963 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 4964 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4965 | // Handle pointer-to-function, reference-to-function, and |
| 4966 | // pointer-to-member-function all in (roughly) the same way. |
| 4967 | if (// -- For a non-type template-parameter of type pointer to |
| 4968 | // function, only the function-to-pointer conversion (4.3) is |
| 4969 | // applied. If the template-argument represents a set of |
| 4970 | // overloaded functions (or a pointer to such), the matching |
| 4971 | // function is selected from the set (13.4). |
| 4972 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4973 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4974 | // -- For a non-type template-parameter of type reference to |
| 4975 | // function, no conversions apply. If the template-argument |
| 4976 | // represents a set of overloaded functions, the matching |
| 4977 | // function is selected from the set (13.4). |
| 4978 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4979 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4980 | // -- For a non-type template-parameter of type pointer to |
| 4981 | // member function, no conversions apply. If the |
| 4982 | // template-argument represents a set of overloaded member |
| 4983 | // functions, the matching member function is selected from |
| 4984 | // the set (13.4). |
| 4985 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4986 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4987 | ->isFunctionType())) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4988 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4989 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4990 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4991 | true, |
| 4992 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4993 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4994 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4995 | |
| 4996 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 4997 | ArgType = Arg->getType(); |
| 4998 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4999 | return ExprError(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5000 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5001 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5002 | if (!ParamType->isMemberPointerType()) { |
| 5003 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5004 | ParamType, |
| 5005 | Arg, Converted)) |
| 5006 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5007 | return Arg; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5008 | } |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5009 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5010 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5011 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5012 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5013 | return Arg; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 5014 | } |
| 5015 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 5016 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5017 | // -- for a non-type template-parameter of type pointer to |
| 5018 | // object, qualification conversions (4.4) and the |
| 5019 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5020 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5021 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5022 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5023 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5024 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5025 | ParamType, |
| 5026 | Arg, Converted)) |
| 5027 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5028 | return Arg; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5029 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5030 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5031 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5032 | // -- For a non-type template-parameter of type reference to |
| 5033 | // object, no conversions apply. The type referred to by the |
| 5034 | // reference may be more cv-qualified than the (otherwise |
| 5035 | // identical) type of the template-argument. The |
| 5036 | // template-parameter is bound directly to the |
| 5037 | // template-argument, which must be an lvalue. |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5038 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5039 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 5040 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5041 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5042 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 5043 | ParamRefType->getPointeeType(), |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5044 | true, |
| 5045 | FoundResult)) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5046 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5047 | return ExprError(); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5048 | |
| 5049 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 5050 | ArgType = Arg->getType(); |
| 5051 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5052 | return ExprError(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5053 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5054 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5055 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 5056 | ParamType, |
| 5057 | Arg, Converted)) |
| 5058 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5059 | return Arg; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 5060 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5061 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5062 | // Deal with parameters of type std::nullptr_t. |
| 5063 | if (ParamType->isNullPtrType()) { |
| 5064 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 5065 | Converted = TemplateArgument(Arg); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5066 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5067 | } |
| 5068 | |
| 5069 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 5070 | case NPV_NotNullPointer: |
| 5071 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 5072 | << Arg->getType() << ParamType; |
| 5073 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 5074 | return ExprError(); |
| 5075 | |
| 5076 | case NPV_Error: |
| 5077 | return ExprError(); |
| 5078 | |
| 5079 | case NPV_NullPointer: |
Richard Smith | bc8c5b5 | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 5080 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Richard Smith | 78bb36f | 2014-07-24 02:27:39 +0000 | [diff] [blame] | 5081 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 5082 | /*isNullPtr*/true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5083 | return Arg; |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5084 | } |
| 5085 | } |
| 5086 | |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 5087 | // -- For a non-type template-parameter of type pointer to data |
| 5088 | // member, qualification conversions (4.4) are applied. |
| 5089 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 5090 | |
Douglas Gregor | 20fdef3 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 5091 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 5092 | Converted)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5093 | return ExprError(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5094 | return Arg; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | /// \brief Check a template argument against its corresponding |
| 5098 | /// template template parameter. |
| 5099 | /// |
| 5100 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 5101 | /// It returns true if an error occurred, and false otherwise. |
| 5102 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Reid Kleckner | 377c159 | 2014-06-10 23:29:48 +0000 | [diff] [blame] | 5103 | TemplateArgumentLoc &Arg, |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5104 | unsigned ArgumentPackIndex) { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5105 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5106 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 5107 | if (!Template) { |
| 5108 | // Any dependent template name is fine. |
| 5109 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 5110 | return false; |
| 5111 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5112 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5113 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5114 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5115 | // the name of a class template or an alias template, expressed as an |
| 5116 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5117 | // primary class templates are considered when matching the |
| 5118 | // template template argument with the corresponding parameter; |
| 5119 | // partial specializations are not considered even if their |
| 5120 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5121 | // |
| 5122 | // Note that we also allow template template parameters here, which |
| 5123 | // will happen when we are dealing with, e.g., class template |
| 5124 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5125 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5126 | !isa<TemplateTemplateParmDecl>(Template) && |
| 5127 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5128 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5129 | "Only function templates are possible here"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5130 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 5131 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5132 | << Template; |
| 5133 | } |
| 5134 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5135 | TemplateParameterList *Params = Param->getTemplateParameters(); |
| 5136 | if (Param->isExpandedParameterPack()) |
| 5137 | Params = Param->getExpansionTemplateParameters(ArgumentPackIndex); |
| 5138 | |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5139 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 5140 | Params, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5141 | true, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5142 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5143 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 5144 | } |
| 5145 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5146 | /// \brief Given a non-type template argument that refers to a |
| 5147 | /// declaration and the type of its corresponding non-type template |
| 5148 | /// parameter, produce an expression that properly refers to that |
| 5149 | /// declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5150 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5151 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 5152 | QualType ParamType, |
| 5153 | SourceLocation Loc) { |
David Blaikie | dc601e3 | 2013-02-27 22:10:40 +0000 | [diff] [blame] | 5154 | // C++ [temp.param]p8: |
| 5155 | // |
| 5156 | // A non-type template-parameter of type "array of T" or |
| 5157 | // "function returning T" is adjusted to be of type "pointer to |
| 5158 | // T" or "pointer to function returning T", respectively. |
| 5159 | if (ParamType->isArrayType()) |
| 5160 | ParamType = Context.getArrayDecayedType(ParamType); |
| 5161 | else if (ParamType->isFunctionType()) |
| 5162 | ParamType = Context.getPointerType(ParamType); |
| 5163 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5164 | // For a NULL non-type template argument, return nullptr casted to the |
| 5165 | // parameter's type. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5166 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5167 | return ImpCastExprToType( |
| 5168 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 5169 | ParamType, |
| 5170 | ParamType->getAs<MemberPointerType>() |
| 5171 | ? CK_NullToMemberPointer |
| 5172 | : CK_NullToPointer); |
| 5173 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5174 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 5175 | "Only declaration template arguments permitted here"); |
| 5176 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5177 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 5178 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5179 | if (VD->getDeclContext()->isRecord() && |
David Majnemer | 3ae0bfa | 2013-10-26 05:02:13 +0000 | [diff] [blame] | 5180 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 5181 | isa<IndirectFieldDecl>(VD))) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5182 | // If the value is a class member, we might have a pointer-to-member. |
| 5183 | // Determine whether the non-type template template parameter is of |
| 5184 | // pointer-to-member type. If so, we need to build an appropriate |
| 5185 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 5186 | // would refer to the member itself. |
| 5187 | if (ParamType->isMemberPointerType()) { |
| 5188 | QualType ClassType |
| 5189 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 5190 | NestedNameSpecifier *Qualifier |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5191 | = NestedNameSpecifier::Create(Context, nullptr, false, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5192 | ClassType.getTypePtr()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5193 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 5194 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5195 | |
| 5196 | // The actual value-ness of this is unimportant, but for |
| 5197 | // internal consistency's sake, references to instance methods |
| 5198 | // are r-values. |
| 5199 | ExprValueKind VK = VK_LValue; |
| 5200 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 5201 | VK = VK_RValue; |
| 5202 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5203 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5204 | VD->getType().getNonReferenceType(), |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 5205 | VK, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5206 | Loc, |
| 5207 | &SS); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5208 | if (RefExpr.isInvalid()) |
| 5209 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5210 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5211 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5212 | |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5213 | // We might need to perform a trailing qualification conversion, since |
| 5214 | // the element type on the parameter could be more qualified than the |
| 5215 | // element type in the expression we constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5216 | bool ObjCLifetimeConversion; |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5217 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5218 | ParamType.getUnqualifiedType(), false, |
| 5219 | ObjCLifetimeConversion)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5220 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5221 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5222 | assert(!RefExpr.isInvalid() && |
| 5223 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | fabf95d | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 5224 | ParamType.getUnqualifiedType())); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5225 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5226 | } |
| 5227 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5228 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5229 | QualType T = VD->getType().getNonReferenceType(); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5230 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5231 | if (ParamType->isPointerType()) { |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5232 | // When the non-type template parameter is a pointer, take the |
| 5233 | // address of the declaration. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5234 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5235 | if (RefExpr.isInvalid()) |
| 5236 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5237 | |
| 5238 | if (T->isFunctionType() || T->isArrayType()) { |
| 5239 | // Decay functions and arrays. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5240 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5241 | if (RefExpr.isInvalid()) |
| 5242 | return ExprError(); |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5243 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5244 | return RefExpr; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5245 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5246 | |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5247 | // Take the address of everything else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5248 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5249 | } |
| 5250 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5251 | ExprValueKind VK = VK_RValue; |
| 5252 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5253 | // If the non-type template parameter has reference type, qualify the |
| 5254 | // resulting declaration reference with the extra qualifiers on the |
| 5255 | // type that the reference refers to. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5256 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 5257 | VK = VK_LValue; |
| 5258 | T = Context.getQualifiedType(T, |
| 5259 | TargetRef->getPointeeType().getQualifiers()); |
Douglas Gregor | effe2a1 | 2013-01-16 00:52:15 +0000 | [diff] [blame] | 5260 | } else if (isa<FunctionDecl>(VD)) { |
| 5261 | // References to functions are always lvalues. |
| 5262 | VK = VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5263 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5264 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5265 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5266 | } |
| 5267 | |
| 5268 | /// \brief Construct a new expression that refers to the given |
| 5269 | /// integral template argument with the given source-location |
| 5270 | /// information. |
| 5271 | /// |
| 5272 | /// This routine takes care of the mapping from an integral template |
| 5273 | /// argument (which may have any integral type) to the appropriate |
| 5274 | /// literal value. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5275 | ExprResult |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5276 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 5277 | SourceLocation Loc) { |
| 5278 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 5279 | "Operation is only valid for integral template arguments"); |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5280 | QualType OrigT = Arg.getIntegralType(); |
| 5281 | |
| 5282 | // If this is an enum type that we're instantiating, we need to use an integer |
| 5283 | // type the same size as the enumerator. We don't want to build an |
| 5284 | // IntegerLiteral with enum type. The integer type of an enum type can be of |
| 5285 | // any integral type with C++11 enum classes, make sure we create the right |
| 5286 | // type of literal for it. |
| 5287 | QualType T = OrigT; |
| 5288 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 5289 | T = ET->getDecl()->getIntegerType(); |
| 5290 | |
| 5291 | Expr *E; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5292 | if (T->isAnyCharacterType()) { |
| 5293 | CharacterLiteral::CharacterKind Kind; |
| 5294 | if (T->isWideCharType()) |
| 5295 | Kind = CharacterLiteral::Wide; |
| 5296 | else if (T->isChar16Type()) |
| 5297 | Kind = CharacterLiteral::UTF16; |
| 5298 | else if (T->isChar32Type()) |
| 5299 | Kind = CharacterLiteral::UTF32; |
| 5300 | else |
| 5301 | Kind = CharacterLiteral::Ascii; |
| 5302 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5303 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 5304 | Kind, T, Loc); |
| 5305 | } else if (T->isBooleanType()) { |
| 5306 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 5307 | T, Loc); |
| 5308 | } else if (T->isNullPtrType()) { |
| 5309 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 5310 | } else { |
| 5311 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 5312 | } |
| 5313 | |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5314 | if (OrigT->isEnumeralType()) { |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5315 | // FIXME: This is a hack. We need a better way to handle substituted |
| 5316 | // non-type template parameters. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5317 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 5318 | nullptr, |
Benjamin Kramer | 3d3ddce | 2012-11-21 17:42:47 +0000 | [diff] [blame] | 5319 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
John McCall | 6730e4d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 5320 | Loc, Loc); |
| 5321 | } |
| 5322 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5323 | return E; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5324 | } |
| 5325 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5326 | /// \brief Match two template parameters within template parameter lists. |
| 5327 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 5328 | bool Complain, |
| 5329 | Sema::TemplateParameterListEqualKind Kind, |
| 5330 | SourceLocation TemplateArgLoc) { |
| 5331 | // Check the actual kind (type, non-type, template). |
| 5332 | if (Old->getKind() != New->getKind()) { |
| 5333 | if (Complain) { |
| 5334 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 5335 | if (TemplateArgLoc.isValid()) { |
| 5336 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5337 | NextDiag = diag::note_template_param_different_kind; |
| 5338 | } |
| 5339 | S.Diag(New->getLocation(), NextDiag) |
| 5340 | << (Kind != Sema::TPL_TemplateMatch); |
| 5341 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 5342 | << (Kind != Sema::TPL_TemplateMatch); |
| 5343 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5344 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5345 | return false; |
| 5346 | } |
| 5347 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5348 | // Check that both are parameter packs are neither are parameter packs. |
| 5349 | // However, if we are matching a template template argument to a |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5350 | // template template parameter, the template template parameter can have |
| 5351 | // a parameter pack where the template template argument does not. |
| 5352 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 5353 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5354 | Old->isTemplateParameterPack())) { |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5355 | if (Complain) { |
| 5356 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 5357 | if (TemplateArgLoc.isValid()) { |
| 5358 | S.Diag(TemplateArgLoc, |
| 5359 | diag::err_template_arg_template_params_mismatch); |
| 5360 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 5361 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5362 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5363 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 5364 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 5365 | : 2; |
| 5366 | S.Diag(New->getLocation(), NextDiag) |
| 5367 | << ParamKind << New->isParameterPack(); |
| 5368 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 5369 | << ParamKind << Old->isParameterPack(); |
| 5370 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5371 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5372 | return false; |
| 5373 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5374 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5375 | // For non-type template parameters, check the type of the parameter. |
| 5376 | if (NonTypeTemplateParmDecl *OldNTTP |
| 5377 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 5378 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5379 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5380 | // If we are matching a template template argument to a template |
| 5381 | // template parameter and one of the non-type template parameter types |
| 5382 | // is dependent, then we must wait until template instantiation time |
| 5383 | // to actually compare the arguments. |
| 5384 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 5385 | (OldNTTP->getType()->isDependentType() || |
| 5386 | NewNTTP->getType()->isDependentType())) |
| 5387 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5388 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5389 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 5390 | if (Complain) { |
| 5391 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 5392 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5393 | S.Diag(TemplateArgLoc, |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5394 | diag::err_template_arg_template_params_mismatch); |
| 5395 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 5396 | } |
| 5397 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 5398 | << NewNTTP->getType() |
| 5399 | << (Kind != Sema::TPL_TemplateMatch); |
| 5400 | S.Diag(OldNTTP->getLocation(), |
| 5401 | diag::note_template_nontype_parm_prev_declaration) |
| 5402 | << OldNTTP->getType(); |
| 5403 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5404 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5405 | return false; |
| 5406 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5407 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5408 | return true; |
| 5409 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5410 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5411 | // For template template parameters, check the template parameter types. |
| 5412 | // The template parameter lists of template template |
| 5413 | // parameters must agree. |
| 5414 | if (TemplateTemplateParmDecl *OldTTP |
| 5415 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5416 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5417 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 5418 | OldTTP->getTemplateParameters(), |
| 5419 | Complain, |
| 5420 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5421 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5422 | : Kind), |
| 5423 | TemplateArgLoc); |
| 5424 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5425 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5426 | return true; |
| 5427 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 5428 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5429 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 5430 | /// lists. |
| 5431 | static |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5432 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5433 | TemplateParameterList *New, |
| 5434 | TemplateParameterList *Old, |
| 5435 | Sema::TemplateParameterListEqualKind Kind, |
| 5436 | SourceLocation TemplateArgLoc) { |
| 5437 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 5438 | if (TemplateArgLoc.isValid()) { |
| 5439 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 5440 | NextDiag = diag::note_template_param_list_different_arity; |
| 5441 | } |
| 5442 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 5443 | << (New->size() > Old->size()) |
| 5444 | << (Kind != Sema::TPL_TemplateMatch) |
| 5445 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 5446 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 5447 | << (Kind != Sema::TPL_TemplateMatch) |
| 5448 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 5449 | } |
| 5450 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5451 | /// \brief Determine whether the given template parameter lists are |
| 5452 | /// equivalent. |
| 5453 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5454 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5455 | /// source code as part of a new template declaration. |
| 5456 | /// |
| 5457 | /// \param Old The old template parameter list, typically found via |
| 5458 | /// name lookup of the template declared with this template parameter |
| 5459 | /// list. |
| 5460 | /// |
| 5461 | /// \param Complain If true, this routine will produce a diagnostic if |
| 5462 | /// the template parameter lists are not equivalent. |
| 5463 | /// |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5464 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5465 | /// |
| 5466 | /// \param TemplateArgLoc If this source location is valid, then we |
| 5467 | /// are actually checking the template parameter list of a template |
| 5468 | /// argument (New) against the template parameter list of its |
| 5469 | /// corresponding template template parameter (Old). We produce |
| 5470 | /// slightly different diagnostics in this scenario. |
| 5471 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5472 | /// \returns True if the template parameter lists are equal, false |
| 5473 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5474 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5475 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 5476 | TemplateParameterList *Old, |
| 5477 | bool Complain, |
Douglas Gregor | 19ac2d6 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 5478 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 5479 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5480 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 5481 | if (Complain) |
| 5482 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5483 | TemplateArgLoc); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5484 | |
| 5485 | return false; |
| 5486 | } |
| 5487 | |
Douglas Gregor | 641040a | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 5488 | // C++0x [temp.arg.template]p3: |
| 5489 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5490 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5491 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5492 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5493 | // template-parameter-list of P. [...] |
| 5494 | TemplateParameterList::iterator NewParm = New->begin(); |
| 5495 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5496 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5497 | OldParmEnd = Old->end(); |
| 5498 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | 018778a | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 5499 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 5500 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5501 | if (NewParm == NewParmEnd) { |
| 5502 | if (Complain) |
| 5503 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5504 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5505 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5506 | return false; |
| 5507 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5508 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5509 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5510 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5511 | return false; |
| 5512 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5513 | ++NewParm; |
| 5514 | continue; |
| 5515 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5516 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5517 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 5518 | // [...] When P's template- parameter-list contains a template parameter |
| 5519 | // pack (14.5.3), the template parameter pack will match zero or more |
| 5520 | // template parameters or template parameter packs in the |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5521 | // template-parameter-list of A with the same type and form as the |
| 5522 | // template parameter pack in P (ignoring whether those template |
| 5523 | // parameters are template parameter packs). |
| 5524 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 5525 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 5526 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5527 | return false; |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5528 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5529 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5530 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5531 | // Make sure we exhausted all of the arguments. |
| 5532 | if (NewParm != NewParmEnd) { |
| 5533 | if (Complain) |
| 5534 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 5535 | TemplateArgLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5536 | |
Douglas Gregor | fd4344b | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 5537 | return false; |
| 5538 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5539 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5540 | return true; |
| 5541 | } |
| 5542 | |
| 5543 | /// \brief Check whether a template can be declared within this scope. |
| 5544 | /// |
| 5545 | /// If the template declaration is valid in this scope, returns |
| 5546 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5547 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5548 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 5549 | if (!S) |
| 5550 | return false; |
| 5551 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5552 | // Find the nearest enclosing declaration scope. |
| 5553 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5554 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5555 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5556 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5557 | // C++ [temp]p4: |
| 5558 | // A template [...] shall not have C linkage. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 5559 | DeclContext *Ctx = S->getEntity(); |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5560 | if (Ctx && Ctx->isExternCContext()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5561 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5562 | << TemplateParams->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5563 | |
Eli Friedman | dfbd0c4 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 5564 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5565 | Ctx = Ctx->getParent(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5566 | |
Benjamin Kramer | 35e6fee | 2014-02-02 16:35:43 +0000 | [diff] [blame] | 5567 | // C++ [temp]p2: |
| 5568 | // A template-declaration can appear only as a namespace scope or |
| 5569 | // class scope declaration. |
David Majnemer | 766e259 | 2013-10-22 04:14:18 +0000 | [diff] [blame] | 5570 | if (Ctx) { |
| 5571 | if (Ctx->isFileContext()) |
| 5572 | return false; |
| 5573 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 5574 | // C++ [temp.mem]p2: |
| 5575 | // A local class shall not have member templates. |
| 5576 | if (RD->isLocalClass()) |
| 5577 | return Diag(TemplateParams->getTemplateLoc(), |
| 5578 | diag::err_template_inside_local_class) |
| 5579 | << TemplateParams->getSourceRange(); |
| 5580 | else |
| 5581 | return false; |
| 5582 | } |
| 5583 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5584 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5585 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5586 | diag::err_template_outside_namespace_or_class_scope) |
| 5587 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 5588 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5589 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5590 | /// \brief Determine what kind of template specialization the given declaration |
| 5591 | /// is. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5592 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5593 | if (!D) |
| 5594 | return TSK_Undeclared; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5595 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5596 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 5597 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5598 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 5599 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5600 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 5601 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5602 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5603 | return TSK_Undeclared; |
| 5604 | } |
| 5605 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5606 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5607 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5608 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5609 | /// This routine determines whether a template specialization can be declared |
| 5610 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5611 | /// |
| 5612 | /// \param S the semantic analysis object for which this check is being |
| 5613 | /// performed. |
| 5614 | /// |
| 5615 | /// \param Specialized the entity being specialized or instantiated, which |
| 5616 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5617 | /// a member of a class template (member function, static data member, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5618 | /// member class). |
| 5619 | /// |
| 5620 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 5621 | /// |
| 5622 | /// \param Loc the location of the explicit specialization or instantiation of |
| 5623 | /// this entity. |
| 5624 | /// |
| 5625 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 5626 | /// a class template. |
| 5627 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5628 | /// \returns true if there was an error that we cannot recover from, false |
| 5629 | /// otherwise. |
| 5630 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 5631 | NamedDecl *Specialized, |
| 5632 | NamedDecl *PrevDecl, |
| 5633 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5634 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5635 | // Keep these "kind" numbers in sync with the %select statements in the |
| 5636 | // various diagnostics emitted by this routine. |
| 5637 | int EntityKind = 0; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5638 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5639 | EntityKind = IsPartialSpecialization? 1 : 0; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5640 | else if (isa<VarTemplateDecl>(Specialized)) |
| 5641 | EntityKind = IsPartialSpecialization ? 3 : 2; |
Ted Kremenek | 7f1f3f6 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 5642 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5643 | EntityKind = 4; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5644 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5645 | EntityKind = 5; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5646 | else if (isa<VarDecl>(Specialized)) |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5647 | EntityKind = 6; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5648 | else if (isa<RecordDecl>(Specialized)) |
| 5649 | EntityKind = 7; |
| 5650 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 5651 | EntityKind = 8; |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5652 | else { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5653 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5654 | << S.getLangOpts().CPlusPlus11; |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5655 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5656 | return true; |
| 5657 | } |
| 5658 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5659 | // C++ [temp.expl.spec]p2: |
| 5660 | // An explicit specialization shall be declared in the namespace |
| 5661 | // of which the template is a member, or, for member templates, in |
| 5662 | // the namespace of which the enclosing class or enclosing class |
| 5663 | // template is a member. An explicit specialization of a member |
| 5664 | // function, member class or static data member of a class |
| 5665 | // template shall be declared in the namespace of which the class |
| 5666 | // template is a member. Such a declaration may also be a |
| 5667 | // definition. If the declaration is not a definition, the |
| 5668 | // specialization may be defined later in the name- space in which |
| 5669 | // the explicit specialization was declared, or in a namespace |
| 5670 | // that encloses the one in which the explicit specialization was |
| 5671 | // declared. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5672 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5673 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5674 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5675 | return true; |
| 5676 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5677 | |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5678 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5679 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 5680 | // Do not warn for class scope explicit specialization during |
| 5681 | // instantiation, warning was already emitted during pattern |
| 5682 | // semantic analysis. |
| 5683 | if (!S.ActiveTemplateInstantiations.size()) |
| 5684 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 5685 | << Specialized; |
| 5686 | } else { |
| 5687 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5688 | << Specialized; |
| 5689 | return true; |
| 5690 | } |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 5691 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5692 | |
Douglas Gregor | 44e5a0a | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 5693 | if (S.CurContext->isRecord() && |
| 5694 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 5695 | // Make sure that we're specializing in the right record context. |
| 5696 | // Otherwise, things can go horribly wrong. |
| 5697 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 5698 | << Specialized; |
| 5699 | return true; |
| 5700 | } |
| 5701 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5702 | // C++ [temp.class.spec]p6: |
| 5703 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5704 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 5705 | // and 14.5.2). |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5706 | DeclContext *SpecializedContext |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5707 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5708 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5709 | |
| 5710 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
| 5711 | // namespace. |
| 5712 | // Note that HandleDeclarator() performs this check for explicit |
| 5713 | // specializations of function templates, static data members, and member |
| 5714 | // functions, so we skip the check here for those kinds of entities. |
| 5715 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
| 5716 | // Should we refactor that check, so that it occurs later? |
| 5717 | if (!DC->Encloses(SpecializedContext) && |
| 5718 | !(isa<FunctionTemplateDecl>(Specialized) || |
| 5719 | isa<FunctionDecl>(Specialized) || |
| 5720 | isa<VarTemplateDecl>(Specialized) || |
| 5721 | isa<VarDecl>(Specialized))) { |
| 5722 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 5723 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 5724 | << EntityKind << Specialized; |
| 5725 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 5726 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 5727 | << EntityKind << Specialized |
| 5728 | << cast<NamedDecl>(SpecializedContext); |
| 5729 | else |
| 5730 | llvm_unreachable("unexpected namespace context for specialization"); |
| 5731 | |
| 5732 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 5733 | } else if ((!PrevDecl || |
| 5734 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 5735 | getTemplateSpecializationKind(PrevDecl) == |
| 5736 | TSK_ImplicitInstantiation)) { |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5737 | // C++ [temp.exp.spec]p2: |
| 5738 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5739 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5740 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5741 | // An explicit specialization of a member function, member class or |
| 5742 | // static data member of a class template shall be declared in the |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5743 | // namespace of which the class template is a member. |
| 5744 | // |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5745 | // C++11 [temp.expl.spec]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5746 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | b1aab43 | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 5747 | // the specialized template. |
Richard Smith | a98f8fc | 2013-12-07 05:09:50 +0000 | [diff] [blame] | 5748 | // C++11 [temp.explicit]p3: |
| 5749 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 5750 | // template. |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5751 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5752 | bool IsCPlusPlus11Extension = DC->Encloses(SpecializedContext); |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5753 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5754 | assert(!IsCPlusPlus11Extension && |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5755 | "DC encloses TU but isn't in enclosing namespace set"); |
| 5756 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | 8ce6315 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 5757 | << EntityKind << Specialized; |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5758 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 5759 | int Diag; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5760 | if (!IsCPlusPlus11Extension) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5761 | Diag = diag::err_template_spec_decl_out_of_scope; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5762 | else if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5763 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 5764 | else |
| 5765 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 5766 | S.Diag(Loc, Diag) |
| 5767 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 5768 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5769 | |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5770 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5771 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5772 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5773 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5774 | return false; |
| 5775 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5776 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5777 | static SourceRange findTemplateParameter(unsigned Depth, Expr *E) { |
| 5778 | if (!E->isInstantiationDependent()) |
| 5779 | return SourceLocation(); |
| 5780 | DependencyChecker Checker(Depth); |
| 5781 | Checker.TraverseStmt(E); |
| 5782 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 5783 | return E->getSourceRange(); |
| 5784 | return Checker.MatchLoc; |
| 5785 | } |
| 5786 | |
| 5787 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 5788 | if (!TL.getType()->isDependentType()) |
| 5789 | return SourceLocation(); |
| 5790 | DependencyChecker Checker(Depth); |
| 5791 | Checker.TraverseTypeLoc(TL); |
| 5792 | if (Checker.Match && Checker.MatchLoc.isInvalid()) |
| 5793 | return TL.getSourceRange(); |
| 5794 | return Checker.MatchLoc; |
| 5795 | } |
| 5796 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5797 | /// \brief Subroutine of Sema::CheckTemplatePartialSpecializationArgs |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5798 | /// that checks non-type template partial specialization arguments. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5799 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5800 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 5801 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5802 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 5803 | if (Args[I].getKind() == TemplateArgument::Pack) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5804 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5805 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 5806 | Args[I].pack_size(), IsDefaultArgument)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5807 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5808 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5809 | continue; |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5810 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5811 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5812 | if (Args[I].getKind() != TemplateArgument::Expression) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5813 | continue; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5814 | |
| 5815 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5816 | |
Douglas Gregor | 98318c2 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 5817 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5818 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 5819 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 5820 | |
| 5821 | // Strip off any implicit casts we added as part of type checking. |
| 5822 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 5823 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5824 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5825 | // C++ [temp.class.spec]p8: |
| 5826 | // A non-type argument is non-specialized if it is the name of a |
| 5827 | // non-type parameter. All other non-type arguments are |
| 5828 | // specialized. |
| 5829 | // |
| 5830 | // Below, we check the two conditions that only apply to |
| 5831 | // specialized non-type arguments, so skip any non-specialized |
| 5832 | // arguments. |
| 5833 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 5834 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5835 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5836 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5837 | // C++ [temp.class.spec]p9: |
| 5838 | // Within the argument list of a class template partial |
| 5839 | // specialization, the following restrictions apply: |
| 5840 | // -- A partially specialized non-type argument expression |
| 5841 | // shall not involve a template parameter of the partial |
| 5842 | // specialization except when the argument expression is a |
| 5843 | // simple identifier. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5844 | SourceRange ParamUseRange = |
| 5845 | findTemplateParameter(Param->getDepth(), ArgExpr); |
| 5846 | if (ParamUseRange.isValid()) { |
| 5847 | if (IsDefaultArgument) { |
| 5848 | S.Diag(TemplateNameLoc, |
| 5849 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 5850 | S.Diag(ParamUseRange.getBegin(), |
| 5851 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 5852 | << ParamUseRange; |
| 5853 | } else { |
| 5854 | S.Diag(ParamUseRange.getBegin(), |
| 5855 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 5856 | << ParamUseRange; |
| 5857 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5858 | return true; |
| 5859 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5860 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5861 | // -- The type of a template parameter corresponding to a |
| 5862 | // specialized non-type argument shall not be dependent on a |
| 5863 | // parameter of the specialization. |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5864 | // |
| 5865 | // FIXME: We need to delay this check until instantiation in some cases: |
| 5866 | // |
| 5867 | // template<template<typename> class X> struct A { |
| 5868 | // template<typename T, X<T> N> struct B; |
| 5869 | // template<typename T> struct B<T, 0>; |
| 5870 | // }; |
| 5871 | // template<typename> using X = int; |
| 5872 | // A<X>::B<int, 0> b; |
| 5873 | ParamUseRange = findTemplateParameter( |
| 5874 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
| 5875 | if (ParamUseRange.isValid()) { |
| 5876 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getLocStart(), |
| 5877 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 5878 | << Param->getType() << ParamUseRange; |
| 5879 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
| 5880 | << (IsDefaultArgument ? ParamUseRange : SourceRange()); |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5881 | return true; |
| 5882 | } |
| 5883 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5884 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5885 | return false; |
| 5886 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5887 | |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5888 | /// \brief Check the non-type template arguments of a class template |
| 5889 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 5890 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5891 | /// \param TemplateNameLoc the location of the template name. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5892 | /// \param TemplateParams the template parameters of the primary class |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5893 | /// template. |
| 5894 | /// \param NumExplicit the number of explicitly-specified template arguments. |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5895 | /// \param TemplateArgs the template arguments of the class template |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5896 | /// partial specialization. |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5897 | /// |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5898 | /// \returns \c true if there was an error, \c false otherwise. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5899 | static bool CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5900 | Sema &S, SourceLocation TemplateNameLoc, |
| 5901 | TemplateParameterList *TemplateParams, unsigned NumExplicit, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5902 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5903 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 5904 | |
| 5905 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 5906 | NonTypeTemplateParmDecl *Param |
| 5907 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 5908 | if (!Param) |
| 5909 | continue; |
| 5910 | |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 5911 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
| 5912 | S, TemplateNameLoc, Param, &ArgList[I], 1, I >= NumExplicit)) |
Douglas Gregor | 875b6fe | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5913 | return true; |
| 5914 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5915 | |
| 5916 | return false; |
| 5917 | } |
| 5918 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5919 | DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5920 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 5921 | TagUseKind TUK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5922 | SourceLocation KWLoc, |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5923 | SourceLocation ModulePrivateLoc, |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 5924 | TemplateIdAnnotation &TemplateId, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5925 | AttributeList *Attr, |
| 5926 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5927 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 5928 | |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 5929 | CXXScopeSpec &SS = TemplateId.SS; |
| 5930 | |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5931 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 5932 | // store the location of the outermost template keyword in the declaration. |
| 5933 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 5934 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 5935 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 5936 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 5937 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5938 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5939 | // Find the class template we're specializing |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 5940 | TemplateName Name = TemplateId.Template.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5941 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 5942 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5943 | |
| 5944 | if (!ClassTemplate) { |
| 5945 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5946 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | dd6c035 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 5947 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 5948 | return true; |
| 5949 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5950 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5951 | bool isExplicitSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5952 | bool isPartialSpecialization = false; |
| 5953 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5954 | // Check the validity of the template headers that introduce this |
| 5955 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5956 | // FIXME: We probably shouldn't complain about these headers for |
| 5957 | // friend declarations. |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 5958 | bool Invalid = false; |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 5959 | TemplateParameterList *TemplateParams = |
| 5960 | MatchTemplateParametersToScopeSpecifier( |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 5961 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
| 5962 | TemplateParameterLists, TUK == TUK_Friend, isExplicitSpecialization, |
| 5963 | Invalid); |
Douglas Gregor | 5f0e252 | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 5964 | if (Invalid) |
| 5965 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5966 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5967 | if (TemplateParams && TemplateParams->size() > 0) { |
| 5968 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5969 | |
Douglas Gregor | ec9518b | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 5970 | if (TUK == TUK_Friend) { |
| 5971 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 5972 | << SourceRange(LAngleLoc, RAngleLoc); |
| 5973 | return true; |
| 5974 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5975 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5976 | // C++ [temp.class.spec]p10: |
| 5977 | // The template parameter list of a specialization shall not |
| 5978 | // contain default template argument values. |
| 5979 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 5980 | Decl *Param = TemplateParams->getParam(I); |
| 5981 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 5982 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5983 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5984 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5985 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5986 | } |
| 5987 | } else if (NonTypeTemplateParmDecl *NTTP |
| 5988 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 5989 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5990 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5991 | diag::err_default_arg_in_partial_spec) |
| 5992 | << DefArg->getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 5993 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5994 | } |
| 5995 | } else { |
| 5996 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5997 | if (TTP->hasDefaultArgument()) { |
| 5998 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5999 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 6000 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | 656e300 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 6001 | TTP->removeDefaultArgument(); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6002 | } |
| 6003 | } |
| 6004 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6005 | } else if (TemplateParams) { |
| 6006 | if (TUK == TUK_Friend) |
| 6007 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6008 | << FixItHint::CreateRemoval( |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6009 | SourceRange(TemplateParams->getTemplateLoc(), |
| 6010 | TemplateParams->getRAngleLoc())) |
| 6011 | << SourceRange(LAngleLoc, RAngleLoc); |
| 6012 | else |
| 6013 | isExplicitSpecialization = true; |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6014 | } else { |
| 6015 | assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6016 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6017 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6018 | // Check that the specialization uses the same tag kind as the |
| 6019 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6020 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6021 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6022 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6023 | Kind, TUK == TUK_Definition, KWLoc, |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6024 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6025 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6026 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6027 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6028 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6029 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6030 | diag::note_previous_use); |
| 6031 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6032 | } |
| 6033 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6034 | // Translate the parser's template argument list in our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 6035 | TemplateArgumentListInfo TemplateArgs = |
| 6036 | makeTemplateArgumentListInfo(*this, TemplateId); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 6037 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6038 | // Check for unexpanded parameter packs in any of the template arguments. |
| 6039 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6040 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 6041 | UPPC_PartialSpecialization)) |
| 6042 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6043 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6044 | // Check that the template argument list is well-formed for this |
| 6045 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6046 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6047 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6048 | TemplateArgs, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6049 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6050 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6051 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6052 | // corresponds to these arguments. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 6053 | if (isPartialSpecialization) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6054 | if (CheckTemplatePartialSpecializationArgs( |
Richard Smith | 6056d5e | 2014-02-09 00:54:43 +0000 | [diff] [blame] | 6055 | *this, TemplateNameLoc, ClassTemplate->getTemplateParameters(), |
| 6056 | TemplateArgs.size(), Converted)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 6057 | return true; |
| 6058 | |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6059 | bool InstantiationDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6060 | if (!Name.isDependent() && |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6061 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6062 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 6063 | TemplateArgs.size(), |
| 6064 | InstantiationDependent)) { |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6065 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 6066 | << ClassTemplate->getDeclName(); |
| 6067 | isPartialSpecialization = false; |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6068 | } |
| 6069 | } |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6070 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6071 | void *InsertPos = nullptr; |
| 6072 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6073 | |
| 6074 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6075 | // FIXME: Template parameter list matters, too |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6076 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6077 | else |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 6078 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6079 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6080 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6081 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 6082 | // Check whether we can declare a class template specialization in |
| 6083 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6084 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6085 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 6086 | TemplateNameLoc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6087 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6088 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6089 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6090 | // The canonical type |
| 6091 | QualType CanonType; |
Richard Smith | 871cd4c | 2014-05-23 21:00:28 +0000 | [diff] [blame] | 6092 | if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6093 | // Build the canonical type that describes the converted template |
| 6094 | // arguments of the class template partial specialization. |
Douglas Gregor | 92354b6 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 6095 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 6096 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6097 | Converted.data(), |
| 6098 | Converted.size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6099 | |
| 6100 | if (Context.hasSameType(CanonType, |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6101 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 6102 | // C++ [temp.class.spec]p9b3: |
| 6103 | // |
| 6104 | // -- The argument list of the specialization shall not be identical |
| 6105 | // to the implicit argument list of the primary template. |
| 6106 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6107 | << /*class template*/0 << (TUK == TUK_Definition) |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 6108 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6109 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 6110 | ClassTemplate->getIdentifier(), |
| 6111 | TemplateNameLoc, |
| 6112 | Attr, |
| 6113 | TemplateParams, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6114 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Nikola Smiljanic | 4fc9153 | 2014-07-17 01:59:34 +0000 | [diff] [blame] | 6115 | /*FriendLoc*/SourceLocation(), |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6116 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6117 | TemplateParameterLists.data()); |
Douglas Gregor | dd7ec463 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 6118 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6119 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6120 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6121 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 6122 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6123 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6124 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6125 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6126 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 6127 | TemplateParams, |
| 6128 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6129 | Converted.data(), |
| 6130 | Converted.size(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6131 | TemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6132 | CanonType, |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 6133 | PrevPartial); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6134 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6135 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6136 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6137 | TemplateParameterLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6138 | TemplateParameterLists.data()); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6139 | } |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6140 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6141 | if (!PrevPartial) |
| 6142 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6143 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6144 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6145 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6146 | // template specialization, make a note of that. |
| 6147 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 6148 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6149 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6150 | // Check that all of the template parameters of the class template |
| 6151 | // partial specialization are deducible from the template |
| 6152 | // arguments. If not, this class template partial specialization |
| 6153 | // will never be used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6154 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6155 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 6156 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 6157 | DeducibleParams); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6158 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 6159 | if (!DeducibleParams.all()) { |
| 6160 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6161 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
Richard Smith | 300e0c3 | 2013-09-24 04:49:23 +0000 | [diff] [blame] | 6162 | << /*class template*/0 << (NumNonDeducible > 1) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6163 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 6164 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 6165 | if (!DeducibleParams[I]) { |
| 6166 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 6167 | if (Param->getDeclName()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6168 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6169 | diag::note_partial_spec_unused_parameter) |
| 6170 | << Param->getDeclName(); |
| 6171 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6172 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6173 | diag::note_partial_spec_unused_parameter) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 6174 | << "(anonymous)"; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 6175 | } |
| 6176 | } |
| 6177 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6178 | } else { |
| 6179 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6180 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6181 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6182 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6183 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6184 | KWLoc, TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6185 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6186 | Converted.data(), |
| 6187 | Converted.size(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6188 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6189 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6190 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 6191 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6192 | TemplateParameterLists.size(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 6193 | TemplateParameterLists.data()); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 6194 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6195 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6196 | if (!PrevDecl) |
| 6197 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 6198 | |
| 6199 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6200 | } |
| 6201 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6202 | // C++ [temp.expl.spec]p6: |
| 6203 | // 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] | 6204 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6205 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6206 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6207 | // use occurs; no diagnostic is required. |
| 6208 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6209 | bool Okay = false; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6210 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6211 | // Is there any previous explicit specialization declaration? |
| 6212 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6213 | Okay = true; |
| 6214 | break; |
| 6215 | } |
| 6216 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6217 | |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6218 | if (!Okay) { |
| 6219 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 6220 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 6221 | << Context.getTypeDeclType(Specialization) << Range; |
| 6222 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6223 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6224 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6225 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6226 | != TSK_ImplicitInstantiation); |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6227 | return true; |
| 6228 | } |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6229 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6230 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6231 | // If this is not a friend, note that this is an explicit specialization. |
| 6232 | if (TUK != TUK_Friend) |
| 6233 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6234 | |
| 6235 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6236 | if (TUK == TUK_Definition) { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6237 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6238 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6239 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 6240 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6241 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 6242 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 6243 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6244 | } |
| 6245 | } |
| 6246 | |
John McCall | 659a337 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 6247 | if (Attr) |
| 6248 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6249 | |
Richard Smith | 034b94a | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 6250 | // Add alignment attributes if necessary; these attributes are checked when |
| 6251 | // the ASTContext lays out the structure. |
| 6252 | if (TUK == TUK_Definition) { |
| 6253 | AddAlignmentAttributesForRecord(Specialization); |
| 6254 | AddMsStructLayoutForRecord(Specialization); |
| 6255 | } |
| 6256 | |
Douglas Gregor | 3c7cd6a | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 6257 | if (ModulePrivateLoc.isValid()) |
| 6258 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 6259 | << (isPartialSpecialization? 1 : 0) |
| 6260 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 6261 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 6262 | // Build the fully-sugared type for this class template |
| 6263 | // specialization as the user wrote in the specialization |
| 6264 | // itself. This means that we'll pretty-print the type retrieved |
| 6265 | // from the specialization's declaration the way that the user |
| 6266 | // actually wrote the specialization, rather than formatting the |
| 6267 | // name based on the "canonical" representation used to store the |
| 6268 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6269 | TypeSourceInfo *WrittenTy |
| 6270 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6271 | TemplateArgs, CanonType); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6272 | if (TUK != TUK_Friend) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6273 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 6274 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6275 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6276 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 6277 | // C++ [temp.expl.spec]p9: |
| 6278 | // A template explicit specialization is in the scope of the |
| 6279 | // namespace in which the template was defined. |
| 6280 | // |
| 6281 | // We actually implement this paragraph where we set the semantic |
| 6282 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 6283 | // but we also maintain the lexical context where the actual |
| 6284 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6285 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6286 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6287 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 6288 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6289 | Specialization->startDefinition(); |
| 6290 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6291 | if (TUK == TUK_Friend) { |
| 6292 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 6293 | TemplateNameLoc, |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6294 | WrittenTy, |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 6295 | /*FIXME:*/KWLoc); |
| 6296 | Friend->setAccess(AS_public); |
| 6297 | CurContext->addDecl(Friend); |
| 6298 | } else { |
| 6299 | // Add the specialization into its lexical context, so that it can |
| 6300 | // be seen when iterating through the list of declarations in that |
| 6301 | // context. However, specializations are not found by name lookup. |
| 6302 | CurContext->addDecl(Specialization); |
| 6303 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6304 | return Specialization; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 6305 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6306 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6307 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6308 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6309 | Declarator &D) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6310 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 6311 | ActOnDocumentableDecl(NewDecl); |
| 6312 | return NewDecl; |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 6313 | } |
| 6314 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6315 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6316 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6317 | Declarator &D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6318 | assert(getCurFunctionDecl() == nullptr && "Function parsing confused"); |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6319 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6320 | |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6321 | if (FTI.hasPrototype) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6322 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6323 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6324 | |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6325 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6326 | |
Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 6327 | D.setFunctionDefinitionKind(FDK_Definition); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6328 | Decl *DP = HandleDeclarator(ParentScope, D, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6329 | TemplateParameterLists); |
Argyrios Kyrtzidis | 6fada2d | 2012-12-14 06:53:58 +0000 | [diff] [blame] | 6330 | return ActOnStartOfFunctionDef(FnBodyScope, DP); |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 6331 | } |
| 6332 | |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6333 | /// \brief Strips various properties off an implicit instantiation |
| 6334 | /// that has just been explicitly specialized. |
| 6335 | static void StripImplicitInstantiation(NamedDecl *D) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 6336 | D->dropAttrs(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6337 | |
| 6338 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 6339 | FD->setInlineSpecified(false); |
Jordan Rose | a0a86be | 2013-03-08 22:25:36 +0000 | [diff] [blame] | 6340 | |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 6341 | for (auto I : FD->params()) |
| 6342 | I->dropAttrs(); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6343 | } |
| 6344 | } |
| 6345 | |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6346 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 6347 | // declaration or definition. |
| 6348 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6349 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6350 | // Explicit instantiations following a specialization have no effect and |
| 6351 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 6352 | // until a valid name loc is found. |
| 6353 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6354 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 6355 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6356 | PrevDiagLoc = Prev->getLocation(); |
| 6357 | } |
| 6358 | assert(PrevDiagLoc.isValid() && |
| 6359 | "Explicit instantiation without point of instantiation?"); |
| 6360 | return PrevDiagLoc; |
| 6361 | } |
| 6362 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6363 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6364 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6365 | /// for those cases where they are required and determining whether the |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6366 | /// new specialization/instantiation will have any effect. |
| 6367 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6368 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6369 | /// instantiation. |
| 6370 | /// |
| 6371 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 6372 | /// |
| 6373 | /// \param PrevDecl the previous declaration of the entity. |
| 6374 | /// |
| 6375 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 6376 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6377 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6378 | /// declaration was instantiated (either implicitly or explicitly). |
| 6379 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6380 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6381 | /// specialization or instantiation has no effect and should be ignored. |
| 6382 | /// |
| 6383 | /// \returns true if there was an error that should prevent the introduction of |
| 6384 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6385 | bool |
| 6386 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 6387 | TemplateSpecializationKind NewTSK, |
| 6388 | NamedDecl *PrevDecl, |
| 6389 | TemplateSpecializationKind PrevTSK, |
| 6390 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6391 | bool &HasNoEffect) { |
| 6392 | HasNoEffect = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6393 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6394 | switch (NewTSK) { |
| 6395 | case TSK_Undeclared: |
| 6396 | case TSK_ImplicitInstantiation: |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 6397 | assert( |
| 6398 | (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 6399 | "previous declaration must be implicit!"); |
| 6400 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6401 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6402 | case TSK_ExplicitSpecialization: |
| 6403 | switch (PrevTSK) { |
| 6404 | case TSK_Undeclared: |
| 6405 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6406 | // Okay, we're just specializing something that is either already |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6407 | // explicitly specialized or has merely been mentioned without any |
| 6408 | // instantiation. |
| 6409 | return false; |
| 6410 | |
| 6411 | case TSK_ImplicitInstantiation: |
| 6412 | if (PrevPointOfInstantiation.isInvalid()) { |
| 6413 | // The declaration itself has not actually been instantiated, so it is |
| 6414 | // still okay to specialize it. |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6415 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6416 | return false; |
| 6417 | } |
| 6418 | // Fall through |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6419 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6420 | case TSK_ExplicitInstantiationDeclaration: |
| 6421 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6422 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 6423 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6424 | "Explicit instantiation without point of instantiation?"); |
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 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6427 | // 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] | 6428 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6429 | // before the first use of that specialization that would cause an |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6430 | // implicit instantiation to take place, in every translation unit in |
| 6431 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6432 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | c854c66 | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 6433 | // Is there any previous explicit specialization declaration? |
| 6434 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 6435 | return false; |
| 6436 | } |
| 6437 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6438 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6439 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6440 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6441 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6442 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6443 | return true; |
| 6444 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6445 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6446 | case TSK_ExplicitInstantiationDeclaration: |
| 6447 | switch (PrevTSK) { |
| 6448 | case TSK_ExplicitInstantiationDeclaration: |
| 6449 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6450 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6451 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6452 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6453 | case TSK_Undeclared: |
| 6454 | case TSK_ImplicitInstantiation: |
| 6455 | // We're explicitly instantiating something that may have already been |
| 6456 | // implicitly instantiated; that's fine. |
| 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_ExplicitSpecialization: |
| 6460 | // C++0x [temp.explicit]p4: |
| 6461 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6462 | // of a template appears after a declaration of an explicit |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6463 | // specialization for that template, the explicit instantiation has no |
| 6464 | // effect. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6465 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6466 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6467 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6468 | case TSK_ExplicitInstantiationDefinition: |
| 6469 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6470 | // If an entity is the subject of both an explicit instantiation |
| 6471 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6472 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6473 | Diag(NewLoc, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6474 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6475 | |
| 6476 | // Explicit instantiations following a specialization have no effect and |
| 6477 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 6478 | // until a valid name loc is found. |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6479 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 6480 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6481 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6482 | return false; |
| 6483 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6484 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6485 | case TSK_ExplicitInstantiationDefinition: |
| 6486 | switch (PrevTSK) { |
| 6487 | case TSK_Undeclared: |
| 6488 | case TSK_ImplicitInstantiation: |
| 6489 | // We're explicitly instantiating something that may have already been |
| 6490 | // implicitly instantiated; that's fine. |
| 6491 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6492 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6493 | case TSK_ExplicitSpecialization: |
| 6494 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 6495 | // For a given set of template parameters, if an explicit |
| 6496 | // instantiation of a template appears after a declaration of |
| 6497 | // an explicit specialization for that template, the explicit |
| 6498 | // instantiation has no effect. |
| 6499 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6500 | // 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] | 6501 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6502 | // has been explicitly specialized. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6503 | Diag(NewLoc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6504 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 6505 | diag::ext_explicit_instantiation_after_specialization) |
| 6506 | << PrevDecl; |
| 6507 | Diag(PrevDecl->getLocation(), |
| 6508 | diag::note_previous_template_specialization); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6509 | HasNoEffect = true; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6510 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6511 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6512 | case TSK_ExplicitInstantiationDeclaration: |
| 6513 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6514 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6515 | |
| 6516 | // C++0x [temp.explicit]p4: |
| 6517 | // For a given set of template parameters, if an explicit instantiation |
| 6518 | // of a template appears after a declaration of an explicit |
| 6519 | // specialization for that template, the explicit instantiation has no |
| 6520 | // effect. |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 6521 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | d3bdadf | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6522 | // Is there any previous explicit specialization declaration? |
| 6523 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 6524 | HasNoEffect = true; |
| 6525 | break; |
| 6526 | } |
| 6527 | } |
| 6528 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6529 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6530 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6531 | case TSK_ExplicitInstantiationDefinition: |
| 6532 | // C++0x [temp.spec]p5: |
| 6533 | // For a given template and a given set of template-arguments, |
| 6534 | // - an explicit instantiation definition shall appear at most once |
| 6535 | // in a program, |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6536 | |
| 6537 | // MSVCCompat: MSVC silently ignores duplicate explicit instantiations. |
| 6538 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 6539 | ? diag::ext_explicit_instantiation_duplicate |
Will Wilson | eadcdbb | 2014-05-09 09:52:13 +0000 | [diff] [blame] | 6540 | : diag::err_explicit_instantiation_duplicate) |
| 6541 | << PrevDecl; |
Nico Weber | a8f80b3 | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 6542 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6543 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6544 | HasNoEffect = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6545 | return false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6546 | } |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6547 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6548 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6549 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6550 | } |
| 6551 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6552 | /// \brief Perform semantic analysis for the given dependent function |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6553 | /// template specialization. |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6554 | /// |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6555 | /// The only possible way to get a dependent function template specialization |
| 6556 | /// is with a friend declaration, like so: |
| 6557 | /// |
| 6558 | /// \code |
| 6559 | /// template \<class T> void foo(T); |
| 6560 | /// template \<class T> class A { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6561 | /// friend void foo<>(T); |
| 6562 | /// }; |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 6563 | /// \endcode |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6564 | /// |
| 6565 | /// There really isn't any useful analysis we can do here, so we |
| 6566 | /// just store the information. |
| 6567 | bool |
| 6568 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 6569 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 6570 | LookupResult &Previous) { |
| 6571 | // Remove anything from Previous that isn't a function template in |
| 6572 | // the correct context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6573 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6574 | LookupResult::Filter F = Previous.makeFilter(); |
| 6575 | while (F.hasNext()) { |
| 6576 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 6577 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6578 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 6579 | D->getDeclContext()->getRedeclContext())) |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 6580 | F.erase(); |
| 6581 | } |
| 6582 | F.done(); |
| 6583 | |
| 6584 | // Should this be diagnosed here? |
| 6585 | if (Previous.empty()) return true; |
| 6586 | |
| 6587 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 6588 | ExplicitTemplateArgs); |
| 6589 | return false; |
| 6590 | } |
| 6591 | |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6592 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6593 | /// specialization. |
| 6594 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6595 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6596 | /// explicit function template specialization. On successful completion, |
| 6597 | /// the function declaration \p FD will become a function template |
| 6598 | /// specialization. |
| 6599 | /// |
| 6600 | /// \param FD the function declaration, which will be updated to become a |
| 6601 | /// function template specialization. |
| 6602 | /// |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6603 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 6604 | /// if any. Note that this may be valid info even when 0 arguments are |
| 6605 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 6606 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6607 | /// |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6608 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6609 | /// this function specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6610 | bool Sema::CheckFunctionTemplateSpecialization( |
| 6611 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 6612 | LookupResult &Previous) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6613 | // The set of function template specializations that could match this |
| 6614 | // explicit function template specialization. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6615 | UnresolvedSet<8> Candidates; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6616 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6617 | |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6618 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6619 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6620 | I != E; ++I) { |
| 6621 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 6622 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6623 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6624 | // FD. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6625 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 6626 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6627 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6628 | |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6629 | // When matching a constexpr member function template specialization |
| 6630 | // against the primary template, we don't yet know whether the |
| 6631 | // specialization has an implicit 'const' (because we don't know whether |
| 6632 | // it will be a static member function until we know which template it |
| 6633 | // specializes), so adjust it now assuming it specializes this template. |
| 6634 | QualType FT = FD->getType(); |
| 6635 | if (FD->isConstexpr()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6636 | CXXMethodDecl *OldMD = |
| 6637 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6638 | if (OldMD && OldMD->isConst()) { |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 6639 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6640 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6641 | EPI.TypeQuals |= Qualifiers::Const; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6642 | FT = Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6643 | FPT->getParamTypes(), EPI); |
Richard Smith | 574f4f6 | 2013-01-14 05:37:29 +0000 | [diff] [blame] | 6644 | } |
| 6645 | } |
| 6646 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6647 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6648 | // A trailing template-argument can be left unspecified in the |
| 6649 | // template-id naming an explicit function template specialization |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6650 | // provided it can be deduced from the function argument type. |
| 6651 | // Perform template argument deduction to determine whether we may be |
| 6652 | // specializing this template. |
| 6653 | // FIXME: It is somewhat wasteful to build |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6654 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6655 | FunctionDecl *Specialization = nullptr; |
Richard Smith | 3298368 | 2013-12-14 03:18:05 +0000 | [diff] [blame] | 6656 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 6657 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
| 6658 | ExplicitTemplateArgs, FT, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6659 | // Template argument deduction failed; record why it failed, so |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6660 | // that we can provide nifty diagnostics. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6661 | FailedCandidates.addCandidate() |
| 6662 | .set(FunTmpl->getTemplatedDecl(), |
| 6663 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6664 | (void)TDK; |
| 6665 | continue; |
| 6666 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6667 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6668 | // Record this candidate. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6669 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6670 | } |
| 6671 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6672 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 6673 | // Find the most specialized function template. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6674 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 6675 | Candidates.begin(), Candidates.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6676 | FD->getLocation(), |
| 6677 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 6678 | PDiag(diag::err_function_template_spec_ambiguous) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6679 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 6680 | PDiag(diag::note_function_template_spec_matched)); |
| 6681 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6682 | if (Result == Candidates.end()) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6683 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6684 | |
| 6685 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 6686 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | b9893d6 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 6687 | |
| 6688 | FunctionTemplateSpecializationInfo *SpecInfo |
| 6689 | = Specialization->getTemplateSpecializationInfo(); |
| 6690 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6691 | |
| 6692 | // Note: do not overwrite location info if previous template |
| 6693 | // specialization kind was explicit. |
| 6694 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6695 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 3a44e43 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 6696 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 6697 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 6698 | // function can differ from the template declaration with respect to |
| 6699 | // the constexpr specifier. |
| 6700 | Specialization->setConstexpr(FD->isConstexpr()); |
| 6701 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6702 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6703 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6704 | // If so, we have run afoul of . |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6705 | |
| 6706 | // If this is a friend declaration, then we're not really declaring |
| 6707 | // an explicit specialization. |
| 6708 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6709 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6710 | // Check the scope of this explicit specialization. |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6711 | if (!isFriend && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6712 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6713 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6714 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6715 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6716 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6717 | |
| 6718 | // C++ [temp.expl.spec]p6: |
| 6719 | // 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] | 6720 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6721 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6722 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6723 | // use occurs; no diagnostic is required. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6724 | bool HasNoEffect = false; |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6725 | if (!isFriend && |
| 6726 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6727 | TSK_ExplicitSpecialization, |
| 6728 | Specialization, |
| 6729 | SpecInfo->getTemplateSpecializationKind(), |
| 6730 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6731 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6732 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6733 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6734 | // Mark the prior declaration as an explicit specialization, so that later |
| 6735 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6736 | if (!isFriend) { |
John McCall | 816d75b | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 6737 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6738 | MarkUnusedFileScopedDecl(Specialization); |
| 6739 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6740 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6741 | // Turn the given function declaration into a function template |
| 6742 | // specialization, with the template arguments from the previous |
| 6743 | // specialization. |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6744 | // Take copies of (semantic and syntactic) template argument lists. |
| 6745 | const TemplateArgumentList* TemplArgs = new (Context) |
| 6746 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 6747 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6748 | TemplArgs, /*InsertPos=*/nullptr, |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 6749 | SpecInfo->getTemplateSpecializationKind(), |
Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 6750 | ExplicitTemplateArgs); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6751 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6752 | // The "previous declaration" for this function template specialization is |
| 6753 | // the prior function template specialization. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6754 | Previous.clear(); |
| 6755 | Previous.addDecl(Specialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6756 | return false; |
| 6757 | } |
| 6758 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6759 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6760 | /// specialization. |
| 6761 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6762 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6763 | /// explicit member function specialization. On successful completion, |
| 6764 | /// the function declaration \p FD will become a member function |
| 6765 | /// specialization. |
| 6766 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6767 | /// \param Member the member declaration, which will be updated to become a |
| 6768 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6769 | /// |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6770 | /// \param Previous the set of declarations, one of which may be specialized |
| 6771 | /// by this function specialization; the set will be modified to contain the |
| 6772 | /// redeclared member. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6773 | bool |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6774 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6775 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 6776 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6777 | // Try to find the member we are instantiating. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6778 | NamedDecl *Instantiation = nullptr; |
| 6779 | NamedDecl *InstantiatedFrom = nullptr; |
| 6780 | MemberSpecializationInfo *MSInfo = nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6781 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6782 | if (Previous.empty()) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6783 | // Nowhere to look anyway. |
| 6784 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6785 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6786 | I != E; ++I) { |
| 6787 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 6788 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 6789 | QualType Adjusted = Function->getType(); |
| 6790 | if (!hasExplicitCallingConv(Adjusted)) |
| 6791 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
| 6792 | if (Context.hasSameType(Adjusted, Method->getType())) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6793 | Instantiation = Method; |
| 6794 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6795 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6796 | break; |
| 6797 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6798 | } |
| 6799 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6800 | } else if (isa<VarDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6801 | VarDecl *PrevVar; |
| 6802 | if (Previous.isSingleResult() && |
| 6803 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6804 | if (PrevVar->isStaticDataMember()) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6805 | Instantiation = PrevVar; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6806 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6807 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6808 | } |
| 6809 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6810 | CXXRecordDecl *PrevRecord; |
| 6811 | if (Previous.isSingleResult() && |
| 6812 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 6813 | Instantiation = PrevRecord; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6814 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6815 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6816 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6817 | } else if (isa<EnumDecl>(Member)) { |
| 6818 | EnumDecl *PrevEnum; |
| 6819 | if (Previous.isSingleResult() && |
| 6820 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
| 6821 | Instantiation = PrevEnum; |
| 6822 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 6823 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 6824 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6825 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6826 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6827 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6828 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6829 | // specializations are always out-of-line, the caller will complain about |
| 6830 | // this mismatch later. |
| 6831 | return false; |
| 6832 | } |
John McCall | e820e5e | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 6833 | |
| 6834 | // If this is a friend, just bail out here before we start turning |
| 6835 | // things into explicit specializations. |
| 6836 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 6837 | // Preserve instantiation information. |
| 6838 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 6839 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 6840 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 6841 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 6842 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 6843 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 6844 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 6845 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 6846 | } |
| 6847 | |
| 6848 | Previous.clear(); |
| 6849 | Previous.addDecl(Instantiation); |
| 6850 | return false; |
| 6851 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6852 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6853 | // Make sure that this is a specialization of a member. |
| 6854 | if (!InstantiatedFrom) { |
| 6855 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 6856 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6857 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 6858 | return true; |
| 6859 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6860 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6861 | // C++ [temp.expl.spec]p6: |
| 6862 | // 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] | 6863 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6864 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6865 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6866 | // use occurs; no diagnostic is required. |
| 6867 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6868 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6869 | bool HasNoEffect = false; |
John McCall | 4f7ced6 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6870 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 6871 | TSK_ExplicitSpecialization, |
| 6872 | Instantiation, |
| 6873 | MSInfo->getTemplateSpecializationKind(), |
| 6874 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6875 | HasNoEffect)) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6876 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6877 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6878 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6879 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6880 | InstantiatedFrom, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6881 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6882 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6883 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 6884 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6885 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6886 | // the original declaration to note that it is an explicit specialization |
| 6887 | // (if it was previously an implicit instantiation). This latter step |
| 6888 | // makes bookkeeping easier. |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6889 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6890 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 6891 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 6892 | TSK_ImplicitInstantiation) { |
| 6893 | InstantiationFunction->setTemplateSpecializationKind( |
| 6894 | TSK_ExplicitSpecialization); |
| 6895 | InstantiationFunction->setLocation(Member->getLocation()); |
| 6896 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6897 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6898 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 6899 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 6900 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6901 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6902 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6903 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 6904 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 6905 | TSK_ImplicitInstantiation) { |
| 6906 | InstantiationVar->setTemplateSpecializationKind( |
| 6907 | TSK_ExplicitSpecialization); |
| 6908 | InstantiationVar->setLocation(Member->getLocation()); |
| 6909 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6910 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6911 | cast<VarDecl>(Member)->setInstantiationOfStaticDataMember( |
| 6912 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6913 | MarkUnusedFileScopedDecl(InstantiationVar); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6914 | } else if (isa<CXXRecordDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6915 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 6916 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 6917 | TSK_ImplicitInstantiation) { |
| 6918 | InstantiationClass->setTemplateSpecializationKind( |
| 6919 | TSK_ExplicitSpecialization); |
| 6920 | InstantiationClass->setLocation(Member->getLocation()); |
| 6921 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6922 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6923 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6924 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 6925 | TSK_ExplicitSpecialization); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6926 | } else { |
| 6927 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); |
| 6928 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); |
| 6929 | if (InstantiationEnum->getTemplateSpecializationKind() == |
| 6930 | TSK_ImplicitInstantiation) { |
| 6931 | InstantiationEnum->setTemplateSpecializationKind( |
| 6932 | TSK_ExplicitSpecialization); |
| 6933 | InstantiationEnum->setLocation(Member->getLocation()); |
| 6934 | } |
| 6935 | |
| 6936 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( |
| 6937 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6938 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6939 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6940 | // Save the caller the trouble of having to figure out which declaration |
| 6941 | // this specialization matches. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6942 | Previous.clear(); |
| 6943 | Previous.addDecl(Instantiation); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6944 | return false; |
| 6945 | } |
| 6946 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6947 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6948 | /// |
| 6949 | /// \returns true if a serious error occurs, false otherwise. |
| 6950 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6951 | SourceLocation InstLoc, |
| 6952 | bool WasQualifiedName) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6953 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 6954 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6955 | |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6956 | if (CurContext->isRecord()) { |
| 6957 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 6958 | << D; |
| 6959 | return true; |
| 6960 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6961 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6962 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6963 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6964 | // template. If the name declared in the explicit instantiation is an |
| 6965 | // unqualified name, the explicit instantiation shall appear in the |
| 6966 | // namespace where its template is declared or, if that namespace is inline |
| 6967 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6968 | // |
| 6969 | // 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] | 6970 | if (WasQualifiedName) { |
| 6971 | if (CurContext->Encloses(OrigContext)) |
| 6972 | return false; |
| 6973 | } else { |
| 6974 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 6975 | return false; |
| 6976 | } |
| 6977 | |
| 6978 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 6979 | if (WasQualifiedName) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6980 | S.Diag(InstLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6981 | S.getLangOpts().CPlusPlus11? |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6982 | diag::err_explicit_instantiation_out_of_scope : |
| 6983 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6984 | << D << NS; |
| 6985 | else |
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_unqualified_wrong_namespace : |
| 6989 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 6990 | << D << NS; |
| 6991 | } else |
| 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_must_be_global : |
| 6995 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 6996 | << D; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6997 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6998 | return false; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6999 | } |
| 7000 | |
| 7001 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 7002 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 7003 | if (!SS.isSet()) |
| 7004 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7005 | |
Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 7006 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7007 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7008 | // or a static data member of a class template specialization, the name of |
| 7009 | // the class template specialization in the qualified-id for the member |
| 7010 | // name shall be a simple-template-id. |
| 7011 | // |
| 7012 | // C++98 has the same restriction, just worded differently. |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7013 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 7014 | NNS = NNS->getPrefix()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7015 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7016 | if (isa<TemplateSpecializationType>(T)) |
| 7017 | return true; |
| 7018 | |
| 7019 | return false; |
| 7020 | } |
| 7021 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7022 | // Explicit instantiation of a class template specialization |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7023 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7024 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7025 | SourceLocation ExternLoc, |
| 7026 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7027 | unsigned TagSpec, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7028 | SourceLocation KWLoc, |
| 7029 | const CXXScopeSpec &SS, |
| 7030 | TemplateTy TemplateD, |
| 7031 | SourceLocation TemplateNameLoc, |
| 7032 | SourceLocation LAngleLoc, |
| 7033 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7034 | SourceLocation RAngleLoc, |
| 7035 | AttributeList *Attr) { |
| 7036 | // Find the class template we're specializing |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 7037 | TemplateName Name = TemplateD.get(); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7038 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7039 | // Check that the specialization uses the same tag kind as the |
| 7040 | // original template. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7041 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7042 | assert(Kind != TTK_Enum && |
| 7043 | "Invalid enum tag in class template explicit instantiation!"); |
Richard Smith | 392497b | 2013-06-22 22:03:31 +0000 | [diff] [blame] | 7044 | |
| 7045 | if (isa<TypeAliasTemplateDecl>(TD)) { |
| 7046 | Diag(KWLoc, diag::err_tag_reference_non_tag) << Kind; |
| 7047 | Diag(TD->getTemplatedDecl()->getLocation(), |
| 7048 | diag::note_previous_use); |
| 7049 | return true; |
| 7050 | } |
| 7051 | |
| 7052 | ClassTemplateDecl *ClassTemplate = cast<ClassTemplateDecl>(TD); |
| 7053 | |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7054 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | caa33d3 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 7055 | Kind, /*isDefinition*/false, KWLoc, |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 7056 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7057 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7058 | << ClassTemplate |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7059 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7060 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7061 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7062 | diag::note_previous_use); |
| 7063 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7064 | } |
| 7065 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7066 | // C++0x [temp.explicit]p2: |
| 7067 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7068 | // definition and an explicit instantiation declaration. An explicit |
| 7069 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7070 | TemplateSpecializationKind TSK |
| 7071 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7072 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7073 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7074 | // Translate the parser's template argument list in our AST format. |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7075 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 7076 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7077 | |
| 7078 | // Check that the template argument list is well-formed for this |
| 7079 | // template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7080 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7081 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7082 | TemplateArgs, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7083 | return true; |
| 7084 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7085 | // Find the class template specialization declaration that |
| 7086 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7087 | void *InsertPos = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7088 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 7089 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7090 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7091 | TemplateSpecializationKind PrevDecl_TSK |
| 7092 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 7093 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 7094 | // C++0x [temp.explicit]p2: |
| 7095 | // [...] An explicit instantiation shall appear in an enclosing |
| 7096 | // namespace of its template. [...] |
| 7097 | // |
| 7098 | // This is C++ DR 275. |
Douglas Gregor | 6cc1df5 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 7099 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 7100 | SS.isSet())) |
| 7101 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7102 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7103 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7104 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7105 | bool HasNoEffect = false; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7106 | if (PrevDecl) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7107 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7108 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7109 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7110 | HasNoEffect)) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7111 | return PrevDecl; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7112 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7113 | // Even though HasNoEffect == true means that this explicit instantiation |
| 7114 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 7115 | |
| 7116 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 7117 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7118 | // Since the only prior class template specialization with these |
| 7119 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7120 | // declaration node as our own, updating the source location |
| 7121 | // for the template name to reflect our new declaration. |
| 7122 | // (Other source locations will be updated later.) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7123 | Specialization = PrevDecl; |
| 7124 | Specialization->setLocation(TemplateNameLoc); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7125 | PrevDecl = nullptr; |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7126 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7127 | } |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7128 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 7129 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7130 | // Create a new class template specialization declaration node for |
| 7131 | // this explicit specialization. |
| 7132 | Specialization |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 7133 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7134 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 7135 | KWLoc, TemplateNameLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7136 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7137 | Converted.data(), |
| 7138 | Converted.size(), |
| 7139 | PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 7140 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7141 | |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7142 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7143 | // Insert the new specialization. |
Argyrios Kyrtzidis | 47470f2 | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 7144 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7145 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7146 | } |
| 7147 | |
| 7148 | // Build the fully-sugared type for this explicit instantiation as |
| 7149 | // the user wrote in the explicit instantiation itself. This means |
| 7150 | // that we'll pretty-print the type retrieved from the |
| 7151 | // specialization's declaration the way that the user actually wrote |
| 7152 | // the explicit instantiation, rather than formatting the name based |
| 7153 | // on the "canonical" representation used to store the template |
| 7154 | // arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 7155 | TypeSourceInfo *WrittenTy |
| 7156 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7157 | TemplateArgs, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7158 | Context.getTypeDeclType(Specialization)); |
| 7159 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7160 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7161 | // Set source locations for keywords. |
| 7162 | Specialization->setExternLoc(ExternLoc); |
| 7163 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
Argyrios Kyrtzidis | 40bcfd7 | 2013-04-22 23:23:42 +0000 | [diff] [blame] | 7164 | Specialization->setRBraceLoc(SourceLocation()); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7165 | |
Rafael Espindola | 0b06207 | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 7166 | if (Attr) |
| 7167 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 7168 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7169 | // Add the explicit instantiation into its lexical context. However, |
| 7170 | // since explicit instantiations are never found by name lookup, we |
| 7171 | // just put it into the declaration context directly. |
| 7172 | Specialization->setLexicalDeclContext(CurContext); |
| 7173 | CurContext->addDecl(Specialization); |
| 7174 | |
| 7175 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 7176 | if (HasNoEffect) { |
| 7177 | // Set the template specialization kind. |
| 7178 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7179 | return Specialization; |
Douglas Gregor | 0681a35 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 7180 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7181 | |
| 7182 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7183 | // A definition of a class template or class member template |
| 7184 | // shall be in scope at the point of the explicit instantiation of |
| 7185 | // the class template or class member template. |
| 7186 | // |
| 7187 | // This check comes when we actually try to perform the |
| 7188 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7189 | ClassTemplateSpecializationDecl *Def |
| 7190 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7191 | Specialization->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7192 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7193 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7194 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7195 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7196 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 7197 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7198 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7199 | // Instantiate the members of this class template specialization. |
| 7200 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7201 | Specialization->getDefinition()); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7202 | if (Def) { |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7203 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 7204 | |
| 7205 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 7206 | // TSK_ExplicitInstantiationDefinition |
| 7207 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 7208 | TSK == TSK_ExplicitInstantiationDefinition) |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7209 | // FIXME: Need to notify the ASTMutationListener that we did this. |
Rafael Espindola | fa1708fd | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 7210 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7211 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7212 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 7213 | } |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7214 | |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7215 | // Set the template specialization kind. |
| 7216 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7217 | return Specialization; |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 7218 | } |
| 7219 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7220 | // Explicit instantiation of a member class of a class template. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7221 | DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7222 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 7223 | SourceLocation ExternLoc, |
| 7224 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7225 | unsigned TagSpec, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7226 | SourceLocation KWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7227 | CXXScopeSpec &SS, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7228 | IdentifierInfo *Name, |
| 7229 | SourceLocation NameLoc, |
| 7230 | AttributeList *Attr) { |
| 7231 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 7232 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7233 | bool IsDependent = false; |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7234 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7235 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 7236 | /*ModulePrivateLoc=*/SourceLocation(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 7237 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 7238 | SourceLocation(), false, TypeResult(), |
| 7239 | /*IsTypeSpecifier*/false); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7240 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 7241 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7242 | if (!TagD) |
| 7243 | return true; |
| 7244 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7245 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 7246 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7247 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 7248 | if (Tag->isInvalidDecl()) |
| 7249 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7250 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7251 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 7252 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 7253 | if (!Pattern) { |
| 7254 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 7255 | << Context.getTypeDeclType(Record); |
| 7256 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 7257 | return true; |
| 7258 | } |
| 7259 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7260 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7261 | // If the explicit instantiation is for a class or member class, the |
| 7262 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7263 | // simple-template-id. |
| 7264 | // |
| 7265 | // C++98 has the same restriction, just worded differently. |
| 7266 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7267 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7268 | << Record << SS.getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7269 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7270 | // C++0x [temp.explicit]p2: |
| 7271 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7272 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7273 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 7274 | TemplateSpecializationKind TSK |
| 7275 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7276 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7277 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7278 | // C++0x [temp.explicit]p2: |
| 7279 | // [...] An explicit instantiation shall appear in an enclosing |
| 7280 | // namespace of its template. [...] |
| 7281 | // |
| 7282 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7283 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7284 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7285 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7286 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7287 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7288 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7289 | PrevDecl = Record; |
| 7290 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7291 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7292 | bool HasNoEffect = false; |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7293 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7294 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7295 | PrevDecl, |
| 7296 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7297 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7298 | HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7299 | return true; |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7300 | if (HasNoEffect) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7301 | return TagD; |
| 7302 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7303 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7304 | CXXRecordDecl *RecordDef |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7305 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 7306 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7307 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7308 | // 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] | 7309 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7310 | CXXRecordDecl *Def |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7311 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7312 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 7313 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 7314 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 7315 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 7316 | << Pattern; |
| 7317 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7318 | } else { |
| 7319 | if (InstantiateClass(NameLoc, Record, Def, |
| 7320 | getTemplateInstantiationArgs(Record), |
| 7321 | TSK)) |
| 7322 | return true; |
| 7323 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 7324 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7325 | if (!RecordDef) |
| 7326 | return true; |
| 7327 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7328 | } |
| 7329 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7330 | // Instantiate all of the members of the class. |
| 7331 | InstantiateClassMembers(NameLoc, RecordDef, |
| 7332 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7333 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7334 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7335 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 7336 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7337 | // FIXME: We don't have any representation for explicit instantiations of |
| 7338 | // member classes. Such a representation is not needed for compilation, but it |
| 7339 | // should be available for clients that want to see all of the declarations in |
| 7340 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 7341 | return TagD; |
| 7342 | } |
| 7343 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7344 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 7345 | SourceLocation ExternLoc, |
| 7346 | SourceLocation TemplateLoc, |
| 7347 | Declarator &D) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7348 | // Explicit instantiations always require a name. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7349 | // TODO: check if/when DNInfo should replace Name. |
| 7350 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 7351 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7352 | if (!Name) { |
| 7353 | if (!D.isInvalidType()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7354 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7355 | diag::err_explicit_instantiation_requires_name) |
| 7356 | << D.getDeclSpec().getSourceRange() |
| 7357 | << D.getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7358 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7359 | return true; |
| 7360 | } |
| 7361 | |
| 7362 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 7363 | // we find one that is. |
| 7364 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 7365 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 7366 | S = S->getParent(); |
| 7367 | |
| 7368 | // Determine the type of the declaration. |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7369 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 7370 | QualType R = T->getType(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7371 | if (R.isNull()) |
| 7372 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7373 | |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7374 | // C++ [dcl.stc]p1: |
| 7375 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 7376 | // instantiation (14.7.2) directive. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7377 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7378 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 7379 | << Name; |
| 7380 | return true; |
Douglas Gregor | 781ba6e | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 7381 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 7382 | != DeclSpec::SCS_unspecified) { |
| 7383 | // Complain about then remove the storage class specifier. |
| 7384 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 7385 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 7386 | |
| 7387 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7388 | } |
| 7389 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 7390 | // C++0x [temp.explicit]p1: |
| 7391 | // [...] An explicit instantiation of a function template shall not use the |
| 7392 | // inline or constexpr specifiers. |
| 7393 | // Presumably, this also applies to member functions of class templates as |
| 7394 | // well. |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7395 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7396 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7397 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 83c1929 | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 7398 | diag::err_explicit_instantiation_inline : |
| 7399 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7400 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7401 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
Richard Smith | 465841e | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 7402 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 7403 | // not already specified. |
| 7404 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 7405 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7406 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7407 | // C++0x [temp.explicit]p2: |
| 7408 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7409 | // definition and an explicit instantiation declaration. An explicit |
| 7410 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7411 | TemplateSpecializationKind TSK |
| 7412 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 7413 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7414 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7415 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7416 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7417 | |
| 7418 | if (!R->isFunctionType()) { |
| 7419 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7420 | // A [...] static data member of a class template can be explicitly |
| 7421 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7422 | // template. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7423 | // C++1y [temp.explicit]p1: |
| 7424 | // A [...] variable [...] template specialization can be explicitly |
| 7425 | // instantiated from its template. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7426 | if (Previous.isAmbiguous()) |
| 7427 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7428 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 7429 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7430 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7431 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7432 | if (!PrevTemplate) { |
| 7433 | if (!Prev || !Prev->isStaticDataMember()) { |
| 7434 | // We expect to see a data data member here. |
| 7435 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 7436 | << Name; |
| 7437 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7438 | P != PEnd; ++P) |
| 7439 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 7440 | return true; |
| 7441 | } |
| 7442 | |
| 7443 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 7444 | // FIXME: Check for explicit specialization? |
| 7445 | Diag(D.getIdentifierLoc(), |
| 7446 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 7447 | << Prev; |
| 7448 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 7449 | // FIXME: Can we provide a note showing where this was declared? |
| 7450 | return true; |
| 7451 | } |
| 7452 | } else { |
| 7453 | // Explicitly instantiate a variable template. |
| 7454 | |
| 7455 | // C++1y [dcl.spec.auto]p6: |
| 7456 | // ... A program that uses auto or decltype(auto) in a context not |
| 7457 | // explicitly allowed in this section is ill-formed. |
| 7458 | // |
| 7459 | // This includes auto-typed variable template instantiations. |
| 7460 | if (R->isUndeducedType()) { |
| 7461 | Diag(T->getTypeLoc().getLocStart(), |
| 7462 | diag::err_auto_not_allowed_var_inst); |
| 7463 | return true; |
| 7464 | } |
| 7465 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7466 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 7467 | // C++1y [temp.explicit]p3: |
| 7468 | // If the explicit instantiation is for a variable, the unqualified-id |
| 7469 | // in the declaration shall be a template-id. |
| 7470 | Diag(D.getIdentifierLoc(), |
| 7471 | diag::err_explicit_instantiation_without_template_id) |
| 7472 | << PrevTemplate; |
| 7473 | Diag(PrevTemplate->getLocation(), |
| 7474 | diag::note_explicit_instantiation_here); |
| 7475 | return true; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7476 | } |
| 7477 | |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7478 | // Translate the parser's template argument list into our AST format. |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7479 | TemplateArgumentListInfo TemplateArgs = |
| 7480 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Richard Smith | ef985ac | 2013-09-18 02:10:12 +0000 | [diff] [blame] | 7481 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7482 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 7483 | D.getIdentifierLoc(), TemplateArgs); |
| 7484 | if (Res.isInvalid()) |
| 7485 | return true; |
| 7486 | |
| 7487 | // Ignore access control bits, we don't need them for redeclaration |
| 7488 | // checking. |
| 7489 | Prev = cast<VarDecl>(Res.get()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7490 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7491 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7492 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7493 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7494 | // or a static data member of a class template specialization, the name of |
| 7495 | // the class template specialization in the qualified-id for the member |
| 7496 | // name shall be a simple-template-id. |
| 7497 | // |
| 7498 | // C++98 has the same restriction, just worded differently. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7499 | // |
Richard Smith | 5977d87 | 2013-09-18 21:55:14 +0000 | [diff] [blame] | 7500 | // This does not apply to variable template specializations, where the |
| 7501 | // template-id is in the unqualified-id instead. |
| 7502 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7503 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7504 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7505 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7506 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7507 | // Check the scope of this explicit instantiation. |
| 7508 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7509 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7510 | // Verify that it is okay to explicitly instantiate here. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 7511 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 7512 | SourceLocation POI = Prev->getPointOfInstantiation(); |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7513 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7514 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7515 | PrevTSK, POI, HasNoEffect)) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 7516 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7517 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 7518 | if (!HasNoEffect) { |
| 7519 | // Instantiate static data member or variable template. |
| 7520 | |
| 7521 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 7522 | if (PrevTemplate) { |
| 7523 | // Merge attributes. |
| 7524 | if (AttributeList *Attr = D.getDeclSpec().getAttributes().getList()) |
| 7525 | ProcessDeclAttributeList(S, Prev, Attr); |
| 7526 | } |
| 7527 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 7528 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 7529 | } |
| 7530 | |
| 7531 | // Check the new variable specialization against the parsed input. |
| 7532 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
| 7533 | Diag(T->getTypeLoc().getLocStart(), |
| 7534 | diag::err_invalid_var_template_spec_type) |
| 7535 | << 0 << PrevTemplate << R << Prev->getType(); |
| 7536 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 7537 | << 2 << PrevTemplate->getDeclName(); |
| 7538 | return true; |
| 7539 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7540 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7541 | // FIXME: Create an ExplicitInstantiation node? |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7542 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7543 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7544 | |
| 7545 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 7546 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7547 | bool HasExplicitTemplateArgs = false; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7548 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7549 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
Richard Smith | 4b55a9c | 2014-04-17 03:29:33 +0000 | [diff] [blame] | 7550 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7551 | HasExplicitTemplateArgs = true; |
| 7552 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7553 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7554 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7555 | // A [...] function [...] can be explicitly instantiated from its template. |
| 7556 | // A member function [...] of a class template can be explicitly |
| 7557 | // instantiated from the member definition associated with its class |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7558 | // template. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7559 | UnresolvedSet<8> Matches; |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7560 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7561 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 7562 | P != PEnd; ++P) { |
| 7563 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7564 | if (!HasExplicitTemplateArgs) { |
| 7565 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 7566 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType()); |
| 7567 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7568 | Matches.clear(); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7569 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7570 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 7571 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 7572 | break; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 7573 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7574 | } |
| 7575 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7576 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7577 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 7578 | if (!FunTmpl) |
| 7579 | continue; |
| 7580 | |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7581 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7582 | FunctionDecl *Specialization = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7583 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7584 | = DeduceTemplateArguments(FunTmpl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7585 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 7586 | : nullptr), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7587 | R, Specialization, Info)) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7588 | // Keep track of almost-matches. |
| 7589 | FailedCandidates.addCandidate() |
| 7590 | .set(FunTmpl->getTemplatedDecl(), |
| 7591 | MakeDeductionFailureInfo(Context, TDK, Info)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7592 | (void)TDK; |
| 7593 | continue; |
| 7594 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7595 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7596 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7597 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7598 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7599 | // Find the most specialized function template specialization. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7600 | UnresolvedSetIterator Result = getMostSpecialized( |
Richard Smith | 35e1da2 | 2013-09-10 22:59:25 +0000 | [diff] [blame] | 7601 | Matches.begin(), Matches.end(), FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 7602 | D.getIdentifierLoc(), |
| 7603 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 7604 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 7605 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7606 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7607 | if (Result == Matches.end()) |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7608 | return true; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7609 | |
| 7610 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 7611 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7612 | |
Alexey Bataev | 7398391 | 2014-11-06 10:10:50 +0000 | [diff] [blame] | 7613 | // C++11 [except.spec]p4 |
| 7614 | // In an explicit instantiation an exception-specification may be specified, |
| 7615 | // but is not required. |
| 7616 | // If an exception-specification is specified in an explicit instantiation |
| 7617 | // directive, it shall be compatible with the exception-specifications of |
| 7618 | // other declarations of that function. |
| 7619 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 7620 | if (FPT->hasExceptionSpec()) { |
| 7621 | unsigned DiagID = |
| 7622 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 7623 | if (getLangOpts().MicrosoftExt) |
| 7624 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 7625 | bool Result = CheckEquivalentExceptionSpec( |
| 7626 | PDiag(DiagID) << Specialization->getType(), |
| 7627 | PDiag(diag::note_explicit_instantiation_here), |
| 7628 | Specialization->getType()->getAs<FunctionProtoType>(), |
| 7629 | Specialization->getLocation(), FPT, D.getLocStart()); |
| 7630 | // In Microsoft mode, mismatching exception specifications just cause a |
| 7631 | // warning. |
| 7632 | if (!getLangOpts().MicrosoftExt && Result) |
| 7633 | return true; |
| 7634 | } |
| 7635 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7636 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7637 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7638 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 7639 | << Specialization |
| 7640 | << (Specialization->getTemplateSpecializationKind() == |
| 7641 | TSK_ExplicitSpecialization); |
| 7642 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 7643 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7644 | } |
| 7645 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 7646 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 7647 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 7648 | PrevDecl = Specialization; |
| 7649 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7650 | if (PrevDecl) { |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7651 | bool HasNoEffect = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 7652 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7653 | PrevDecl, |
| 7654 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7655 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7656 | HasNoEffect)) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7657 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7658 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7659 | // FIXME: We may still want to build some representation of this |
| 7660 | // explicit specialization. |
Abramo Bagnara | 8075c85 | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 7661 | if (HasNoEffect) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7662 | return (Decl*) nullptr; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7663 | } |
Anders Carlsson | 65e6d13 | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 7664 | |
| 7665 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 2aa7acf | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 7666 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 7667 | if (Attr) |
| 7668 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7669 | |
Richard Smith | eb36ddf | 2014-04-24 22:45:46 +0000 | [diff] [blame] | 7670 | if (Specialization->isDefined()) { |
| 7671 | // Let the ASTConsumer know that this function has been explicitly |
| 7672 | // instantiated now, and its linkage might have changed. |
| 7673 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 7674 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 7675 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7676 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7677 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7678 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7679 | // or a static data member of a class template specialization, the name of |
| 7680 | // the class template specialization in the qualified-id for the member |
| 7681 | // name shall be a simple-template-id. |
| 7682 | // |
| 7683 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 7684 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7685 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7686 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7687 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7688 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | 010815a | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 7689 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7690 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7691 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7692 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7693 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7694 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7695 | D.getIdentifierLoc(), |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 7696 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7697 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7698 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7699 | return (Decl*) nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 7700 | } |
| 7701 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7702 | TypeResult |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7703 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 7704 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 7705 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 7706 | // This has to hold, because SS is expected to be defined. |
| 7707 | assert(Name && "Expected a name in a dependent tag"); |
| 7708 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7709 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7710 | if (!NNS) |
| 7711 | return true; |
| 7712 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7713 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | f4b37e1 | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 7714 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 7715 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 7716 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7717 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 7718 | return true; |
| 7719 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7720 | |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7721 | // Create the resulting type. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7722 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7723 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 7724 | |
| 7725 | // Create type-source location information for this type. |
| 7726 | TypeLocBuilder TLB; |
| 7727 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7728 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 7729 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 7730 | TL.setNameLoc(NameLoc); |
| 7731 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 7732 | } |
| 7733 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7734 | TypeResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7735 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 7736 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | f7d7771 | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 7737 | SourceLocation IdLoc) { |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7738 | if (SS.isInvalid()) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7739 | return true; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7740 | |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7741 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 7742 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7743 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7744 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 7745 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7746 | << FixItHint::CreateRemoval(TypenameLoc); |
| 7747 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 7748 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 7749 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 7750 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 7751 | if (T.isNull()) |
| 7752 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7753 | |
| 7754 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 7755 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7756 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7757 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 7758 | TL.setQualifierLoc(QualifierLoc); |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 7759 | TL.setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7760 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7761 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7762 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 7763 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7764 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7765 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7766 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7767 | return CreateParsedType(T, TSI); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7768 | } |
| 7769 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7770 | TypeResult |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7771 | Sema::ActOnTypenameType(Scope *S, |
| 7772 | SourceLocation TypenameLoc, |
| 7773 | const CXXScopeSpec &SS, |
| 7774 | SourceLocation TemplateKWLoc, |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7775 | TemplateTy TemplateIn, |
| 7776 | SourceLocation TemplateNameLoc, |
| 7777 | SourceLocation LAngleLoc, |
| 7778 | ASTTemplateArgsPtr TemplateArgsIn, |
| 7779 | SourceLocation RAngleLoc) { |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7780 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 7781 | Diag(TypenameLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7782 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7783 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 7784 | diag::ext_typename_outside_of_template) |
| 7785 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7786 | |
| 7787 | // Translate the parser's template argument list in our AST format. |
| 7788 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 7789 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 7790 | |
| 7791 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7792 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 7793 | // Construct a dependent template specialization type. |
| 7794 | assert(DTN && "dependent template has non-dependent name?"); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7795 | assert(DTN->getQualifier() == SS.getScopeRep()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7796 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 7797 | DTN->getQualifier(), |
| 7798 | DTN->getIdentifier(), |
| 7799 | TemplateArgs); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7800 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7801 | // Create source-location information for this type. |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 7802 | TypeLocBuilder Builder; |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7803 | DependentTemplateSpecializationTypeLoc SpecTL |
| 7804 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7805 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 7806 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 7807 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7808 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7809 | SpecTL.setLAngleLoc(LAngleLoc); |
| 7810 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7811 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 7812 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7813 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 7814 | } |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7815 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7816 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 7817 | if (T.isNull()) |
| 7818 | return true; |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7819 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7820 | // Provide source-location information for the template specialization type. |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7821 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7822 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7823 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 7824 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 7825 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7826 | SpecTL.setLAngleLoc(LAngleLoc); |
| 7827 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7828 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 7829 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 7830 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7831 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 7832 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 7833 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 7834 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 7835 | |
Douglas Gregor | 84a6a0a | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 7836 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 7837 | return CreateParsedType(T, TSI); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 7838 | } |
| 7839 | |
Douglas Gregor | b09518c | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 7840 | |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7841 | /// Determine whether this failed name lookup should be treated as being |
| 7842 | /// disabled by a usage of std::enable_if. |
| 7843 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
| 7844 | SourceRange &CondRange) { |
| 7845 | // We must be looking for a ::type... |
| 7846 | if (!II.isStr("type")) |
| 7847 | return false; |
| 7848 | |
| 7849 | // ... within an explicitly-written template specialization... |
| 7850 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 7851 | return false; |
| 7852 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7853 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 7854 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 7855 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7856 | return false; |
| 7857 | const TemplateSpecializationType *EnableIfTST = |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7858 | cast<TemplateSpecializationType>(EnableIfTSTLoc.getTypePtr()); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7859 | |
| 7860 | // ... which names a complete class template declaration... |
| 7861 | const TemplateDecl *EnableIfDecl = |
| 7862 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 7863 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 7864 | return false; |
| 7865 | |
| 7866 | // ... called "enable_if". |
| 7867 | const IdentifierInfo *EnableIfII = |
| 7868 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 7869 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 7870 | return false; |
| 7871 | |
| 7872 | // Assume the first template argument is the condition. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 7873 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7874 | return true; |
| 7875 | } |
| 7876 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7877 | /// \brief Build the type that describes a C++ typename specifier, |
| 7878 | /// e.g., "typename T::type". |
| 7879 | QualType |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7880 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 7881 | SourceLocation KeywordLoc, |
| 7882 | NestedNameSpecifierLoc QualifierLoc, |
| 7883 | const IdentifierInfo &II, |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 7884 | SourceLocation IILoc) { |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7885 | CXXScopeSpec SS; |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7886 | SS.Adopt(QualifierLoc); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7887 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7888 | DeclContext *Ctx = computeDeclContext(SS); |
| 7889 | if (!Ctx) { |
| 7890 | // If the nested-name-specifier is dependent and couldn't be |
| 7891 | // resolved to a type, build a typename type. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7892 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 7893 | return Context.getDependentNameType(Keyword, |
| 7894 | QualifierLoc.getNestedNameSpecifier(), |
| 7895 | &II); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 7896 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7897 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7898 | // If the nested-name-specifier refers to the current instantiation, |
| 7899 | // the "typename" keyword itself is superfluous. In C++03, the |
| 7900 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 7901 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | c9d2682 | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 7902 | // 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] | 7903 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7904 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 7905 | return QualType(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7906 | |
| 7907 | DeclarationName Name(&II); |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 7908 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 7909 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
| 7910 | if (NNS->getKind() == NestedNameSpecifier::Super) |
| 7911 | LookupInSuper(Result, NNS->getAsRecordDecl()); |
| 7912 | else |
| 7913 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7914 | unsigned DiagID = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7915 | Decl *Referenced = nullptr; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7916 | switch (Result.getResultKind()) { |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7917 | case LookupResult::NotFound: { |
| 7918 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 7919 | // a more specific diagnostic. |
| 7920 | SourceRange CondRange; |
| 7921 | if (isEnableIf(QualifierLoc, II, CondRange)) { |
| 7922 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
| 7923 | << Ctx << CondRange; |
| 7924 | return QualType(); |
| 7925 | } |
| 7926 | |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 7927 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7928 | break; |
Richard Smith | 6f8d2c6 | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7929 | } |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 7930 | |
| 7931 | case LookupResult::FoundUnresolvedValue: { |
| 7932 | // We found a using declaration that is a value. Most likely, the using |
| 7933 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7934 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 7935 | IILoc); |
| 7936 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 7937 | << Name << Ctx << FullRange; |
| 7938 | if (UnresolvedUsingValueDecl *Using |
| 7939 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7940 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | aed2efb | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 7941 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 7942 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 7943 | } |
| 7944 | } |
| 7945 | // Fall through to create a dependent typename type, from which we can recover |
| 7946 | // better. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7947 | |
Douglas Gregor | d0d2ee0 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 7948 | case LookupResult::NotFoundInCurrentInstantiation: |
| 7949 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7950 | return Context.getDependentNameType(Keyword, |
| 7951 | QualifierLoc.getNestedNameSpecifier(), |
| 7952 | &II); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7953 | |
| 7954 | case LookupResult::Found: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7955 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7956 | // We found a type. Build an ElaboratedType, since the |
| 7957 | // typename-specifier was just sugar. |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 7958 | MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7959 | return Context.getElaboratedType(ETK_Typename, |
| 7960 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7961 | Context.getTypeDeclType(Type)); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7962 | } |
| 7963 | |
| 7964 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7965 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7966 | break; |
| 7967 | |
| 7968 | case LookupResult::FoundOverloaded: |
| 7969 | DiagID = diag::err_typename_nested_not_type; |
| 7970 | Referenced = *Result.begin(); |
| 7971 | break; |
| 7972 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 7973 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7974 | return QualType(); |
| 7975 | } |
| 7976 | |
| 7977 | // If we get here, it's because name lookup did not find a |
| 7978 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7979 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 7980 | IILoc); |
| 7981 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7982 | if (Referenced) |
| 7983 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 7984 | << Name; |
| 7985 | return QualType(); |
| 7986 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7987 | |
| 7988 | namespace { |
| 7989 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 7990 | class CurrentInstantiationRebuilder |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7991 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7992 | SourceLocation Loc; |
| 7993 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7994 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7995 | public: |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 7996 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7997 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7998 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7999 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8000 | DeclarationName Entity) |
| 8001 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8002 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8003 | |
| 8004 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8005 | /// transformed. |
| 8006 | /// |
| 8007 | /// For the purposes of type reconstruction, a type has already been |
| 8008 | /// transformed if it is NULL or if it is not dependent. |
| 8009 | bool AlreadyTransformed(QualType T) { |
| 8010 | return T.isNull() || !T->isDependentType(); |
| 8011 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8012 | |
| 8013 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8014 | /// rebuilt. |
| 8015 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8016 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8017 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 8018 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8019 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 8020 | /// \brief Sets the "base" location and entity when that |
| 8021 | /// information is known based on another transformation. |
| 8022 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 8023 | this->Loc = Loc; |
| 8024 | this->Entity = Entity; |
| 8025 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 8026 | |
| 8027 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 8028 | // Lambdas never need to be transformed. |
| 8029 | return E; |
| 8030 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8031 | }; |
| 8032 | } |
| 8033 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8034 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 8035 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8036 | /// 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] | 8037 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8038 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8039 | /// partial specialization thereof). This routine will rebuild that type now |
| 8040 | /// that we have entered the declarator's scope, which may produce different |
| 8041 | /// canonical types, e.g., |
| 8042 | /// |
| 8043 | /// \code |
| 8044 | /// template<typename T> |
| 8045 | /// struct X { |
| 8046 | /// typedef T* pointer; |
| 8047 | /// pointer data(); |
| 8048 | /// }; |
| 8049 | /// |
| 8050 | /// template<typename T> |
| 8051 | /// typename X<T>::pointer X<T>::data() { ... } |
| 8052 | /// \endcode |
| 8053 | /// |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 8054 | /// 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] | 8055 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 8056 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 8057 | /// 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] | 8058 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 8059 | /// definition and the declaration to match. |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8060 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 8061 | SourceLocation Loc, |
| 8062 | DeclarationName Name) { |
| 8063 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8064 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8065 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 8066 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 8067 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 8068 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8069 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8070 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8071 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 8072 | DeclarationName()); |
| 8073 | return Rebuilder.TransformExpr(E); |
| 8074 | } |
| 8075 | |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8076 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8077 | if (SS.isInvalid()) |
| 8078 | return true; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8079 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8080 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8081 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 8082 | DeclarationName()); |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8083 | NestedNameSpecifierLoc Rebuilt |
| 8084 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 8085 | if (!Rebuilt) |
| 8086 | return true; |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8087 | |
Douglas Gregor | 1017641 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 8088 | SS.Adopt(Rebuilt); |
John McCall | 99b2fe5 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 8089 | return false; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 8090 | } |
| 8091 | |
Douglas Gregor | 041b084 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 8092 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 8093 | /// instantiation. |
| 8094 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 8095 | TemplateParameterList *Params) { |
| 8096 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 8097 | Decl *Param = Params->getParam(I); |
| 8098 | |
| 8099 | // There is nothing to rebuild in a type parameter. |
| 8100 | if (isa<TemplateTypeParmDecl>(Param)) |
| 8101 | continue; |
| 8102 | |
| 8103 | // Rebuild the template parameter list of a template template parameter. |
| 8104 | if (TemplateTemplateParmDecl *TTP |
| 8105 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 8106 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 8107 | TTP->getTemplateParameters())) |
| 8108 | return true; |
| 8109 | |
| 8110 | continue; |
| 8111 | } |
| 8112 | |
| 8113 | // Rebuild the type of a non-type template parameter. |
| 8114 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 8115 | TypeSourceInfo *NewTSI |
| 8116 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 8117 | NTTP->getLocation(), |
| 8118 | NTTP->getDeclName()); |
| 8119 | if (!NewTSI) |
| 8120 | return true; |
| 8121 | |
| 8122 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 8123 | NTTP->setTypeSourceInfo(NewTSI); |
| 8124 | NTTP->setType(NewTSI->getType()); |
| 8125 | } |
| 8126 | } |
| 8127 | |
| 8128 | return false; |
| 8129 | } |
| 8130 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8131 | /// \brief Produces a formatted string that describes the binding of |
| 8132 | /// template parameters to template arguments. |
| 8133 | std::string |
| 8134 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8135 | const TemplateArgumentList &Args) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8136 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8137 | } |
| 8138 | |
| 8139 | std::string |
| 8140 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 8141 | const TemplateArgument *Args, |
| 8142 | unsigned NumArgs) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8143 | SmallString<128> Str; |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8144 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8145 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8146 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8147 | return std::string(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8148 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8149 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 8150 | if (I >= NumArgs) |
| 8151 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8152 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8153 | if (I == 0) |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8154 | Out << "[with "; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8155 | else |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8156 | Out << ", "; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8157 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8158 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8159 | Out << Id->getName(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8160 | } else { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8161 | Out << '$' << I; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8162 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8163 | |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8164 | Out << " = "; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 8165 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8166 | } |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 8167 | |
| 8168 | Out << ']'; |
| 8169 | return Out.str(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 8170 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8171 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8172 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 8173 | CachedTokens &Toks) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8174 | if (!FD) |
| 8175 | return; |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 8176 | |
| 8177 | LateParsedTemplate *LPT = new LateParsedTemplate; |
| 8178 | |
| 8179 | // Take tokens to avoid allocations |
| 8180 | LPT->Toks.swap(Toks); |
| 8181 | LPT->D = FnD; |
| 8182 | LateParsedTemplateMap[FD] = LPT; |
| 8183 | |
| 8184 | FD->setLateTemplateParsed(true); |
| 8185 | } |
| 8186 | |
| 8187 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 8188 | if (!FD) |
| 8189 | return; |
| 8190 | FD->setLateTemplateParsed(false); |
| 8191 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 8192 | |
| 8193 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 8194 | DeclContext *DC = CurContext; |
| 8195 | |
| 8196 | while (DC) { |
| 8197 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 8198 | const FunctionDecl *FD = RD->isLocalClass(); |
| 8199 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 8200 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 8201 | return false; |
| 8202 | |
| 8203 | DC = DC->getParent(); |
| 8204 | } |
| 8205 | return false; |
| 8206 | } |