Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1 | //===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/ |
Douglas Gregor | 72c3f31 | 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 | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 8 | // |
| 9 | // This file implements semantic analysis for C++ templates. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 10 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 11 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 12 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 13 | #include "clang/Sema/Lookup.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Scope.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Template.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 16 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 17 | #include "TreeTransform.h" |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
John McCall | 92b7f70 | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 24 | #include "clang/AST/TypeVisitor.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 25 | #include "clang/Sema/DeclSpec.h" |
| 26 | #include "clang/Sema/ParsedTemplate.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 27 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallBitVector.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 32 | using namespace clang; |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 33 | using namespace sema; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 34 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 35 | // Exported for use by Parser. |
| 36 | SourceRange |
| 37 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
| 38 | unsigned N) { |
| 39 | if (!N) return SourceRange(); |
| 40 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
| 41 | } |
| 42 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 43 | /// \brief Determine whether the declaration found is acceptable as the name |
| 44 | /// of a template and, if so, return that template declaration. Otherwise, |
| 45 | /// returns NULL. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 46 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, |
Douglas Gregor | 5a7a5bb | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 47 | NamedDecl *Orig, |
| 48 | bool AllowFunctionTemplates) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 49 | NamedDecl *D = Orig->getUnderlyingDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | |
Douglas Gregor | 5a7a5bb | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 51 | if (isa<TemplateDecl>(D)) { |
| 52 | if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D)) |
| 53 | return 0; |
| 54 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 55 | return Orig; |
Douglas Gregor | 5a7a5bb | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 56 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 58 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 59 | // C++ [temp.local]p1: |
| 60 | // Like normal (non-template) classes, class templates have an |
| 61 | // injected-class-name (Clause 9). The injected-class-name |
| 62 | // can be used with or without a template-argument-list. When |
| 63 | // it is used without a template-argument-list, it is |
| 64 | // equivalent to the injected-class-name followed by the |
| 65 | // template-parameters of the class template enclosed in |
| 66 | // <>. When it is used with a template-argument-list, it |
| 67 | // refers to the specified class template specialization, |
| 68 | // which could be the current specialization or another |
| 69 | // specialization. |
| 70 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 542b548 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 71 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 72 | if (Record->getDescribedClassTemplate()) |
| 73 | return Record->getDescribedClassTemplate(); |
| 74 | |
| 75 | if (ClassTemplateSpecializationDecl *Spec |
| 76 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 77 | return Spec->getSpecializedTemplate(); |
| 78 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 80 | return 0; |
| 81 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
Douglas Gregor | 5a7a5bb | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 86 | void Sema::FilterAcceptableTemplateNames(LookupResult &R, |
| 87 | bool AllowFunctionTemplates) { |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 88 | // The set of class templates we've already seen. |
| 89 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 90 | LookupResult::Filter filter = R.makeFilter(); |
| 91 | while (filter.hasNext()) { |
| 92 | NamedDecl *Orig = filter.next(); |
Douglas Gregor | 5a7a5bb | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 93 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig, |
| 94 | AllowFunctionTemplates); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 95 | if (!Repl) |
| 96 | filter.erase(); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 97 | else if (Repl != Orig) { |
| 98 | |
| 99 | // C++ [temp.local]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 100 | // A lookup that finds an injected-class-name (10.2) can result in an |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 101 | // ambiguity in certain cases (for example, if it is found in more than |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 102 | // one base class). If all of the injected-class-names that are found |
| 103 | // refer to specializations of the same class template, and if the name |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 104 | // is used as a template-name, the reference refers to the class |
| 105 | // template itself and not a specialization thereof, and is not |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 106 | // ambiguous. |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 107 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
| 108 | if (!ClassTemplates.insert(ClassTmpl)) { |
| 109 | filter.erase(); |
| 110 | continue; |
| 111 | } |
John McCall | 8ba6691 | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 112 | |
| 113 | // FIXME: we promote access to public here as a workaround to |
| 114 | // the fact that LookupResult doesn't let us remember that we |
| 115 | // found this template through a particular injected class name, |
| 116 | // which means we end up doing nasty things to the invariants. |
| 117 | // Pretending that access is public is *much* safer. |
| 118 | filter.replace(Repl, AS_public); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 119 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 120 | } |
| 121 | filter.done(); |
| 122 | } |
| 123 | |
Douglas Gregor | 5a7a5bb | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 124 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R, |
| 125 | bool AllowFunctionTemplates) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 126 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
Douglas Gregor | 5a7a5bb | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 127 | if (isAcceptableTemplateName(Context, *I, AllowFunctionTemplates)) |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 128 | return true; |
| 129 | |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 130 | return false; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 133 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 134 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 135 | bool hasTemplateKeyword, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 136 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 137 | ParsedType ObjectTypePtr, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 138 | bool EnteringContext, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 139 | TemplateTy &TemplateResult, |
| 140 | bool &MemberOfUnknownSpecialization) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 141 | assert(getLangOpts().CPlusPlus && "No template names in C!"); |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 142 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 143 | DeclarationName TName; |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 144 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 145 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 146 | switch (Name.getKind()) { |
| 147 | case UnqualifiedId::IK_Identifier: |
| 148 | TName = DeclarationName(Name.Identifier); |
| 149 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 150 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 151 | case UnqualifiedId::IK_OperatorFunctionId: |
| 152 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 153 | Name.OperatorFunctionId.Operator); |
| 154 | break; |
| 155 | |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 156 | case UnqualifiedId::IK_LiteralOperatorId: |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 157 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 158 | break; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 159 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 160 | default: |
| 161 | return TNK_Non_template; |
| 162 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 164 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 166 | LookupResult R(*this, TName, Name.getLocStart(), LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 167 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 168 | MemberOfUnknownSpecialization); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 169 | if (R.empty()) return TNK_Non_template; |
| 170 | if (R.isAmbiguous()) { |
| 171 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 172 | R.suppressDiagnostics(); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 173 | |
| 174 | // FIXME: we might have ambiguous templates, in which case we |
| 175 | // should at least parse them properly! |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 176 | return TNK_Non_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 177 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 178 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 179 | TemplateName Template; |
| 180 | TemplateNameKind TemplateKind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 182 | unsigned ResultCount = R.end() - R.begin(); |
| 183 | if (ResultCount > 1) { |
| 184 | // We assume that we'll preserve the qualifier from a function |
| 185 | // template name in other ways. |
| 186 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 187 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 188 | |
| 189 | // We'll do this lookup again later. |
| 190 | R.suppressDiagnostics(); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 191 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 192 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 193 | |
| 194 | if (SS.isSet() && !SS.isInvalid()) { |
| 195 | NestedNameSpecifier *Qualifier |
| 196 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 197 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 198 | hasTemplateKeyword, TD); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 199 | } else { |
| 200 | Template = TemplateName(TD); |
| 201 | } |
| 202 | |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 203 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 204 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 205 | |
| 206 | // We'll do this lookup again later. |
| 207 | R.suppressDiagnostics(); |
| 208 | } else { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 209 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
| 210 | isa<TypeAliasTemplateDecl>(TD)); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 211 | TemplateKind = TNK_Type_template; |
| 212 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 213 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 215 | TemplateResult = TemplateTy::make(Template); |
| 216 | return TemplateKind; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 217 | } |
| 218 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 219 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 220 | SourceLocation IILoc, |
| 221 | Scope *S, |
| 222 | const CXXScopeSpec *SS, |
| 223 | TemplateTy &SuggestedTemplate, |
| 224 | TemplateNameKind &SuggestedKind) { |
| 225 | // We can't recover unless there's a dependent scope specifier preceding the |
| 226 | // template name. |
Douglas Gregor | d5ab9b0 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 227 | // FIXME: Typo correction? |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 228 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 229 | computeDeclContext(*SS)) |
| 230 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 231 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 232 | // The code is missing a 'template' keyword prior to the dependent template |
| 233 | // name. |
| 234 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 235 | Diag(IILoc, diag::err_template_kw_missing) |
| 236 | << Qualifier << II.getName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 237 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 238 | SuggestedTemplate |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 239 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 240 | SuggestedKind = TNK_Dependent_template_name; |
| 241 | return true; |
| 242 | } |
| 243 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 244 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 245 | Scope *S, CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 246 | QualType ObjectType, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 247 | bool EnteringContext, |
| 248 | bool &MemberOfUnknownSpecialization) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 249 | // Determine where to perform name lookup |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 250 | MemberOfUnknownSpecialization = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 251 | DeclContext *LookupCtx = 0; |
| 252 | bool isDependent = false; |
| 253 | if (!ObjectType.isNull()) { |
| 254 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 255 | // x->B::f, and we are looking into the type of the object. |
| 256 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 257 | LookupCtx = computeDeclContext(ObjectType); |
| 258 | isDependent = ObjectType->isDependentType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 259 | assert((isDependent || !ObjectType->isIncompleteType()) && |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 260 | "Caller should have completed object type"); |
Douglas Gregor | 1d7049a | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 261 | |
| 262 | // Template names cannot appear inside an Objective-C class or object type. |
| 263 | if (ObjectType->isObjCObjectOrInterfaceType()) { |
| 264 | Found.clear(); |
| 265 | return; |
| 266 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 267 | } else if (SS.isSet()) { |
| 268 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 269 | // so long into the context associated with the prior nested-name-specifier. |
| 270 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 271 | isDependent = isDependentScopeSpecifier(SS); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 272 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 273 | // The declaration context must be complete. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 274 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 275 | return; |
| 276 | } |
| 277 | |
| 278 | bool ObjectTypeSearchedInScope = false; |
Douglas Gregor | 5a7a5bb | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 279 | bool AllowFunctionTemplatesInLookup = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 280 | if (LookupCtx) { |
| 281 | // Perform "qualified" name lookup into the declaration context we |
| 282 | // computed, which is either the type of the base of a member access |
| 283 | // expression or the declaration context associated with a prior |
| 284 | // nested-name-specifier. |
| 285 | LookupQualifiedName(Found, LookupCtx); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 286 | if (!ObjectType.isNull() && Found.empty()) { |
| 287 | // C++ [basic.lookup.classref]p1: |
| 288 | // In a class member access expression (5.2.5), if the . or -> token is |
| 289 | // immediately followed by an identifier followed by a <, the |
| 290 | // identifier must be looked up to determine whether the < is the |
| 291 | // beginning of a template argument list (14.2) or a less-than operator. |
| 292 | // The identifier is first looked up in the class of the object |
| 293 | // expression. If the identifier is not found, it is then looked up in |
| 294 | // the context of the entire postfix-expression and shall name a class |
| 295 | // or function template. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 296 | if (S) LookupName(Found, S); |
| 297 | ObjectTypeSearchedInScope = true; |
Douglas Gregor | 5a7a5bb | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 298 | AllowFunctionTemplatesInLookup = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 299 | } |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 300 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 301 | // We cannot look into a dependent object type or nested nme |
| 302 | // specifier. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 303 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 304 | return; |
| 305 | } else { |
| 306 | // Perform unqualified name lookup in the current scope. |
| 307 | LookupName(Found, S); |
Douglas Gregor | 5a7a5bb | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 308 | |
| 309 | if (!ObjectType.isNull()) |
| 310 | AllowFunctionTemplatesInLookup = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 313 | if (Found.empty() && !isDependent) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 314 | // If we did not find any names, attempt to correct any typos. |
| 315 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 316 | Found.clear(); |
Kaelyn Uhrain | f8ec8c9 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 317 | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
| 318 | CorrectionCandidateCallback FilterCCC; |
| 319 | FilterCCC.WantTypeSpecifiers = false; |
| 320 | FilterCCC.WantExpressionKeywords = false; |
| 321 | FilterCCC.WantRemainingKeywords = false; |
| 322 | FilterCCC.WantCXXNamedCasts = true; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 323 | if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(), |
| 324 | Found.getLookupKind(), S, &SS, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 325 | FilterCCC, LookupCtx)) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 326 | Found.setLookupName(Corrected.getCorrection()); |
| 327 | if (Corrected.getCorrectionDecl()) |
| 328 | Found.addDecl(Corrected.getCorrectionDecl()); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 329 | FilterAcceptableTemplateNames(Found); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 330 | if (!Found.empty()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 331 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 332 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOpts())); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 333 | if (LookupCtx) |
| 334 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 335 | << Name << LookupCtx << CorrectedQuotedStr << SS.getRange() |
| 336 | << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 337 | else |
| 338 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 339 | << Name << CorrectedQuotedStr |
| 340 | << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 341 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) |
| 342 | Diag(Template->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 343 | << CorrectedQuotedStr; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 344 | } |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 345 | } else { |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 346 | Found.setLookupName(Name); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Douglas Gregor | 5a7a5bb | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 350 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 351 | if (Found.empty()) { |
| 352 | if (isDependent) |
| 353 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 354 | return; |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 355 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 356 | |
Douglas Gregor | 05e6076 | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 357 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && |
| 358 | !(getLangOpts().CPlusPlus0x && !Found.empty())) { |
| 359 | // C++03 [basic.lookup.classref]p1: |
John McCall | f7a1a74 | 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 | 05e6076 | 2012-05-01 20:23:02 +0000 | [diff] [blame] | 364 | // Note: C++11 does not perform this second lookup. |
John McCall | f7a1a74 | 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 | 5a7a5bb | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 368 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 369 | |
John McCall | f7a1a74 | 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 | a6d1e76 | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 373 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 374 | FoundOuter.isAmbiguous()) { |
John McCall | f7a1a74 | 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 | a6d1e76 | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 378 | FoundOuter.clear(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 379 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | f7a1a74 | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 386 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 21d07e4 | 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 | f7a1a74 | 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 | 2f841ba | 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 | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 406 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 407 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 408 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 409 | const DeclarationNameInfo &NameInfo, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 410 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 411 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 412 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 413 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 414 | if (!isAddressOfOperand && |
John McCall | ea1471e | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 418 | |
John McCall | f7a1a74 | 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. |
| 421 | NamedDecl *FirstQualifierInScope = 0; |
| 422 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 423 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 424 | /*This*/ 0, ThisType, |
| 425 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 426 | /*Op*/ SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 427 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 428 | TemplateKWLoc, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 429 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 430 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 431 | TemplateArgs)); |
| 432 | } |
| 433 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 434 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 435 | } |
| 436 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 437 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 438 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 439 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 440 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 441 | const TemplateArgumentListInfo *TemplateArgs) { |
| 442 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 443 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 444 | TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 445 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 446 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 449 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 450 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 451 | /// declaration at location Loc. Returns true to indicate that this is |
| 452 | /// an error, and false otherwise. |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 453 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 454 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 455 | |
| 456 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 457 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 458 | return; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 459 | |
| 460 | // C++ [temp.local]p4: |
| 461 | // A template-parameter shall not be redeclared within its |
| 462 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 464 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 465 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 466 | return; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 469 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 470 | /// the parameter D to reference the templated declaration and return a pointer |
| 471 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 472 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 473 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 474 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 475 | return Temp; |
| 476 | } |
| 477 | return 0; |
| 478 | } |
| 479 | |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 480 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 481 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 482 | assert(Kind == Template && |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 483 | "Only template template arguments can be pack expansions here"); |
| 484 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 485 | "Template template argument pack expansion without packs"); |
| 486 | ParsedTemplateArgument Result(*this); |
| 487 | Result.EllipsisLoc = EllipsisLoc; |
| 488 | return Result; |
| 489 | } |
| 490 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 491 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 492 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 493 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 494 | switch (Arg.getKind()) { |
| 495 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 496 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 497 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 498 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 499 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 500 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 501 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 502 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 503 | case ParsedTemplateArgument::NonType: { |
| 504 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 505 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 506 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 507 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 508 | case ParsedTemplateArgument::Template: { |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 509 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 510 | TemplateArgument TArg; |
| 511 | if (Arg.getEllipsisLoc().isValid()) |
| 512 | TArg = TemplateArgument(Template, llvm::Optional<unsigned int>()); |
| 513 | else |
| 514 | TArg = Template; |
| 515 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 516 | Arg.getScopeSpec().getWithLocInContext( |
| 517 | SemaRef.Context), |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 518 | Arg.getLocation(), |
| 519 | Arg.getEllipsisLoc()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 522 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 523 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 524 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 525 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 526 | /// \brief Translates template arguments as provided by the parser |
| 527 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 528 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 529 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 530 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 531 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 532 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 533 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 534 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 535 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 536 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 537 | /// the keyword "typename" was used to declare the type parameter |
| 538 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 539 | /// "class" or "typename" keyword. ParamName is the name of the |
| 540 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 541 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 542 | /// If the type parameter has a default argument, it will be added |
| 543 | /// later via ActOnTypeParameterDefault. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 544 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
| 545 | SourceLocation EllipsisLoc, |
| 546 | SourceLocation KeyLoc, |
| 547 | IdentifierInfo *ParamName, |
| 548 | SourceLocation ParamNameLoc, |
| 549 | unsigned Depth, unsigned Position, |
| 550 | SourceLocation EqualLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 551 | ParsedType DefaultArg) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | assert(S->isTemplateParamScope() && |
| 553 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 554 | bool Invalid = false; |
| 555 | |
| 556 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 557 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 558 | LookupOrdinaryName, |
| 559 | ForRedeclaration); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 560 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 561 | DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl); |
| 562 | PrevDecl = 0; |
| 563 | } |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 566 | SourceLocation Loc = ParamNameLoc; |
| 567 | if (!ParamName) |
| 568 | Loc = KeyLoc; |
| 569 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 570 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 571 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 572 | KeyLoc, Loc, Depth, Position, ParamName, |
| 573 | Typename, Ellipsis); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 574 | Param->setAccess(AS_public); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 575 | if (Invalid) |
| 576 | Param->setInvalidDecl(); |
| 577 | |
| 578 | if (ParamName) { |
| 579 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 580 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 581 | IdResolver.AddDecl(Param); |
| 582 | } |
| 583 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 584 | // C++0x [temp.param]p9: |
| 585 | // A default template-argument may be specified for any kind of |
| 586 | // template-parameter that is not a template parameter pack. |
| 587 | if (DefaultArg && Ellipsis) { |
| 588 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 589 | DefaultArg = ParsedType(); |
| 590 | } |
| 591 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 592 | // Handle the default argument, if provided. |
| 593 | if (DefaultArg) { |
| 594 | TypeSourceInfo *DefaultTInfo; |
| 595 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 596 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 597 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 598 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 599 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 600 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 601 | UPPC_DefaultArgument)) |
| 602 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 603 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 604 | // Check the template argument itself. |
| 605 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 606 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 607 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 608 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 609 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 610 | Param->setDefaultArgument(DefaultTInfo, false); |
| 611 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 612 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 613 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 616 | /// \brief Check that the type of a non-type template parameter is |
| 617 | /// well-formed. |
| 618 | /// |
| 619 | /// \returns the (possibly-promoted) parameter type if valid; |
| 620 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 621 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 622 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 623 | // We don't allow variably-modified types as the type of non-type template |
| 624 | // parameters. |
| 625 | if (T->isVariablyModifiedType()) { |
| 626 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 627 | << T; |
| 628 | return QualType(); |
| 629 | } |
| 630 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 631 | // C++ [temp.param]p4: |
| 632 | // |
| 633 | // A non-type template-parameter shall have one of the following |
| 634 | // (optionally cv-qualified) types: |
| 635 | // |
| 636 | // -- integral or enumeration type, |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 637 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | // -- pointer to object or pointer to function, |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 639 | T->isPointerType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 641 | T->isReferenceType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 642 | // -- pointer to member, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 643 | T->isMemberPointerType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 644 | // -- std::nullptr_t. |
| 645 | T->isNullPtrType() || |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 646 | // If T is a dependent type, we can't do the check now, so we |
| 647 | // assume that it is well-formed. |
Richard Smith | e37f484 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 648 | T->isDependentType()) { |
| 649 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 650 | // are ignored when determining its type. |
| 651 | return T.getUnqualifiedType(); |
| 652 | } |
| 653 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 654 | // C++ [temp.param]p8: |
| 655 | // |
| 656 | // A non-type template-parameter of type "array of T" or |
| 657 | // "function returning T" is adjusted to be of type "pointer to |
| 658 | // T" or "pointer to function returning T", respectively. |
| 659 | else if (T->isArrayType()) |
| 660 | // FIXME: Keep the type prior to promotion? |
| 661 | return Context.getArrayDecayedType(T); |
| 662 | else if (T->isFunctionType()) |
| 663 | // FIXME: Keep the type prior to promotion? |
| 664 | return Context.getPointerType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 665 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 666 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 667 | << T; |
| 668 | |
| 669 | return QualType(); |
| 670 | } |
| 671 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 672 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 673 | unsigned Depth, |
| 674 | unsigned Position, |
| 675 | SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 676 | Expr *Default) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 677 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 678 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 679 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 680 | assert(S->isTemplateParamScope() && |
| 681 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 682 | bool Invalid = false; |
| 683 | |
| 684 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 685 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 686 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 687 | LookupOrdinaryName, |
| 688 | ForRedeclaration); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 689 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 690 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 691 | PrevDecl = 0; |
| 692 | } |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 695 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 696 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 697 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 698 | Invalid = true; |
| 699 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 700 | |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 701 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 702 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 703 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 704 | D.getLocStart(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 705 | D.getIdentifierLoc(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 706 | Depth, Position, ParamName, T, |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 707 | IsParameterPack, TInfo); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 708 | Param->setAccess(AS_public); |
| 709 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 710 | if (Invalid) |
| 711 | Param->setInvalidDecl(); |
| 712 | |
| 713 | if (D.getIdentifier()) { |
| 714 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 715 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 716 | IdResolver.AddDecl(Param); |
| 717 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 718 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 719 | // C++0x [temp.param]p9: |
| 720 | // A default template-argument may be specified for any kind of |
| 721 | // template-parameter that is not a template parameter pack. |
| 722 | if (Default && IsParameterPack) { |
| 723 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 724 | Default = 0; |
| 725 | } |
| 726 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 727 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 728 | if (Default) { |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 729 | // Check for unexpanded parameter packs. |
| 730 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 731 | return Param; |
| 732 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 733 | TemplateArgument Converted; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 734 | ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
| 735 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 736 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 737 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 738 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 739 | Default = DefaultRes.take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 740 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 741 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 742 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 743 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 744 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 745 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 746 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 747 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 748 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 749 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 750 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 751 | SourceLocation TmpLoc, |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 752 | TemplateParameterList *Params, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 753 | SourceLocation EllipsisLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 754 | IdentifierInfo *Name, |
| 755 | SourceLocation NameLoc, |
| 756 | unsigned Depth, |
| 757 | unsigned Position, |
| 758 | SourceLocation EqualLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 759 | ParsedTemplateArgument Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 760 | assert(S->isTemplateParamScope() && |
| 761 | "Template template parameter not in template parameter scope!"); |
| 762 | |
| 763 | // Construct the parameter object. |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 764 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 765 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 766 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 767 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 768 | Depth, Position, IsParameterPack, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 769 | Name, Params); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 770 | Param->setAccess(AS_public); |
| 771 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 772 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 773 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 774 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 775 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 776 | IdResolver.AddDecl(Param); |
| 777 | } |
| 778 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 779 | if (Params->size() == 0) { |
| 780 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 781 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 782 | Param->setInvalidDecl(); |
| 783 | } |
| 784 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 785 | // C++0x [temp.param]p9: |
| 786 | // A default template-argument may be specified for any kind of |
| 787 | // template-parameter that is not a template parameter pack. |
| 788 | if (IsParameterPack && !Default.isInvalid()) { |
| 789 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 790 | Default = ParsedTemplateArgument(); |
| 791 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 792 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 793 | if (!Default.isInvalid()) { |
| 794 | // Check only that we have a template template argument. We don't want to |
| 795 | // try to check well-formedness now, because our template template parameter |
| 796 | // might have dependent types in its template parameters, which we wouldn't |
| 797 | // be able to match now. |
| 798 | // |
| 799 | // If none of the template template parameter's template arguments mention |
| 800 | // other template parameters, we could actually perform more checking here. |
| 801 | // However, it isn't worth doing. |
| 802 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 803 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 804 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 805 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 806 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 807 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 808 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 809 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 810 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 811 | DefaultArg.getArgument().getAsTemplate(), |
| 812 | UPPC_DefaultArgument)) |
| 813 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 814 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 815 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 816 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 817 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 818 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 821 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 822 | /// contains the template parameters in Params/NumParams. |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 823 | TemplateParameterList * |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 824 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 825 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 826 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 827 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 828 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 829 | SourceLocation RAngleLoc) { |
| 830 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 831 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 832 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 833 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 834 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 835 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 836 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 837 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 838 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 839 | if (SS.isSet()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 840 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 841 | } |
| 842 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 843 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 844 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 845 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 846 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 847 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 848 | TemplateParameterList *TemplateParams, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 849 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 850 | unsigned NumOuterTemplateParamLists, |
| 851 | TemplateParameterList** OuterTemplateParamLists) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 853 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 854 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 855 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 856 | |
| 857 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 858 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 859 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 860 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 861 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 862 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 863 | |
| 864 | // There is no such thing as an unnamed class template. |
| 865 | if (!Name) { |
| 866 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 867 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Richard Smith | 71c598f | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 870 | // Find any previous declaration with this name. For a friend with no |
| 871 | // scope explicitly specified, we only look for tag declarations (per |
| 872 | // C++11 [basic.lookup.elab]p2). |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 873 | DeclContext *SemanticContext; |
Richard Smith | 71c598f | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 874 | LookupResult Previous(*this, Name, NameLoc, |
| 875 | (SS.isEmpty() && TUK == TUK_Friend) |
| 876 | ? LookupTagName : LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 877 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 878 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 879 | SemanticContext = computeDeclContext(SS, true); |
| 880 | if (!SemanticContext) { |
Douglas Gregor | 8b0fa52 | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 881 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 882 | // in the AST, and historically we have just ignored such friend |
| 883 | // class templates, so don't complain here. |
| 884 | if (TUK != TUK_Friend) |
| 885 | Diag(NameLoc, diag::err_template_qualified_declarator_no_match) |
| 886 | << SS.getScopeRep() << SS.getRange(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 887 | return true; |
| 888 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 889 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 890 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 891 | return true; |
| 892 | |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 893 | // If we're adding a template to a dependent context, we may need to |
| 894 | // rebuilding some of the types used within the template parameter list, |
| 895 | // now that we know what the current instantiation is. |
| 896 | if (SemanticContext->isDependentContext()) { |
| 897 | ContextRAII SavedContext(*this, SemanticContext); |
| 898 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 899 | Invalid = true; |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 900 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
| 901 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc); |
Richard Smith | 71c598f | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 902 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 903 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 904 | } else { |
| 905 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 906 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 907 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 908 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 909 | if (Previous.isAmbiguous()) |
| 910 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 911 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 912 | NamedDecl *PrevDecl = 0; |
| 913 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 914 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 915 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 916 | // If there is a previous declaration with the same name, check |
| 917 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 918 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 919 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 920 | |
| 921 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 922 | // class template partial specialization, or class template specialization. |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 923 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 924 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 925 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 926 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 927 | PrevClassTemplate |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 928 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 929 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 930 | PrevClassTemplate |
| 931 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 932 | ->getSpecializedTemplate(); |
| 933 | } |
| 934 | } |
| 935 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 936 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 937 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 938 | // [...] When looking for a prior declaration of a class or a function |
| 939 | // declared as a friend, and when the name of the friend class or |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 940 | // function is neither a qualified name nor a template-id, scopes outside |
| 941 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 942 | if (!SS.isSet()) { |
| 943 | DeclContext *OutermostContext = CurContext; |
| 944 | while (!OutermostContext->isFileContext()) |
| 945 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 946 | |
Richard Smith | c93e014 | 2012-04-20 07:12:26 +0000 | [diff] [blame] | 947 | if (PrevDecl && |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 948 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 949 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 950 | SemanticContext = PrevDecl->getDeclContext(); |
| 951 | } else { |
| 952 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 953 | // context we computed is the semantic context for our new |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 954 | // declaration. |
| 955 | PrevDecl = PrevClassTemplate = 0; |
| 956 | SemanticContext = OutermostContext; |
Richard Smith | 71c598f | 2012-04-21 01:27:54 +0000 | [diff] [blame] | 957 | |
| 958 | // Check that the chosen semantic context doesn't already contain a |
| 959 | // declaration of this name as a non-tag type. |
| 960 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
| 961 | ForRedeclaration); |
| 962 | DeclContext *LookupContext = SemanticContext; |
| 963 | while (LookupContext->isTransparentContext()) |
| 964 | LookupContext = LookupContext->getLookupParent(); |
| 965 | LookupQualifiedName(Previous, LookupContext); |
| 966 | |
| 967 | if (Previous.isAmbiguous()) |
| 968 | return true; |
| 969 | |
| 970 | if (Previous.begin() != Previous.end()) |
| 971 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 972 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 973 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 974 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 975 | PrevDecl = PrevClassTemplate = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 976 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 977 | if (PrevClassTemplate) { |
Richard Smith | 6e21b16 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 978 | // Ensure that the template parameter lists are compatible. Skip this check |
| 979 | // for a friend in a dependent context: the template parameter list itself |
| 980 | // could be dependent. |
| 981 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 982 | !TemplateParameterListsAreEqual(TemplateParams, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 983 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 984 | /*Complain=*/true, |
| 985 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 986 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 987 | |
| 988 | // C++ [temp.class]p4: |
| 989 | // In a redeclaration, partial specialization, explicit |
| 990 | // specialization or explicit instantiation of a class template, |
| 991 | // the class-key shall agree in kind with the original class |
| 992 | // template declaration (7.1.5.3). |
| 993 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 994 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
| 995 | TUK == TUK_Definition, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 996 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 997 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 998 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 999 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 1000 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1003 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1004 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1005 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1006 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1007 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1008 | // FIXME: Would it make sense to try to "forget" the previous |
| 1009 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1010 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1011 | } |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1012 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1013 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 1014 | // Maybe we will complain about the shadowed template parameter. |
| 1015 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 1016 | // Just pretend that we didn't see the previous declaration. |
| 1017 | PrevDecl = 0; |
| 1018 | } else if (PrevDecl) { |
| 1019 | // C++ [temp]p5: |
| 1020 | // A class template shall not have the same name as any other |
| 1021 | // template, class, function, object, enumeration, enumerator, |
| 1022 | // namespace, or type in the same scope (3.3), except as specified |
| 1023 | // in (14.5.4). |
| 1024 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1025 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1026 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1029 | // Check the template parameter list of this declaration, possibly |
| 1030 | // merging in the template parameter list from the previous class |
Richard Smith | 6e21b16 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1031 | // template declaration. Skip this check for a friend in a dependent |
| 1032 | // context, because the template parameter list might be dependent. |
| 1033 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 1034 | CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1035 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1036 | (SS.isSet() && SemanticContext && |
Douglas Gregor | 461bf2e | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 1037 | SemanticContext->isRecord() && |
| 1038 | SemanticContext->isDependentContext()) |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1039 | ? TPC_ClassTemplateMember |
| 1040 | : TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1041 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1042 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1043 | if (SS.isSet()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1044 | // If the name of the template was qualified, we must be defining the |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1045 | // template out-of-line. |
Richard Smith | 6e21b16 | 2012-04-22 02:13:50 +0000 | [diff] [blame] | 1046 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1047 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
| 1048 | : diag::err_member_def_does_not_match) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1049 | << Name << SemanticContext << SS.getRange(); |
Douglas Gregor | ea9f54a | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1050 | Invalid = true; |
| 1051 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1054 | CXXRecordDecl *NewClass = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1055 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1056 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1057 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 1058 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1059 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1060 | if (NumOuterTemplateParamLists > 0) |
| 1061 | NewClass->setTemplateParameterListsInfo(Context, |
| 1062 | NumOuterTemplateParamLists, |
| 1063 | OuterTemplateParamLists); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1064 | |
Eli Friedman | 572ae0a | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1065 | // Add alignment attributes if necessary; these attributes are checked when |
| 1066 | // the ASTContext lays out the structure. |
| 1067 | AddAlignmentAttributesForRecord(NewClass); |
| 1068 | AddMsStructLayoutForRecord(NewClass); |
| 1069 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1070 | ClassTemplateDecl *NewTemplate |
| 1071 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1072 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1073 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1074 | NewClass->setDescribedClassTemplate(NewTemplate); |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1075 | |
Douglas Gregor | 2ccd89c | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1076 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1077 | NewTemplate->setModulePrivate(); |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1078 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1079 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1080 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1081 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1082 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1083 | (void)T; |
| 1084 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1085 | // If we are providing an explicit specialization of a member that is a |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1086 | // class template, make a note of that. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1087 | if (PrevClassTemplate && |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1088 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1089 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1090 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1091 | // Set the access specifier. |
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1092 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1093 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1095 | // Set the lexical context of these templates |
| 1096 | NewClass->setLexicalDeclContext(CurContext); |
| 1097 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1098 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1099 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1100 | NewClass->startDefinition(); |
| 1101 | |
| 1102 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1103 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1104 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1105 | if (TUK != TUK_Friend) |
| 1106 | PushOnScopeChains(NewTemplate, S); |
| 1107 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1108 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1109 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1110 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1111 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1112 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1113 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1114 | PrevClassTemplate != NULL); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1115 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1116 | // Friend templates are visible in fairly strange ways. |
| 1117 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1118 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1119 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1120 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1121 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1122 | /* AddToContext = */ false); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1123 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1124 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1125 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1126 | NewClass->getLocation(), |
| 1127 | NewTemplate, |
| 1128 | /*FIXME:*/NewClass->getLocation()); |
| 1129 | Friend->setAccess(AS_public); |
| 1130 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1131 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1132 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1133 | if (Invalid) { |
| 1134 | NewTemplate->setInvalidDecl(); |
| 1135 | NewClass->setInvalidDecl(); |
| 1136 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1137 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1140 | /// \brief Diagnose the presence of a default template argument on a |
| 1141 | /// template parameter, which is ill-formed in certain contexts. |
| 1142 | /// |
| 1143 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1144 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1145 | Sema::TemplateParamListContext TPC, |
| 1146 | SourceLocation ParamLoc, |
| 1147 | SourceRange DefArgRange) { |
| 1148 | switch (TPC) { |
| 1149 | case Sema::TPC_ClassTemplate: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1150 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1151 | return false; |
| 1152 | |
| 1153 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1154 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1155 | // C++ [temp.param]p9: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1156 | // A default template-argument shall not be specified in a |
| 1157 | // function template declaration or a function template |
| 1158 | // definition [...] |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1159 | // If a friend function template declaration specifies a default |
| 1160 | // template-argument, that declaration shall be a definition and shall be |
| 1161 | // the only declaration of the function template in the translation unit. |
| 1162 | // (C++98/03 doesn't have this wording; see DR226). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1163 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1164 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1165 | : diag::ext_template_parameter_default_in_function_template) |
| 1166 | << DefArgRange; |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1167 | return false; |
| 1168 | |
| 1169 | case Sema::TPC_ClassTemplateMember: |
| 1170 | // C++0x [temp.param]p9: |
| 1171 | // A default template-argument shall not be specified in the |
| 1172 | // template-parameter-lists of the definition of a member of a |
| 1173 | // class template that appears outside of the member's class. |
| 1174 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1175 | << DefArgRange; |
| 1176 | return true; |
| 1177 | |
| 1178 | case Sema::TPC_FriendFunctionTemplate: |
| 1179 | // C++ [temp.param]p9: |
| 1180 | // A default template-argument shall not be specified in a |
| 1181 | // friend template declaration. |
| 1182 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1183 | << DefArgRange; |
| 1184 | return true; |
| 1185 | |
| 1186 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1187 | // for friend function templates if there is only a single |
| 1188 | // declaration (and it is a definition). Strange! |
| 1189 | } |
| 1190 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1191 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1194 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1195 | /// of a template template parameter, recursively. |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1196 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1197 | TemplateTemplateParmDecl *TTP) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1198 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1199 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1200 | NamedDecl *P = Params->getParam(I); |
| 1201 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1202 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1203 | NTTP->getTypeSourceInfo(), |
| 1204 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1205 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1206 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1207 | continue; |
| 1208 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1209 | |
| 1210 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1211 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1212 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1213 | return true; |
| 1214 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1215 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1216 | return false; |
| 1217 | } |
| 1218 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1219 | /// \brief Checks the validity of a template parameter list, possibly |
| 1220 | /// considering the template parameter list from a previous |
| 1221 | /// declaration. |
| 1222 | /// |
| 1223 | /// If an "old" template parameter list is provided, it must be |
| 1224 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1225 | /// template parameter list. |
| 1226 | /// |
| 1227 | /// \param NewParams Template parameter list for a new template |
| 1228 | /// declaration. This template parameter list will be updated with any |
| 1229 | /// default arguments that are carried through from the previous |
| 1230 | /// template parameter list. |
| 1231 | /// |
| 1232 | /// \param OldParams If provided, template parameter list from a |
| 1233 | /// previous declaration of the same template. Default template |
| 1234 | /// arguments will be merged from the old template parameter list to |
| 1235 | /// the new template parameter list. |
| 1236 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1237 | /// \param TPC Describes the context in which we are checking the given |
| 1238 | /// template parameter list. |
| 1239 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1240 | /// \returns true if an error occurred, false otherwise. |
| 1241 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1242 | TemplateParameterList *OldParams, |
| 1243 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1244 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1245 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1246 | // C++ [temp.param]p10: |
| 1247 | // The set of default template-arguments available for use with a |
| 1248 | // template declaration or definition is obtained by merging the |
| 1249 | // default arguments from the definition (if in scope) and all |
| 1250 | // declarations in scope in the same way default function |
| 1251 | // arguments are (8.3.6). |
| 1252 | bool SawDefaultArgument = false; |
| 1253 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1254 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1255 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1256 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1257 | if (OldParams) |
| 1258 | OldParam = OldParams->begin(); |
| 1259 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1260 | bool RemoveDefaultArguments = false; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1261 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1262 | NewParamEnd = NewParams->end(); |
| 1263 | NewParam != NewParamEnd; ++NewParam) { |
| 1264 | // Variables used to diagnose redundant default arguments |
| 1265 | bool RedundantDefaultArg = false; |
| 1266 | SourceLocation OldDefaultLoc; |
| 1267 | SourceLocation NewDefaultLoc; |
| 1268 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1269 | // Variable used to diagnose missing default arguments |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1270 | bool MissingDefaultArg = false; |
| 1271 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1272 | // Variable used to diagnose non-final parameter packs |
| 1273 | bool SawParameterPack = false; |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1274 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1275 | if (TemplateTypeParmDecl *NewTypeParm |
| 1276 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1277 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1278 | if (NewTypeParm->hasDefaultArgument() && |
| 1279 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1280 | NewTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1281 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1282 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1283 | NewTypeParm->removeDefaultArgument(); |
| 1284 | |
| 1285 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1286 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1287 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1289 | if (NewTypeParm->isParameterPack()) { |
| 1290 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1291 | "Parameter packs can't have a default argument!"); |
| 1292 | SawParameterPack = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1293 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1294 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1295 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1296 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1297 | SawDefaultArgument = true; |
| 1298 | RedundantDefaultArg = true; |
| 1299 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1300 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1301 | // Merge the default argument from the old declaration to the |
| 1302 | // new declaration. |
| 1303 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1304 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1305 | true); |
| 1306 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1307 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1308 | SawDefaultArgument = true; |
| 1309 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1310 | } else if (SawDefaultArgument) |
| 1311 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1312 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1313 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1314 | // Check for unexpanded parameter packs. |
| 1315 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1316 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1317 | UPPC_NonTypeTemplateParameterType)) { |
| 1318 | Invalid = true; |
| 1319 | continue; |
| 1320 | } |
| 1321 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1322 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1323 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1324 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1325 | NewNonTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1326 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1327 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1330 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1331 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1332 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1333 | if (NewNonTypeParm->isParameterPack()) { |
| 1334 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1335 | "Parameter packs can't have a default argument!"); |
| 1336 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1337 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1338 | NewNonTypeParm->hasDefaultArgument()) { |
| 1339 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1340 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1341 | SawDefaultArgument = true; |
| 1342 | RedundantDefaultArg = true; |
| 1343 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1344 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1345 | // Merge the default argument from the old declaration to the |
| 1346 | // new declaration. |
| 1347 | SawDefaultArgument = true; |
| 1348 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1349 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1350 | // parameter. |
| 1351 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1352 | OldNonTypeParm->getDefaultArgument(), |
| 1353 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1354 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1355 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1356 | SawDefaultArgument = true; |
| 1357 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1358 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1360 | } else { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1361 | TemplateTemplateParmDecl *NewTemplateParm |
| 1362 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1363 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1364 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1365 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1366 | Invalid = true; |
| 1367 | continue; |
| 1368 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1369 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1370 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1371 | if (NewTemplateParm->hasDefaultArgument() && |
| 1372 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1373 | NewTemplateParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1374 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1375 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1376 | |
| 1377 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1378 | TemplateTemplateParmDecl *OldTemplateParm |
| 1379 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1380 | if (NewTemplateParm->isParameterPack()) { |
| 1381 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1382 | "Parameter packs can't have a default argument!"); |
| 1383 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1384 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1385 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1386 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1387 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1388 | SawDefaultArgument = true; |
| 1389 | RedundantDefaultArg = true; |
| 1390 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1391 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1392 | // Merge the default argument from the old declaration to the |
| 1393 | // new declaration. |
| 1394 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1395 | // FIXME: We need to create a new kind of "default argument" expression |
| 1396 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1397 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1398 | OldTemplateParm->getDefaultArgument(), |
| 1399 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1400 | PreviousDefaultArgLoc |
| 1401 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1402 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1403 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1404 | PreviousDefaultArgLoc |
| 1405 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1406 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1407 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1410 | // C++0x [temp.param]p11: |
| 1411 | // If a template parameter of a primary class template or alias template |
| 1412 | // is a template parameter pack, it shall be the last template parameter. |
| 1413 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
| 1414 | (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) { |
| 1415 | Diag((*NewParam)->getLocation(), |
| 1416 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1417 | Invalid = true; |
| 1418 | } |
| 1419 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1420 | if (RedundantDefaultArg) { |
| 1421 | // C++ [temp.param]p12: |
| 1422 | // A template-parameter shall not be given default arguments |
| 1423 | // by two different declarations in the same scope. |
| 1424 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1425 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1426 | Invalid = true; |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1427 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1428 | // C++ [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1429 | // If a template-parameter of a class template has a default |
| 1430 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1431 | // have a default template-argument supplied or be a template parameter |
| 1432 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1434 | diag::err_template_param_default_arg_missing); |
| 1435 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1436 | Invalid = true; |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1437 | RemoveDefaultArguments = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | // If we have an old template parameter list that we're merging |
| 1441 | // in, move on to the next parameter. |
| 1442 | if (OldParams) |
| 1443 | ++OldParam; |
| 1444 | } |
| 1445 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1446 | // We were missing some default arguments at the end of the list, so remove |
| 1447 | // all of the default arguments. |
| 1448 | if (RemoveDefaultArguments) { |
| 1449 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1450 | NewParamEnd = NewParams->end(); |
| 1451 | NewParam != NewParamEnd; ++NewParam) { |
| 1452 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1453 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1454 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1455 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1456 | NTTP->removeDefaultArgument(); |
| 1457 | else |
| 1458 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1459 | } |
| 1460 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1461 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1462 | return Invalid; |
| 1463 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1464 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1465 | namespace { |
| 1466 | |
| 1467 | /// A class which looks for a use of a certain level of template |
| 1468 | /// parameter. |
| 1469 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1470 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1471 | |
| 1472 | unsigned Depth; |
| 1473 | bool Match; |
| 1474 | |
| 1475 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1476 | NamedDecl *ND = Params->getParam(0); |
| 1477 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1478 | Depth = PD->getDepth(); |
| 1479 | } else if (NonTypeTemplateParmDecl *PD = |
| 1480 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1481 | Depth = PD->getDepth(); |
| 1482 | } else { |
| 1483 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | bool Matches(unsigned ParmDepth) { |
| 1488 | if (ParmDepth >= Depth) { |
| 1489 | Match = true; |
| 1490 | return true; |
| 1491 | } |
| 1492 | return false; |
| 1493 | } |
| 1494 | |
| 1495 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1496 | return !Matches(T->getDepth()); |
| 1497 | } |
| 1498 | |
| 1499 | bool TraverseTemplateName(TemplateName N) { |
| 1500 | if (TemplateTemplateParmDecl *PD = |
| 1501 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1502 | if (Matches(PD->getDepth())) return false; |
| 1503 | return super::TraverseTemplateName(N); |
| 1504 | } |
| 1505 | |
| 1506 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1507 | if (NonTypeTemplateParmDecl *PD = |
| 1508 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1509 | if (PD->getDepth() == Depth) { |
| 1510 | Match = true; |
| 1511 | return false; |
| 1512 | } |
| 1513 | } |
| 1514 | return super::VisitDeclRefExpr(E); |
| 1515 | } |
Douglas Gregor | 18c8339 | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1516 | |
| 1517 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1518 | return TraverseType(T->getInjectedSpecializationType()); |
| 1519 | } |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1520 | }; |
| 1521 | } |
| 1522 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1523 | /// Determines whether a given type depends on the given parameter |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1524 | /// list. |
| 1525 | static bool |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1526 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1527 | DependencyChecker Checker(Params); |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1528 | Checker.TraverseType(T); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1529 | return Checker.Match; |
| 1530 | } |
| 1531 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1532 | // Find the source range corresponding to the named type in the given |
| 1533 | // nested-name-specifier, if any. |
| 1534 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1535 | QualType T, |
| 1536 | const CXXScopeSpec &SS) { |
| 1537 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1538 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1539 | if (const Type *CurType = NNS->getAsType()) { |
| 1540 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1541 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1542 | } else |
| 1543 | break; |
| 1544 | |
| 1545 | NNSLoc = NNSLoc.getPrefix(); |
| 1546 | } |
| 1547 | |
| 1548 | return SourceRange(); |
| 1549 | } |
| 1550 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1551 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1552 | /// specifier, returning the template parameter list that applies to the |
| 1553 | /// name. |
| 1554 | /// |
| 1555 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1556 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1557 | /// |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1558 | /// \param DeclLoc The location of the declaration itself. |
| 1559 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1560 | /// \param SS the scope specifier that will be matched to the given template |
| 1561 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1562 | /// being declared. |
| 1563 | /// |
| 1564 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1565 | /// innermost template parameter lists. |
| 1566 | /// |
| 1567 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1568 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1569 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1570 | /// matching template parameters to scope specifiers in friend |
| 1571 | /// declarations. |
| 1572 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1573 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1574 | /// declared is an explicit specialization, false otherwise. |
| 1575 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1577 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1578 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1580 | /// template specialization), or may be NULL (if what we're declaring isn't |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1581 | /// itself a template). |
| 1582 | TemplateParameterList * |
| 1583 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1584 | SourceLocation DeclLoc, |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1585 | const CXXScopeSpec &SS, |
| 1586 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1587 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1588 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1589 | bool &IsExplicitSpecialization, |
| 1590 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1591 | IsExplicitSpecialization = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1592 | Invalid = false; |
| 1593 | |
| 1594 | // The sequence of nested types to which we will match up the template |
| 1595 | // parameter lists. We first build this list by starting with the type named |
| 1596 | // by the nested-name-specifier and walking out until we run out of types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1597 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1598 | QualType T; |
Douglas Gregor | 714c992 | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1599 | if (SS.getScopeRep()) { |
| 1600 | if (CXXRecordDecl *Record |
| 1601 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1602 | T = Context.getTypeDeclType(Record); |
| 1603 | else |
| 1604 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1605 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1606 | |
| 1607 | // If we found an explicit specialization that prevents us from needing |
| 1608 | // 'template<>' headers, this will be set to the location of that |
| 1609 | // explicit specialization. |
| 1610 | SourceLocation ExplicitSpecLoc; |
| 1611 | |
| 1612 | while (!T.isNull()) { |
| 1613 | NestedTypes.push_back(T); |
| 1614 | |
| 1615 | // Retrieve the parent of a record type. |
| 1616 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1617 | // If this type is an explicit specialization, we're done. |
| 1618 | if (ClassTemplateSpecializationDecl *Spec |
| 1619 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1620 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1621 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1622 | ExplicitSpecLoc = Spec->getLocation(); |
| 1623 | break; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1624 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1625 | } else if (Record->getTemplateSpecializationKind() |
| 1626 | == TSK_ExplicitSpecialization) { |
| 1627 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1628 | break; |
| 1629 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1630 | |
| 1631 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1632 | T = Context.getTypeDeclType(Parent); |
| 1633 | else |
| 1634 | T = QualType(); |
| 1635 | continue; |
| 1636 | } |
| 1637 | |
| 1638 | if (const TemplateSpecializationType *TST |
| 1639 | = T->getAs<TemplateSpecializationType>()) { |
| 1640 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1641 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1642 | T = Context.getTypeDeclType(Parent); |
| 1643 | else |
| 1644 | T = QualType(); |
| 1645 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1646 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | // Look one step prior in a dependent template specialization type. |
| 1650 | if (const DependentTemplateSpecializationType *DependentTST |
| 1651 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1652 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1653 | T = QualType(NNS->getAsType(), 0); |
| 1654 | else |
| 1655 | T = QualType(); |
| 1656 | continue; |
| 1657 | } |
| 1658 | |
| 1659 | // Look one step prior in a dependent name type. |
| 1660 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1661 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1662 | T = QualType(NNS->getAsType(), 0); |
| 1663 | else |
| 1664 | T = QualType(); |
| 1665 | continue; |
| 1666 | } |
| 1667 | |
| 1668 | // Retrieve the parent of an enumeration type. |
| 1669 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1670 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1671 | // check here. |
| 1672 | EnumDecl *Enum = EnumT->getDecl(); |
| 1673 | |
| 1674 | // Get to the parent type. |
| 1675 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1676 | T = Context.getTypeDeclType(Parent); |
| 1677 | else |
| 1678 | T = QualType(); |
| 1679 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1680 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1681 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1682 | T = QualType(); |
| 1683 | } |
| 1684 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1685 | // to the innermost while checking template-parameter-lists. |
| 1686 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1687 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1688 | // C++0x [temp.expl.spec]p17: |
| 1689 | // A member or a member template may be nested within many |
| 1690 | // enclosing class templates. In an explicit specialization for |
| 1691 | // such a member, the member declaration shall be preceded by a |
| 1692 | // template<> for each enclosing class template that is |
| 1693 | // explicitly specialized. |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1694 | bool SawNonEmptyTemplateParameterList = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1695 | unsigned ParamIdx = 0; |
| 1696 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1697 | ++TypeIdx) { |
| 1698 | T = NestedTypes[TypeIdx]; |
| 1699 | |
| 1700 | // Whether we expect a 'template<>' header. |
| 1701 | bool NeedEmptyTemplateHeader = false; |
| 1702 | |
| 1703 | // Whether we expect a template header with parameters. |
| 1704 | bool NeedNonemptyTemplateHeader = false; |
| 1705 | |
| 1706 | // For a dependent type, the set of template parameters that we |
| 1707 | // expect to see. |
| 1708 | TemplateParameterList *ExpectedTemplateParams = 0; |
| 1709 | |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1710 | // C++0x [temp.expl.spec]p15: |
| 1711 | // A member or a member template may be nested within many enclosing |
| 1712 | // class templates. In an explicit specialization for such a member, the |
| 1713 | // member declaration shall be preceded by a template<> for each |
| 1714 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1715 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1716 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1717 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1718 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1719 | NeedNonemptyTemplateHeader = true; |
| 1720 | } else if (Record->isDependentType()) { |
| 1721 | if (Record->getDescribedClassTemplate()) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1722 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1723 | ->getTemplateParameters(); |
| 1724 | NeedNonemptyTemplateHeader = true; |
| 1725 | } |
| 1726 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1727 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1728 | // C++0x [temp.expl.spec]p4: |
| 1729 | // Members of an explicitly specialized class template are defined |
| 1730 | // in the same manner as members of normal classes, and not using |
| 1731 | // the template<> syntax. |
| 1732 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1733 | NeedEmptyTemplateHeader = true; |
| 1734 | else |
Douglas Gregor | 95ea450 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1735 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1736 | } else if (Record->getTemplateSpecializationKind()) { |
| 1737 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1738 | != TSK_ExplicitSpecialization && |
| 1739 | TypeIdx == NumTypes - 1) |
| 1740 | IsExplicitSpecialization = true; |
| 1741 | |
| 1742 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1743 | } |
| 1744 | } else if (const TemplateSpecializationType *TST |
| 1745 | = T->getAs<TemplateSpecializationType>()) { |
| 1746 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1747 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1748 | NeedNonemptyTemplateHeader = true; |
| 1749 | } |
| 1750 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1751 | // FIXME: We actually could/should check the template arguments here |
| 1752 | // against the corresponding template parameter list. |
| 1753 | NeedNonemptyTemplateHeader = false; |
| 1754 | } |
| 1755 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1756 | // C++ [temp.expl.spec]p16: |
| 1757 | // In an explicit specialization declaration for a member of a class |
| 1758 | // template or a member template that ap- pears in namespace scope, the |
| 1759 | // member template and some of its enclosing class templates may remain |
| 1760 | // unspecialized, except that the declaration shall not explicitly |
| 1761 | // specialize a class member template if its en- closing class templates |
| 1762 | // are not explicitly specialized as well. |
| 1763 | if (ParamIdx < NumParamLists) { |
| 1764 | if (ParamLists[ParamIdx]->size() == 0) { |
| 1765 | if (SawNonEmptyTemplateParameterList) { |
| 1766 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1767 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1768 | Invalid = true; |
| 1769 | IsExplicitSpecialization = false; |
| 1770 | return 0; |
| 1771 | } |
| 1772 | } else |
| 1773 | SawNonEmptyTemplateParameterList = true; |
| 1774 | } |
| 1775 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1776 | if (NeedEmptyTemplateHeader) { |
| 1777 | // If we're on the last of the types, and we need a 'template<>' header |
| 1778 | // here, then it's an explicit specialization. |
| 1779 | if (TypeIdx == NumTypes - 1) |
| 1780 | IsExplicitSpecialization = true; |
| 1781 | |
| 1782 | if (ParamIdx < NumParamLists) { |
| 1783 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1784 | // The header has template parameters when it shouldn't. Complain. |
| 1785 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1786 | diag::err_template_param_list_matches_nontemplate) |
| 1787 | << T |
| 1788 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1789 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1790 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1791 | Invalid = true; |
| 1792 | return 0; |
| 1793 | } |
| 1794 | |
| 1795 | // Consume this template header. |
| 1796 | ++ParamIdx; |
| 1797 | continue; |
| 1798 | } |
| 1799 | |
| 1800 | if (!IsFriend) { |
| 1801 | // We don't have a template header, but we should. |
| 1802 | SourceLocation ExpectedTemplateLoc; |
| 1803 | if (NumParamLists > 0) |
| 1804 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1805 | else |
| 1806 | ExpectedTemplateLoc = DeclStartLoc; |
| 1807 | |
| 1808 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1809 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS) |
| 1810 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1811 | } |
| 1812 | |
| 1813 | continue; |
| 1814 | } |
| 1815 | |
| 1816 | if (NeedNonemptyTemplateHeader) { |
| 1817 | // In friend declarations we can have template-ids which don't |
| 1818 | // depend on the corresponding template parameter lists. But |
| 1819 | // assume that empty parameter lists are supposed to match this |
| 1820 | // template-id. |
| 1821 | if (IsFriend && T->isDependentType()) { |
| 1822 | if (ParamIdx < NumParamLists && |
| 1823 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
| 1824 | ExpectedTemplateParams = 0; |
| 1825 | else |
| 1826 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1827 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1828 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1829 | if (ParamIdx < NumParamLists) { |
| 1830 | // Check the template parameter list, if we can. |
| 1831 | if (ExpectedTemplateParams && |
| 1832 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1833 | ExpectedTemplateParams, |
| 1834 | true, TPL_TemplateMatch)) |
| 1835 | Invalid = true; |
| 1836 | |
| 1837 | if (!Invalid && |
| 1838 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1839 | TPC_ClassTemplateMember)) |
| 1840 | Invalid = true; |
| 1841 | |
| 1842 | ++ParamIdx; |
| 1843 | continue; |
| 1844 | } |
| 1845 | |
| 1846 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1847 | << T |
| 1848 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1849 | Invalid = true; |
| 1850 | continue; |
| 1851 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1852 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1853 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1854 | // If there were at least as many template-ids as there were template |
| 1855 | // parameter lists, then there are no template parameter lists remaining for |
| 1856 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1857 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1858 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1860 | // If there were too many template parameter lists, complain about that now. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1861 | if (ParamIdx < NumParamLists - 1) { |
| 1862 | bool HasAnyExplicitSpecHeader = false; |
| 1863 | bool AllExplicitSpecHeaders = true; |
| 1864 | for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) { |
| 1865 | if (ParamLists[I]->size() == 0) |
| 1866 | HasAnyExplicitSpecHeader = true; |
| 1867 | else |
| 1868 | AllExplicitSpecHeaders = false; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1869 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1870 | |
| 1871 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1872 | AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers |
| 1873 | : diag::err_template_spec_extra_headers) |
| 1874 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1875 | ParamLists[NumParamLists - 2]->getRAngleLoc()); |
| 1876 | |
| 1877 | // If there was a specialization somewhere, such that 'template<>' is |
| 1878 | // not required, and there were any 'template<>' headers, note where the |
| 1879 | // specialization occurred. |
| 1880 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1881 | Diag(ExplicitSpecLoc, |
| 1882 | diag::note_explicit_template_spec_does_not_need_header) |
| 1883 | << NestedTypes.back(); |
| 1884 | |
| 1885 | // We have a template parameter list with no corresponding scope, which |
| 1886 | // means that the resulting template declaration can't be instantiated |
| 1887 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1888 | if (!AllExplicitSpecHeaders) |
| 1889 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1890 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1891 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1892 | // C++ [temp.expl.spec]p16: |
| 1893 | // In an explicit specialization declaration for a member of a class |
| 1894 | // template or a member template that ap- pears in namespace scope, the |
| 1895 | // member template and some of its enclosing class templates may remain |
| 1896 | // unspecialized, except that the declaration shall not explicitly |
| 1897 | // specialize a class member template if its en- closing class templates |
| 1898 | // are not explicitly specialized as well. |
| 1899 | if (ParamLists[NumParamLists - 1]->size() == 0 && |
| 1900 | SawNonEmptyTemplateParameterList) { |
| 1901 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1902 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1903 | Invalid = true; |
| 1904 | IsExplicitSpecialization = false; |
| 1905 | return 0; |
| 1906 | } |
| 1907 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1908 | // Return the last template parameter list, which corresponds to the |
| 1909 | // entity being declared. |
| 1910 | return ParamLists[NumParamLists - 1]; |
| 1911 | } |
| 1912 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1913 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1914 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1915 | Diag(Template->getLocation(), diag::note_template_declared_here) |
| 1916 | << (isa<FunctionTemplateDecl>(Template)? 0 |
| 1917 | : isa<ClassTemplateDecl>(Template)? 1 |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1918 | : isa<TypeAliasTemplateDecl>(Template)? 2 |
| 1919 | : 3) |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1920 | << Template->getDeclName(); |
| 1921 | return; |
| 1922 | } |
| 1923 | |
| 1924 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1925 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1926 | IEnd = OST->end(); |
| 1927 | I != IEnd; ++I) |
| 1928 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1929 | << 0 << (*I)->getDeclName(); |
| 1930 | |
| 1931 | return; |
| 1932 | } |
| 1933 | } |
| 1934 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1935 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1936 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1937 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1938 | DependentTemplateName *DTN |
| 1939 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1940 | if (DTN && DTN->isIdentifier()) |
| 1941 | // When building a template-id where the template-name is dependent, |
| 1942 | // assume the template is a type template. Either our assumption is |
| 1943 | // correct, or the code is ill-formed and will be diagnosed when the |
| 1944 | // dependent name is substituted. |
| 1945 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 1946 | DTN->getQualifier(), |
| 1947 | DTN->getIdentifier(), |
| 1948 | TemplateArgs); |
| 1949 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1950 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1951 | if (!Template || isa<FunctionTemplateDecl>(Template)) { |
| 1952 | // We might have a substituted template template parameter pack. If so, |
| 1953 | // build a template specialization type for it. |
| 1954 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 1955 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1956 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1957 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 1958 | << Name; |
| 1959 | NoteAllFoundTemplates(Name); |
| 1960 | return QualType(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1961 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1962 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1963 | // Check that the template argument list is well-formed for this |
| 1964 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1965 | SmallVector<TemplateArgument, 4> Converted; |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1966 | bool ExpansionIntoFixedList = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1967 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1968 | false, Converted, &ExpansionIntoFixedList)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1969 | return QualType(); |
| 1970 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1971 | QualType CanonType; |
| 1972 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1973 | bool InstantiationDependent = false; |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1974 | TypeAliasTemplateDecl *AliasTemplate = 0; |
| 1975 | if (!ExpansionIntoFixedList && |
| 1976 | (AliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Template))) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1977 | // Find the canonical type for this type alias template specialization. |
| 1978 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 1979 | if (Pattern->isInvalidDecl()) |
| 1980 | return QualType(); |
| 1981 | |
| 1982 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1983 | Converted.data(), Converted.size()); |
| 1984 | |
| 1985 | // Only substitute for the innermost template argument list. |
| 1986 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 1804174 | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 1987 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | aff37b4 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 1988 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 1989 | for (unsigned I = 0; I < Depth; ++I) |
| 1990 | TemplateArgLists.addOuterTemplateArguments(0, 0); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1991 | |
| 1992 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
| 1993 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 1994 | TemplateArgLists, AliasTemplate->getLocation(), |
| 1995 | AliasTemplate->getDeclName()); |
| 1996 | if (CanonType.isNull()) |
| 1997 | return QualType(); |
| 1998 | } else if (Name.isDependent() || |
| 1999 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2000 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2001 | // This class template specialization is a dependent |
| 2002 | // type. Therefore, its canonical type is another class template |
| 2003 | // specialization type that contains all of the converted |
| 2004 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 2005 | // A<T, T> have identical types when A is declared as: |
| 2006 | // |
| 2007 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2008 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2010 | Converted.data(), |
| 2011 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2012 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2013 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2014 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2015 | // In the future, we need to teach getTemplateSpecializationType to only |
| 2016 | // build the canonical type and return that to us. |
| 2017 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2018 | |
| 2019 | // This might work out to be a current instantiation, in which |
| 2020 | // case the canonical type needs to be the InjectedClassNameType. |
| 2021 | // |
| 2022 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2023 | // changes to CurContext don't change the set of current |
| 2024 | // instantiations. |
| 2025 | if (isa<ClassTemplateDecl>(Template)) { |
| 2026 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2027 | // If we get out to a namespace, we're done. |
| 2028 | if (Ctx->isFileContext()) break; |
| 2029 | |
| 2030 | // If this isn't a record, keep looking. |
| 2031 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2032 | if (!Record) continue; |
| 2033 | |
| 2034 | // Look for one of the two cases with InjectedClassNameTypes |
| 2035 | // and check whether it's the same template. |
| 2036 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2037 | !Record->getDescribedClassTemplate()) |
| 2038 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2039 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2040 | // Fetch the injected class name type and check whether its |
| 2041 | // injected type is equal to the type we just built. |
| 2042 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2043 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2044 | ->getInjectedSpecializationType(); |
| 2045 | |
| 2046 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2047 | continue; |
| 2048 | |
| 2049 | // If so, the canonical type of this TST is the injected |
| 2050 | // class name type of the record we just found. |
| 2051 | assert(ICNT.isCanonical()); |
| 2052 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2053 | break; |
| 2054 | } |
| 2055 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2056 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2057 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2058 | // Find the class template specialization declaration that |
| 2059 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2060 | void *InsertPos = 0; |
| 2061 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2062 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2063 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2064 | if (!Decl) { |
| 2065 | // This is the first time we have referenced this class template |
| 2066 | // specialization. Create the canonical declaration and add it to |
| 2067 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2068 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2069 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2070 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 09d8212 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2071 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2072 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2073 | ClassTemplate, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2074 | Converted.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2075 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2076 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2077 | Decl->setLexicalDeclContext(CurContext); |
| 2078 | } |
| 2079 | |
| 2080 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2081 | assert(isa<RecordType>(CanonType) && |
| 2082 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2083 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2084 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2085 | // Build the fully-sugared type for this class template |
| 2086 | // specialization, which refers back to the class template |
| 2087 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2088 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2091 | TypeResult |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2092 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2093 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2095 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2096 | SourceLocation RAngleLoc, |
| 2097 | bool IsCtorOrDtorName) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2098 | if (SS.isInvalid()) |
| 2099 | return true; |
| 2100 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2101 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2102 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2103 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2104 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2105 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2106 | |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2107 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2108 | QualType T |
| 2109 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2110 | DTN->getQualifier(), |
| 2111 | DTN->getIdentifier(), |
| 2112 | TemplateArgs); |
| 2113 | // Build type-source information. |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2114 | TypeLocBuilder TLB; |
| 2115 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2116 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2117 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2118 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 66581d4 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2119 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2120 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2121 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2122 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2123 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2124 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2125 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2126 | } |
| 2127 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2128 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2129 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2130 | |
| 2131 | if (Result.isNull()) |
| 2132 | return true; |
| 2133 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2134 | // Build type-source information. |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2135 | TypeLocBuilder TLB; |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2136 | TemplateSpecializationTypeLoc SpecTL |
| 2137 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2138 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2139 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2140 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2141 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2142 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2143 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2144 | |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2145 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2146 | // constructor or destructor name (in such a case, the scope specifier |
| 2147 | // will be attached to the enclosing Decl or Expr node). |
| 2148 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2149 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2150 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2151 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2152 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2153 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2154 | } |
| 2155 | |
| 2156 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2157 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2158 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2159 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2160 | TypeSpecifierType TagSpec, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2161 | SourceLocation TagLoc, |
| 2162 | CXXScopeSpec &SS, |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2163 | SourceLocation TemplateKWLoc, |
| 2164 | TemplateTy TemplateD, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2165 | SourceLocation TemplateLoc, |
| 2166 | SourceLocation LAngleLoc, |
| 2167 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2168 | SourceLocation RAngleLoc) { |
| 2169 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
| 2170 | |
| 2171 | // Translate the parser's template argument list in our AST format. |
| 2172 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2173 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2174 | |
| 2175 | // Determine the tag kind |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2176 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2177 | ElaboratedTypeKeyword Keyword |
| 2178 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2179 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2180 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2181 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2182 | DTN->getQualifier(), |
| 2183 | DTN->getIdentifier(), |
| 2184 | TemplateArgs); |
| 2185 | |
| 2186 | // Build type-source information. |
| 2187 | TypeLocBuilder TLB; |
| 2188 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2189 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2190 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2191 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 66581d4 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2192 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2193 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2194 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2195 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2196 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2197 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2198 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2199 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2200 | |
| 2201 | if (TypeAliasTemplateDecl *TAT = |
| 2202 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2203 | // C++0x [dcl.type.elab]p2: |
| 2204 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2205 | // resolves to an alias template specialization, the |
| 2206 | // elaborated-type-specifier is ill-formed. |
| 2207 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2208 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2209 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2210 | |
| 2211 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2212 | if (Result.isNull()) |
Matt Beaumont-Gay | 3a51d41 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2213 | return TypeResult(true); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2214 | |
| 2215 | // Check the tag kind |
| 2216 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2217 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2218 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2219 | IdentifierInfo *Id = D->getIdentifier(); |
| 2220 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2221 | |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2222 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 2223 | TagLoc, *Id)) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2224 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2225 | << Result |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2226 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2227 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2228 | } |
| 2229 | } |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2230 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2231 | // Provide source-location information for the template specialization. |
| 2232 | TypeLocBuilder TLB; |
| 2233 | TemplateSpecializationTypeLoc SpecTL |
| 2234 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2235 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2236 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2237 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2238 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2239 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2240 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2241 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2242 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2243 | // and tag keyword. |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2244 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2245 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2246 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2247 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2248 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2251 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2252 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2253 | LookupResult &R, |
| 2254 | bool RequiresADL, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2255 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2256 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2257 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2258 | // name refers to a single template. That's not a terribly common case, |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2259 | // though. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2260 | // foo<int> could identify a single function unambiguously |
| 2261 | // This approach does NOT work, since f<int>(1); |
| 2262 | // gets resolved prior to resorting to overload resolution |
| 2263 | // i.e., template<class T> void f(double); |
| 2264 | // vs template<class T, class U> void f(U); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2265 | |
| 2266 | // These should be filtered out by our callers. |
| 2267 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2268 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2269 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2270 | // We don't want lookup warnings at this point. |
| 2271 | R.suppressDiagnostics(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2272 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2273 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2274 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2275 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2276 | TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2277 | R.getLookupNameInfo(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2278 | RequiresADL, TemplateArgs, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2279 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2280 | |
| 2281 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2284 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2285 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2286 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2287 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2288 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2289 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2290 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2291 | DeclContext *DC; |
| 2292 | if (!(DC = computeDeclContext(SS, false)) || |
| 2293 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2294 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2295 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2296 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2297 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2298 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2299 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 2300 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2301 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2302 | if (R.isAmbiguous()) |
| 2303 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2304 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2305 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2306 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2307 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2308 | return ExprError(); |
| 2309 | } |
| 2310 | |
| 2311 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2312 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 2313 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 2314 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2315 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2316 | return ExprError(); |
| 2317 | } |
| 2318 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2319 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2322 | /// \brief Form a dependent template name. |
| 2323 | /// |
| 2324 | /// This action forms a dependent template name given the template |
| 2325 | /// name and its (presumably dependent) scope specifier. For |
| 2326 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2327 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2328 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2329 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2330 | CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2331 | SourceLocation TemplateKWLoc, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2332 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2333 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2334 | bool EnteringContext, |
| 2335 | TemplateTy &Result) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2336 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 2337 | Diag(TemplateKWLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2338 | getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2339 | diag::warn_cxx98_compat_template_outside_of_template : |
| 2340 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2341 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2342 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2343 | DeclContext *LookupCtx = 0; |
| 2344 | if (SS.isSet()) |
| 2345 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2346 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2347 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2348 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2349 | // C++0x [temp.names]p5: |
| 2350 | // If a name prefixed by the keyword template is not the name of |
| 2351 | // a template, the program is ill-formed. [Note: the keyword |
| 2352 | // template may not be applied to non-template members of class |
| 2353 | // templates. -end note ] [ Note: as is the case with the |
| 2354 | // typename prefix, the template prefix is allowed in cases |
| 2355 | // where it is not strictly necessary; i.e., when the |
| 2356 | // nested-name-specifier or the expression on the left of the -> |
| 2357 | // or . is not dependent on a template-parameter, or the use |
| 2358 | // does not appear in the scope of a template. -end note] |
| 2359 | // |
| 2360 | // Note: C++03 was more strict here, because it banned the use of |
| 2361 | // the "template" keyword prior to a template-name that was not a |
| 2362 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2363 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2364 | // rules, even in C++03 mode with a warning, retroactively applying the DR. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2365 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2366 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 2367 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2368 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2369 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2370 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | d078bd2 | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2371 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2372 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2373 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2374 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2375 | Diag(Name.getLocStart(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2376 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2377 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2378 | << Name.getSourceRange() |
| 2379 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2380 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2381 | } else { |
| 2382 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2383 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2384 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2387 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2388 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2389 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2390 | switch (Name.getKind()) { |
| 2391 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2392 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2393 | Name.Identifier)); |
| 2394 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2395 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2396 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2397 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2398 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2399 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2400 | |
| 2401 | case UnqualifiedId::IK_LiteralOperatorId: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2402 | llvm_unreachable( |
| 2403 | "We don't support these; Parse shouldn't have allowed propagation"); |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2404 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2405 | default: |
| 2406 | break; |
| 2407 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2408 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2409 | Diag(Name.getLocStart(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2410 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2411 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2412 | << Name.getSourceRange() |
| 2413 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2414 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2417 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2418 | const TemplateArgumentLoc &AL, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2419 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2420 | const TemplateArgument &Arg = AL.getArgument(); |
| 2421 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2422 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2423 | switch(Arg.getKind()) { |
| 2424 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2425 | // C++ [temp.arg.type]p1: |
| 2426 | // A template-argument for a template-parameter which is a |
| 2427 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2428 | break; |
| 2429 | case TemplateArgument::Template: { |
| 2430 | // We have a template type parameter but the template argument |
| 2431 | // is a template without any arguments. |
| 2432 | SourceRange SR = AL.getSourceRange(); |
| 2433 | TemplateName Name = Arg.getAsTemplate(); |
| 2434 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 2435 | << Name << SR; |
| 2436 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 2437 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2438 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2439 | return true; |
| 2440 | } |
Kaelyn Uhrain | ab7ad72 | 2012-05-18 23:42:49 +0000 | [diff] [blame] | 2441 | case TemplateArgument::Expression: { |
| 2442 | // We have a template type parameter but the template argument is an |
| 2443 | // expression; see if maybe it is missing the "typename" keyword. |
| 2444 | CXXScopeSpec SS; |
| 2445 | DeclarationNameInfo NameInfo; |
| 2446 | |
| 2447 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 2448 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 2449 | NameInfo = ArgExpr->getNameInfo(); |
| 2450 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 2451 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 2452 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 2453 | NameInfo = ArgExpr->getNameInfo(); |
| 2454 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 2455 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
| 2456 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 2457 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 2458 | } |
| 2459 | |
| 2460 | if (NameInfo.getName()) { |
| 2461 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 2462 | LookupParsedName(Result, CurScope, &SS); |
| 2463 | |
| 2464 | bool CouldBeType = Result.getResultKind() == |
| 2465 | LookupResult::NotFoundInCurrentInstantiation; |
| 2466 | |
| 2467 | for (LookupResult::iterator I = Result.begin(), IEnd = Result.end(); |
| 2468 | !CouldBeType && I != IEnd; ++I) { |
| 2469 | CouldBeType = isa<TypeDecl>(*I); |
| 2470 | } |
| 2471 | if (CouldBeType) { |
| 2472 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
| 2473 | Diag(Loc, diag::err_template_arg_must_be_type_suggest); |
| 2474 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2475 | return true; |
| 2476 | } |
| 2477 | } |
| 2478 | // fallthrough |
| 2479 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2480 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2481 | // We have a template type parameter but the template argument |
| 2482 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 2483 | SourceRange SR = AL.getSourceRange(); |
| 2484 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2485 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2486 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2487 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2488 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2489 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2490 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2491 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2492 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2493 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2494 | // Add the converted template type argument. |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 2495 | QualType ArgType = Context.getCanonicalType(Arg.getAsType()); |
| 2496 | |
| 2497 | // Objective-C ARC: |
| 2498 | // If an explicitly-specified template argument type is a lifetime type |
| 2499 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2500 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 2501 | ArgType->isObjCLifetimeType() && |
| 2502 | !ArgType.getObjCLifetime()) { |
| 2503 | Qualifiers Qs; |
| 2504 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 2505 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 2506 | } |
| 2507 | |
| 2508 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2509 | return false; |
| 2510 | } |
| 2511 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2512 | /// \brief Substitute template arguments into the default template argument for |
| 2513 | /// the given template type parameter. |
| 2514 | /// |
| 2515 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2516 | /// the substitution. |
| 2517 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2518 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2519 | /// for. |
| 2520 | /// |
| 2521 | /// \param TemplateLoc the location of the template name that started the |
| 2522 | /// template-id we are checking. |
| 2523 | /// |
| 2524 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2525 | /// terminates the template-id. |
| 2526 | /// |
| 2527 | /// \param Param the template template parameter whose default we are |
| 2528 | /// substituting into. |
| 2529 | /// |
| 2530 | /// \param Converted the list of template arguments provided for template |
| 2531 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2532 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2533 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2534 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2535 | TemplateDecl *Template, |
| 2536 | SourceLocation TemplateLoc, |
| 2537 | SourceLocation RAngleLoc, |
| 2538 | TemplateTypeParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2539 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2540 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2541 | |
| 2542 | // If the argument type is dependent, instantiate it now based |
| 2543 | // on the previously-computed template arguments. |
| 2544 | if (ArgType->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2545 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2546 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2547 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2548 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2549 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2550 | |
| 2551 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2552 | Template, Converted.data(), |
| 2553 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2554 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2555 | |
Argyrios Kyrtzidis | ad57991 | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 2556 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2557 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2558 | Param->getDefaultArgumentLoc(), |
| 2559 | Param->getDeclName()); |
| 2560 | } |
| 2561 | |
| 2562 | return ArgType; |
| 2563 | } |
| 2564 | |
| 2565 | /// \brief Substitute template arguments into the default template argument for |
| 2566 | /// the given non-type template parameter. |
| 2567 | /// |
| 2568 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2569 | /// the substitution. |
| 2570 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2571 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2572 | /// for. |
| 2573 | /// |
| 2574 | /// \param TemplateLoc the location of the template name that started the |
| 2575 | /// template-id we are checking. |
| 2576 | /// |
| 2577 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2578 | /// terminates the template-id. |
| 2579 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2580 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2581 | /// substituting into. |
| 2582 | /// |
| 2583 | /// \param Converted the list of template arguments provided for template |
| 2584 | /// parameters that precede \p Param in the template parameter list. |
| 2585 | /// |
| 2586 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2587 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2588 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2589 | TemplateDecl *Template, |
| 2590 | SourceLocation TemplateLoc, |
| 2591 | SourceLocation RAngleLoc, |
| 2592 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2593 | SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2594 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2595 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2596 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2597 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2598 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2599 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2600 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2601 | Template, Converted.data(), |
| 2602 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2603 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2604 | |
Argyrios Kyrtzidis | ad57991 | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 2605 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
Eli Friedman | 9b94cd1 | 2012-04-26 22:43:24 +0000 | [diff] [blame] | 2606 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2607 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2608 | } |
| 2609 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2610 | /// \brief Substitute template arguments into the default template argument for |
| 2611 | /// the given template template parameter. |
| 2612 | /// |
| 2613 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2614 | /// the substitution. |
| 2615 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2616 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2617 | /// for. |
| 2618 | /// |
| 2619 | /// \param TemplateLoc the location of the template name that started the |
| 2620 | /// template-id we are checking. |
| 2621 | /// |
| 2622 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2623 | /// terminates the template-id. |
| 2624 | /// |
| 2625 | /// \param Param the template template parameter whose default we are |
| 2626 | /// substituting into. |
| 2627 | /// |
| 2628 | /// \param Converted the list of template arguments provided for template |
| 2629 | /// parameters that precede \p Param in the template parameter list. |
| 2630 | /// |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2631 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 2632 | /// source-location information) that precedes the template name. |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2633 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2634 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2635 | static TemplateName |
| 2636 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2637 | TemplateDecl *Template, |
| 2638 | SourceLocation TemplateLoc, |
| 2639 | SourceLocation RAngleLoc, |
| 2640 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2641 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2642 | NestedNameSpecifierLoc &QualifierLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2643 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2644 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2645 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2646 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2647 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2648 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2649 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2650 | Template, Converted.data(), |
| 2651 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2652 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2653 | |
Argyrios Kyrtzidis | ad57991 | 2012-04-25 18:39:17 +0000 | [diff] [blame] | 2654 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2655 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2656 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2657 | if (QualifierLoc) { |
| 2658 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2659 | AllTemplateArgs); |
| 2660 | if (!QualifierLoc) |
| 2661 | return TemplateName(); |
| 2662 | } |
| 2663 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2664 | return SemaRef.SubstTemplateName(QualifierLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2665 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2666 | Param->getDefaultArgument().getTemplateNameLoc(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2667 | AllTemplateArgs); |
| 2668 | } |
| 2669 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2670 | /// \brief If the given template parameter has a default template |
| 2671 | /// argument, substitute into that default template argument and |
| 2672 | /// return the corresponding template argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2673 | TemplateArgumentLoc |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2674 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2675 | SourceLocation TemplateLoc, |
| 2676 | SourceLocation RAngleLoc, |
| 2677 | Decl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2678 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2679 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2680 | if (!TypeParm->hasDefaultArgument()) |
| 2681 | return TemplateArgumentLoc(); |
| 2682 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2683 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2684 | TemplateLoc, |
| 2685 | RAngleLoc, |
| 2686 | TypeParm, |
| 2687 | Converted); |
| 2688 | if (DI) |
| 2689 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2690 | |
| 2691 | return TemplateArgumentLoc(); |
| 2692 | } |
| 2693 | |
| 2694 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2695 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2696 | if (!NonTypeParm->hasDefaultArgument()) |
| 2697 | return TemplateArgumentLoc(); |
| 2698 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2699 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2700 | TemplateLoc, |
| 2701 | RAngleLoc, |
| 2702 | NonTypeParm, |
| 2703 | Converted); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2704 | if (Arg.isInvalid()) |
| 2705 | return TemplateArgumentLoc(); |
| 2706 | |
| 2707 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2708 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2709 | } |
| 2710 | |
| 2711 | TemplateTemplateParmDecl *TempTempParm |
| 2712 | = cast<TemplateTemplateParmDecl>(Param); |
| 2713 | if (!TempTempParm->hasDefaultArgument()) |
| 2714 | return TemplateArgumentLoc(); |
| 2715 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2716 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2717 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2718 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2719 | TemplateLoc, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2720 | RAngleLoc, |
| 2721 | TempTempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2722 | Converted, |
| 2723 | QualifierLoc); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2724 | if (TName.isNull()) |
| 2725 | return TemplateArgumentLoc(); |
| 2726 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2727 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2728 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2729 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2730 | } |
| 2731 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2732 | /// \brief Check that the given template argument corresponds to the given |
| 2733 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2734 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2735 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2736 | /// checked. |
| 2737 | /// |
| 2738 | /// \param Arg The template argument. |
| 2739 | /// |
| 2740 | /// \param Template The template in which the template argument resides. |
| 2741 | /// |
| 2742 | /// \param TemplateLoc The location of the template name for the template |
| 2743 | /// whose argument list we're matching. |
| 2744 | /// |
| 2745 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2746 | /// the template argument list. |
| 2747 | /// |
| 2748 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2749 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2750 | /// |
| 2751 | /// \param Converted The checked, converted argument will be added to the |
| 2752 | /// end of this small vector. |
| 2753 | /// |
| 2754 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2755 | /// explicitly written, deduced, etc. |
| 2756 | /// |
| 2757 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2758 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2759 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2760 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2761 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2762 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2763 | unsigned ArgumentPackIndex, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2764 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2765 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2766 | // Check template type parameters. |
| 2767 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2768 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2769 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2770 | // Check non-type template parameters. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2771 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2772 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2773 | // with the template arguments we've seen thus far. But if the |
| 2774 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2775 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2776 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2777 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2778 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2779 | if (NTTPType->isDependentType() && |
| 2780 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2781 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2782 | // Do substitution on the type of the non-type template parameter. |
| 2783 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2784 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2785 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2786 | |
| 2787 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2788 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2789 | NTTPType = SubstType(NTTPType, |
| 2790 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2791 | NTTP->getLocation(), |
| 2792 | NTTP->getDeclName()); |
| 2793 | // If that worked, check the non-type template parameter type |
| 2794 | // for validity. |
| 2795 | if (!NTTPType.isNull()) |
| 2796 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2797 | NTTP->getLocation()); |
| 2798 | if (NTTPType.isNull()) |
| 2799 | return true; |
| 2800 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2801 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2802 | switch (Arg.getArgument().getKind()) { |
| 2803 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2804 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2805 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2806 | case TemplateArgument::Expression: { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2807 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2808 | ExprResult Res = |
| 2809 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 2810 | Result, CTAK); |
| 2811 | if (Res.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2812 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2813 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2814 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2815 | break; |
| 2816 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2817 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2818 | case TemplateArgument::Declaration: |
| 2819 | case TemplateArgument::Integral: |
| 2820 | // We've already checked this template argument, so just copy |
| 2821 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2822 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2823 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2824 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2825 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2826 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2827 | // We were given a template template argument. It may not be ill-formed; |
| 2828 | // see below. |
| 2829 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2830 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2831 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2832 | // We have a template argument such as \c T::template X, which we |
| 2833 | // parsed as a template template argument. However, since we now |
| 2834 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2835 | // template name into an expression. |
| 2836 | |
| 2837 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2838 | Arg.getTemplateNameLoc()); |
| 2839 | |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2840 | CXXScopeSpec SS; |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2841 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2842 | // FIXME: the template-template arg was a DependentTemplateName, |
| 2843 | // so it was provided with a template keyword. However, its source |
| 2844 | // location is not stored in the template argument structure. |
| 2845 | SourceLocation TemplateKWLoc; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2846 | ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2847 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2848 | TemplateKWLoc, |
| 2849 | NameInfo, 0)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2850 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2851 | // If we parsed the template argument as a pack expansion, create a |
| 2852 | // pack expansion expression. |
| 2853 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2854 | E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc()); |
| 2855 | if (E.isInvalid()) |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2856 | return true; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2857 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2858 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2859 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2860 | E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result); |
| 2861 | if (E.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2862 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2863 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2864 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2865 | break; |
| 2866 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2867 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2868 | // We have a template argument that actually does refer to a class |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2869 | // template, alias template, or template template parameter, and |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2870 | // therefore cannot be a non-type template argument. |
| 2871 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2872 | << Arg.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2873 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2874 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2875 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2876 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2877 | case TemplateArgument::Type: { |
| 2878 | // We have a non-type template parameter but the template |
| 2879 | // argument is a type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2880 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2881 | // C++ [temp.arg]p2: |
| 2882 | // In a template-argument, an ambiguity between a type-id and |
| 2883 | // an expression is resolved to a type-id, regardless of the |
| 2884 | // form of the corresponding template-parameter. |
| 2885 | // |
| 2886 | // We warn specifically about this case, since it can be rather |
| 2887 | // confusing for users. |
| 2888 | QualType T = Arg.getArgument().getAsType(); |
| 2889 | SourceRange SR = Arg.getSourceRange(); |
| 2890 | if (T->isFunctionType()) |
| 2891 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2892 | else |
| 2893 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2894 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2895 | return true; |
| 2896 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2897 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2898 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2899 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2900 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2901 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2902 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2906 | // Check template template parameters. |
| 2907 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2908 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2909 | // Substitute into the template parameter list of the template |
| 2910 | // template parameter, since previously-supplied template arguments |
| 2911 | // may appear within the template template parameter. |
| 2912 | { |
| 2913 | // Set up a template instantiation context. |
| 2914 | LocalInstantiationScope Scope(*this); |
| 2915 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2916 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2917 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2918 | |
| 2919 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2920 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2921 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2922 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2923 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2924 | if (!TempParm) |
| 2925 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2926 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2927 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2928 | switch (Arg.getArgument().getKind()) { |
| 2929 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2930 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2931 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2932 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2933 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2934 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2935 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2936 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2937 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2938 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2939 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2940 | case TemplateArgument::Expression: |
| 2941 | case TemplateArgument::Type: |
| 2942 | // We have a template template parameter but the template |
| 2943 | // argument does not refer to a template. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2944 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2945 | << getLangOpts().CPlusPlus0x; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2946 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2947 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2948 | case TemplateArgument::Declaration: |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2949 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2950 | case TemplateArgument::Integral: |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2951 | llvm_unreachable("Integral argument with template template parameter"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2952 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2953 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2954 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2955 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2956 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2957 | return false; |
| 2958 | } |
| 2959 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2960 | /// \brief Diagnose an arity mismatch in the |
| 2961 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 2962 | SourceLocation TemplateLoc, |
| 2963 | TemplateArgumentListInfo &TemplateArgs) { |
| 2964 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2965 | unsigned NumParams = Params->size(); |
| 2966 | unsigned NumArgs = TemplateArgs.size(); |
| 2967 | |
| 2968 | SourceRange Range; |
| 2969 | if (NumArgs > NumParams) |
| 2970 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
| 2971 | TemplateArgs.getRAngleLoc()); |
| 2972 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2973 | << (NumArgs > NumParams) |
| 2974 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2975 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2976 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2977 | << Template << Range; |
| 2978 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2979 | << Params->getSourceRange(); |
| 2980 | return true; |
| 2981 | } |
| 2982 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2983 | /// \brief Check that the given template argument list is well-formed |
| 2984 | /// for specializing the given template. |
| 2985 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2986 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2987 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2988 | bool PartialTemplateArgs, |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2989 | SmallVectorImpl<TemplateArgument> &Converted, |
| 2990 | bool *ExpansionIntoFixedList) { |
| 2991 | if (ExpansionIntoFixedList) |
| 2992 | *ExpansionIntoFixedList = false; |
| 2993 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2994 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2995 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2996 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2997 | bool Invalid = false; |
| 2998 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2999 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 3000 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3001 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 3002 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3003 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3004 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3005 | // [...] The type and form of each template-argument specified in |
| 3006 | // a template-id shall match the type and form specified for the |
| 3007 | // corresponding parameter declared by the template in its |
| 3008 | // template-parameter-list. |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3009 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3010 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3011 | TemplateParameterList::iterator Param = Params->begin(), |
| 3012 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3013 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3014 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3015 | bool SawPackExpansion = false; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3016 | while (Param != ParamEnd) { |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3017 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3018 | // If we have an expanded parameter pack, make sure we don't have too |
| 3019 | // many arguments. |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3020 | // FIXME: This really should fall out from the normal arity checking. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3021 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3022 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3023 | if (NTTP->isExpandedParameterPack() && |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3024 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 3025 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3026 | << true |
| 3027 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3028 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3029 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3030 | << Template; |
| 3031 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3032 | << Params->getSourceRange(); |
| 3033 | return true; |
| 3034 | } |
| 3035 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3036 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3037 | // Check the template argument we were given. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3038 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 3039 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3040 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3041 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3042 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3043 | if ((*Param)->isTemplateParameterPack()) { |
| 3044 | // The template parameter was a template parameter pack, so take the |
| 3045 | // deduced argument and place it on the argument pack. Note that we |
| 3046 | // stay on the same template parameter so that we can deduce more |
| 3047 | // arguments. |
| 3048 | ArgumentPack.push_back(Converted.back()); |
| 3049 | Converted.pop_back(); |
| 3050 | } else { |
| 3051 | // Move to the next template parameter. |
| 3052 | ++Param; |
| 3053 | } |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3054 | |
| 3055 | // If this template argument is a pack expansion, record that fact |
| 3056 | // and break out; we can't actually check any more. |
| 3057 | if (TemplateArgs[ArgIdx].getArgument().isPackExpansion()) { |
| 3058 | SawPackExpansion = true; |
| 3059 | ++ArgIdx; |
| 3060 | break; |
| 3061 | } |
| 3062 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3063 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3064 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3065 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3066 | |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3067 | // If we're checking a partial template argument list, we're done. |
| 3068 | if (PartialTemplateArgs) { |
| 3069 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 3070 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3071 | ArgumentPack.data(), |
| 3072 | ArgumentPack.size())); |
| 3073 | |
| 3074 | return Invalid; |
| 3075 | } |
| 3076 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3077 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3078 | // arguments, just break out now and we'll fill in the argument pack below. |
| 3079 | if ((*Param)->isTemplateParameterPack()) |
| 3080 | break; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 3081 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3082 | // Check whether we have a default argument. |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3083 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3084 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3085 | // Retrieve the default template argument from the template |
| 3086 | // parameter. For each kind of template parameter, we substitute the |
| 3087 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3088 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3089 | // the default argument. |
| 3090 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3091 | if (!TTP->hasDefaultArgument()) |
| 3092 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3093 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3094 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3095 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3096 | Template, |
| 3097 | TemplateLoc, |
| 3098 | RAngleLoc, |
| 3099 | TTP, |
| 3100 | Converted); |
| 3101 | if (!ArgType) |
| 3102 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3103 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3104 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 3105 | ArgType); |
| 3106 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3107 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3108 | if (!NTTP->hasDefaultArgument()) |
| 3109 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3110 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3111 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3112 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3113 | TemplateLoc, |
| 3114 | RAngleLoc, |
| 3115 | NTTP, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3116 | Converted); |
| 3117 | if (E.isInvalid()) |
| 3118 | return true; |
| 3119 | |
| 3120 | Expr *Ex = E.takeAs<Expr>(); |
| 3121 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 3122 | } else { |
| 3123 | TemplateTemplateParmDecl *TempParm |
| 3124 | = cast<TemplateTemplateParmDecl>(*Param); |
| 3125 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3126 | if (!TempParm->hasDefaultArgument()) |
| 3127 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3128 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3129 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3130 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3131 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3132 | TemplateLoc, |
| 3133 | RAngleLoc, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3134 | TempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3135 | Converted, |
| 3136 | QualifierLoc); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3137 | if (Name.isNull()) |
| 3138 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3139 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3140 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3141 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3142 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3143 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3144 | // Introduce an instantiation record that describes where we are using |
| 3145 | // the default template argument. |
| 3146 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3147 | Converted.data(), Converted.size(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3148 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3149 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3150 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3151 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3152 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3153 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3154 | |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3155 | // Core issue 150 (assumed resolution): if this is a template template |
| 3156 | // parameter, keep track of the default template arguments from the |
| 3157 | // template definition. |
| 3158 | if (isTemplateTemplateParameter) |
| 3159 | TemplateArgs.addArgument(Arg); |
| 3160 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3161 | // Move to the next template parameter and argument. |
| 3162 | ++Param; |
| 3163 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3164 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3165 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3166 | // If we saw a pack expansion, then directly convert the remaining arguments, |
| 3167 | // because we don't know what parameters they'll match up with. |
| 3168 | if (SawPackExpansion) { |
| 3169 | bool AddToArgumentPack |
| 3170 | = Param != ParamEnd && (*Param)->isTemplateParameterPack(); |
| 3171 | while (ArgIdx < NumArgs) { |
| 3172 | if (AddToArgumentPack) |
| 3173 | ArgumentPack.push_back(TemplateArgs[ArgIdx].getArgument()); |
| 3174 | else |
| 3175 | Converted.push_back(TemplateArgs[ArgIdx].getArgument()); |
| 3176 | ++ArgIdx; |
| 3177 | } |
| 3178 | |
| 3179 | // Push the argument pack onto the list of converted arguments. |
| 3180 | if (AddToArgumentPack) { |
| 3181 | if (ArgumentPack.empty()) |
| 3182 | Converted.push_back(TemplateArgument(0, 0)); |
| 3183 | else { |
| 3184 | Converted.push_back( |
| 3185 | TemplateArgument::CreatePackCopy(Context, |
| 3186 | ArgumentPack.data(), |
| 3187 | ArgumentPack.size())); |
| 3188 | ArgumentPack.clear(); |
| 3189 | } |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3190 | } else if (ExpansionIntoFixedList) { |
| 3191 | // We have expanded a pack into a fixed list. |
| 3192 | *ExpansionIntoFixedList = true; |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | return Invalid; |
| 3196 | } |
| 3197 | |
| 3198 | // If we have any leftover arguments, then there were too many arguments. |
| 3199 | // Complain and fail. |
| 3200 | if (ArgIdx < NumArgs) |
| 3201 | return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs); |
| 3202 | |
| 3203 | // If we have an expanded parameter pack, make sure we don't have too |
| 3204 | // many arguments. |
| 3205 | // FIXME: This really should fall out from the normal arity checking. |
| 3206 | if (Param != ParamEnd) { |
| 3207 | if (NonTypeTemplateParmDecl *NTTP |
| 3208 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 3209 | if (NTTP->isExpandedParameterPack() && |
| 3210 | ArgumentPack.size() < NTTP->getNumExpansionTypes()) { |
| 3211 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3212 | << false |
| 3213 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3214 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3215 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3216 | << Template; |
| 3217 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3218 | << Params->getSourceRange(); |
| 3219 | return true; |
| 3220 | } |
| 3221 | } |
| 3222 | } |
| 3223 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3224 | // Form argument packs for each of the parameter packs remaining. |
| 3225 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3226 | // If we're checking a partial list of template arguments, don't fill |
| 3227 | // in arguments for non-template parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3228 | if ((*Param)->isTemplateParameterPack()) { |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 3229 | if (!HasParameterPack) |
| 3230 | return true; |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3231 | if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3232 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3233 | else { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3234 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3235 | ArgumentPack.data(), |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3236 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3237 | ArgumentPack.clear(); |
| 3238 | } |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3239 | } else if (!PartialTemplateArgs) |
| 3240 | return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3241 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3242 | ++Param; |
| 3243 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3244 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3245 | return Invalid; |
| 3246 | } |
| 3247 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3248 | namespace { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3249 | class UnnamedLocalNoLinkageFinder |
| 3250 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3251 | { |
| 3252 | Sema &S; |
| 3253 | SourceRange SR; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3254 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3255 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3256 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3257 | public: |
| 3258 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3259 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3260 | bool Visit(QualType T) { |
| 3261 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3262 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3263 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3264 | #define TYPE(Class, Parent) \ |
| 3265 | bool Visit##Class##Type(const Class##Type *); |
| 3266 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 3267 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3268 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 3269 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3270 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3271 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3272 | bool VisitTagDecl(const TagDecl *Tag); |
| 3273 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3274 | }; |
| 3275 | } |
| 3276 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3277 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3278 | return false; |
| 3279 | } |
| 3280 | |
| 3281 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3282 | return Visit(T->getElementType()); |
| 3283 | } |
| 3284 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3285 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3286 | return Visit(T->getPointeeType()); |
| 3287 | } |
| 3288 | |
| 3289 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3290 | const BlockPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3291 | return Visit(T->getPointeeType()); |
| 3292 | } |
| 3293 | |
| 3294 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3295 | const LValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3296 | return Visit(T->getPointeeType()); |
| 3297 | } |
| 3298 | |
| 3299 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3300 | const RValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3301 | return Visit(T->getPointeeType()); |
| 3302 | } |
| 3303 | |
| 3304 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3305 | const MemberPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3306 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3307 | } |
| 3308 | |
| 3309 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3310 | const ConstantArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3311 | return Visit(T->getElementType()); |
| 3312 | } |
| 3313 | |
| 3314 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3315 | const IncompleteArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3316 | return Visit(T->getElementType()); |
| 3317 | } |
| 3318 | |
| 3319 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3320 | const VariableArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3321 | return Visit(T->getElementType()); |
| 3322 | } |
| 3323 | |
| 3324 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3325 | const DependentSizedArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3326 | return Visit(T->getElementType()); |
| 3327 | } |
| 3328 | |
| 3329 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3330 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3331 | return Visit(T->getElementType()); |
| 3332 | } |
| 3333 | |
| 3334 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3335 | return Visit(T->getElementType()); |
| 3336 | } |
| 3337 | |
| 3338 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 3339 | return Visit(T->getElementType()); |
| 3340 | } |
| 3341 | |
| 3342 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 3343 | const FunctionProtoType* T) { |
| 3344 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3345 | AEnd = T->arg_type_end(); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3346 | A != AEnd; ++A) { |
| 3347 | if (Visit(*A)) |
| 3348 | return true; |
| 3349 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3350 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3351 | return Visit(T->getResultType()); |
| 3352 | } |
| 3353 | |
| 3354 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 3355 | const FunctionNoProtoType* T) { |
| 3356 | return Visit(T->getResultType()); |
| 3357 | } |
| 3358 | |
| 3359 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 3360 | const UnresolvedUsingType*) { |
| 3361 | return false; |
| 3362 | } |
| 3363 | |
| 3364 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 3365 | return false; |
| 3366 | } |
| 3367 | |
| 3368 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 3369 | return Visit(T->getUnderlyingType()); |
| 3370 | } |
| 3371 | |
| 3372 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 3373 | return false; |
| 3374 | } |
| 3375 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3376 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 3377 | const UnaryTransformType*) { |
| 3378 | return false; |
| 3379 | } |
| 3380 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3381 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 3382 | return Visit(T->getDeducedType()); |
| 3383 | } |
| 3384 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3385 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 3386 | return VisitTagDecl(T->getDecl()); |
| 3387 | } |
| 3388 | |
| 3389 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 3390 | return VisitTagDecl(T->getDecl()); |
| 3391 | } |
| 3392 | |
| 3393 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 3394 | const TemplateTypeParmType*) { |
| 3395 | return false; |
| 3396 | } |
| 3397 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3398 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 3399 | const SubstTemplateTypeParmPackType *) { |
| 3400 | return false; |
| 3401 | } |
| 3402 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3403 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 3404 | const TemplateSpecializationType*) { |
| 3405 | return false; |
| 3406 | } |
| 3407 | |
| 3408 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 3409 | const InjectedClassNameType* T) { |
| 3410 | return VisitTagDecl(T->getDecl()); |
| 3411 | } |
| 3412 | |
| 3413 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 3414 | const DependentNameType* T) { |
| 3415 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3416 | } |
| 3417 | |
| 3418 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 3419 | const DependentTemplateSpecializationType* T) { |
| 3420 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3421 | } |
| 3422 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3423 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 3424 | const PackExpansionType* T) { |
| 3425 | return Visit(T->getPattern()); |
| 3426 | } |
| 3427 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3428 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 3429 | return false; |
| 3430 | } |
| 3431 | |
| 3432 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 3433 | const ObjCInterfaceType *) { |
| 3434 | return false; |
| 3435 | } |
| 3436 | |
| 3437 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 3438 | const ObjCObjectPointerType *) { |
| 3439 | return false; |
| 3440 | } |
| 3441 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3442 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 3443 | return Visit(T->getValueType()); |
| 3444 | } |
| 3445 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3446 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 3447 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3448 | S.Diag(SR.getBegin(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3449 | S.getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3450 | diag::warn_cxx98_compat_template_arg_local_type : |
| 3451 | diag::ext_template_arg_local_type) |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3452 | << S.Context.getTypeDeclType(Tag) << SR; |
| 3453 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3454 | } |
| 3455 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3456 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3457 | S.Diag(SR.getBegin(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3458 | S.getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3459 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 3460 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3461 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 3462 | return true; |
| 3463 | } |
| 3464 | |
| 3465 | return false; |
| 3466 | } |
| 3467 | |
| 3468 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 3469 | NestedNameSpecifier *NNS) { |
| 3470 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 3471 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3472 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3473 | switch (NNS->getKind()) { |
| 3474 | case NestedNameSpecifier::Identifier: |
| 3475 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3476 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3477 | case NestedNameSpecifier::Global: |
| 3478 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3479 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3480 | case NestedNameSpecifier::TypeSpec: |
| 3481 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3482 | return Visit(QualType(NNS->getAsType(), 0)); |
| 3483 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3484 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3485 | } |
| 3486 | |
| 3487 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3488 | /// \brief Check a template argument against its corresponding |
| 3489 | /// template type parameter. |
| 3490 | /// |
| 3491 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 3492 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3493 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3494 | TypeSourceInfo *ArgInfo) { |
| 3495 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3496 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 3497 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 3498 | |
| 3499 | if (Arg->isVariablyModifiedType()) { |
| 3500 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3501 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3502 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3505 | // C++03 [temp.arg.type]p2: |
| 3506 | // A local type, a type with no linkage, an unnamed type or a type |
| 3507 | // compounded from any of these types shall not be used as a |
| 3508 | // template-argument for a template type-parameter. |
| 3509 | // |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3510 | // C++11 allows these, and even in C++03 we allow them as an extension with |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3511 | // a warning. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3512 | if (LangOpts.CPlusPlus0x ? |
| 3513 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 3514 | SR.getBegin()) != DiagnosticsEngine::Ignored || |
| 3515 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type, |
| 3516 | SR.getBegin()) != DiagnosticsEngine::Ignored : |
| 3517 | Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3518 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 3519 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 3520 | } |
| 3521 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3522 | return false; |
| 3523 | } |
| 3524 | |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3525 | enum NullPointerValueKind { |
| 3526 | NPV_NotNullPointer, |
| 3527 | NPV_NullPointer, |
| 3528 | NPV_Error |
| 3529 | }; |
| 3530 | |
| 3531 | /// \brief Determine whether the given template argument is a null pointer |
| 3532 | /// value of the appropriate type. |
| 3533 | static NullPointerValueKind |
| 3534 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 3535 | QualType ParamType, Expr *Arg) { |
| 3536 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 3537 | return NPV_NotNullPointer; |
| 3538 | |
| 3539 | if (!S.getLangOpts().CPlusPlus0x) |
| 3540 | return NPV_NotNullPointer; |
| 3541 | |
| 3542 | // Determine whether we have a constant expression. |
Douglas Gregor | 50fadd1 | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 3543 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 3544 | if (ArgRV.isInvalid()) |
| 3545 | return NPV_Error; |
| 3546 | Arg = ArgRV.take(); |
| 3547 | |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3548 | Expr::EvalResult EvalResult; |
Douglas Gregor | 50fadd1 | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 3549 | llvm::SmallVector<PartialDiagnosticAt, 8> Notes; |
| 3550 | EvalResult.Diag = &Notes; |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3551 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 50fadd1 | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 3552 | EvalResult.HasSideEffects) { |
| 3553 | SourceLocation DiagLoc = Arg->getExprLoc(); |
| 3554 | |
| 3555 | // If our only note is the usual "invalid subexpression" note, just point |
| 3556 | // the caret at its location rather than producing an essentially |
| 3557 | // redundant note. |
| 3558 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 3559 | diag::note_invalid_subexpr_in_const_expr) { |
| 3560 | DiagLoc = Notes[0].first; |
| 3561 | Notes.clear(); |
| 3562 | } |
| 3563 | |
| 3564 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 3565 | << Arg->getType() << Arg->getSourceRange(); |
| 3566 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 3567 | S.Diag(Notes[I].first, Notes[I].second); |
| 3568 | |
| 3569 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3570 | return NPV_Error; |
| 3571 | } |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3572 | |
| 3573 | // C++11 [temp.arg.nontype]p1: |
| 3574 | // - an address constant expression of type std::nullptr_t |
| 3575 | if (Arg->getType()->isNullPtrType()) |
| 3576 | return NPV_NullPointer; |
| 3577 | |
| 3578 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 3579 | // - a constant expression that evaluates to a null member pointer value |
| 3580 | // (4.11); or |
| 3581 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 3582 | (EvalResult.Val.isMemberPointer() && |
| 3583 | !EvalResult.Val.getMemberPointerDecl())) { |
| 3584 | // If our expression has an appropriate type, we've succeeded. |
| 3585 | bool ObjCLifetimeConversion; |
| 3586 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 3587 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 3588 | ObjCLifetimeConversion)) |
| 3589 | return NPV_NullPointer; |
| 3590 | |
| 3591 | // The types didn't match, but we know we got a null pointer; complain, |
| 3592 | // then recover as if the types were correct. |
| 3593 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 3594 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 3595 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3596 | return NPV_NullPointer; |
| 3597 | } |
| 3598 | |
| 3599 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 3600 | // constant, suggest a cast to the appropriate type. |
| 3601 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 3602 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 3603 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
| 3604 | << ParamType |
| 3605 | << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 3606 | << FixItHint::CreateInsertion(S.PP.getLocForEndOfToken(Arg->getLocEnd()), |
| 3607 | ")"); |
| 3608 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3609 | return NPV_NullPointer; |
| 3610 | } |
| 3611 | |
| 3612 | // FIXME: If we ever want to support general, address-constant expressions |
| 3613 | // as non-type template arguments, we should return the ExprResult here to |
| 3614 | // be interpreted by the caller. |
| 3615 | return NPV_NotNullPointer; |
| 3616 | } |
| 3617 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3618 | /// \brief Checks whether the given template argument is the address |
| 3619 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3620 | static bool |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3621 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 3622 | NonTypeTemplateParmDecl *Param, |
| 3623 | QualType ParamType, |
| 3624 | Expr *ArgIn, |
| 3625 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3626 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3627 | Expr *Arg = ArgIn; |
| 3628 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3629 | |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3630 | // If our parameter has pointer type, check for a null template value. |
| 3631 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 3632 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 3633 | case NPV_NullPointer: |
Richard Smith | 86e6fdc | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 3634 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3635 | Converted = TemplateArgument((Decl *)0); |
| 3636 | return false; |
| 3637 | |
| 3638 | case NPV_Error: |
| 3639 | return true; |
| 3640 | |
| 3641 | case NPV_NotNullPointer: |
| 3642 | break; |
| 3643 | } |
| 3644 | } |
| 3645 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3646 | // See through any implicit casts we added to fix the type. |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3647 | Arg = Arg->IgnoreImpCasts(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3648 | |
| 3649 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3650 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3651 | // A template-argument for a non-type, non-template |
| 3652 | // template-parameter shall be one of: [...] |
| 3653 | // |
| 3654 | // -- the address of an object or function with external |
| 3655 | // linkage, including function templates and function |
| 3656 | // template-ids but excluding non-static class members, |
| 3657 | // expressed as & id-expression where the & is optional if |
| 3658 | // the name refers to a function or array, or if the |
| 3659 | // corresponding template-parameter is a reference; or |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3660 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3661 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3662 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3663 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3664 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3665 | if (!Invalid && !ExtraParens) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3666 | S.Diag(Arg->getLocStart(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3667 | S.getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3668 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3669 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3670 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3671 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | Arg = Parens->getSubExpr(); |
| 3675 | } |
| 3676 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3677 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3678 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3679 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3680 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3681 | bool AddressTaken = false; |
| 3682 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3683 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3684 | if (UnOp->getOpcode() == UO_AddrOf) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3685 | Arg = UnOp->getSubExpr(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3686 | AddressTaken = true; |
| 3687 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 3688 | } |
Francois Pichet | a343a41 | 2011-04-29 09:08:14 +0000 | [diff] [blame] | 3689 | } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3690 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3691 | if (S.getLangOpts().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3692 | Converted = TemplateArgument(ArgIn); |
| 3693 | return false; |
| 3694 | } |
| 3695 | |
| 3696 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3697 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3698 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3699 | |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3700 | // Stop checking the precise nature of the argument if it is value dependent, |
| 3701 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3702 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3703 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3704 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3705 | } |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 3706 | |
| 3707 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 3708 | if (!DRE) { |
| 3709 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 3710 | << Arg->getSourceRange(); |
| 3711 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3712 | return true; |
| 3713 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3714 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3715 | if (!isa<ValueDecl>(DRE->getDecl())) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3716 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3717 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3718 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3719 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3720 | return true; |
| 3721 | } |
| 3722 | |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3723 | NamedDecl *Entity = DRE->getDecl(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3724 | |
| 3725 | // Cannot refer to non-static data members |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3726 | if (FieldDecl *Field = dyn_cast<FieldDecl>(Entity)) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3727 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3728 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3729 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3730 | return true; |
| 3731 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3732 | |
| 3733 | // Cannot refer to non-static member functions |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3734 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3735 | if (!Method->isStatic()) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3736 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3737 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3738 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3739 | return true; |
| 3740 | } |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3741 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3742 | |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3743 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 3744 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3745 | |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3746 | // A non-type template argument must refer to an object or function. |
| 3747 | if (!Func && !Var) { |
| 3748 | // We found something, but we don't know specifically what it is. |
| 3749 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 3750 | << Arg->getSourceRange(); |
| 3751 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3752 | return true; |
| 3753 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3754 | |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3755 | // Address / reference template args must have external linkage in C++98. |
| 3756 | if (Entity->getLinkage() == InternalLinkage) { |
| 3757 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus0x ? |
| 3758 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 3759 | diag::ext_template_arg_object_internal) |
| 3760 | << !Func << Entity << Arg->getSourceRange(); |
| 3761 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 3762 | << !Func; |
| 3763 | } else if (Entity->getLinkage() == NoLinkage) { |
| 3764 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 3765 | << !Func << Entity << Arg->getSourceRange(); |
| 3766 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 3767 | << !Func; |
| 3768 | return true; |
| 3769 | } |
| 3770 | |
| 3771 | if (Func) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3772 | // If the template parameter has pointer type, the function decays. |
| 3773 | if (ParamType->isPointerType() && !AddressTaken) |
| 3774 | ArgType = S.Context.getPointerType(Func->getType()); |
| 3775 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 3776 | // If we originally had an address-of operator, but the |
| 3777 | // parameter has reference type, complain and (if things look |
| 3778 | // like they will work) drop the address-of operator. |
| 3779 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 3780 | ParamType.getNonReferenceType())) { |
| 3781 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3782 | << ParamType; |
| 3783 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3784 | return true; |
| 3785 | } |
| 3786 | |
| 3787 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3788 | << ParamType |
| 3789 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3790 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3791 | |
| 3792 | ArgType = Func->getType(); |
| 3793 | } |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3794 | } else { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3795 | // A value of reference type is not an object. |
| 3796 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3797 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3798 | diag::err_template_arg_reference_var) |
| 3799 | << Var->getType() << Arg->getSourceRange(); |
| 3800 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3801 | return true; |
| 3802 | } |
| 3803 | |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3804 | // A template argument must have static storage duration. |
| 3805 | // FIXME: Ensure this works for thread_local as well as __thread. |
| 3806 | if (Var->isThreadSpecified()) { |
| 3807 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 3808 | << Arg->getSourceRange(); |
| 3809 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 3810 | return true; |
| 3811 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3812 | |
| 3813 | // If the template parameter has pointer type, we must have taken |
| 3814 | // the address of this object. |
| 3815 | if (ParamType->isReferenceType()) { |
| 3816 | if (AddressTaken) { |
| 3817 | // If we originally had an address-of operator, but the |
| 3818 | // parameter has reference type, complain and (if things look |
| 3819 | // like they will work) drop the address-of operator. |
| 3820 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3821 | ParamType.getNonReferenceType())) { |
| 3822 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3823 | << ParamType; |
| 3824 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3825 | return true; |
| 3826 | } |
| 3827 | |
| 3828 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3829 | << ParamType |
| 3830 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3831 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3832 | |
| 3833 | ArgType = Var->getType(); |
| 3834 | } |
| 3835 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3836 | if (Var->getType()->isArrayType()) { |
| 3837 | // Array-to-pointer decay. |
| 3838 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3839 | } else { |
| 3840 | // If the template parameter has pointer type but the address of |
| 3841 | // this object was not taken, complain and (possibly) recover by |
| 3842 | // taking the address of the entity. |
| 3843 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3844 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3845 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3846 | << ParamType; |
| 3847 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3848 | return true; |
| 3849 | } |
| 3850 | |
| 3851 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3852 | << ParamType |
| 3853 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3854 | |
| 3855 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3856 | } |
| 3857 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3858 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3859 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3860 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3861 | if (ParamType->isPointerType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3862 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3863 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 3864 | ObjCLifetimeConversion)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3865 | // For pointer-to-object types, qualification conversions are |
| 3866 | // permitted. |
| 3867 | } else { |
| 3868 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3869 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3870 | // C++ [temp.arg.nontype]p5b3: |
| 3871 | // For a non-type template-parameter of type reference to |
| 3872 | // object, no conversions apply. The type referred to by the |
| 3873 | // reference may be more cv-qualified than the (otherwise |
| 3874 | // identical) type of the template- argument. The |
| 3875 | // template-parameter is bound directly to the |
| 3876 | // template-argument, which shall be an lvalue. |
| 3877 | |
| 3878 | // FIXME: Other qualifiers? |
| 3879 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3880 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3881 | |
| 3882 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3883 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3884 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3885 | << ParamType << Arg->getType() |
| 3886 | << Arg->getSourceRange(); |
| 3887 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3888 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3889 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3890 | } |
| 3891 | } |
| 3892 | |
| 3893 | // At this point, the template argument refers to an object or |
| 3894 | // function with external linkage. We now need to check whether the |
| 3895 | // argument and parameter types are compatible. |
| 3896 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3897 | ParamType.getNonReferenceType())) { |
| 3898 | // We can't perform this conversion or binding. |
| 3899 | if (ParamType->isReferenceType()) |
| 3900 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3901 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3902 | else |
| 3903 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3904 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3905 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3906 | return true; |
| 3907 | } |
| 3908 | } |
| 3909 | |
| 3910 | // Create the template argument. |
| 3911 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3912 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3913 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
| 3916 | /// \brief Checks whether the given template argument is a pointer to |
| 3917 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3918 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 3919 | NonTypeTemplateParmDecl *Param, |
| 3920 | QualType ParamType, |
| 3921 | Expr *&ResultArg, |
| 3922 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3923 | bool Invalid = false; |
| 3924 | |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3925 | // Check for a null pointer value. |
| 3926 | Expr *Arg = ResultArg; |
| 3927 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 3928 | case NPV_Error: |
| 3929 | return true; |
| 3930 | case NPV_NullPointer: |
Richard Smith | 86e6fdc | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 3931 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3932 | Converted = TemplateArgument((Decl *)0); |
| 3933 | return false; |
| 3934 | case NPV_NotNullPointer: |
| 3935 | break; |
| 3936 | } |
| 3937 | |
| 3938 | bool ObjCLifetimeConversion; |
| 3939 | if (S.IsQualificationConversion(Arg->getType(), |
| 3940 | ParamType.getNonReferenceType(), |
| 3941 | false, ObjCLifetimeConversion)) { |
| 3942 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, |
| 3943 | Arg->getValueKind()).take(); |
| 3944 | ResultArg = Arg; |
| 3945 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), |
| 3946 | ParamType.getNonReferenceType())) { |
| 3947 | // We can't perform this conversion. |
| 3948 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 3949 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 3950 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3951 | return true; |
| 3952 | } |
| 3953 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3954 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3955 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3956 | Arg = Cast->getSubExpr(); |
| 3957 | |
| 3958 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3959 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3960 | // A template-argument for a non-type, non-template |
| 3961 | // template-parameter shall be one of: [...] |
| 3962 | // |
| 3963 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3964 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3965 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3966 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3967 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3968 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3969 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3970 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3971 | S.Diag(Arg->getLocStart(), |
| 3972 | S.getLangOpts().CPlusPlus0x ? |
| 3973 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3974 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3975 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3976 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
| 3979 | Arg = Parens->getSubExpr(); |
| 3980 | } |
| 3981 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3982 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3983 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3984 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3985 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3986 | // A pointer-to-member constant written &Class::member. |
| 3987 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3988 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3989 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3990 | if (DRE && !DRE->getQualifier()) |
| 3991 | DRE = 0; |
| 3992 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3993 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3994 | // A constant of pointer-to-member type. |
| 3995 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3996 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3997 | if (VD->getType()->isMemberPointerType()) { |
| 3998 | if (isa<NonTypeTemplateParmDecl>(VD) || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3999 | (isa<VarDecl>(VD) && |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4000 | S.Context.getCanonicalType(VD->getType()).isConstQualified())) { |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4001 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4002 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4003 | else |
| 4004 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 4005 | return Invalid; |
| 4006 | } |
| 4007 | } |
| 4008 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4009 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4010 | DRE = 0; |
| 4011 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4012 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4013 | if (!DRE) |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4014 | return S.Diag(Arg->getLocStart(), |
| 4015 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4016 | << Arg->getSourceRange(); |
| 4017 | |
| 4018 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 4019 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 4020 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 4021 | "Only non-static member pointers can make it here"); |
| 4022 | |
| 4023 | // Okay: this is the address of a non-static member, and therefore |
| 4024 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4025 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4026 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 4027 | else |
| 4028 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4029 | return Invalid; |
| 4030 | } |
| 4031 | |
| 4032 | // We found something else, but we don't know specifically what it is. |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4033 | S.Diag(Arg->getLocStart(), |
| 4034 | diag::err_template_arg_not_pointer_to_member_form) |
| 4035 | << Arg->getSourceRange(); |
| 4036 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 4037 | return true; |
| 4038 | } |
| 4039 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4040 | /// \brief Check a template argument against its corresponding |
| 4041 | /// non-type template parameter. |
| 4042 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4043 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4044 | /// If an error occurred, it returns ExprError(); otherwise, it |
| 4045 | /// returns the converted template argument. \p |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4046 | /// InstantiatedParamType is the type of the non-type template |
| 4047 | /// parameter after it has been instantiated. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4048 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 4049 | QualType InstantiatedParamType, Expr *Arg, |
| 4050 | TemplateArgument &Converted, |
| 4051 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4052 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4053 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4054 | // If either the parameter has a dependent type or the argument is |
| 4055 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4056 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 4057 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 4058 | Converted = TemplateArgument(Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4059 | return Owned(Arg); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4060 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4061 | |
| 4062 | // C++ [temp.arg.nontype]p5: |
| 4063 | // The following conversions are performed on each expression used |
| 4064 | // as a non-type template-argument. If a non-type |
| 4065 | // template-argument cannot be converted to the type of the |
| 4066 | // corresponding template-parameter then the program is |
| 4067 | // ill-formed. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4068 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4069 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4070 | // C++11: |
| 4071 | // -- for a non-type template-parameter of integral or |
| 4072 | // enumeration type, conversions permitted in a converted |
| 4073 | // constant expression are applied. |
| 4074 | // |
| 4075 | // C++98: |
| 4076 | // -- for a non-type template-parameter of integral or |
| 4077 | // enumeration type, integral promotions (4.5) and integral |
| 4078 | // conversions (4.7) are applied. |
| 4079 | |
| 4080 | if (CTAK == CTAK_Deduced && |
| 4081 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 4082 | // C++ [temp.deduct.type]p17: |
| 4083 | // If, in the declaration of a function template with a non-type |
| 4084 | // template-parameter, the non-type template-parameter is used |
| 4085 | // in an expression in the function parameter-list and, if the |
| 4086 | // corresponding template-argument is deduced, the |
| 4087 | // template-argument type shall match the type of the |
| 4088 | // template-parameter exactly, except that a template-argument |
| 4089 | // deduced from an array bound may be of any integral type. |
| 4090 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 4091 | << Arg->getType().getUnqualifiedType() |
| 4092 | << ParamType.getUnqualifiedType(); |
| 4093 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4094 | return ExprError(); |
| 4095 | } |
| 4096 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4097 | if (getLangOpts().CPlusPlus0x) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4098 | // We can't check arbitrary value-dependent arguments. |
| 4099 | // FIXME: If there's no viable conversion to the template parameter type, |
| 4100 | // we should be able to diagnose that prior to instantiation. |
| 4101 | if (Arg->isValueDependent()) { |
| 4102 | Converted = TemplateArgument(Arg); |
| 4103 | return Owned(Arg); |
| 4104 | } |
| 4105 | |
| 4106 | // C++ [temp.arg.nontype]p1: |
| 4107 | // A template-argument for a non-type, non-template template-parameter |
| 4108 | // shall be one of: |
| 4109 | // |
| 4110 | // -- for a non-type template-parameter of integral or enumeration |
| 4111 | // type, a converted constant expression of the type of the |
| 4112 | // template-parameter; or |
| 4113 | llvm::APSInt Value; |
| 4114 | ExprResult ArgResult = |
| 4115 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 4116 | CCEK_TemplateArg); |
| 4117 | if (ArgResult.isInvalid()) |
| 4118 | return ExprError(); |
| 4119 | |
| 4120 | // Widen the argument value to sizeof(parameter type). This is almost |
| 4121 | // always a no-op, except when the parameter type is bool. In |
| 4122 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 4123 | QualType IntegerType = ParamType; |
| 4124 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 4125 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 4126 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 4127 | |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4128 | Converted = TemplateArgument(Context, Value, |
| 4129 | Context.getCanonicalType(ParamType)); |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4130 | return ArgResult; |
| 4131 | } |
| 4132 | |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4133 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 4134 | if (ArgResult.isInvalid()) |
| 4135 | return ExprError(); |
| 4136 | Arg = ArgResult.take(); |
| 4137 | |
| 4138 | QualType ArgType = Arg->getType(); |
| 4139 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4140 | // C++ [temp.arg.nontype]p1: |
| 4141 | // A template-argument for a non-type, non-template |
| 4142 | // template-parameter shall be one of: |
| 4143 | // |
| 4144 | // -- an integral constant-expression of integral or enumeration |
| 4145 | // type; or |
| 4146 | // -- the name of a non-type template-parameter; or |
| 4147 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4148 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4149 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4150 | Diag(Arg->getLocStart(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4151 | diag::err_template_arg_not_integral_or_enumeral) |
| 4152 | << ArgType << Arg->getSourceRange(); |
| 4153 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4154 | return ExprError(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4155 | } else if (!Arg->isValueDependent()) { |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 4156 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 4157 | QualType T; |
| 4158 | |
| 4159 | public: |
| 4160 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
| 4161 | |
| 4162 | virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 4163 | SourceRange SR) { |
| 4164 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 4165 | } |
| 4166 | } Diagnoser(ArgType); |
| 4167 | |
| 4168 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
| 4169 | false).take(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4170 | if (!Arg) |
| 4171 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4174 | // From here on out, all we care about are the unqualified forms |
| 4175 | // of the parameter and argument types. |
| 4176 | ParamType = ParamType.getUnqualifiedType(); |
| 4177 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4178 | |
| 4179 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 4180 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4181 | // Okay: no conversion necessary |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4182 | } else if (ParamType->isBooleanType()) { |
| 4183 | // This is an integral-to-boolean conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4184 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4185 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 4186 | !ParamType->isEnumeralType()) { |
| 4187 | // This is an integral promotion or conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4188 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4189 | } else { |
| 4190 | // We can't perform this conversion. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4191 | Diag(Arg->getLocStart(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4192 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4193 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4194 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4195 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4196 | } |
| 4197 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4198 | // Add the value of this argument to the list of converted |
| 4199 | // arguments. We use the bitwidth and signedness of the template |
| 4200 | // parameter. |
| 4201 | if (Arg->isValueDependent()) { |
| 4202 | // The argument is value-dependent. Create a new |
| 4203 | // TemplateArgument with the converted expression. |
| 4204 | Converted = TemplateArgument(Arg); |
| 4205 | return Owned(Arg); |
| 4206 | } |
| 4207 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4208 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4209 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 4210 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4211 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4212 | if (ParamType->isBooleanType()) { |
| 4213 | // Value must be zero or one. |
| 4214 | Value = Value != 0; |
| 4215 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 4216 | if (Value.getBitWidth() != AllowedBits) |
| 4217 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4218 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4219 | } else { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4220 | llvm::APSInt OldValue = Value; |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4221 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4222 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4223 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 4224 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 4225 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4226 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4227 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4228 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4229 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4230 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4231 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4232 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4233 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 4234 | << Arg->getSourceRange(); |
| 4235 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4236 | } |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4237 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4238 | // Complain if we overflowed the template parameter's type. |
| 4239 | unsigned RequiredBits; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4240 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4241 | RequiredBits = OldValue.getActiveBits(); |
| 4242 | else if (OldValue.isUnsigned()) |
| 4243 | RequiredBits = OldValue.getActiveBits() + 1; |
| 4244 | else |
| 4245 | RequiredBits = OldValue.getMinSignedBits(); |
| 4246 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4247 | Diag(Arg->getLocStart(), |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4248 | diag::warn_template_arg_too_large) |
| 4249 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 4250 | << Arg->getSourceRange(); |
| 4251 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4252 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4253 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4254 | |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4255 | Converted = TemplateArgument(Context, Value, |
Douglas Gregor | 6b63f55 | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 4256 | ParamType->isEnumeralType() |
| 4257 | ? Context.getCanonicalType(ParamType) |
| 4258 | : IntegerType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4259 | return Owned(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4260 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4261 | |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4262 | QualType ArgType = Arg->getType(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4263 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 4264 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4265 | // Handle pointer-to-function, reference-to-function, and |
| 4266 | // pointer-to-member-function all in (roughly) the same way. |
| 4267 | if (// -- For a non-type template-parameter of type pointer to |
| 4268 | // function, only the function-to-pointer conversion (4.3) is |
| 4269 | // applied. If the template-argument represents a set of |
| 4270 | // overloaded functions (or a pointer to such), the matching |
| 4271 | // function is selected from the set (13.4). |
| 4272 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4273 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4274 | // -- For a non-type template-parameter of type reference to |
| 4275 | // function, no conversions apply. If the template-argument |
| 4276 | // represents a set of overloaded functions, the matching |
| 4277 | // function is selected from the set (13.4). |
| 4278 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4279 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4280 | // -- For a non-type template-parameter of type pointer to |
| 4281 | // member function, no conversions apply. If the |
| 4282 | // template-argument represents a set of overloaded member |
| 4283 | // functions, the matching member function is selected from |
| 4284 | // the set (13.4). |
| 4285 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4286 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4287 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4288 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4289 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4290 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4291 | true, |
| 4292 | FoundResult)) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4293 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4294 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4295 | |
| 4296 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 4297 | ArgType = Arg->getType(); |
| 4298 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4299 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4300 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4301 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4302 | if (!ParamType->isMemberPointerType()) { |
| 4303 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4304 | ParamType, |
| 4305 | Arg, Converted)) |
| 4306 | return ExprError(); |
| 4307 | return Owned(Arg); |
| 4308 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4309 | |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4310 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 4311 | Converted)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4312 | return ExprError(); |
| 4313 | return Owned(Arg); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 4316 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4317 | // -- for a non-type template-parameter of type pointer to |
| 4318 | // object, qualification conversions (4.4) and the |
| 4319 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4320 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4321 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4322 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4323 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4324 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4325 | ParamType, |
| 4326 | Arg, Converted)) |
| 4327 | return ExprError(); |
| 4328 | return Owned(Arg); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4329 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4330 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4331 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4332 | // -- For a non-type template-parameter of type reference to |
| 4333 | // object, no conversions apply. The type referred to by the |
| 4334 | // reference may be more cv-qualified than the (otherwise |
| 4335 | // identical) type of the template-argument. The |
| 4336 | // template-parameter is bound directly to the |
| 4337 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4338 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4339 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4340 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4341 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4342 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 4343 | ParamRefType->getPointeeType(), |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4344 | true, |
| 4345 | FoundResult)) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4346 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4347 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4348 | |
| 4349 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 4350 | ArgType = Arg->getType(); |
| 4351 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4352 | return ExprError(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4353 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4354 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4355 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4356 | ParamType, |
| 4357 | Arg, Converted)) |
| 4358 | return ExprError(); |
| 4359 | return Owned(Arg); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4360 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4361 | |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4362 | // Deal with parameters of type std::nullptr_t. |
| 4363 | if (ParamType->isNullPtrType()) { |
| 4364 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 4365 | Converted = TemplateArgument(Arg); |
| 4366 | return Owned(Arg); |
| 4367 | } |
| 4368 | |
| 4369 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 4370 | case NPV_NotNullPointer: |
| 4371 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 4372 | << Arg->getType() << ParamType; |
| 4373 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4374 | return ExprError(); |
| 4375 | |
| 4376 | case NPV_Error: |
| 4377 | return ExprError(); |
| 4378 | |
| 4379 | case NPV_NullPointer: |
Richard Smith | 86e6fdc | 2012-04-26 01:51:03 +0000 | [diff] [blame] | 4380 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4381 | Converted = TemplateArgument((Decl *)0); |
| 4382 | return Owned(Arg);; |
| 4383 | } |
| 4384 | } |
| 4385 | |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4386 | // -- For a non-type template-parameter of type pointer to data |
| 4387 | // member, qualification conversions (4.4) are applied. |
| 4388 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 4389 | |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4390 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 4391 | Converted)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4392 | return ExprError(); |
| 4393 | return Owned(Arg); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4394 | } |
| 4395 | |
| 4396 | /// \brief Check a template argument against its corresponding |
| 4397 | /// template template parameter. |
| 4398 | /// |
| 4399 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 4400 | /// It returns true if an error occurred, and false otherwise. |
| 4401 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4402 | const TemplateArgumentLoc &Arg) { |
| 4403 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 4404 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 4405 | if (!Template) { |
| 4406 | // Any dependent template name is fine. |
| 4407 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 4408 | return false; |
| 4409 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4410 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4411 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4412 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4413 | // the name of a class template or an alias template, expressed as an |
| 4414 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4415 | // primary class templates are considered when matching the |
| 4416 | // template template argument with the corresponding parameter; |
| 4417 | // partial specializations are not considered even if their |
| 4418 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4419 | // |
| 4420 | // Note that we also allow template template parameters here, which |
| 4421 | // will happen when we are dealing with, e.g., class template |
| 4422 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4423 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4424 | !isa<TemplateTemplateParmDecl>(Template) && |
| 4425 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4426 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4427 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4428 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4429 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4430 | << Template; |
| 4431 | } |
| 4432 | |
| 4433 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 4434 | Param->getTemplateParameters(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4435 | true, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4436 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4437 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4438 | } |
| 4439 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4440 | /// \brief Given a non-type template argument that refers to a |
| 4441 | /// declaration and the type of its corresponding non-type template |
| 4442 | /// parameter, produce an expression that properly refers to that |
| 4443 | /// declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4444 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4445 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 4446 | QualType ParamType, |
| 4447 | SourceLocation Loc) { |
| 4448 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 4449 | "Only declaration template arguments permitted here"); |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4450 | |
| 4451 | // For a NULL non-type template argument, return nullptr casted to the |
| 4452 | // parameter's type. |
| 4453 | if (!Arg.getAsDecl()) { |
| 4454 | return ImpCastExprToType( |
| 4455 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 4456 | ParamType, |
| 4457 | ParamType->getAs<MemberPointerType>() |
| 4458 | ? CK_NullToMemberPointer |
| 4459 | : CK_NullToPointer); |
| 4460 | } |
| 4461 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4462 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 4463 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4464 | if (VD->getDeclContext()->isRecord() && |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4465 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 4466 | // If the value is a class member, we might have a pointer-to-member. |
| 4467 | // Determine whether the non-type template template parameter is of |
| 4468 | // pointer-to-member type. If so, we need to build an appropriate |
| 4469 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 4470 | // would refer to the member itself. |
| 4471 | if (ParamType->isMemberPointerType()) { |
| 4472 | QualType ClassType |
| 4473 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 4474 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4475 | = NestedNameSpecifier::Create(Context, 0, false, |
| 4476 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4477 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 4478 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4479 | |
| 4480 | // The actual value-ness of this is unimportant, but for |
| 4481 | // internal consistency's sake, references to instance methods |
| 4482 | // are r-values. |
| 4483 | ExprValueKind VK = VK_LValue; |
| 4484 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 4485 | VK = VK_RValue; |
| 4486 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4487 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4488 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4489 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4490 | Loc, |
| 4491 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4492 | if (RefExpr.isInvalid()) |
| 4493 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4494 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4495 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4496 | |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4497 | // We might need to perform a trailing qualification conversion, since |
| 4498 | // the element type on the parameter could be more qualified than the |
| 4499 | // element type in the expression we constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4500 | bool ObjCLifetimeConversion; |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4501 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4502 | ParamType.getUnqualifiedType(), false, |
| 4503 | ObjCLifetimeConversion)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4504 | RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4505 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4506 | assert(!RefExpr.isInvalid() && |
| 4507 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4508 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4509 | return move(RefExpr); |
| 4510 | } |
| 4511 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4512 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4513 | QualType T = VD->getType().getNonReferenceType(); |
| 4514 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4515 | // When the non-type template parameter is a pointer, take the |
| 4516 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4517 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4518 | if (RefExpr.isInvalid()) |
| 4519 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4520 | |
| 4521 | if (T->isFunctionType() || T->isArrayType()) { |
| 4522 | // Decay functions and arrays. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4523 | RefExpr = DefaultFunctionArrayConversion(RefExpr.take()); |
| 4524 | if (RefExpr.isInvalid()) |
| 4525 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4526 | |
| 4527 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4528 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4529 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4530 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4531 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4532 | } |
| 4533 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4534 | ExprValueKind VK = VK_RValue; |
| 4535 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4536 | // If the non-type template parameter has reference type, qualify the |
| 4537 | // resulting declaration reference with the extra qualifiers on the |
| 4538 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4539 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 4540 | VK = VK_LValue; |
| 4541 | T = Context.getQualifiedType(T, |
| 4542 | TargetRef->getPointeeType().getQualifiers()); |
| 4543 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4544 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4545 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | /// \brief Construct a new expression that refers to the given |
| 4549 | /// integral template argument with the given source-location |
| 4550 | /// information. |
| 4551 | /// |
| 4552 | /// This routine takes care of the mapping from an integral template |
| 4553 | /// argument (which may have any integral type) to the appropriate |
| 4554 | /// literal value. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4555 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4556 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 4557 | SourceLocation Loc) { |
| 4558 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4559 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4560 | QualType T = Arg.getIntegralType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4561 | if (T->isAnyCharacterType()) { |
| 4562 | CharacterLiteral::CharacterKind Kind; |
| 4563 | if (T->isWideCharType()) |
| 4564 | Kind = CharacterLiteral::Wide; |
| 4565 | else if (T->isChar16Type()) |
| 4566 | Kind = CharacterLiteral::UTF16; |
| 4567 | else if (T->isChar32Type()) |
| 4568 | Kind = CharacterLiteral::UTF32; |
| 4569 | else |
| 4570 | Kind = CharacterLiteral::Ascii; |
| 4571 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4572 | return Owned(new (Context) CharacterLiteral( |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4573 | Arg.getAsIntegral().getZExtValue(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4574 | Kind, T, Loc)); |
| 4575 | } |
| 4576 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4577 | if (T->isBooleanType()) |
| 4578 | return Owned(new (Context) CXXBoolLiteralExpr( |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4579 | Arg.getAsIntegral().getBoolValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4580 | T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4581 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4582 | if (T->isNullPtrType()) |
| 4583 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 4584 | |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4585 | // If this is an enum type that we're instantiating, we need to use an integer |
| 4586 | // type the same size as the enumerator. We don't want to build an |
| 4587 | // IntegerLiteral with enum type. |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4588 | QualType BT; |
| 4589 | if (const EnumType *ET = T->getAs<EnumType>()) |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4590 | BT = ET->getDecl()->getIntegerType(); |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4591 | else |
| 4592 | BT = T; |
| 4593 | |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4594 | Expr *E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), BT, Loc); |
John McCall | 4e9272d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 4595 | if (T->isEnumeralType()) { |
| 4596 | // FIXME: This is a hack. We need a better way to handle substituted |
| 4597 | // non-type template parameters. |
| 4598 | E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0, |
| 4599 | Context.getTrivialTypeSourceInfo(T, Loc), |
| 4600 | Loc, Loc); |
| 4601 | } |
| 4602 | |
| 4603 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4604 | } |
| 4605 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4606 | /// \brief Match two template parameters within template parameter lists. |
| 4607 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 4608 | bool Complain, |
| 4609 | Sema::TemplateParameterListEqualKind Kind, |
| 4610 | SourceLocation TemplateArgLoc) { |
| 4611 | // Check the actual kind (type, non-type, template). |
| 4612 | if (Old->getKind() != New->getKind()) { |
| 4613 | if (Complain) { |
| 4614 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 4615 | if (TemplateArgLoc.isValid()) { |
| 4616 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4617 | NextDiag = diag::note_template_param_different_kind; |
| 4618 | } |
| 4619 | S.Diag(New->getLocation(), NextDiag) |
| 4620 | << (Kind != Sema::TPL_TemplateMatch); |
| 4621 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 4622 | << (Kind != Sema::TPL_TemplateMatch); |
| 4623 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4624 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4625 | return false; |
| 4626 | } |
| 4627 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4628 | // Check that both are parameter packs are neither are parameter packs. |
| 4629 | // However, if we are matching a template template argument to a |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4630 | // template template parameter, the template template parameter can have |
| 4631 | // a parameter pack where the template template argument does not. |
| 4632 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 4633 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4634 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4635 | if (Complain) { |
| 4636 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 4637 | if (TemplateArgLoc.isValid()) { |
| 4638 | S.Diag(TemplateArgLoc, |
| 4639 | diag::err_template_arg_template_params_mismatch); |
| 4640 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 4641 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4642 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4643 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 4644 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 4645 | : 2; |
| 4646 | S.Diag(New->getLocation(), NextDiag) |
| 4647 | << ParamKind << New->isParameterPack(); |
| 4648 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 4649 | << ParamKind << Old->isParameterPack(); |
| 4650 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4651 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4652 | return false; |
| 4653 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4654 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4655 | // For non-type template parameters, check the type of the parameter. |
| 4656 | if (NonTypeTemplateParmDecl *OldNTTP |
| 4657 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 4658 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4659 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4660 | // If we are matching a template template argument to a template |
| 4661 | // template parameter and one of the non-type template parameter types |
| 4662 | // is dependent, then we must wait until template instantiation time |
| 4663 | // to actually compare the arguments. |
| 4664 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4665 | (OldNTTP->getType()->isDependentType() || |
| 4666 | NewNTTP->getType()->isDependentType())) |
| 4667 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4668 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4669 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 4670 | if (Complain) { |
| 4671 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 4672 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4673 | S.Diag(TemplateArgLoc, |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4674 | diag::err_template_arg_template_params_mismatch); |
| 4675 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 4676 | } |
| 4677 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 4678 | << NewNTTP->getType() |
| 4679 | << (Kind != Sema::TPL_TemplateMatch); |
| 4680 | S.Diag(OldNTTP->getLocation(), |
| 4681 | diag::note_template_nontype_parm_prev_declaration) |
| 4682 | << OldNTTP->getType(); |
| 4683 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4684 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4685 | return false; |
| 4686 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4687 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4688 | return true; |
| 4689 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4690 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4691 | // For template template parameters, check the template parameter types. |
| 4692 | // The template parameter lists of template template |
| 4693 | // parameters must agree. |
| 4694 | if (TemplateTemplateParmDecl *OldTTP |
| 4695 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4696 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4697 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 4698 | OldTTP->getTemplateParameters(), |
| 4699 | Complain, |
| 4700 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4701 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4702 | : Kind), |
| 4703 | TemplateArgLoc); |
| 4704 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4705 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4706 | return true; |
| 4707 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4708 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4709 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 4710 | /// lists. |
| 4711 | static |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4712 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4713 | TemplateParameterList *New, |
| 4714 | TemplateParameterList *Old, |
| 4715 | Sema::TemplateParameterListEqualKind Kind, |
| 4716 | SourceLocation TemplateArgLoc) { |
| 4717 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 4718 | if (TemplateArgLoc.isValid()) { |
| 4719 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4720 | NextDiag = diag::note_template_param_list_different_arity; |
| 4721 | } |
| 4722 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 4723 | << (New->size() > Old->size()) |
| 4724 | << (Kind != Sema::TPL_TemplateMatch) |
| 4725 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 4726 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 4727 | << (Kind != Sema::TPL_TemplateMatch) |
| 4728 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 4729 | } |
| 4730 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4731 | /// \brief Determine whether the given template parameter lists are |
| 4732 | /// equivalent. |
| 4733 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4734 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4735 | /// source code as part of a new template declaration. |
| 4736 | /// |
| 4737 | /// \param Old The old template parameter list, typically found via |
| 4738 | /// name lookup of the template declared with this template parameter |
| 4739 | /// list. |
| 4740 | /// |
| 4741 | /// \param Complain If true, this routine will produce a diagnostic if |
| 4742 | /// the template parameter lists are not equivalent. |
| 4743 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4744 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4745 | /// |
| 4746 | /// \param TemplateArgLoc If this source location is valid, then we |
| 4747 | /// are actually checking the template parameter list of a template |
| 4748 | /// argument (New) against the template parameter list of its |
| 4749 | /// corresponding template template parameter (Old). We produce |
| 4750 | /// slightly different diagnostics in this scenario. |
| 4751 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4752 | /// \returns True if the template parameter lists are equal, false |
| 4753 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4754 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4755 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 4756 | TemplateParameterList *Old, |
| 4757 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4758 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4759 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4760 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 4761 | if (Complain) |
| 4762 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4763 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4764 | |
| 4765 | return false; |
| 4766 | } |
| 4767 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4768 | // C++0x [temp.arg.template]p3: |
| 4769 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4770 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4771 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4772 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4773 | // template-parameter-list of P. [...] |
| 4774 | TemplateParameterList::iterator NewParm = New->begin(); |
| 4775 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4776 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4777 | OldParmEnd = Old->end(); |
| 4778 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 4779 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 4780 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4781 | if (NewParm == NewParmEnd) { |
| 4782 | if (Complain) |
| 4783 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4784 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4785 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4786 | return false; |
| 4787 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4788 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4789 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4790 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4791 | return false; |
| 4792 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4793 | ++NewParm; |
| 4794 | continue; |
| 4795 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4796 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4797 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4798 | // [...] When P's template- parameter-list contains a template parameter |
| 4799 | // pack (14.5.3), the template parameter pack will match zero or more |
| 4800 | // template parameters or template parameter packs in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4801 | // template-parameter-list of A with the same type and form as the |
| 4802 | // template parameter pack in P (ignoring whether those template |
| 4803 | // parameters are template parameter packs). |
| 4804 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 4805 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4806 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4807 | return false; |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4808 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4809 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4810 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4811 | // Make sure we exhausted all of the arguments. |
| 4812 | if (NewParm != NewParmEnd) { |
| 4813 | if (Complain) |
| 4814 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4815 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4816 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4817 | return false; |
| 4818 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4819 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4820 | return true; |
| 4821 | } |
| 4822 | |
| 4823 | /// \brief Check whether a template can be declared within this scope. |
| 4824 | /// |
| 4825 | /// If the template declaration is valid in this scope, returns |
| 4826 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4827 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4828 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 4829 | if (!S) |
| 4830 | return false; |
| 4831 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4832 | // Find the nearest enclosing declaration scope. |
| 4833 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4834 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4835 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4836 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4837 | // C++ [temp]p2: |
| 4838 | // A template-declaration can appear only as a namespace scope or |
| 4839 | // class scope declaration. |
| 4840 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4841 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 4842 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4843 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4844 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4845 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4846 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4847 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4848 | |
| 4849 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 4850 | return false; |
| 4851 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4852 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4853 | diag::err_template_outside_namespace_or_class_scope) |
| 4854 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4855 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4856 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4857 | /// \brief Determine what kind of template specialization the given declaration |
| 4858 | /// is. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 4859 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4860 | if (!D) |
| 4861 | return TSK_Undeclared; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4862 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4863 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 4864 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4865 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 4866 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4867 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 4868 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4869 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4870 | return TSK_Undeclared; |
| 4871 | } |
| 4872 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4873 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4874 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4875 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4876 | /// This routine determines whether a template specialization can be declared |
| 4877 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4878 | /// |
| 4879 | /// \param S the semantic analysis object for which this check is being |
| 4880 | /// performed. |
| 4881 | /// |
| 4882 | /// \param Specialized the entity being specialized or instantiated, which |
| 4883 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4884 | /// a member of a class template (member function, static data member, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4885 | /// member class). |
| 4886 | /// |
| 4887 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 4888 | /// |
| 4889 | /// \param Loc the location of the explicit specialization or instantiation of |
| 4890 | /// this entity. |
| 4891 | /// |
| 4892 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 4893 | /// a class template. |
| 4894 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4895 | /// \returns true if there was an error that we cannot recover from, false |
| 4896 | /// otherwise. |
| 4897 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 4898 | NamedDecl *Specialized, |
| 4899 | NamedDecl *PrevDecl, |
| 4900 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4901 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4902 | // Keep these "kind" numbers in sync with the %select statements in the |
| 4903 | // various diagnostics emitted by this routine. |
| 4904 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4905 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4906 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4907 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4908 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4909 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4910 | EntityKind = 3; |
| 4911 | else if (isa<VarDecl>(Specialized)) |
| 4912 | EntityKind = 4; |
| 4913 | else if (isa<RecordDecl>(Specialized)) |
| 4914 | EntityKind = 5; |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4915 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus0x) |
| 4916 | EntityKind = 6; |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4917 | else { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4918 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
| 4919 | << S.getLangOpts().CPlusPlus0x; |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4920 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4921 | return true; |
| 4922 | } |
| 4923 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4924 | // C++ [temp.expl.spec]p2: |
| 4925 | // An explicit specialization shall be declared in the namespace |
| 4926 | // of which the template is a member, or, for member templates, in |
| 4927 | // the namespace of which the enclosing class or enclosing class |
| 4928 | // template is a member. An explicit specialization of a member |
| 4929 | // function, member class or static data member of a class |
| 4930 | // template shall be declared in the namespace of which the class |
| 4931 | // template is a member. Such a declaration may also be a |
| 4932 | // definition. If the declaration is not a definition, the |
| 4933 | // specialization may be defined later in the name- space in which |
| 4934 | // the explicit specialization was declared, or in a namespace |
| 4935 | // that encloses the one in which the explicit specialization was |
| 4936 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4937 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4938 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4939 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4940 | return true; |
| 4941 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4942 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4943 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4944 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4945 | // Do not warn for class scope explicit specialization during |
| 4946 | // instantiation, warning was already emitted during pattern |
| 4947 | // semantic analysis. |
| 4948 | if (!S.ActiveTemplateInstantiations.size()) |
| 4949 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 4950 | << Specialized; |
| 4951 | } else { |
| 4952 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4953 | << Specialized; |
| 4954 | return true; |
| 4955 | } |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4956 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4957 | |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4958 | if (S.CurContext->isRecord() && |
| 4959 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 4960 | // Make sure that we're specializing in the right record context. |
| 4961 | // Otherwise, things can go horribly wrong. |
| 4962 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4963 | << Specialized; |
| 4964 | return true; |
| 4965 | } |
| 4966 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4967 | // C++ [temp.class.spec]p6: |
| 4968 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4969 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4970 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4971 | bool ComplainedAboutScope = false; |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4972 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4973 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4974 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4975 | if ((!PrevDecl || |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4976 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4977 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4978 | // C++ [temp.exp.spec]p2: |
| 4979 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4980 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4981 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4982 | // An explicit specialization of a member function, member class or |
| 4983 | // static data member of a class template shall be declared in the |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4984 | // namespace of which the class template is a member. |
| 4985 | // |
| 4986 | // C++0x [temp.expl.spec]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4987 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4988 | // the specialized template. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4989 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
| 4990 | bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext); |
| 4991 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
| 4992 | assert(!IsCPlusPlus0xExtension && |
| 4993 | "DC encloses TU but isn't in enclosing namespace set"); |
| 4994 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4995 | << EntityKind << Specialized; |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4996 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 4997 | int Diag; |
| 4998 | if (!IsCPlusPlus0xExtension) |
| 4999 | Diag = diag::err_template_spec_decl_out_of_scope; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5000 | else if (!S.getLangOpts().CPlusPlus0x) |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5001 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 5002 | else |
| 5003 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 5004 | S.Diag(Loc, Diag) |
| 5005 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 5006 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5007 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5008 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5009 | ComplainedAboutScope = |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5010 | !(IsCPlusPlus0xExtension && S.getLangOpts().CPlusPlus0x); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5011 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5012 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5013 | |
| 5014 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5015 | // namespace. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5016 | // Note that HandleDeclarator() performs this check for explicit |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5017 | // specializations of function templates, static data members, and member |
| 5018 | // functions, so we skip the check here for those kinds of entities. |
| 5019 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 5020 | // Should we refactor that check, so that it occurs later? |
| 5021 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5022 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 5023 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5024 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 5025 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 5026 | << EntityKind << Specialized; |
| 5027 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 5028 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 5029 | << EntityKind << Specialized |
| 5030 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5031 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5032 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5033 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5034 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5035 | // FIXME: check for specialization-after-instantiation errors and such. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5036 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5037 | return false; |
| 5038 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5039 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5040 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 5041 | /// that checks non-type template partial specialization arguments. |
| 5042 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 5043 | NonTypeTemplateParmDecl *Param, |
| 5044 | const TemplateArgument *Args, |
| 5045 | unsigned NumArgs) { |
| 5046 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 5047 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 5048 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5049 | Args[I].pack_begin(), |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5050 | Args[I].pack_size())) |
| 5051 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5052 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5053 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5054 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5055 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5056 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 5057 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5058 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 5059 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5060 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 5061 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5062 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 5063 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 5064 | |
| 5065 | // Strip off any implicit casts we added as part of type checking. |
| 5066 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 5067 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5068 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5069 | // C++ [temp.class.spec]p8: |
| 5070 | // A non-type argument is non-specialized if it is the name of a |
| 5071 | // non-type parameter. All other non-type arguments are |
| 5072 | // specialized. |
| 5073 | // |
| 5074 | // Below, we check the two conditions that only apply to |
| 5075 | // specialized non-type arguments, so skip any non-specialized |
| 5076 | // arguments. |
| 5077 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 5078 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5079 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5080 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5081 | // C++ [temp.class.spec]p9: |
| 5082 | // Within the argument list of a class template partial |
| 5083 | // specialization, the following restrictions apply: |
| 5084 | // -- A partially specialized non-type argument expression |
| 5085 | // shall not involve a template parameter of the partial |
| 5086 | // specialization except when the argument expression is a |
| 5087 | // simple identifier. |
| 5088 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5089 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5090 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 5091 | << ArgExpr->getSourceRange(); |
| 5092 | return true; |
| 5093 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5094 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5095 | // -- The type of a template parameter corresponding to a |
| 5096 | // specialized non-type argument shall not be dependent on a |
| 5097 | // parameter of the specialization. |
| 5098 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5099 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5100 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 5101 | << Param->getType() |
| 5102 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5103 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5104 | return true; |
| 5105 | } |
| 5106 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5107 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5108 | return false; |
| 5109 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5110 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5111 | /// \brief Check the non-type template arguments of a class template |
| 5112 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 5113 | /// |
| 5114 | /// \param TemplateParams the template parameters of the primary class |
| 5115 | /// template. |
| 5116 | /// |
| 5117 | /// \param TemplateArg the template arguments of the class template |
| 5118 | /// partial specialization. |
| 5119 | /// |
| 5120 | /// \returns true if there was an error, false otherwise. |
| 5121 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 5122 | TemplateParameterList *TemplateParams, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5123 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5124 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 5125 | |
| 5126 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 5127 | NonTypeTemplateParmDecl *Param |
| 5128 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 5129 | if (!Param) |
| 5130 | continue; |
| 5131 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5132 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5133 | &ArgList[I], 1)) |
| 5134 | return true; |
| 5135 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5136 | |
| 5137 | return false; |
| 5138 | } |
| 5139 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5140 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5141 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 5142 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5143 | SourceLocation KWLoc, |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5144 | SourceLocation ModulePrivateLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5145 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5146 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5147 | SourceLocation TemplateNameLoc, |
| 5148 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5149 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5150 | SourceLocation RAngleLoc, |
| 5151 | AttributeList *Attr, |
| 5152 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5153 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 5154 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5155 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 5156 | // store the location of the outermost template keyword in the declaration. |
| 5157 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
| 5158 | ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation(); |
| 5159 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5160 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5161 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5162 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 5163 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5164 | |
| 5165 | if (!ClassTemplate) { |
| 5166 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5167 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 5168 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 5169 | return true; |
| 5170 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5171 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5172 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5173 | bool isPartialSpecialization = false; |
| 5174 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5175 | // Check the validity of the template headers that introduce this |
| 5176 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5177 | // FIXME: We probably shouldn't complain about these headers for |
| 5178 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 5179 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5180 | TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 5181 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, |
| 5182 | TemplateNameLoc, |
| 5183 | SS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5184 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5185 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5186 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 5187 | isExplicitSpecialization, |
| 5188 | Invalid); |
| 5189 | if (Invalid) |
| 5190 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5191 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5192 | if (TemplateParams && TemplateParams->size() > 0) { |
| 5193 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5194 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 5195 | if (TUK == TUK_Friend) { |
| 5196 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 5197 | << SourceRange(LAngleLoc, RAngleLoc); |
| 5198 | return true; |
| 5199 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5200 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5201 | // C++ [temp.class.spec]p10: |
| 5202 | // The template parameter list of a specialization shall not |
| 5203 | // contain default template argument values. |
| 5204 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 5205 | Decl *Param = TemplateParams->getParam(I); |
| 5206 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 5207 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5208 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5209 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5210 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5211 | } |
| 5212 | } else if (NonTypeTemplateParmDecl *NTTP |
| 5213 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 5214 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5215 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5216 | diag::err_default_arg_in_partial_spec) |
| 5217 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 5218 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5219 | } |
| 5220 | } else { |
| 5221 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5222 | if (TTP->hasDefaultArgument()) { |
| 5223 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5224 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5225 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 5226 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5227 | } |
| 5228 | } |
| 5229 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 5230 | } else if (TemplateParams) { |
| 5231 | if (TUK == TUK_Friend) |
| 5232 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5233 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 5234 | SourceRange(TemplateParams->getTemplateLoc(), |
| 5235 | TemplateParams->getRAngleLoc())) |
| 5236 | << SourceRange(LAngleLoc, RAngleLoc); |
| 5237 | else |
| 5238 | isExplicitSpecialization = true; |
| 5239 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5240 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5241 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5242 | isExplicitSpecialization = true; |
| 5243 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5244 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5245 | // Check that the specialization uses the same tag kind as the |
| 5246 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5247 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5248 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5249 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 5250 | Kind, TUK == TUK_Definition, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5251 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5252 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5253 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5254 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5255 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5256 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5257 | diag::note_previous_use); |
| 5258 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5259 | } |
| 5260 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5261 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5262 | TemplateArgumentListInfo TemplateArgs; |
| 5263 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 5264 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5265 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5266 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 5267 | // Check for unexpanded parameter packs in any of the template arguments. |
| 5268 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5269 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 5270 | UPPC_PartialSpecialization)) |
| 5271 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5272 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5273 | // Check that the template argument list is well-formed for this |
| 5274 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5275 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5276 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5277 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5278 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5279 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5280 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5281 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5282 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5283 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5284 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5285 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5286 | return true; |
| 5287 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 5288 | bool InstantiationDependent; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5289 | if (!Name.isDependent() && |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5290 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5291 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 5292 | TemplateArgs.size(), |
| 5293 | InstantiationDependent)) { |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5294 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 5295 | << ClassTemplate->getDeclName(); |
| 5296 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5297 | } |
| 5298 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5299 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5300 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5301 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 5302 | |
| 5303 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5304 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5305 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5306 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 5307 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5308 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5309 | else |
| 5310 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5311 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5312 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5313 | |
| 5314 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5315 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5316 | // Check whether we can declare a class template specialization in |
| 5317 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5318 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5319 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 5320 | TemplateNameLoc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5321 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5322 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5323 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5324 | // The canonical type |
| 5325 | QualType CanonType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5326 | if (PrevDecl && |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5327 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5328 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5329 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5330 | // arguments was referenced but not declared, or we're only |
| 5331 | // referencing this specialization as a friend, reuse that |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5332 | // declaration node as our own, updating its source location and |
| 5333 | // the list of outer template parameters to reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5334 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5335 | Specialization->setLocation(TemplateNameLoc); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5336 | if (TemplateParameterLists.size() > 0) { |
| 5337 | Specialization->setTemplateParameterListsInfo(Context, |
| 5338 | TemplateParameterLists.size(), |
| 5339 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5340 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5341 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5342 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5343 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5344 | // Build the canonical type that describes the converted template |
| 5345 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5346 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 5347 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5348 | Converted.data(), |
| 5349 | Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5350 | |
| 5351 | if (Context.hasSameType(CanonType, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5352 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 5353 | // C++ [temp.class.spec]p9b3: |
| 5354 | // |
| 5355 | // -- The argument list of the specialization shall not be identical |
| 5356 | // to the implicit argument list of the primary template. |
| 5357 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 5358 | << (TUK == TUK_Definition) |
| 5359 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5360 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 5361 | ClassTemplate->getIdentifier(), |
| 5362 | TemplateNameLoc, |
| 5363 | Attr, |
| 5364 | TemplateParams, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 5365 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5366 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 5367 | (TemplateParameterList**) TemplateParameterLists.release()); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5368 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5369 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5370 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5371 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 5372 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5373 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5374 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5375 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5376 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5377 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5378 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 5379 | TemplateParams, |
| 5380 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5381 | Converted.data(), |
| 5382 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5383 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5384 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5385 | PrevPartial, |
| 5386 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5387 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5388 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5389 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5390 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5391 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5392 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5393 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5394 | if (!PrevPartial) |
| 5395 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5396 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5397 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5398 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5399 | // template specialization, make a note of that. |
| 5400 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 5401 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5402 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5403 | // Check that all of the template parameters of the class template |
| 5404 | // partial specialization are deducible from the template |
| 5405 | // arguments. If not, this class template partial specialization |
| 5406 | // will never be used. |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5407 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5408 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5409 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5410 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5411 | |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5412 | if (!DeducibleParams.all()) { |
| 5413 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5414 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 5415 | << (NumNonDeducible > 1) |
| 5416 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 5417 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 5418 | if (!DeducibleParams[I]) { |
| 5419 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 5420 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5421 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5422 | diag::note_partial_spec_unused_parameter) |
| 5423 | << Param->getDeclName(); |
| 5424 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5425 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5426 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 5427 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5428 | } |
| 5429 | } |
| 5430 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5431 | } else { |
| 5432 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5433 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5434 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5435 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5436 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5437 | KWLoc, TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5438 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5439 | Converted.data(), |
| 5440 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5441 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5442 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5443 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5444 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5445 | TemplateParameterLists.size(), |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5446 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5447 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5448 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5449 | if (!PrevDecl) |
| 5450 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5451 | |
| 5452 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5453 | } |
| 5454 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5455 | // C++ [temp.expl.spec]p6: |
| 5456 | // If a template, a member template or the member of a class template is |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5457 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5458 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5459 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5460 | // use occurs; no diagnostic is required. |
| 5461 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5462 | bool Okay = false; |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5463 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5464 | // Is there any previous explicit specialization declaration? |
| 5465 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5466 | Okay = true; |
| 5467 | break; |
| 5468 | } |
| 5469 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5470 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5471 | if (!Okay) { |
| 5472 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 5473 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 5474 | << Context.getTypeDeclType(Specialization) << Range; |
| 5475 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5476 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5477 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5478 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5479 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5480 | return true; |
| 5481 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5482 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5483 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5484 | // If this is not a friend, note that this is an explicit specialization. |
| 5485 | if (TUK != TUK_Friend) |
| 5486 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5487 | |
| 5488 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5489 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5490 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5491 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5492 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5493 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5494 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 5495 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5496 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5497 | } |
| 5498 | } |
| 5499 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 5500 | if (Attr) |
| 5501 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 5502 | |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5503 | if (ModulePrivateLoc.isValid()) |
| 5504 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 5505 | << (isPartialSpecialization? 1 : 0) |
| 5506 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 5507 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 5508 | // Build the fully-sugared type for this class template |
| 5509 | // specialization as the user wrote in the specialization |
| 5510 | // itself. This means that we'll pretty-print the type retrieved |
| 5511 | // from the specialization's declaration the way that the user |
| 5512 | // actually wrote the specialization, rather than formatting the |
| 5513 | // name based on the "canonical" representation used to store the |
| 5514 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5515 | TypeSourceInfo *WrittenTy |
| 5516 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5517 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5518 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5519 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5520 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5521 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5522 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5523 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5524 | // C++ [temp.expl.spec]p9: |
| 5525 | // A template explicit specialization is in the scope of the |
| 5526 | // namespace in which the template was defined. |
| 5527 | // |
| 5528 | // We actually implement this paragraph where we set the semantic |
| 5529 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 5530 | // but we also maintain the lexical context where the actual |
| 5531 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5532 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5533 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5534 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5535 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5536 | Specialization->startDefinition(); |
| 5537 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5538 | if (TUK == TUK_Friend) { |
| 5539 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 5540 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5541 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5542 | /*FIXME:*/KWLoc); |
| 5543 | Friend->setAccess(AS_public); |
| 5544 | CurContext->addDecl(Friend); |
| 5545 | } else { |
| 5546 | // Add the specialization into its lexical context, so that it can |
| 5547 | // be seen when iterating through the list of declarations in that |
| 5548 | // context. However, specializations are not found by name lookup. |
| 5549 | CurContext->addDecl(Specialization); |
| 5550 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5551 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5552 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5553 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5554 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5555 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5556 | Declarator &D) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5557 | return HandleDeclarator(S, D, move(TemplateParameterLists)); |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5558 | } |
| 5559 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5560 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5561 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5562 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5563 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5564 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5565 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5566 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5567 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5568 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5569 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5570 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5571 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 5572 | D.setFunctionDefinitionKind(FDK_Definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5573 | Decl *DP = HandleDeclarator(ParentScope, D, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5574 | move(TemplateParameterLists)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5575 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5576 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5577 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5578 | FunctionTemplate->getTemplatedDecl()); |
| 5579 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 5580 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 5581 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5582 | } |
| 5583 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5584 | /// \brief Strips various properties off an implicit instantiation |
| 5585 | /// that has just been explicitly specialized. |
| 5586 | static void StripImplicitInstantiation(NamedDecl *D) { |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 5587 | // FIXME: "make check" is clean if the call to dropAttrs() is commented out. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5588 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5589 | |
| 5590 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5591 | FD->setInlineSpecified(false); |
| 5592 | } |
| 5593 | } |
| 5594 | |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5595 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 5596 | // declaration or definition. |
| 5597 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5598 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5599 | // Explicit instantiations following a specialization have no effect and |
| 5600 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 5601 | // until a valid name loc is found. |
| 5602 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5603 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 5604 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5605 | PrevDiagLoc = Prev->getLocation(); |
| 5606 | } |
| 5607 | assert(PrevDiagLoc.isValid() && |
| 5608 | "Explicit instantiation without point of instantiation?"); |
| 5609 | return PrevDiagLoc; |
| 5610 | } |
| 5611 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5612 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5613 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5614 | /// for those cases where they are required and determining whether the |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5615 | /// new specialization/instantiation will have any effect. |
| 5616 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5617 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5618 | /// instantiation. |
| 5619 | /// |
| 5620 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 5621 | /// |
| 5622 | /// \param PrevDecl the previous declaration of the entity. |
| 5623 | /// |
| 5624 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 5625 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5626 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5627 | /// declaration was instantiated (either implicitly or explicitly). |
| 5628 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5629 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5630 | /// specialization or instantiation has no effect and should be ignored. |
| 5631 | /// |
| 5632 | /// \returns true if there was an error that should prevent the introduction of |
| 5633 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5634 | bool |
| 5635 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 5636 | TemplateSpecializationKind NewTSK, |
| 5637 | NamedDecl *PrevDecl, |
| 5638 | TemplateSpecializationKind PrevTSK, |
| 5639 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5640 | bool &HasNoEffect) { |
| 5641 | HasNoEffect = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5642 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5643 | switch (NewTSK) { |
| 5644 | case TSK_Undeclared: |
| 5645 | case TSK_ImplicitInstantiation: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5646 | llvm_unreachable("Don't check implicit instantiations here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5647 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5648 | case TSK_ExplicitSpecialization: |
| 5649 | switch (PrevTSK) { |
| 5650 | case TSK_Undeclared: |
| 5651 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5652 | // Okay, we're just specializing something that is either already |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5653 | // explicitly specialized or has merely been mentioned without any |
| 5654 | // instantiation. |
| 5655 | return false; |
| 5656 | |
| 5657 | case TSK_ImplicitInstantiation: |
| 5658 | if (PrevPointOfInstantiation.isInvalid()) { |
| 5659 | // The declaration itself has not actually been instantiated, so it is |
| 5660 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5661 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5662 | return false; |
| 5663 | } |
| 5664 | // Fall through |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5665 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5666 | case TSK_ExplicitInstantiationDeclaration: |
| 5667 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5668 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 5669 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5670 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5671 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5672 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5673 | // If a template, a member template or the member of a class template |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5674 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5675 | // before the first use of that specialization that would cause an |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5676 | // implicit instantiation to take place, in every translation unit in |
| 5677 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5678 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5679 | // Is there any previous explicit specialization declaration? |
| 5680 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 5681 | return false; |
| 5682 | } |
| 5683 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5684 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5685 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5686 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5687 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5688 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5689 | return true; |
| 5690 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5691 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5692 | case TSK_ExplicitInstantiationDeclaration: |
| 5693 | switch (PrevTSK) { |
| 5694 | case TSK_ExplicitInstantiationDeclaration: |
| 5695 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5696 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5697 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5698 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5699 | case TSK_Undeclared: |
| 5700 | case TSK_ImplicitInstantiation: |
| 5701 | // We're explicitly instantiating something that may have already been |
| 5702 | // implicitly instantiated; that's fine. |
| 5703 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5704 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5705 | case TSK_ExplicitSpecialization: |
| 5706 | // C++0x [temp.explicit]p4: |
| 5707 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5708 | // of a template appears after a declaration of an explicit |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5709 | // specialization for that template, the explicit instantiation has no |
| 5710 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5711 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5712 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5713 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5714 | case TSK_ExplicitInstantiationDefinition: |
| 5715 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5716 | // If an entity is the subject of both an explicit instantiation |
| 5717 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5718 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5719 | Diag(NewLoc, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5720 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5721 | |
| 5722 | // Explicit instantiations following a specialization have no effect and |
| 5723 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 5724 | // until a valid name loc is found. |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5725 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 5726 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5727 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5728 | return false; |
| 5729 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5730 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5731 | case TSK_ExplicitInstantiationDefinition: |
| 5732 | switch (PrevTSK) { |
| 5733 | case TSK_Undeclared: |
| 5734 | case TSK_ImplicitInstantiation: |
| 5735 | // We're explicitly instantiating something that may have already been |
| 5736 | // implicitly instantiated; that's fine. |
| 5737 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5738 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5739 | case TSK_ExplicitSpecialization: |
| 5740 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 5741 | // For a given set of template parameters, if an explicit |
| 5742 | // instantiation of a template appears after a declaration of |
| 5743 | // an explicit specialization for that template, the explicit |
| 5744 | // instantiation has no effect. |
| 5745 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5746 | // In C++98/03 mode, we only give an extension warning here, because it |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 5747 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5748 | // has been explicitly specialized. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5749 | Diag(NewLoc, getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5750 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 5751 | diag::ext_explicit_instantiation_after_specialization) |
| 5752 | << PrevDecl; |
| 5753 | Diag(PrevDecl->getLocation(), |
| 5754 | diag::note_previous_template_specialization); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5755 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5756 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5757 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5758 | case TSK_ExplicitInstantiationDeclaration: |
| 5759 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5760 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5761 | |
| 5762 | // C++0x [temp.explicit]p4: |
| 5763 | // For a given set of template parameters, if an explicit instantiation |
| 5764 | // of a template appears after a declaration of an explicit |
| 5765 | // specialization for that template, the explicit instantiation has no |
| 5766 | // effect. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5767 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5768 | // Is there any previous explicit specialization declaration? |
| 5769 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5770 | HasNoEffect = true; |
| 5771 | break; |
| 5772 | } |
| 5773 | } |
| 5774 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5775 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5776 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5777 | case TSK_ExplicitInstantiationDefinition: |
| 5778 | // C++0x [temp.spec]p5: |
| 5779 | // For a given template and a given set of template-arguments, |
| 5780 | // - an explicit instantiation definition shall appear at most once |
| 5781 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5782 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5783 | << PrevDecl; |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5784 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5785 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5786 | HasNoEffect = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5787 | return false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5788 | } |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5789 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5790 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5791 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5792 | } |
| 5793 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5794 | /// \brief Perform semantic analysis for the given dependent function |
| 5795 | /// template specialization. The only possible way to get a dependent |
| 5796 | /// function template specialization is with a friend declaration, |
| 5797 | /// like so: |
| 5798 | /// |
| 5799 | /// template <class T> void foo(T); |
| 5800 | /// template <class T> class A { |
| 5801 | /// friend void foo<>(T); |
| 5802 | /// }; |
| 5803 | /// |
| 5804 | /// There really isn't any useful analysis we can do here, so we |
| 5805 | /// just store the information. |
| 5806 | bool |
| 5807 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 5808 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 5809 | LookupResult &Previous) { |
| 5810 | // Remove anything from Previous that isn't a function template in |
| 5811 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5812 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5813 | LookupResult::Filter F = Previous.makeFilter(); |
| 5814 | while (F.hasNext()) { |
| 5815 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 5816 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5817 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 5818 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5819 | F.erase(); |
| 5820 | } |
| 5821 | F.done(); |
| 5822 | |
| 5823 | // Should this be diagnosed here? |
| 5824 | if (Previous.empty()) return true; |
| 5825 | |
| 5826 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 5827 | ExplicitTemplateArgs); |
| 5828 | return false; |
| 5829 | } |
| 5830 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5831 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5832 | /// specialization. |
| 5833 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5834 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5835 | /// explicit function template specialization. On successful completion, |
| 5836 | /// the function declaration \p FD will become a function template |
| 5837 | /// specialization. |
| 5838 | /// |
| 5839 | /// \param FD the function declaration, which will be updated to become a |
| 5840 | /// function template specialization. |
| 5841 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5842 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 5843 | /// if any. Note that this may be valid info even when 0 arguments are |
| 5844 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 5845 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5846 | /// |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5847 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5848 | /// this function specialization. |
| 5849 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5850 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5851 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5852 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5853 | // The set of function template specializations that could match this |
| 5854 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5855 | UnresolvedSet<8> Candidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5856 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5857 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5858 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5859 | I != E; ++I) { |
| 5860 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 5861 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5862 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5863 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5864 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 5865 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5866 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5867 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5868 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5869 | // A trailing template-argument can be left unspecified in the |
| 5870 | // template-id naming an explicit function template specialization |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5871 | // provided it can be deduced from the function argument type. |
| 5872 | // Perform template argument deduction to determine whether we may be |
| 5873 | // specializing this template. |
| 5874 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5875 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5876 | FunctionDecl *Specialization = 0; |
| 5877 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5878 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5879 | FD->getType(), |
| 5880 | Specialization, |
| 5881 | Info)) { |
| 5882 | // FIXME: Template argument deduction failed; record why it failed, so |
| 5883 | // that we can provide nifty diagnostics. |
| 5884 | (void)TDK; |
| 5885 | continue; |
| 5886 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5887 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5888 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5889 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5890 | } |
| 5891 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5892 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5893 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5894 | UnresolvedSetIterator Result |
| 5895 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5896 | TPOC_Other, 0, FD->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5897 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5898 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5899 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5900 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5901 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5902 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5903 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5904 | |
| 5905 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 5906 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | abfb405 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 5907 | |
| 5908 | FunctionTemplateSpecializationInfo *SpecInfo |
| 5909 | = Specialization->getTemplateSpecializationInfo(); |
| 5910 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5911 | |
| 5912 | // Note: do not overwrite location info if previous template |
| 5913 | // specialization kind was explicit. |
| 5914 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 5915 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5916 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 5917 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 5918 | // function can differ from the template declaration with respect to |
| 5919 | // the constexpr specifier. |
| 5920 | Specialization->setConstexpr(FD->isConstexpr()); |
| 5921 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5922 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5923 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5924 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5925 | |
| 5926 | // If this is a friend declaration, then we're not really declaring |
| 5927 | // an explicit specialization. |
| 5928 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5929 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5930 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5931 | if (!isFriend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5932 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5933 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5934 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5935 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5936 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5937 | |
| 5938 | // C++ [temp.expl.spec]p6: |
| 5939 | // If a template, a member template or the member of a class template is |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5940 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5941 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5942 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5943 | // use occurs; no diagnostic is required. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5944 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5945 | if (!isFriend && |
| 5946 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5947 | TSK_ExplicitSpecialization, |
| 5948 | Specialization, |
| 5949 | SpecInfo->getTemplateSpecializationKind(), |
| 5950 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5951 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5952 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5953 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5954 | // Mark the prior declaration as an explicit specialization, so that later |
| 5955 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5956 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5957 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5958 | MarkUnusedFileScopedDecl(Specialization); |
| 5959 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5960 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5961 | // Turn the given function declaration into a function template |
| 5962 | // specialization, with the template arguments from the previous |
| 5963 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5964 | // Take copies of (semantic and syntactic) template argument lists. |
| 5965 | const TemplateArgumentList* TemplArgs = new (Context) |
| 5966 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5967 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5968 | TemplArgs, /*InsertPos=*/0, |
| 5969 | SpecInfo->getTemplateSpecializationKind(), |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 5970 | ExplicitTemplateArgs); |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5971 | FD->setStorageClass(Specialization->getStorageClass()); |
| 5972 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5973 | // The "previous declaration" for this function template specialization is |
| 5974 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5975 | Previous.clear(); |
| 5976 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5977 | return false; |
| 5978 | } |
| 5979 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5980 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5981 | /// specialization. |
| 5982 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5983 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5984 | /// explicit member function specialization. On successful completion, |
| 5985 | /// the function declaration \p FD will become a member function |
| 5986 | /// specialization. |
| 5987 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5988 | /// \param Member the member declaration, which will be updated to become a |
| 5989 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5990 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5991 | /// \param Previous the set of declarations, one of which may be specialized |
| 5992 | /// by this function specialization; the set will be modified to contain the |
| 5993 | /// redeclared member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5994 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5995 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5996 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5997 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5998 | // Try to find the member we are instantiating. |
| 5999 | NamedDecl *Instantiation = 0; |
| 6000 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6001 | MemberSpecializationInfo *MSInfo = 0; |
| 6002 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6003 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6004 | // Nowhere to look anyway. |
| 6005 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6006 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6007 | I != E; ++I) { |
| 6008 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 6009 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6010 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 6011 | Instantiation = Method; |
| 6012 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6013 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6014 | break; |
| 6015 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6016 | } |
| 6017 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6018 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6019 | VarDecl *PrevVar; |
| 6020 | if (Previous.isSingleResult() && |
| 6021 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6022 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6023 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6024 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6025 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6026 | } |
| 6027 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6028 | CXXRecordDecl *PrevRecord; |
| 6029 | if (Previous.isSingleResult() && |
| 6030 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 6031 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6032 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6033 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6034 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6035 | } else if (isa<EnumDecl>(Member)) { |
| 6036 | EnumDecl *PrevEnum; |
| 6037 | if (Previous.isSingleResult() && |
| 6038 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
| 6039 | Instantiation = PrevEnum; |
| 6040 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 6041 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 6042 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6043 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6044 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6045 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6046 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6047 | // specializations are always out-of-line, the caller will complain about |
| 6048 | // this mismatch later. |
| 6049 | return false; |
| 6050 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 6051 | |
| 6052 | // If this is a friend, just bail out here before we start turning |
| 6053 | // things into explicit specializations. |
| 6054 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 6055 | // Preserve instantiation information. |
| 6056 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 6057 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 6058 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 6059 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 6060 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 6061 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 6062 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 6063 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 6064 | } |
| 6065 | |
| 6066 | Previous.clear(); |
| 6067 | Previous.addDecl(Instantiation); |
| 6068 | return false; |
| 6069 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6070 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6071 | // Make sure that this is a specialization of a member. |
| 6072 | if (!InstantiatedFrom) { |
| 6073 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 6074 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6075 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 6076 | return true; |
| 6077 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6078 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6079 | // C++ [temp.expl.spec]p6: |
| 6080 | // If a template, a member template or the member of a class template is |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 6081 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6082 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6083 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6084 | // use occurs; no diagnostic is required. |
| 6085 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6086 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6087 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6088 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 6089 | TSK_ExplicitSpecialization, |
| 6090 | Instantiation, |
| 6091 | MSInfo->getTemplateSpecializationKind(), |
| 6092 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6093 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6094 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6095 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6096 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6097 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6098 | InstantiatedFrom, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6099 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6100 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6101 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 6102 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6103 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6104 | // the original declaration to note that it is an explicit specialization |
| 6105 | // (if it was previously an implicit instantiation). This latter step |
| 6106 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6107 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6108 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 6109 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 6110 | TSK_ImplicitInstantiation) { |
| 6111 | InstantiationFunction->setTemplateSpecializationKind( |
| 6112 | TSK_ExplicitSpecialization); |
| 6113 | InstantiationFunction->setLocation(Member->getLocation()); |
| 6114 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6115 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6116 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 6117 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 6118 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6119 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6120 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6121 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 6122 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 6123 | TSK_ImplicitInstantiation) { |
| 6124 | InstantiationVar->setTemplateSpecializationKind( |
| 6125 | TSK_ExplicitSpecialization); |
| 6126 | InstantiationVar->setLocation(Member->getLocation()); |
| 6127 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6128 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6129 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 6130 | cast<VarDecl>(InstantiatedFrom), |
| 6131 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6132 | MarkUnusedFileScopedDecl(InstantiationVar); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6133 | } else if (isa<CXXRecordDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6134 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 6135 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 6136 | TSK_ImplicitInstantiation) { |
| 6137 | InstantiationClass->setTemplateSpecializationKind( |
| 6138 | TSK_ExplicitSpecialization); |
| 6139 | InstantiationClass->setLocation(Member->getLocation()); |
| 6140 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6141 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6142 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6143 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 6144 | TSK_ExplicitSpecialization); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6145 | } else { |
| 6146 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); |
| 6147 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); |
| 6148 | if (InstantiationEnum->getTemplateSpecializationKind() == |
| 6149 | TSK_ImplicitInstantiation) { |
| 6150 | InstantiationEnum->setTemplateSpecializationKind( |
| 6151 | TSK_ExplicitSpecialization); |
| 6152 | InstantiationEnum->setLocation(Member->getLocation()); |
| 6153 | } |
| 6154 | |
| 6155 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( |
| 6156 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6157 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6158 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6159 | // Save the caller the trouble of having to figure out which declaration |
| 6160 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6161 | Previous.clear(); |
| 6162 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6163 | return false; |
| 6164 | } |
| 6165 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6166 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6167 | /// |
| 6168 | /// \returns true if a serious error occurs, false otherwise. |
| 6169 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6170 | SourceLocation InstLoc, |
| 6171 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6172 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 6173 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6174 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6175 | if (CurContext->isRecord()) { |
| 6176 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 6177 | << D; |
| 6178 | return true; |
| 6179 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6180 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6181 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6182 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6183 | // template. If the name declared in the explicit instantiation is an |
| 6184 | // unqualified name, the explicit instantiation shall appear in the |
| 6185 | // namespace where its template is declared or, if that namespace is inline |
| 6186 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6187 | // |
| 6188 | // This is DR275, which we do not retroactively apply to C++98/03. |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6189 | if (WasQualifiedName) { |
| 6190 | if (CurContext->Encloses(OrigContext)) |
| 6191 | return false; |
| 6192 | } else { |
| 6193 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 6194 | return false; |
| 6195 | } |
| 6196 | |
| 6197 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 6198 | if (WasQualifiedName) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6199 | S.Diag(InstLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6200 | S.getLangOpts().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6201 | diag::err_explicit_instantiation_out_of_scope : |
| 6202 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6203 | << D << NS; |
| 6204 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6205 | S.Diag(InstLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6206 | S.getLangOpts().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6207 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 6208 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 6209 | << D << NS; |
| 6210 | } else |
| 6211 | S.Diag(InstLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6212 | S.getLangOpts().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6213 | diag::err_explicit_instantiation_must_be_global : |
| 6214 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 6215 | << D; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6216 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6217 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6218 | } |
| 6219 | |
| 6220 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 6221 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 6222 | if (!SS.isSet()) |
| 6223 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6224 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6225 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6226 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6227 | // or a static data member of a class template specialization, the name of |
| 6228 | // the class template specialization in the qualified-id for the member |
| 6229 | // name shall be a simple-template-id. |
| 6230 | // |
| 6231 | // C++98 has the same restriction, just worded differently. |
| 6232 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 6233 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6234 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6235 | if (isa<TemplateSpecializationType>(T)) |
| 6236 | return true; |
| 6237 | |
| 6238 | return false; |
| 6239 | } |
| 6240 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6241 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6242 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6243 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 6244 | SourceLocation ExternLoc, |
| 6245 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6246 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6247 | SourceLocation KWLoc, |
| 6248 | const CXXScopeSpec &SS, |
| 6249 | TemplateTy TemplateD, |
| 6250 | SourceLocation TemplateNameLoc, |
| 6251 | SourceLocation LAngleLoc, |
| 6252 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6253 | SourceLocation RAngleLoc, |
| 6254 | AttributeList *Attr) { |
| 6255 | // Find the class template we're specializing |
| 6256 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6257 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6258 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 6259 | |
| 6260 | // Check that the specialization uses the same tag kind as the |
| 6261 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6262 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6263 | assert(Kind != TTK_Enum && |
| 6264 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6265 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6266 | Kind, /*isDefinition*/false, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6267 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6268 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6269 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6270 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6271 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6272 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6273 | diag::note_previous_use); |
| 6274 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6275 | } |
| 6276 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6277 | // C++0x [temp.explicit]p2: |
| 6278 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6279 | // definition and an explicit instantiation declaration. An explicit |
| 6280 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6281 | TemplateSpecializationKind TSK |
| 6282 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6283 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6284 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6285 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6286 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 6287 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6288 | |
| 6289 | // Check that the template argument list is well-formed for this |
| 6290 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6291 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6292 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6293 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6294 | return true; |
| 6295 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6296 | // Find the class template specialization declaration that |
| 6297 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6298 | void *InsertPos = 0; |
| 6299 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6300 | = ClassTemplate->findSpecialization(Converted.data(), |
| 6301 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6302 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6303 | TemplateSpecializationKind PrevDecl_TSK |
| 6304 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 6305 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6306 | // C++0x [temp.explicit]p2: |
| 6307 | // [...] An explicit instantiation shall appear in an enclosing |
| 6308 | // namespace of its template. [...] |
| 6309 | // |
| 6310 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6311 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 6312 | SS.isSet())) |
| 6313 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6314 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6315 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 6316 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6317 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6318 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6319 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6320 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6321 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6322 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6323 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6324 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6325 | // Even though HasNoEffect == true means that this explicit instantiation |
| 6326 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 6327 | |
| 6328 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 6329 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6330 | // Since the only prior class template specialization with these |
| 6331 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6332 | // declaration node as our own, updating the source location |
| 6333 | // for the template name to reflect our new declaration. |
| 6334 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6335 | Specialization = PrevDecl; |
| 6336 | Specialization->setLocation(TemplateNameLoc); |
| 6337 | PrevDecl = 0; |
| 6338 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6339 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6340 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6341 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6342 | // Create a new class template specialization declaration node for |
| 6343 | // this explicit specialization. |
| 6344 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6345 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6346 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6347 | KWLoc, TemplateNameLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6348 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6349 | Converted.data(), |
| 6350 | Converted.size(), |
| 6351 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6352 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6353 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6354 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6355 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6356 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6357 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6358 | } |
| 6359 | |
| 6360 | // Build the fully-sugared type for this explicit instantiation as |
| 6361 | // the user wrote in the explicit instantiation itself. This means |
| 6362 | // that we'll pretty-print the type retrieved from the |
| 6363 | // specialization's declaration the way that the user actually wrote |
| 6364 | // the explicit instantiation, rather than formatting the name based |
| 6365 | // on the "canonical" representation used to store the template |
| 6366 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6367 | TypeSourceInfo *WrittenTy |
| 6368 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6369 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6370 | Context.getTypeDeclType(Specialization)); |
| 6371 | Specialization->setTypeAsWritten(WrittenTy); |
| 6372 | TemplateArgsIn.release(); |
| 6373 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6374 | // Set source locations for keywords. |
| 6375 | Specialization->setExternLoc(ExternLoc); |
| 6376 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 6377 | |
Rafael Espindola | 0257b7f | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 6378 | if (Attr) |
| 6379 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6380 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6381 | // Add the explicit instantiation into its lexical context. However, |
| 6382 | // since explicit instantiations are never found by name lookup, we |
| 6383 | // just put it into the declaration context directly. |
| 6384 | Specialization->setLexicalDeclContext(CurContext); |
| 6385 | CurContext->addDecl(Specialization); |
| 6386 | |
| 6387 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 6388 | if (HasNoEffect) { |
| 6389 | // Set the template specialization kind. |
| 6390 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6391 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 6392 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6393 | |
| 6394 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6395 | // A definition of a class template or class member template |
| 6396 | // shall be in scope at the point of the explicit instantiation of |
| 6397 | // the class template or class member template. |
| 6398 | // |
| 6399 | // This check comes when we actually try to perform the |
| 6400 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6401 | ClassTemplateSpecializationDecl *Def |
| 6402 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6403 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6404 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6405 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6406 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6407 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6408 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 6409 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6410 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6411 | // Instantiate the members of this class template specialization. |
| 6412 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6413 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6414 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 6415 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 6416 | |
| 6417 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 6418 | // TSK_ExplicitInstantiationDefinition |
| 6419 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 6420 | TSK == TSK_ExplicitInstantiationDefinition) |
| 6421 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6422 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6423 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6424 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6425 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6426 | // Set the template specialization kind. |
| 6427 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6428 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6429 | } |
| 6430 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6431 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6432 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6433 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 6434 | SourceLocation ExternLoc, |
| 6435 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6436 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6437 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6438 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6439 | IdentifierInfo *Name, |
| 6440 | SourceLocation NameLoc, |
| 6441 | AttributeList *Attr) { |
| 6442 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 6443 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6444 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6445 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6446 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6447 | /*ModulePrivateLoc=*/SourceLocation(), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6448 | MultiTemplateParamsArg(*this, 0, 0), |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 6449 | Owned, IsDependent, SourceLocation(), false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6450 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6451 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 6452 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6453 | if (!TagD) |
| 6454 | return true; |
| 6455 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6456 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6457 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6458 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 6459 | if (Tag->isInvalidDecl()) |
| 6460 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6461 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6462 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 6463 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 6464 | if (!Pattern) { |
| 6465 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 6466 | << Context.getTypeDeclType(Record); |
| 6467 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 6468 | return true; |
| 6469 | } |
| 6470 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6471 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6472 | // If the explicit instantiation is for a class or member class, the |
| 6473 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6474 | // simple-template-id. |
| 6475 | // |
| 6476 | // C++98 has the same restriction, just worded differently. |
| 6477 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6478 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6479 | << Record << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6480 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6481 | // C++0x [temp.explicit]p2: |
| 6482 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6483 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6484 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 6485 | TemplateSpecializationKind TSK |
| 6486 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6487 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6488 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6489 | // C++0x [temp.explicit]p2: |
| 6490 | // [...] An explicit instantiation shall appear in an enclosing |
| 6491 | // namespace of its template. [...] |
| 6492 | // |
| 6493 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6494 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6495 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6496 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6497 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 6498 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6499 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6500 | PrevDecl = Record; |
| 6501 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6502 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6503 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6504 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6505 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6506 | PrevDecl, |
| 6507 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6508 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6509 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6510 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6511 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6512 | return TagD; |
| 6513 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6514 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6515 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6516 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6517 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6518 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6519 | // A definition of a member class of a class template shall be in scope |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6520 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6521 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6522 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6523 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 6524 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 6525 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6526 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 6527 | << Pattern; |
| 6528 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6529 | } else { |
| 6530 | if (InstantiateClass(NameLoc, Record, Def, |
| 6531 | getTemplateInstantiationArgs(Record), |
| 6532 | TSK)) |
| 6533 | return true; |
| 6534 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6535 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6536 | if (!RecordDef) |
| 6537 | return true; |
| 6538 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6539 | } |
| 6540 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6541 | // Instantiate all of the members of the class. |
| 6542 | InstantiateClassMembers(NameLoc, RecordDef, |
| 6543 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6544 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6545 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 6546 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 6547 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6548 | // FIXME: We don't have any representation for explicit instantiations of |
| 6549 | // member classes. Such a representation is not needed for compilation, but it |
| 6550 | // should be available for clients that want to see all of the declarations in |
| 6551 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6552 | return TagD; |
| 6553 | } |
| 6554 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6555 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 6556 | SourceLocation ExternLoc, |
| 6557 | SourceLocation TemplateLoc, |
| 6558 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6559 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6560 | // TODO: check if/when DNInfo should replace Name. |
| 6561 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6562 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6563 | if (!Name) { |
| 6564 | if (!D.isInvalidType()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6565 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6566 | diag::err_explicit_instantiation_requires_name) |
| 6567 | << D.getDeclSpec().getSourceRange() |
| 6568 | << D.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6569 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6570 | return true; |
| 6571 | } |
| 6572 | |
| 6573 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 6574 | // we find one that is. |
| 6575 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 6576 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 6577 | S = S->getParent(); |
| 6578 | |
| 6579 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6580 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 6581 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6582 | if (R.isNull()) |
| 6583 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6584 | |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6585 | // C++ [dcl.stc]p1: |
| 6586 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 6587 | // instantiation (14.7.2) directive. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6588 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6589 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 6590 | << Name; |
| 6591 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6592 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 6593 | != DeclSpec::SCS_unspecified) { |
| 6594 | // Complain about then remove the storage class specifier. |
| 6595 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 6596 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6597 | |
| 6598 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6599 | } |
| 6600 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6601 | // C++0x [temp.explicit]p1: |
| 6602 | // [...] An explicit instantiation of a function template shall not use the |
| 6603 | // inline or constexpr specifiers. |
| 6604 | // Presumably, this also applies to member functions of class templates as |
| 6605 | // well. |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6606 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6607 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6608 | getLangOpts().CPlusPlus0x ? |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6609 | diag::err_explicit_instantiation_inline : |
| 6610 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | fe6f648 | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 6611 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
| 6612 | if (D.getDeclSpec().isConstexprSpecified()) |
| 6613 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 6614 | // not already specified. |
| 6615 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 6616 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6617 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6618 | // C++0x [temp.explicit]p2: |
| 6619 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6620 | // definition and an explicit instantiation declaration. An explicit |
| 6621 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6622 | TemplateSpecializationKind TSK |
| 6623 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6624 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6625 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6626 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6627 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6628 | |
| 6629 | if (!R->isFunctionType()) { |
| 6630 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6631 | // A [...] static data member of a class template can be explicitly |
| 6632 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6633 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6634 | if (Previous.isAmbiguous()) |
| 6635 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6636 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6637 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6638 | if (!Prev || !Prev->isStaticDataMember()) { |
| 6639 | // We expect to see a data data member here. |
| 6640 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 6641 | << Name; |
| 6642 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6643 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6644 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6645 | return true; |
| 6646 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6647 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6648 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 6649 | // FIXME: Check for explicit specialization? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6650 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6651 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 6652 | << Prev; |
| 6653 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 6654 | // FIXME: Can we provide a note showing where this was declared? |
| 6655 | return true; |
| 6656 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6657 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6658 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6659 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6660 | // or a static data member of a class template specialization, the name of |
| 6661 | // the class template specialization in the qualified-id for the member |
| 6662 | // name shall be a simple-template-id. |
| 6663 | // |
| 6664 | // C++98 has the same restriction, just worded differently. |
| 6665 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6666 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6667 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6668 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6669 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6670 | // Check the scope of this explicit instantiation. |
| 6671 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6672 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6673 | // Verify that it is okay to explicitly instantiate here. |
| 6674 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 6675 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6676 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6677 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6678 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6679 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6680 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6681 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6682 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6683 | return (Decl*) 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6684 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6685 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6686 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6687 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6688 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6689 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6690 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6691 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6692 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6693 | |
| 6694 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6695 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6696 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6697 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6698 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 6699 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6700 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 6701 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6702 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 6703 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6704 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6705 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6706 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 6707 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6708 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6709 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6710 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6711 | // A [...] function [...] can be explicitly instantiated from its template. |
| 6712 | // A member function [...] of a class template can be explicitly |
| 6713 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6714 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6715 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6716 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6717 | P != PEnd; ++P) { |
| 6718 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6719 | if (!HasExplicitTemplateArgs) { |
| 6720 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 6721 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 6722 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6723 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6724 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6725 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 6726 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6727 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6728 | } |
| 6729 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6730 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6731 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 6732 | if (!FunTmpl) |
| 6733 | continue; |
| 6734 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6735 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6736 | FunctionDecl *Specialization = 0; |
| 6737 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6738 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6739 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6740 | R, Specialization, Info)) { |
| 6741 | // FIXME: Keep track of almost-matches? |
| 6742 | (void)TDK; |
| 6743 | continue; |
| 6744 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6745 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6746 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6747 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6748 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6749 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6750 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6751 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6752 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6753 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 6754 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 6755 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6756 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6757 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6758 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6759 | |
| 6760 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 6761 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6762 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6763 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6764 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6765 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 6766 | << Specialization |
| 6767 | << (Specialization->getTemplateSpecializationKind() == |
| 6768 | TSK_ExplicitSpecialization); |
| 6769 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 6770 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6771 | } |
| 6772 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 6773 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6774 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 6775 | PrevDecl = Specialization; |
| 6776 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6777 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6778 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6779 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6780 | PrevDecl, |
| 6781 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6782 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6783 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6784 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6785 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6786 | // FIXME: We may still want to build some representation of this |
| 6787 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6788 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6789 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6790 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 6791 | |
| 6792 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 256fc4d | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 6793 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 6794 | if (Attr) |
| 6795 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6796 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6797 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6798 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6799 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6800 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6801 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6802 | // or a static data member of a class template specialization, the name of |
| 6803 | // the class template specialization in the qualified-id for the member |
| 6804 | // name shall be a simple-template-id. |
| 6805 | // |
| 6806 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6807 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6808 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6809 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6810 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6811 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6812 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6813 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6814 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6815 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6816 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6817 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6818 | D.getIdentifierLoc(), |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6819 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6820 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6821 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6822 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6823 | } |
| 6824 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6825 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6826 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 6827 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 6828 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 6829 | // This has to hold, because SS is expected to be defined. |
| 6830 | assert(Name && "Expected a name in a dependent tag"); |
| 6831 | |
| 6832 | NestedNameSpecifier *NNS |
| 6833 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6834 | if (!NNS) |
| 6835 | return true; |
| 6836 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6837 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 6838 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6839 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 6840 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6841 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6842 | return true; |
| 6843 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6844 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6845 | // Create the resulting type. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6846 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6847 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 6848 | |
| 6849 | // Create type-source location information for this type. |
| 6850 | TypeLocBuilder TLB; |
| 6851 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6852 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6853 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6854 | TL.setNameLoc(NameLoc); |
| 6855 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6856 | } |
| 6857 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6858 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6859 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6860 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6861 | SourceLocation IdLoc) { |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6862 | if (SS.isInvalid()) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6863 | return true; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6864 | |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6865 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6866 | Diag(TypenameLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6867 | getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6868 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6869 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6870 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6871 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6872 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6873 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 6874 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 6875 | if (T.isNull()) |
| 6876 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6877 | |
| 6878 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6879 | if (isa<DependentNameType>(T)) { |
| 6880 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6881 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6882 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6883 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6884 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6885 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6886 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6887 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6888 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6889 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6890 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6891 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6892 | } |
| 6893 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6894 | TypeResult |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6895 | Sema::ActOnTypenameType(Scope *S, |
| 6896 | SourceLocation TypenameLoc, |
| 6897 | const CXXScopeSpec &SS, |
| 6898 | SourceLocation TemplateKWLoc, |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6899 | TemplateTy TemplateIn, |
| 6900 | SourceLocation TemplateNameLoc, |
| 6901 | SourceLocation LAngleLoc, |
| 6902 | ASTTemplateArgsPtr TemplateArgsIn, |
| 6903 | SourceLocation RAngleLoc) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6904 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6905 | Diag(TypenameLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6906 | getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6907 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6908 | diag::ext_typename_outside_of_template) |
| 6909 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6910 | |
| 6911 | // Translate the parser's template argument list in our AST format. |
| 6912 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 6913 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 6914 | |
| 6915 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6916 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 6917 | // Construct a dependent template specialization type. |
| 6918 | assert(DTN && "dependent template has non-dependent name?"); |
| 6919 | assert(DTN->getQualifier() |
| 6920 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 6921 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 6922 | DTN->getQualifier(), |
| 6923 | DTN->getIdentifier(), |
| 6924 | TemplateArgs); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6925 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6926 | // Create source-location information for this type. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6927 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6928 | DependentTemplateSpecializationTypeLoc SpecTL |
| 6929 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6930 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 6931 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 66581d4 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 6932 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6933 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6934 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6935 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6936 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6937 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6938 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 6939 | } |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6940 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6941 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 6942 | if (T.isNull()) |
| 6943 | return true; |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6944 | |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6945 | // Provide source-location information for the template specialization type. |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6946 | TypeLocBuilder Builder; |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6947 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6948 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6949 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 6950 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6951 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6952 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6953 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6954 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 6955 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6956 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 6957 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6958 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6959 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6960 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6961 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 6962 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 6963 | } |
| 6964 | |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6965 | |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 6966 | /// Determine whether this failed name lookup should be treated as being |
| 6967 | /// disabled by a usage of std::enable_if. |
| 6968 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
| 6969 | SourceRange &CondRange) { |
| 6970 | // We must be looking for a ::type... |
| 6971 | if (!II.isStr("type")) |
| 6972 | return false; |
| 6973 | |
| 6974 | // ... within an explicitly-written template specialization... |
| 6975 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 6976 | return false; |
| 6977 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
| 6978 | TemplateSpecializationTypeLoc *EnableIfTSTLoc = |
| 6979 | dyn_cast<TemplateSpecializationTypeLoc>(&EnableIfTy); |
| 6980 | if (!EnableIfTSTLoc || EnableIfTSTLoc->getNumArgs() == 0) |
| 6981 | return false; |
| 6982 | const TemplateSpecializationType *EnableIfTST = |
| 6983 | cast<TemplateSpecializationType>(EnableIfTSTLoc->getTypePtr()); |
| 6984 | |
| 6985 | // ... which names a complete class template declaration... |
| 6986 | const TemplateDecl *EnableIfDecl = |
| 6987 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 6988 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 6989 | return false; |
| 6990 | |
| 6991 | // ... called "enable_if". |
| 6992 | const IdentifierInfo *EnableIfII = |
| 6993 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 6994 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 6995 | return false; |
| 6996 | |
| 6997 | // Assume the first template argument is the condition. |
| 6998 | CondRange = EnableIfTSTLoc->getArgLoc(0).getSourceRange(); |
| 6999 | return true; |
| 7000 | } |
| 7001 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7002 | /// \brief Build the type that describes a C++ typename specifier, |
| 7003 | /// e.g., "typename T::type". |
| 7004 | QualType |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7005 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 7006 | SourceLocation KeywordLoc, |
| 7007 | NestedNameSpecifierLoc QualifierLoc, |
| 7008 | const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 7009 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7010 | CXXScopeSpec SS; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7011 | SS.Adopt(QualifierLoc); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7012 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7013 | DeclContext *Ctx = computeDeclContext(SS); |
| 7014 | if (!Ctx) { |
| 7015 | // If the nested-name-specifier is dependent and couldn't be |
| 7016 | // resolved to a type, build a typename type. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7017 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 7018 | return Context.getDependentNameType(Keyword, |
| 7019 | QualifierLoc.getNestedNameSpecifier(), |
| 7020 | &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 7021 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7022 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7023 | // If the nested-name-specifier refers to the current instantiation, |
| 7024 | // the "typename" keyword itself is superfluous. In C++03, the |
| 7025 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 7026 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 7027 | // apply this DR to C++03 code with only a warning. In any case we continue. |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 7028 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7029 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 7030 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7031 | |
| 7032 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 7033 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7034 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7035 | unsigned DiagID = 0; |
| 7036 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7037 | switch (Result.getResultKind()) { |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7038 | case LookupResult::NotFound: { |
| 7039 | // If we're looking up 'type' within a template named 'enable_if', produce |
| 7040 | // a more specific diagnostic. |
| 7041 | SourceRange CondRange; |
| 7042 | if (isEnableIf(QualifierLoc, II, CondRange)) { |
| 7043 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
| 7044 | << Ctx << CondRange; |
| 7045 | return QualType(); |
| 7046 | } |
| 7047 | |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 7048 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7049 | break; |
Richard Smith | 4493c0a | 2012-05-09 05:17:00 +0000 | [diff] [blame] | 7050 | } |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 7051 | |
| 7052 | case LookupResult::FoundUnresolvedValue: { |
| 7053 | // We found a using declaration that is a value. Most likely, the using |
| 7054 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7055 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 7056 | IILoc); |
| 7057 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 7058 | << Name << Ctx << FullRange; |
| 7059 | if (UnresolvedUsingValueDecl *Using |
| 7060 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7061 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 7062 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 7063 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 7064 | } |
| 7065 | } |
| 7066 | // Fall through to create a dependent typename type, from which we can recover |
| 7067 | // better. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7068 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 7069 | case LookupResult::NotFoundInCurrentInstantiation: |
| 7070 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7071 | return Context.getDependentNameType(Keyword, |
| 7072 | QualifierLoc.getNestedNameSpecifier(), |
| 7073 | &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7074 | |
| 7075 | case LookupResult::Found: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7076 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7077 | // We found a type. Build an ElaboratedType, since the |
| 7078 | // typename-specifier was just sugar. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7079 | return Context.getElaboratedType(ETK_Typename, |
| 7080 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7081 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7082 | } |
| 7083 | |
| 7084 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7085 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7086 | break; |
| 7087 | |
| 7088 | case LookupResult::FoundOverloaded: |
| 7089 | DiagID = diag::err_typename_nested_not_type; |
| 7090 | Referenced = *Result.begin(); |
| 7091 | break; |
| 7092 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 7093 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7094 | return QualType(); |
| 7095 | } |
| 7096 | |
| 7097 | // If we get here, it's because name lookup did not find a |
| 7098 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7099 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 7100 | IILoc); |
| 7101 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 7102 | if (Referenced) |
| 7103 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 7104 | << Name; |
| 7105 | return QualType(); |
| 7106 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7107 | |
| 7108 | namespace { |
| 7109 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 7110 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7111 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7112 | SourceLocation Loc; |
| 7113 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7114 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7115 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 7116 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7117 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7118 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7119 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7120 | DeclarationName Entity) |
| 7121 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7122 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7123 | |
| 7124 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7125 | /// transformed. |
| 7126 | /// |
| 7127 | /// For the purposes of type reconstruction, a type has already been |
| 7128 | /// transformed if it is NULL or if it is not dependent. |
| 7129 | bool AlreadyTransformed(QualType T) { |
| 7130 | return T.isNull() || !T->isDependentType(); |
| 7131 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7132 | |
| 7133 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7134 | /// rebuilt. |
| 7135 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7136 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7137 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 7138 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7139 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7140 | /// \brief Sets the "base" location and entity when that |
| 7141 | /// information is known based on another transformation. |
| 7142 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 7143 | this->Loc = Loc; |
| 7144 | this->Entity = Entity; |
| 7145 | } |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 7146 | |
| 7147 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 7148 | // Lambdas never need to be transformed. |
| 7149 | return E; |
| 7150 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7151 | }; |
| 7152 | } |
| 7153 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7154 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 7155 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7156 | /// The type \p T is part of the type of an out-of-line member definition of |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7157 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7158 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7159 | /// partial specialization thereof). This routine will rebuild that type now |
| 7160 | /// that we have entered the declarator's scope, which may produce different |
| 7161 | /// canonical types, e.g., |
| 7162 | /// |
| 7163 | /// \code |
| 7164 | /// template<typename T> |
| 7165 | /// struct X { |
| 7166 | /// typedef T* pointer; |
| 7167 | /// pointer data(); |
| 7168 | /// }; |
| 7169 | /// |
| 7170 | /// template<typename T> |
| 7171 | /// typename X<T>::pointer X<T>::data() { ... } |
| 7172 | /// \endcode |
| 7173 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 7174 | /// Here, the type "typename X<T>::pointer" will be created as a DependentNameType, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7175 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 7176 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7177 | /// in X<T> and returning an ElaboratedType whose canonical type is the same |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7178 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 7179 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7180 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 7181 | SourceLocation Loc, |
| 7182 | DeclarationName Name) { |
| 7183 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7184 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7185 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7186 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 7187 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 7188 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7189 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7190 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7191 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 7192 | DeclarationName()); |
| 7193 | return Rebuilder.TransformExpr(E); |
| 7194 | } |
| 7195 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7196 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 7197 | if (SS.isInvalid()) |
| 7198 | return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 7199 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 7200 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 7201 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 7202 | DeclarationName()); |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 7203 | NestedNameSpecifierLoc Rebuilt |
| 7204 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 7205 | if (!Rebuilt) |
| 7206 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7207 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 7208 | SS.Adopt(Rebuilt); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7209 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 7210 | } |
| 7211 | |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 7212 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 7213 | /// instantiation. |
| 7214 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 7215 | TemplateParameterList *Params) { |
| 7216 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 7217 | Decl *Param = Params->getParam(I); |
| 7218 | |
| 7219 | // There is nothing to rebuild in a type parameter. |
| 7220 | if (isa<TemplateTypeParmDecl>(Param)) |
| 7221 | continue; |
| 7222 | |
| 7223 | // Rebuild the template parameter list of a template template parameter. |
| 7224 | if (TemplateTemplateParmDecl *TTP |
| 7225 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 7226 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 7227 | TTP->getTemplateParameters())) |
| 7228 | return true; |
| 7229 | |
| 7230 | continue; |
| 7231 | } |
| 7232 | |
| 7233 | // Rebuild the type of a non-type template parameter. |
| 7234 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 7235 | TypeSourceInfo *NewTSI |
| 7236 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 7237 | NTTP->getLocation(), |
| 7238 | NTTP->getDeclName()); |
| 7239 | if (!NewTSI) |
| 7240 | return true; |
| 7241 | |
| 7242 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 7243 | NTTP->setTypeSourceInfo(NewTSI); |
| 7244 | NTTP->setType(NewTSI->getType()); |
| 7245 | } |
| 7246 | } |
| 7247 | |
| 7248 | return false; |
| 7249 | } |
| 7250 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7251 | /// \brief Produces a formatted string that describes the binding of |
| 7252 | /// template parameters to template arguments. |
| 7253 | std::string |
| 7254 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 7255 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7256 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 7257 | } |
| 7258 | |
| 7259 | std::string |
| 7260 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 7261 | const TemplateArgument *Args, |
| 7262 | unsigned NumArgs) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 7263 | SmallString<128> Str; |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7264 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7265 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 7266 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7267 | return std::string(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7268 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7269 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 7270 | if (I >= NumArgs) |
| 7271 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7272 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7273 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7274 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7275 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7276 | Out << ", "; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7277 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7278 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7279 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7280 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7281 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7282 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7283 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7284 | Out << " = "; |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7285 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7286 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7287 | |
| 7288 | Out << ']'; |
| 7289 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7290 | } |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 7291 | |
| 7292 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) { |
| 7293 | if (!FD) |
| 7294 | return; |
| 7295 | FD->setLateTemplateParsed(Flag); |
| 7296 | } |
| 7297 | |
| 7298 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 7299 | DeclContext *DC = CurContext; |
| 7300 | |
| 7301 | while (DC) { |
| 7302 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 7303 | const FunctionDecl *FD = RD->isLocalClass(); |
| 7304 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 7305 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 7306 | return false; |
| 7307 | |
| 7308 | DC = DC->getParent(); |
| 7309 | } |
| 7310 | return false; |
| 7311 | } |