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 | |
| 357 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
| 358 | // C++ [basic.lookup.classref]p1: |
| 359 | // [...] If the lookup in the class of the object expression finds a |
| 360 | // template, the name is also looked up in the context of the entire |
| 361 | // postfix-expression and [...] |
| 362 | // |
| 363 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 364 | LookupOrdinaryName); |
| 365 | LookupName(FoundOuter, S); |
Douglas Gregor | 5a7a5bb | 2012-03-10 23:52:41 +0000 | [diff] [blame] | 366 | FilterAcceptableTemplateNames(FoundOuter, /*AllowFunctionTemplates=*/false); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 367 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 368 | if (FoundOuter.empty()) { |
| 369 | // - if the name is not found, the name found in the class of the |
| 370 | // object expression is used, otherwise |
Douglas Gregor | a6d1e76 | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 371 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 372 | FoundOuter.isAmbiguous()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 373 | // - if the name is found in the context of the entire |
| 374 | // postfix-expression and does not name a class template, the name |
| 375 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | a6d1e76 | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 376 | FoundOuter.clear(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 377 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 378 | // - if the name found is a class template, it must refer to the same |
| 379 | // entity as the one found in the class of the object expression, |
| 380 | // otherwise the program is ill-formed. |
| 381 | if (!Found.isSingleResult() || |
| 382 | Found.getFoundDecl()->getCanonicalDecl() |
| 383 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 384 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 21d07e4 | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 385 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 386 | << Found.getLookupName() |
| 387 | << ObjectType; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 388 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 389 | diag::note_ambig_member_ref_object_type) |
| 390 | << ObjectType; |
| 391 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 392 | diag::note_ambig_member_ref_scope); |
| 393 | |
| 394 | // Recover by taking the template that we found in the object |
| 395 | // expression's type. |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 401 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 402 | /// was just parsed. This is only possible with an explicit scope |
| 403 | /// specifier naming a dependent type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 404 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 405 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 406 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 407 | const DeclarationNameInfo &NameInfo, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 408 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 409 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 410 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 411 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 412 | if (!isAddressOfOperand && |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 413 | isa<CXXMethodDecl>(DC) && |
| 414 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 415 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 416 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 417 | // Since the 'this' expression is synthesized, we don't need to |
| 418 | // perform the double-lookup check. |
| 419 | NamedDecl *FirstQualifierInScope = 0; |
| 420 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 421 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 422 | /*This*/ 0, ThisType, |
| 423 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 424 | /*Op*/ SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 425 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 426 | TemplateKWLoc, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 427 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 428 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 429 | TemplateArgs)); |
| 430 | } |
| 431 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 432 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 433 | } |
| 434 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 435 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 436 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 437 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 438 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 439 | const TemplateArgumentListInfo *TemplateArgs) { |
| 440 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 441 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 442 | TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 443 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 444 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 447 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 448 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 449 | /// declaration at location Loc. Returns true to indicate that this is |
| 450 | /// an error, and false otherwise. |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 451 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 452 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 453 | |
| 454 | // Microsoft Visual C++ permits template parameters to be shadowed. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 455 | if (getLangOpts().MicrosoftExt) |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 456 | return; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 457 | |
| 458 | // C++ [temp.local]p4: |
| 459 | // A template-parameter shall not be redeclared within its |
| 460 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 462 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 463 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 464 | return; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 467 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 468 | /// the parameter D to reference the templated declaration and return a pointer |
| 469 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 470 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 471 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 472 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 473 | return Temp; |
| 474 | } |
| 475 | return 0; |
| 476 | } |
| 477 | |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 478 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 479 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 480 | assert(Kind == Template && |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 481 | "Only template template arguments can be pack expansions here"); |
| 482 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 483 | "Template template argument pack expansion without packs"); |
| 484 | ParsedTemplateArgument Result(*this); |
| 485 | Result.EllipsisLoc = EllipsisLoc; |
| 486 | return Result; |
| 487 | } |
| 488 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 489 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 490 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 491 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 492 | switch (Arg.getKind()) { |
| 493 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 494 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 495 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 496 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 497 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 498 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 499 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 500 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 501 | case ParsedTemplateArgument::NonType: { |
| 502 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 503 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 504 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 505 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 506 | case ParsedTemplateArgument::Template: { |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 507 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 508 | TemplateArgument TArg; |
| 509 | if (Arg.getEllipsisLoc().isValid()) |
| 510 | TArg = TemplateArgument(Template, llvm::Optional<unsigned int>()); |
| 511 | else |
| 512 | TArg = Template; |
| 513 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 514 | Arg.getScopeSpec().getWithLocInContext( |
| 515 | SemaRef.Context), |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 516 | Arg.getLocation(), |
| 517 | Arg.getEllipsisLoc()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 520 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 521 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 522 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 523 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 524 | /// \brief Translates template arguments as provided by the parser |
| 525 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 526 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 527 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 528 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 529 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 530 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 531 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 532 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 533 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 534 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 535 | /// the keyword "typename" was used to declare the type parameter |
| 536 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 537 | /// "class" or "typename" keyword. ParamName is the name of the |
| 538 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 539 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 540 | /// If the type parameter has a default argument, it will be added |
| 541 | /// later via ActOnTypeParameterDefault. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 542 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
| 543 | SourceLocation EllipsisLoc, |
| 544 | SourceLocation KeyLoc, |
| 545 | IdentifierInfo *ParamName, |
| 546 | SourceLocation ParamNameLoc, |
| 547 | unsigned Depth, unsigned Position, |
| 548 | SourceLocation EqualLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 549 | ParsedType DefaultArg) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | assert(S->isTemplateParamScope() && |
| 551 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 552 | bool Invalid = false; |
| 553 | |
| 554 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 555 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 556 | LookupOrdinaryName, |
| 557 | ForRedeclaration); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 558 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 559 | DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl); |
| 560 | PrevDecl = 0; |
| 561 | } |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 564 | SourceLocation Loc = ParamNameLoc; |
| 565 | if (!ParamName) |
| 566 | Loc = KeyLoc; |
| 567 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 568 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 569 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 570 | KeyLoc, Loc, Depth, Position, ParamName, |
| 571 | Typename, Ellipsis); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 572 | Param->setAccess(AS_public); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 573 | if (Invalid) |
| 574 | Param->setInvalidDecl(); |
| 575 | |
| 576 | if (ParamName) { |
| 577 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 578 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 579 | IdResolver.AddDecl(Param); |
| 580 | } |
| 581 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 582 | // C++0x [temp.param]p9: |
| 583 | // A default template-argument may be specified for any kind of |
| 584 | // template-parameter that is not a template parameter pack. |
| 585 | if (DefaultArg && Ellipsis) { |
| 586 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 587 | DefaultArg = ParsedType(); |
| 588 | } |
| 589 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 590 | // Handle the default argument, if provided. |
| 591 | if (DefaultArg) { |
| 592 | TypeSourceInfo *DefaultTInfo; |
| 593 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 594 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 595 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 596 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 597 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 598 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 599 | UPPC_DefaultArgument)) |
| 600 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 601 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 602 | // Check the template argument itself. |
| 603 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 604 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 605 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 606 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 607 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 608 | Param->setDefaultArgument(DefaultTInfo, false); |
| 609 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 610 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 611 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 614 | /// \brief Check that the type of a non-type template parameter is |
| 615 | /// well-formed. |
| 616 | /// |
| 617 | /// \returns the (possibly-promoted) parameter type if valid; |
| 618 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 619 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 620 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 621 | // We don't allow variably-modified types as the type of non-type template |
| 622 | // parameters. |
| 623 | if (T->isVariablyModifiedType()) { |
| 624 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 625 | << T; |
| 626 | return QualType(); |
| 627 | } |
| 628 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 629 | // C++ [temp.param]p4: |
| 630 | // |
| 631 | // A non-type template-parameter shall have one of the following |
| 632 | // (optionally cv-qualified) types: |
| 633 | // |
| 634 | // -- integral or enumeration type, |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 635 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 636 | // -- pointer to object or pointer to function, |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 637 | T->isPointerType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 639 | T->isReferenceType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 640 | // -- pointer to member, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 641 | T->isMemberPointerType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 642 | // -- std::nullptr_t. |
| 643 | T->isNullPtrType() || |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 644 | // If T is a dependent type, we can't do the check now, so we |
| 645 | // assume that it is well-formed. |
Richard Smith | e37f484 | 2012-03-13 07:21:50 +0000 | [diff] [blame] | 646 | T->isDependentType()) { |
| 647 | // C++ [temp.param]p5: The top-level cv-qualifiers on the template-parameter |
| 648 | // are ignored when determining its type. |
| 649 | return T.getUnqualifiedType(); |
| 650 | } |
| 651 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 652 | // C++ [temp.param]p8: |
| 653 | // |
| 654 | // A non-type template-parameter of type "array of T" or |
| 655 | // "function returning T" is adjusted to be of type "pointer to |
| 656 | // T" or "pointer to function returning T", respectively. |
| 657 | else if (T->isArrayType()) |
| 658 | // FIXME: Keep the type prior to promotion? |
| 659 | return Context.getArrayDecayedType(T); |
| 660 | else if (T->isFunctionType()) |
| 661 | // FIXME: Keep the type prior to promotion? |
| 662 | return Context.getPointerType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 663 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 664 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 665 | << T; |
| 666 | |
| 667 | return QualType(); |
| 668 | } |
| 669 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 670 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 671 | unsigned Depth, |
| 672 | unsigned Position, |
| 673 | SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 674 | Expr *Default) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 675 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 676 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 677 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 678 | assert(S->isTemplateParamScope() && |
| 679 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 680 | bool Invalid = false; |
| 681 | |
| 682 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 683 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 684 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 685 | LookupOrdinaryName, |
| 686 | ForRedeclaration); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 687 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 688 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 689 | PrevDecl = 0; |
| 690 | } |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 693 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 694 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 695 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 696 | Invalid = true; |
| 697 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 698 | |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 699 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 700 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 701 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 702 | D.getLocStart(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 703 | D.getIdentifierLoc(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 704 | Depth, Position, ParamName, T, |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 705 | IsParameterPack, TInfo); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 706 | Param->setAccess(AS_public); |
| 707 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 708 | if (Invalid) |
| 709 | Param->setInvalidDecl(); |
| 710 | |
| 711 | if (D.getIdentifier()) { |
| 712 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 713 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 714 | IdResolver.AddDecl(Param); |
| 715 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 716 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 717 | // C++0x [temp.param]p9: |
| 718 | // A default template-argument may be specified for any kind of |
| 719 | // template-parameter that is not a template parameter pack. |
| 720 | if (Default && IsParameterPack) { |
| 721 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 722 | Default = 0; |
| 723 | } |
| 724 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 725 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 726 | if (Default) { |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 727 | // Check for unexpanded parameter packs. |
| 728 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 729 | return Param; |
| 730 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 731 | TemplateArgument Converted; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 732 | ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
| 733 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 734 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 735 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 736 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 737 | Default = DefaultRes.take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 738 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 739 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 740 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 741 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 742 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 743 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 744 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 745 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 746 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 747 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 748 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 749 | SourceLocation TmpLoc, |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 750 | TemplateParameterList *Params, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 751 | SourceLocation EllipsisLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 752 | IdentifierInfo *Name, |
| 753 | SourceLocation NameLoc, |
| 754 | unsigned Depth, |
| 755 | unsigned Position, |
| 756 | SourceLocation EqualLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 757 | ParsedTemplateArgument Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 758 | assert(S->isTemplateParamScope() && |
| 759 | "Template template parameter not in template parameter scope!"); |
| 760 | |
| 761 | // Construct the parameter object. |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 762 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 763 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 764 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 765 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 766 | Depth, Position, IsParameterPack, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 767 | Name, Params); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 768 | Param->setAccess(AS_public); |
| 769 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 770 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 771 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 772 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 773 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 774 | IdResolver.AddDecl(Param); |
| 775 | } |
| 776 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 777 | if (Params->size() == 0) { |
| 778 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 779 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 780 | Param->setInvalidDecl(); |
| 781 | } |
| 782 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 783 | // C++0x [temp.param]p9: |
| 784 | // A default template-argument may be specified for any kind of |
| 785 | // template-parameter that is not a template parameter pack. |
| 786 | if (IsParameterPack && !Default.isInvalid()) { |
| 787 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 788 | Default = ParsedTemplateArgument(); |
| 789 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 790 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 791 | if (!Default.isInvalid()) { |
| 792 | // Check only that we have a template template argument. We don't want to |
| 793 | // try to check well-formedness now, because our template template parameter |
| 794 | // might have dependent types in its template parameters, which we wouldn't |
| 795 | // be able to match now. |
| 796 | // |
| 797 | // If none of the template template parameter's template arguments mention |
| 798 | // other template parameters, we could actually perform more checking here. |
| 799 | // However, it isn't worth doing. |
| 800 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 801 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 802 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 803 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 804 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 805 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 806 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 807 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 808 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 809 | DefaultArg.getArgument().getAsTemplate(), |
| 810 | UPPC_DefaultArgument)) |
| 811 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 812 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 813 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 814 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 815 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 816 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 819 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 820 | /// contains the template parameters in Params/NumParams. |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 821 | TemplateParameterList * |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 822 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 823 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 825 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 826 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 827 | SourceLocation RAngleLoc) { |
| 828 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 829 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 830 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 831 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 832 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 833 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 834 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 835 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 836 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 837 | if (SS.isSet()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 838 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 839 | } |
| 840 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 841 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 842 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 843 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 844 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 845 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 846 | TemplateParameterList *TemplateParams, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 847 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 848 | unsigned NumOuterTemplateParamLists, |
| 849 | TemplateParameterList** OuterTemplateParamLists) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 851 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 852 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 853 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 854 | |
| 855 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 856 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 857 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 858 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 859 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 860 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 861 | |
| 862 | // There is no such thing as an unnamed class template. |
| 863 | if (!Name) { |
| 864 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 865 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 869 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 870 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 871 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 872 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 873 | SemanticContext = computeDeclContext(SS, true); |
| 874 | if (!SemanticContext) { |
Douglas Gregor | 957ff27 | 2012-03-18 00:15:42 +0000 | [diff] [blame^] | 875 | Diag(NameLoc, diag::err_template_qualified_declarator_no_match) |
| 876 | << SS.getScopeRep() << SS.getRange(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 877 | return true; |
| 878 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 879 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 880 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 881 | return true; |
| 882 | |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 883 | // If we're adding a template to a dependent context, we may need to |
| 884 | // rebuilding some of the types used within the template parameter list, |
| 885 | // now that we know what the current instantiation is. |
| 886 | if (SemanticContext->isDependentContext()) { |
| 887 | ContextRAII SavedContext(*this, SemanticContext); |
| 888 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 889 | Invalid = true; |
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 890 | } else if (CurContext->isRecord() && TUK != TUK_Friend && |
| 891 | TUK != TUK_Reference) |
| 892 | diagnoseQualifiedDeclInClass(SS, SemanticContext, Name, NameLoc); |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 893 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 894 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 895 | } else { |
| 896 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 897 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 898 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 900 | if (Previous.isAmbiguous()) |
| 901 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 902 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 903 | NamedDecl *PrevDecl = 0; |
| 904 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 905 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 906 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 907 | // If there is a previous declaration with the same name, check |
| 908 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 909 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 910 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 911 | |
| 912 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 913 | // class template partial specialization, or class template specialization. |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 914 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 915 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 916 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 917 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 918 | PrevClassTemplate |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 919 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 920 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 921 | PrevClassTemplate |
| 922 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 923 | ->getSpecializedTemplate(); |
| 924 | } |
| 925 | } |
| 926 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 927 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 928 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 929 | // [...] When looking for a prior declaration of a class or a function |
| 930 | // 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] | 931 | // function is neither a qualified name nor a template-id, scopes outside |
| 932 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 933 | if (!SS.isSet()) { |
| 934 | DeclContext *OutermostContext = CurContext; |
| 935 | while (!OutermostContext->isFileContext()) |
| 936 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 937 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 938 | if (PrevDecl && |
| 939 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 940 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 941 | SemanticContext = PrevDecl->getDeclContext(); |
| 942 | } else { |
| 943 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 944 | // context we computed is the semantic context for our new |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 945 | // declaration. |
| 946 | PrevDecl = PrevClassTemplate = 0; |
| 947 | SemanticContext = OutermostContext; |
| 948 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 949 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 950 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 951 | if (CurContext->isDependentContext()) { |
| 952 | // If this is a dependent context, we don't want to link the friend |
| 953 | // class template to the template in scope, because that would perform |
| 954 | // checking of the template parameter lists that can't be performed |
| 955 | // until the outer context is instantiated. |
| 956 | PrevDecl = PrevClassTemplate = 0; |
| 957 | } |
| 958 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 959 | PrevDecl = PrevClassTemplate = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 960 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 961 | if (PrevClassTemplate) { |
| 962 | // Ensure that the template parameter lists are compatible. |
| 963 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 964 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 965 | /*Complain=*/true, |
| 966 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 967 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 968 | |
| 969 | // C++ [temp.class]p4: |
| 970 | // In a redeclaration, partial specialization, explicit |
| 971 | // specialization or explicit instantiation of a class template, |
| 972 | // the class-key shall agree in kind with the original class |
| 973 | // template declaration (7.1.5.3). |
| 974 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 975 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
| 976 | TUK == TUK_Definition, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 978 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 979 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 980 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 981 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 984 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 985 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 986 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 987 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 988 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 989 | // FIXME: Would it make sense to try to "forget" the previous |
| 990 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 991 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 992 | } |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 993 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 994 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 995 | // Maybe we will complain about the shadowed template parameter. |
| 996 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 997 | // Just pretend that we didn't see the previous declaration. |
| 998 | PrevDecl = 0; |
| 999 | } else if (PrevDecl) { |
| 1000 | // C++ [temp]p5: |
| 1001 | // A class template shall not have the same name as any other |
| 1002 | // template, class, function, object, enumeration, enumerator, |
| 1003 | // namespace, or type in the same scope (3.3), except as specified |
| 1004 | // in (14.5.4). |
| 1005 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1006 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1007 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1010 | // Check the template parameter list of this declaration, possibly |
| 1011 | // merging in the template parameter list from the previous class |
| 1012 | // template declaration. |
| 1013 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1014 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1015 | (SS.isSet() && SemanticContext && |
Douglas Gregor | 461bf2e | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 1016 | SemanticContext->isRecord() && |
| 1017 | SemanticContext->isDependentContext()) |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1018 | ? TPC_ClassTemplateMember |
| 1019 | : TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1020 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1021 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1022 | if (SS.isSet()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1023 | // 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] | 1024 | // template out-of-line. |
| 1025 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
Douglas Gregor | ea9f54a | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1026 | !(TUK == TUK_Friend && CurContext->isDependentContext())) { |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1027 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 1028 | << Name << SemanticContext << SS.getRange(); |
Douglas Gregor | ea9f54a | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1029 | Invalid = true; |
| 1030 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1033 | CXXRecordDecl *NewClass = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1034 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1035 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1036 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 1037 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1038 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1039 | if (NumOuterTemplateParamLists > 0) |
| 1040 | NewClass->setTemplateParameterListsInfo(Context, |
| 1041 | NumOuterTemplateParamLists, |
| 1042 | OuterTemplateParamLists); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1043 | |
Eli Friedman | 572ae0a | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1044 | // Add alignment attributes if necessary; these attributes are checked when |
| 1045 | // the ASTContext lays out the structure. |
| 1046 | AddAlignmentAttributesForRecord(NewClass); |
| 1047 | AddMsStructLayoutForRecord(NewClass); |
| 1048 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1049 | ClassTemplateDecl *NewTemplate |
| 1050 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1051 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1052 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1053 | NewClass->setDescribedClassTemplate(NewTemplate); |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1054 | |
Douglas Gregor | 2ccd89c | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1055 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1056 | NewTemplate->setModulePrivate(); |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1057 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1058 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1059 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1060 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1061 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1062 | (void)T; |
| 1063 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1064 | // 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] | 1065 | // class template, make a note of that. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1066 | if (PrevClassTemplate && |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1067 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1068 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1069 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1070 | // Set the access specifier. |
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1071 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1072 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1074 | // Set the lexical context of these templates |
| 1075 | NewClass->setLexicalDeclContext(CurContext); |
| 1076 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1077 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1078 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1079 | NewClass->startDefinition(); |
| 1080 | |
| 1081 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1082 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1083 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1084 | if (TUK != TUK_Friend) |
| 1085 | PushOnScopeChains(NewTemplate, S); |
| 1086 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1087 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1088 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1089 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1090 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1091 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1092 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1093 | PrevClassTemplate != NULL); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1094 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1095 | // Friend templates are visible in fairly strange ways. |
| 1096 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1097 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1098 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1099 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1100 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1101 | /* AddToContext = */ false); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1102 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1103 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1104 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1105 | NewClass->getLocation(), |
| 1106 | NewTemplate, |
| 1107 | /*FIXME:*/NewClass->getLocation()); |
| 1108 | Friend->setAccess(AS_public); |
| 1109 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1110 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1111 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1112 | if (Invalid) { |
| 1113 | NewTemplate->setInvalidDecl(); |
| 1114 | NewClass->setInvalidDecl(); |
| 1115 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1116 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1119 | /// \brief Diagnose the presence of a default template argument on a |
| 1120 | /// template parameter, which is ill-formed in certain contexts. |
| 1121 | /// |
| 1122 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1123 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1124 | Sema::TemplateParamListContext TPC, |
| 1125 | SourceLocation ParamLoc, |
| 1126 | SourceRange DefArgRange) { |
| 1127 | switch (TPC) { |
| 1128 | case Sema::TPC_ClassTemplate: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1129 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1130 | return false; |
| 1131 | |
| 1132 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1133 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1134 | // C++ [temp.param]p9: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1135 | // A default template-argument shall not be specified in a |
| 1136 | // function template declaration or a function template |
| 1137 | // definition [...] |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1138 | // If a friend function template declaration specifies a default |
| 1139 | // template-argument, that declaration shall be a definition and shall be |
| 1140 | // the only declaration of the function template in the translation unit. |
| 1141 | // (C++98/03 doesn't have this wording; see DR226). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1142 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1143 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1144 | : diag::ext_template_parameter_default_in_function_template) |
| 1145 | << DefArgRange; |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1146 | return false; |
| 1147 | |
| 1148 | case Sema::TPC_ClassTemplateMember: |
| 1149 | // C++0x [temp.param]p9: |
| 1150 | // A default template-argument shall not be specified in the |
| 1151 | // template-parameter-lists of the definition of a member of a |
| 1152 | // class template that appears outside of the member's class. |
| 1153 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1154 | << DefArgRange; |
| 1155 | return true; |
| 1156 | |
| 1157 | case Sema::TPC_FriendFunctionTemplate: |
| 1158 | // C++ [temp.param]p9: |
| 1159 | // A default template-argument shall not be specified in a |
| 1160 | // friend template declaration. |
| 1161 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1162 | << DefArgRange; |
| 1163 | return true; |
| 1164 | |
| 1165 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1166 | // for friend function templates if there is only a single |
| 1167 | // declaration (and it is a definition). Strange! |
| 1168 | } |
| 1169 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1170 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1173 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1174 | /// of a template template parameter, recursively. |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1175 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1176 | TemplateTemplateParmDecl *TTP) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1177 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1178 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1179 | NamedDecl *P = Params->getParam(I); |
| 1180 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1181 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1182 | NTTP->getTypeSourceInfo(), |
| 1183 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1184 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1185 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1186 | continue; |
| 1187 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1188 | |
| 1189 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1190 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1191 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1192 | return true; |
| 1193 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1194 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1195 | return false; |
| 1196 | } |
| 1197 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1198 | /// \brief Checks the validity of a template parameter list, possibly |
| 1199 | /// considering the template parameter list from a previous |
| 1200 | /// declaration. |
| 1201 | /// |
| 1202 | /// If an "old" template parameter list is provided, it must be |
| 1203 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1204 | /// template parameter list. |
| 1205 | /// |
| 1206 | /// \param NewParams Template parameter list for a new template |
| 1207 | /// declaration. This template parameter list will be updated with any |
| 1208 | /// default arguments that are carried through from the previous |
| 1209 | /// template parameter list. |
| 1210 | /// |
| 1211 | /// \param OldParams If provided, template parameter list from a |
| 1212 | /// previous declaration of the same template. Default template |
| 1213 | /// arguments will be merged from the old template parameter list to |
| 1214 | /// the new template parameter list. |
| 1215 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1216 | /// \param TPC Describes the context in which we are checking the given |
| 1217 | /// template parameter list. |
| 1218 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1219 | /// \returns true if an error occurred, false otherwise. |
| 1220 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1221 | TemplateParameterList *OldParams, |
| 1222 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1223 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1224 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1225 | // C++ [temp.param]p10: |
| 1226 | // The set of default template-arguments available for use with a |
| 1227 | // template declaration or definition is obtained by merging the |
| 1228 | // default arguments from the definition (if in scope) and all |
| 1229 | // declarations in scope in the same way default function |
| 1230 | // arguments are (8.3.6). |
| 1231 | bool SawDefaultArgument = false; |
| 1232 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1233 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1234 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1235 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1236 | if (OldParams) |
| 1237 | OldParam = OldParams->begin(); |
| 1238 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1239 | bool RemoveDefaultArguments = false; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1240 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1241 | NewParamEnd = NewParams->end(); |
| 1242 | NewParam != NewParamEnd; ++NewParam) { |
| 1243 | // Variables used to diagnose redundant default arguments |
| 1244 | bool RedundantDefaultArg = false; |
| 1245 | SourceLocation OldDefaultLoc; |
| 1246 | SourceLocation NewDefaultLoc; |
| 1247 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1248 | // Variable used to diagnose missing default arguments |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1249 | bool MissingDefaultArg = false; |
| 1250 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1251 | // Variable used to diagnose non-final parameter packs |
| 1252 | bool SawParameterPack = false; |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1253 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1254 | if (TemplateTypeParmDecl *NewTypeParm |
| 1255 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1256 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1257 | if (NewTypeParm->hasDefaultArgument() && |
| 1258 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1259 | NewTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1260 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1261 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1262 | NewTypeParm->removeDefaultArgument(); |
| 1263 | |
| 1264 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1266 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1268 | if (NewTypeParm->isParameterPack()) { |
| 1269 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1270 | "Parameter packs can't have a default argument!"); |
| 1271 | SawParameterPack = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1272 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1273 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1274 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1275 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1276 | SawDefaultArgument = true; |
| 1277 | RedundantDefaultArg = true; |
| 1278 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1279 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1280 | // Merge the default argument from the old declaration to the |
| 1281 | // new declaration. |
| 1282 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1283 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1284 | true); |
| 1285 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1286 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1287 | SawDefaultArgument = true; |
| 1288 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1289 | } else if (SawDefaultArgument) |
| 1290 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1291 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1292 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1293 | // Check for unexpanded parameter packs. |
| 1294 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1295 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1296 | UPPC_NonTypeTemplateParameterType)) { |
| 1297 | Invalid = true; |
| 1298 | continue; |
| 1299 | } |
| 1300 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1301 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1302 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1303 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1304 | NewNonTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1305 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1306 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1309 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1310 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1311 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1312 | if (NewNonTypeParm->isParameterPack()) { |
| 1313 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1314 | "Parameter packs can't have a default argument!"); |
| 1315 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1316 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1317 | NewNonTypeParm->hasDefaultArgument()) { |
| 1318 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1319 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1320 | SawDefaultArgument = true; |
| 1321 | RedundantDefaultArg = true; |
| 1322 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1323 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1324 | // Merge the default argument from the old declaration to the |
| 1325 | // new declaration. |
| 1326 | SawDefaultArgument = true; |
| 1327 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1328 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1329 | // parameter. |
| 1330 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1331 | OldNonTypeParm->getDefaultArgument(), |
| 1332 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1333 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1334 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1335 | SawDefaultArgument = true; |
| 1336 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1337 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1338 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1339 | } else { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1340 | TemplateTemplateParmDecl *NewTemplateParm |
| 1341 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1342 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1343 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1344 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1345 | Invalid = true; |
| 1346 | continue; |
| 1347 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1348 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1349 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1350 | if (NewTemplateParm->hasDefaultArgument() && |
| 1351 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1352 | NewTemplateParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1353 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1354 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1355 | |
| 1356 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1357 | TemplateTemplateParmDecl *OldTemplateParm |
| 1358 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1359 | if (NewTemplateParm->isParameterPack()) { |
| 1360 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1361 | "Parameter packs can't have a default argument!"); |
| 1362 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1363 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1364 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1365 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1366 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1367 | SawDefaultArgument = true; |
| 1368 | RedundantDefaultArg = true; |
| 1369 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1370 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1371 | // Merge the default argument from the old declaration to the |
| 1372 | // new declaration. |
| 1373 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1374 | // FIXME: We need to create a new kind of "default argument" expression |
| 1375 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1376 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1377 | OldTemplateParm->getDefaultArgument(), |
| 1378 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1379 | PreviousDefaultArgLoc |
| 1380 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1381 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1382 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1383 | PreviousDefaultArgLoc |
| 1384 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1385 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1386 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1389 | // C++0x [temp.param]p11: |
| 1390 | // If a template parameter of a primary class template or alias template |
| 1391 | // is a template parameter pack, it shall be the last template parameter. |
| 1392 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
| 1393 | (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) { |
| 1394 | Diag((*NewParam)->getLocation(), |
| 1395 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1396 | Invalid = true; |
| 1397 | } |
| 1398 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1399 | if (RedundantDefaultArg) { |
| 1400 | // C++ [temp.param]p12: |
| 1401 | // A template-parameter shall not be given default arguments |
| 1402 | // by two different declarations in the same scope. |
| 1403 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1404 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1405 | Invalid = true; |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1406 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1407 | // C++ [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1408 | // If a template-parameter of a class template has a default |
| 1409 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1410 | // have a default template-argument supplied or be a template parameter |
| 1411 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1413 | diag::err_template_param_default_arg_missing); |
| 1414 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1415 | Invalid = true; |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1416 | RemoveDefaultArguments = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | // If we have an old template parameter list that we're merging |
| 1420 | // in, move on to the next parameter. |
| 1421 | if (OldParams) |
| 1422 | ++OldParam; |
| 1423 | } |
| 1424 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1425 | // We were missing some default arguments at the end of the list, so remove |
| 1426 | // all of the default arguments. |
| 1427 | if (RemoveDefaultArguments) { |
| 1428 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1429 | NewParamEnd = NewParams->end(); |
| 1430 | NewParam != NewParamEnd; ++NewParam) { |
| 1431 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1432 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1433 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1434 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1435 | NTTP->removeDefaultArgument(); |
| 1436 | else |
| 1437 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1438 | } |
| 1439 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1440 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1441 | return Invalid; |
| 1442 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1443 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1444 | namespace { |
| 1445 | |
| 1446 | /// A class which looks for a use of a certain level of template |
| 1447 | /// parameter. |
| 1448 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1449 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1450 | |
| 1451 | unsigned Depth; |
| 1452 | bool Match; |
| 1453 | |
| 1454 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1455 | NamedDecl *ND = Params->getParam(0); |
| 1456 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1457 | Depth = PD->getDepth(); |
| 1458 | } else if (NonTypeTemplateParmDecl *PD = |
| 1459 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1460 | Depth = PD->getDepth(); |
| 1461 | } else { |
| 1462 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | bool Matches(unsigned ParmDepth) { |
| 1467 | if (ParmDepth >= Depth) { |
| 1468 | Match = true; |
| 1469 | return true; |
| 1470 | } |
| 1471 | return false; |
| 1472 | } |
| 1473 | |
| 1474 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1475 | return !Matches(T->getDepth()); |
| 1476 | } |
| 1477 | |
| 1478 | bool TraverseTemplateName(TemplateName N) { |
| 1479 | if (TemplateTemplateParmDecl *PD = |
| 1480 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1481 | if (Matches(PD->getDepth())) return false; |
| 1482 | return super::TraverseTemplateName(N); |
| 1483 | } |
| 1484 | |
| 1485 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1486 | if (NonTypeTemplateParmDecl *PD = |
| 1487 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1488 | if (PD->getDepth() == Depth) { |
| 1489 | Match = true; |
| 1490 | return false; |
| 1491 | } |
| 1492 | } |
| 1493 | return super::VisitDeclRefExpr(E); |
| 1494 | } |
Douglas Gregor | 18c8339 | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1495 | |
| 1496 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1497 | return TraverseType(T->getInjectedSpecializationType()); |
| 1498 | } |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1499 | }; |
| 1500 | } |
| 1501 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1502 | /// Determines whether a given type depends on the given parameter |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1503 | /// list. |
| 1504 | static bool |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1505 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1506 | DependencyChecker Checker(Params); |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1507 | Checker.TraverseType(T); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1508 | return Checker.Match; |
| 1509 | } |
| 1510 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1511 | // Find the source range corresponding to the named type in the given |
| 1512 | // nested-name-specifier, if any. |
| 1513 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1514 | QualType T, |
| 1515 | const CXXScopeSpec &SS) { |
| 1516 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1517 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1518 | if (const Type *CurType = NNS->getAsType()) { |
| 1519 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1520 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1521 | } else |
| 1522 | break; |
| 1523 | |
| 1524 | NNSLoc = NNSLoc.getPrefix(); |
| 1525 | } |
| 1526 | |
| 1527 | return SourceRange(); |
| 1528 | } |
| 1529 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1530 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1531 | /// specifier, returning the template parameter list that applies to the |
| 1532 | /// name. |
| 1533 | /// |
| 1534 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1535 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | /// |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1537 | /// \param DeclLoc The location of the declaration itself. |
| 1538 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1539 | /// \param SS the scope specifier that will be matched to the given template |
| 1540 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1541 | /// being declared. |
| 1542 | /// |
| 1543 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1544 | /// innermost template parameter lists. |
| 1545 | /// |
| 1546 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1547 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1548 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1549 | /// matching template parameters to scope specifiers in friend |
| 1550 | /// declarations. |
| 1551 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1552 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1553 | /// declared is an explicit specialization, false otherwise. |
| 1554 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1556 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1557 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1559 | /// 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] | 1560 | /// itself a template). |
| 1561 | TemplateParameterList * |
| 1562 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1563 | SourceLocation DeclLoc, |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1564 | const CXXScopeSpec &SS, |
| 1565 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1566 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1567 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1568 | bool &IsExplicitSpecialization, |
| 1569 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1570 | IsExplicitSpecialization = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1571 | Invalid = false; |
| 1572 | |
| 1573 | // The sequence of nested types to which we will match up the template |
| 1574 | // parameter lists. We first build this list by starting with the type named |
| 1575 | // 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] | 1576 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1577 | QualType T; |
Douglas Gregor | 714c992 | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1578 | if (SS.getScopeRep()) { |
| 1579 | if (CXXRecordDecl *Record |
| 1580 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1581 | T = Context.getTypeDeclType(Record); |
| 1582 | else |
| 1583 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1584 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1585 | |
| 1586 | // If we found an explicit specialization that prevents us from needing |
| 1587 | // 'template<>' headers, this will be set to the location of that |
| 1588 | // explicit specialization. |
| 1589 | SourceLocation ExplicitSpecLoc; |
| 1590 | |
| 1591 | while (!T.isNull()) { |
| 1592 | NestedTypes.push_back(T); |
| 1593 | |
| 1594 | // Retrieve the parent of a record type. |
| 1595 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1596 | // If this type is an explicit specialization, we're done. |
| 1597 | if (ClassTemplateSpecializationDecl *Spec |
| 1598 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1599 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1600 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1601 | ExplicitSpecLoc = Spec->getLocation(); |
| 1602 | break; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1603 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1604 | } else if (Record->getTemplateSpecializationKind() |
| 1605 | == TSK_ExplicitSpecialization) { |
| 1606 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1607 | break; |
| 1608 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1609 | |
| 1610 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1611 | T = Context.getTypeDeclType(Parent); |
| 1612 | else |
| 1613 | T = QualType(); |
| 1614 | continue; |
| 1615 | } |
| 1616 | |
| 1617 | if (const TemplateSpecializationType *TST |
| 1618 | = T->getAs<TemplateSpecializationType>()) { |
| 1619 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1620 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1621 | T = Context.getTypeDeclType(Parent); |
| 1622 | else |
| 1623 | T = QualType(); |
| 1624 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1625 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | // Look one step prior in a dependent template specialization type. |
| 1629 | if (const DependentTemplateSpecializationType *DependentTST |
| 1630 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1631 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1632 | T = QualType(NNS->getAsType(), 0); |
| 1633 | else |
| 1634 | T = QualType(); |
| 1635 | continue; |
| 1636 | } |
| 1637 | |
| 1638 | // Look one step prior in a dependent name type. |
| 1639 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1640 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1641 | T = QualType(NNS->getAsType(), 0); |
| 1642 | else |
| 1643 | T = QualType(); |
| 1644 | continue; |
| 1645 | } |
| 1646 | |
| 1647 | // Retrieve the parent of an enumeration type. |
| 1648 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1649 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1650 | // check here. |
| 1651 | EnumDecl *Enum = EnumT->getDecl(); |
| 1652 | |
| 1653 | // Get to the parent type. |
| 1654 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1655 | T = Context.getTypeDeclType(Parent); |
| 1656 | else |
| 1657 | T = QualType(); |
| 1658 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1659 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1660 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1661 | T = QualType(); |
| 1662 | } |
| 1663 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1664 | // to the innermost while checking template-parameter-lists. |
| 1665 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1666 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1667 | // C++0x [temp.expl.spec]p17: |
| 1668 | // A member or a member template may be nested within many |
| 1669 | // enclosing class templates. In an explicit specialization for |
| 1670 | // such a member, the member declaration shall be preceded by a |
| 1671 | // template<> for each enclosing class template that is |
| 1672 | // explicitly specialized. |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1673 | bool SawNonEmptyTemplateParameterList = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1674 | unsigned ParamIdx = 0; |
| 1675 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1676 | ++TypeIdx) { |
| 1677 | T = NestedTypes[TypeIdx]; |
| 1678 | |
| 1679 | // Whether we expect a 'template<>' header. |
| 1680 | bool NeedEmptyTemplateHeader = false; |
| 1681 | |
| 1682 | // Whether we expect a template header with parameters. |
| 1683 | bool NeedNonemptyTemplateHeader = false; |
| 1684 | |
| 1685 | // For a dependent type, the set of template parameters that we |
| 1686 | // expect to see. |
| 1687 | TemplateParameterList *ExpectedTemplateParams = 0; |
| 1688 | |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1689 | // C++0x [temp.expl.spec]p15: |
| 1690 | // A member or a member template may be nested within many enclosing |
| 1691 | // class templates. In an explicit specialization for such a member, the |
| 1692 | // member declaration shall be preceded by a template<> for each |
| 1693 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1694 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1695 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1696 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1697 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1698 | NeedNonemptyTemplateHeader = true; |
| 1699 | } else if (Record->isDependentType()) { |
| 1700 | if (Record->getDescribedClassTemplate()) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1701 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1702 | ->getTemplateParameters(); |
| 1703 | NeedNonemptyTemplateHeader = true; |
| 1704 | } |
| 1705 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1706 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1707 | // C++0x [temp.expl.spec]p4: |
| 1708 | // Members of an explicitly specialized class template are defined |
| 1709 | // in the same manner as members of normal classes, and not using |
| 1710 | // the template<> syntax. |
| 1711 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1712 | NeedEmptyTemplateHeader = true; |
| 1713 | else |
Douglas Gregor | 95ea450 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1714 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1715 | } else if (Record->getTemplateSpecializationKind()) { |
| 1716 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1717 | != TSK_ExplicitSpecialization && |
| 1718 | TypeIdx == NumTypes - 1) |
| 1719 | IsExplicitSpecialization = true; |
| 1720 | |
| 1721 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1722 | } |
| 1723 | } else if (const TemplateSpecializationType *TST |
| 1724 | = T->getAs<TemplateSpecializationType>()) { |
| 1725 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1726 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1727 | NeedNonemptyTemplateHeader = true; |
| 1728 | } |
| 1729 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1730 | // FIXME: We actually could/should check the template arguments here |
| 1731 | // against the corresponding template parameter list. |
| 1732 | NeedNonemptyTemplateHeader = false; |
| 1733 | } |
| 1734 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1735 | // C++ [temp.expl.spec]p16: |
| 1736 | // In an explicit specialization declaration for a member of a class |
| 1737 | // template or a member template that ap- pears in namespace scope, the |
| 1738 | // member template and some of its enclosing class templates may remain |
| 1739 | // unspecialized, except that the declaration shall not explicitly |
| 1740 | // specialize a class member template if its en- closing class templates |
| 1741 | // are not explicitly specialized as well. |
| 1742 | if (ParamIdx < NumParamLists) { |
| 1743 | if (ParamLists[ParamIdx]->size() == 0) { |
| 1744 | if (SawNonEmptyTemplateParameterList) { |
| 1745 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1746 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1747 | Invalid = true; |
| 1748 | IsExplicitSpecialization = false; |
| 1749 | return 0; |
| 1750 | } |
| 1751 | } else |
| 1752 | SawNonEmptyTemplateParameterList = true; |
| 1753 | } |
| 1754 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1755 | if (NeedEmptyTemplateHeader) { |
| 1756 | // If we're on the last of the types, and we need a 'template<>' header |
| 1757 | // here, then it's an explicit specialization. |
| 1758 | if (TypeIdx == NumTypes - 1) |
| 1759 | IsExplicitSpecialization = true; |
| 1760 | |
| 1761 | if (ParamIdx < NumParamLists) { |
| 1762 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1763 | // The header has template parameters when it shouldn't. Complain. |
| 1764 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1765 | diag::err_template_param_list_matches_nontemplate) |
| 1766 | << T |
| 1767 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1768 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1769 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1770 | Invalid = true; |
| 1771 | return 0; |
| 1772 | } |
| 1773 | |
| 1774 | // Consume this template header. |
| 1775 | ++ParamIdx; |
| 1776 | continue; |
| 1777 | } |
| 1778 | |
| 1779 | if (!IsFriend) { |
| 1780 | // We don't have a template header, but we should. |
| 1781 | SourceLocation ExpectedTemplateLoc; |
| 1782 | if (NumParamLists > 0) |
| 1783 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1784 | else |
| 1785 | ExpectedTemplateLoc = DeclStartLoc; |
| 1786 | |
| 1787 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1788 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS) |
| 1789 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1790 | } |
| 1791 | |
| 1792 | continue; |
| 1793 | } |
| 1794 | |
| 1795 | if (NeedNonemptyTemplateHeader) { |
| 1796 | // In friend declarations we can have template-ids which don't |
| 1797 | // depend on the corresponding template parameter lists. But |
| 1798 | // assume that empty parameter lists are supposed to match this |
| 1799 | // template-id. |
| 1800 | if (IsFriend && T->isDependentType()) { |
| 1801 | if (ParamIdx < NumParamLists && |
| 1802 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
| 1803 | ExpectedTemplateParams = 0; |
| 1804 | else |
| 1805 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1806 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1807 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1808 | if (ParamIdx < NumParamLists) { |
| 1809 | // Check the template parameter list, if we can. |
| 1810 | if (ExpectedTemplateParams && |
| 1811 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1812 | ExpectedTemplateParams, |
| 1813 | true, TPL_TemplateMatch)) |
| 1814 | Invalid = true; |
| 1815 | |
| 1816 | if (!Invalid && |
| 1817 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1818 | TPC_ClassTemplateMember)) |
| 1819 | Invalid = true; |
| 1820 | |
| 1821 | ++ParamIdx; |
| 1822 | continue; |
| 1823 | } |
| 1824 | |
| 1825 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1826 | << T |
| 1827 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1828 | Invalid = true; |
| 1829 | continue; |
| 1830 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1831 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1832 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1833 | // If there were at least as many template-ids as there were template |
| 1834 | // parameter lists, then there are no template parameter lists remaining for |
| 1835 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1836 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1837 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1839 | // If there were too many template parameter lists, complain about that now. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1840 | if (ParamIdx < NumParamLists - 1) { |
| 1841 | bool HasAnyExplicitSpecHeader = false; |
| 1842 | bool AllExplicitSpecHeaders = true; |
| 1843 | for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) { |
| 1844 | if (ParamLists[I]->size() == 0) |
| 1845 | HasAnyExplicitSpecHeader = true; |
| 1846 | else |
| 1847 | AllExplicitSpecHeaders = false; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1848 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1849 | |
| 1850 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1851 | AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers |
| 1852 | : diag::err_template_spec_extra_headers) |
| 1853 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1854 | ParamLists[NumParamLists - 2]->getRAngleLoc()); |
| 1855 | |
| 1856 | // If there was a specialization somewhere, such that 'template<>' is |
| 1857 | // not required, and there were any 'template<>' headers, note where the |
| 1858 | // specialization occurred. |
| 1859 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1860 | Diag(ExplicitSpecLoc, |
| 1861 | diag::note_explicit_template_spec_does_not_need_header) |
| 1862 | << NestedTypes.back(); |
| 1863 | |
| 1864 | // We have a template parameter list with no corresponding scope, which |
| 1865 | // means that the resulting template declaration can't be instantiated |
| 1866 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1867 | if (!AllExplicitSpecHeaders) |
| 1868 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1869 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1870 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1871 | // C++ [temp.expl.spec]p16: |
| 1872 | // In an explicit specialization declaration for a member of a class |
| 1873 | // template or a member template that ap- pears in namespace scope, the |
| 1874 | // member template and some of its enclosing class templates may remain |
| 1875 | // unspecialized, except that the declaration shall not explicitly |
| 1876 | // specialize a class member template if its en- closing class templates |
| 1877 | // are not explicitly specialized as well. |
| 1878 | if (ParamLists[NumParamLists - 1]->size() == 0 && |
| 1879 | SawNonEmptyTemplateParameterList) { |
| 1880 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1881 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1882 | Invalid = true; |
| 1883 | IsExplicitSpecialization = false; |
| 1884 | return 0; |
| 1885 | } |
| 1886 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1887 | // Return the last template parameter list, which corresponds to the |
| 1888 | // entity being declared. |
| 1889 | return ParamLists[NumParamLists - 1]; |
| 1890 | } |
| 1891 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1892 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1893 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1894 | Diag(Template->getLocation(), diag::note_template_declared_here) |
| 1895 | << (isa<FunctionTemplateDecl>(Template)? 0 |
| 1896 | : isa<ClassTemplateDecl>(Template)? 1 |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1897 | : isa<TypeAliasTemplateDecl>(Template)? 2 |
| 1898 | : 3) |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1899 | << Template->getDeclName(); |
| 1900 | return; |
| 1901 | } |
| 1902 | |
| 1903 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1904 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1905 | IEnd = OST->end(); |
| 1906 | I != IEnd; ++I) |
| 1907 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1908 | << 0 << (*I)->getDeclName(); |
| 1909 | |
| 1910 | return; |
| 1911 | } |
| 1912 | } |
| 1913 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1914 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1915 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1916 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1917 | DependentTemplateName *DTN |
| 1918 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1919 | if (DTN && DTN->isIdentifier()) |
| 1920 | // When building a template-id where the template-name is dependent, |
| 1921 | // assume the template is a type template. Either our assumption is |
| 1922 | // correct, or the code is ill-formed and will be diagnosed when the |
| 1923 | // dependent name is substituted. |
| 1924 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 1925 | DTN->getQualifier(), |
| 1926 | DTN->getIdentifier(), |
| 1927 | TemplateArgs); |
| 1928 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1929 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1930 | if (!Template || isa<FunctionTemplateDecl>(Template)) { |
| 1931 | // We might have a substituted template template parameter pack. If so, |
| 1932 | // build a template specialization type for it. |
| 1933 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 1934 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1935 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1936 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 1937 | << Name; |
| 1938 | NoteAllFoundTemplates(Name); |
| 1939 | return QualType(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1940 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1941 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1942 | // Check that the template argument list is well-formed for this |
| 1943 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1944 | SmallVector<TemplateArgument, 4> Converted; |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1945 | bool ExpansionIntoFixedList = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1946 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1947 | false, Converted, &ExpansionIntoFixedList)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1948 | return QualType(); |
| 1949 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1950 | QualType CanonType; |
| 1951 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1952 | bool InstantiationDependent = false; |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1953 | TypeAliasTemplateDecl *AliasTemplate = 0; |
| 1954 | if (!ExpansionIntoFixedList && |
| 1955 | (AliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Template))) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1956 | // Find the canonical type for this type alias template specialization. |
| 1957 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 1958 | if (Pattern->isInvalidDecl()) |
| 1959 | return QualType(); |
| 1960 | |
| 1961 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1962 | Converted.data(), Converted.size()); |
| 1963 | |
| 1964 | // Only substitute for the innermost template argument list. |
| 1965 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 1804174 | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 1966 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | aff37b4 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 1967 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 1968 | for (unsigned I = 0; I < Depth; ++I) |
| 1969 | TemplateArgLists.addOuterTemplateArguments(0, 0); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1970 | |
| 1971 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
| 1972 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 1973 | TemplateArgLists, AliasTemplate->getLocation(), |
| 1974 | AliasTemplate->getDeclName()); |
| 1975 | if (CanonType.isNull()) |
| 1976 | return QualType(); |
| 1977 | } else if (Name.isDependent() || |
| 1978 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1979 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1980 | // This class template specialization is a dependent |
| 1981 | // type. Therefore, its canonical type is another class template |
| 1982 | // specialization type that contains all of the converted |
| 1983 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1984 | // A<T, T> have identical types when A is declared as: |
| 1985 | // |
| 1986 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1987 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1989 | Converted.data(), |
| 1990 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1991 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1992 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1993 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1994 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1995 | // build the canonical type and return that to us. |
| 1996 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1997 | |
| 1998 | // This might work out to be a current instantiation, in which |
| 1999 | // case the canonical type needs to be the InjectedClassNameType. |
| 2000 | // |
| 2001 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2002 | // changes to CurContext don't change the set of current |
| 2003 | // instantiations. |
| 2004 | if (isa<ClassTemplateDecl>(Template)) { |
| 2005 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2006 | // If we get out to a namespace, we're done. |
| 2007 | if (Ctx->isFileContext()) break; |
| 2008 | |
| 2009 | // If this isn't a record, keep looking. |
| 2010 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2011 | if (!Record) continue; |
| 2012 | |
| 2013 | // Look for one of the two cases with InjectedClassNameTypes |
| 2014 | // and check whether it's the same template. |
| 2015 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2016 | !Record->getDescribedClassTemplate()) |
| 2017 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2018 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2019 | // Fetch the injected class name type and check whether its |
| 2020 | // injected type is equal to the type we just built. |
| 2021 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2022 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2023 | ->getInjectedSpecializationType(); |
| 2024 | |
| 2025 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2026 | continue; |
| 2027 | |
| 2028 | // If so, the canonical type of this TST is the injected |
| 2029 | // class name type of the record we just found. |
| 2030 | assert(ICNT.isCanonical()); |
| 2031 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2032 | break; |
| 2033 | } |
| 2034 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2035 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2036 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2037 | // Find the class template specialization declaration that |
| 2038 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2039 | void *InsertPos = 0; |
| 2040 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2041 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2042 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2043 | if (!Decl) { |
| 2044 | // This is the first time we have referenced this class template |
| 2045 | // specialization. Create the canonical declaration and add it to |
| 2046 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2047 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2048 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2049 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 09d8212 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2050 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2051 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2052 | ClassTemplate, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2053 | Converted.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2054 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2055 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2056 | Decl->setLexicalDeclContext(CurContext); |
| 2057 | } |
| 2058 | |
| 2059 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2060 | assert(isa<RecordType>(CanonType) && |
| 2061 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2062 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2063 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2064 | // Build the fully-sugared type for this class template |
| 2065 | // specialization, which refers back to the class template |
| 2066 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2067 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2070 | TypeResult |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2071 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2072 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2073 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2074 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2075 | SourceLocation RAngleLoc, |
| 2076 | bool IsCtorOrDtorName) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2077 | if (SS.isInvalid()) |
| 2078 | return true; |
| 2079 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2080 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2081 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2082 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2083 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2084 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2085 | |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2086 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2087 | QualType T |
| 2088 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2089 | DTN->getQualifier(), |
| 2090 | DTN->getIdentifier(), |
| 2091 | TemplateArgs); |
| 2092 | // Build type-source information. |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2093 | TypeLocBuilder TLB; |
| 2094 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2095 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2096 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2097 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 66581d4 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2098 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2099 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2100 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2101 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2102 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2103 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2104 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2105 | } |
| 2106 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2107 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2108 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2109 | |
| 2110 | if (Result.isNull()) |
| 2111 | return true; |
| 2112 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2113 | // Build type-source information. |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2114 | TypeLocBuilder TLB; |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2115 | TemplateSpecializationTypeLoc SpecTL |
| 2116 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2117 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2118 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2119 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2120 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2121 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2122 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2123 | |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2124 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2125 | // constructor or destructor name (in such a case, the scope specifier |
| 2126 | // will be attached to the enclosing Decl or Expr node). |
| 2127 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2128 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2129 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2130 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2131 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2132 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2133 | } |
| 2134 | |
| 2135 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2136 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2137 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2138 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2139 | TypeSpecifierType TagSpec, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2140 | SourceLocation TagLoc, |
| 2141 | CXXScopeSpec &SS, |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2142 | SourceLocation TemplateKWLoc, |
| 2143 | TemplateTy TemplateD, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2144 | SourceLocation TemplateLoc, |
| 2145 | SourceLocation LAngleLoc, |
| 2146 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2147 | SourceLocation RAngleLoc) { |
| 2148 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
| 2149 | |
| 2150 | // Translate the parser's template argument list in our AST format. |
| 2151 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2152 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2153 | |
| 2154 | // Determine the tag kind |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2155 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2156 | ElaboratedTypeKeyword Keyword |
| 2157 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2158 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2159 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2160 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2161 | DTN->getQualifier(), |
| 2162 | DTN->getIdentifier(), |
| 2163 | TemplateArgs); |
| 2164 | |
| 2165 | // Build type-source information. |
| 2166 | TypeLocBuilder TLB; |
| 2167 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2168 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2169 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2170 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 66581d4 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2171 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2172 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2173 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2174 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2175 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2176 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2177 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2178 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2179 | |
| 2180 | if (TypeAliasTemplateDecl *TAT = |
| 2181 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2182 | // C++0x [dcl.type.elab]p2: |
| 2183 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2184 | // resolves to an alias template specialization, the |
| 2185 | // elaborated-type-specifier is ill-formed. |
| 2186 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2187 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2188 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2189 | |
| 2190 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2191 | if (Result.isNull()) |
Matt Beaumont-Gay | 3a51d41 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2192 | return TypeResult(true); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2193 | |
| 2194 | // Check the tag kind |
| 2195 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2196 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2197 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2198 | IdentifierInfo *Id = D->getIdentifier(); |
| 2199 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2200 | |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2201 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 2202 | TagLoc, *Id)) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2203 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2204 | << Result |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2205 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2206 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2207 | } |
| 2208 | } |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2209 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2210 | // Provide source-location information for the template specialization. |
| 2211 | TypeLocBuilder TLB; |
| 2212 | TemplateSpecializationTypeLoc SpecTL |
| 2213 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2214 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2215 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2216 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2217 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2218 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2219 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2220 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2221 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2222 | // and tag keyword. |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2223 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2224 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2225 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2226 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2227 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2230 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2231 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2232 | LookupResult &R, |
| 2233 | bool RequiresADL, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2234 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2235 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2236 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2237 | // 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] | 2238 | // though. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2239 | // foo<int> could identify a single function unambiguously |
| 2240 | // This approach does NOT work, since f<int>(1); |
| 2241 | // gets resolved prior to resorting to overload resolution |
| 2242 | // i.e., template<class T> void f(double); |
| 2243 | // vs template<class T, class U> void f(U); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2244 | |
| 2245 | // These should be filtered out by our callers. |
| 2246 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2247 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2248 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2249 | // We don't want lookup warnings at this point. |
| 2250 | R.suppressDiagnostics(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2251 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2252 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2253 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2254 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2255 | TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2256 | R.getLookupNameInfo(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2257 | RequiresADL, TemplateArgs, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2258 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2259 | |
| 2260 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2263 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2264 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2265 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2266 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2267 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2268 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2269 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2270 | DeclContext *DC; |
| 2271 | if (!(DC = computeDeclContext(SS, false)) || |
| 2272 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2273 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2274 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2275 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2276 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2277 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2278 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 2279 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2280 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2281 | if (R.isAmbiguous()) |
| 2282 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2283 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2284 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2285 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2286 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2287 | return ExprError(); |
| 2288 | } |
| 2289 | |
| 2290 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2291 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 2292 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 2293 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2294 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2295 | return ExprError(); |
| 2296 | } |
| 2297 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2298 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2301 | /// \brief Form a dependent template name. |
| 2302 | /// |
| 2303 | /// This action forms a dependent template name given the template |
| 2304 | /// name and its (presumably dependent) scope specifier. For |
| 2305 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2306 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2307 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2308 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2309 | CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2310 | SourceLocation TemplateKWLoc, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2311 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2312 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2313 | bool EnteringContext, |
| 2314 | TemplateTy &Result) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2315 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 2316 | Diag(TemplateKWLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2317 | getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2318 | diag::warn_cxx98_compat_template_outside_of_template : |
| 2319 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2320 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2321 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2322 | DeclContext *LookupCtx = 0; |
| 2323 | if (SS.isSet()) |
| 2324 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2325 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2326 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2327 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2328 | // C++0x [temp.names]p5: |
| 2329 | // If a name prefixed by the keyword template is not the name of |
| 2330 | // a template, the program is ill-formed. [Note: the keyword |
| 2331 | // template may not be applied to non-template members of class |
| 2332 | // templates. -end note ] [ Note: as is the case with the |
| 2333 | // typename prefix, the template prefix is allowed in cases |
| 2334 | // where it is not strictly necessary; i.e., when the |
| 2335 | // nested-name-specifier or the expression on the left of the -> |
| 2336 | // or . is not dependent on a template-parameter, or the use |
| 2337 | // does not appear in the scope of a template. -end note] |
| 2338 | // |
| 2339 | // Note: C++03 was more strict here, because it banned the use of |
| 2340 | // the "template" keyword prior to a template-name that was not a |
| 2341 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2342 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2343 | // 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] | 2344 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2345 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 2346 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2347 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2348 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2349 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | d078bd2 | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2350 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2351 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2352 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2353 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2354 | Diag(Name.getLocStart(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2355 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2356 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2357 | << Name.getSourceRange() |
| 2358 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2359 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2360 | } else { |
| 2361 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2362 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2363 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2366 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2367 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2368 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2369 | switch (Name.getKind()) { |
| 2370 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2371 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2372 | Name.Identifier)); |
| 2373 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2374 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2375 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2376 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2377 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2378 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2379 | |
| 2380 | case UnqualifiedId::IK_LiteralOperatorId: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2381 | llvm_unreachable( |
| 2382 | "We don't support these; Parse shouldn't have allowed propagation"); |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2383 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2384 | default: |
| 2385 | break; |
| 2386 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2387 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2388 | Diag(Name.getLocStart(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2389 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2390 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2391 | << Name.getSourceRange() |
| 2392 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2393 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2396 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2397 | const TemplateArgumentLoc &AL, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2398 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2399 | const TemplateArgument &Arg = AL.getArgument(); |
| 2400 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2401 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2402 | switch(Arg.getKind()) { |
| 2403 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2404 | // C++ [temp.arg.type]p1: |
| 2405 | // A template-argument for a template-parameter which is a |
| 2406 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2407 | break; |
| 2408 | case TemplateArgument::Template: { |
| 2409 | // We have a template type parameter but the template argument |
| 2410 | // is a template without any arguments. |
| 2411 | SourceRange SR = AL.getSourceRange(); |
| 2412 | TemplateName Name = Arg.getAsTemplate(); |
| 2413 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 2414 | << Name << SR; |
| 2415 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 2416 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2417 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2418 | return true; |
| 2419 | } |
| 2420 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2421 | // We have a template type parameter but the template argument |
| 2422 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 2423 | SourceRange SR = AL.getSourceRange(); |
| 2424 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2425 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2426 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2427 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2428 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2429 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2430 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2431 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2432 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2433 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2434 | // Add the converted template type argument. |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 2435 | QualType ArgType = Context.getCanonicalType(Arg.getAsType()); |
| 2436 | |
| 2437 | // Objective-C ARC: |
| 2438 | // If an explicitly-specified template argument type is a lifetime type |
| 2439 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2440 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 2441 | ArgType->isObjCLifetimeType() && |
| 2442 | !ArgType.getObjCLifetime()) { |
| 2443 | Qualifiers Qs; |
| 2444 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 2445 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 2446 | } |
| 2447 | |
| 2448 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2449 | return false; |
| 2450 | } |
| 2451 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2452 | /// \brief Substitute template arguments into the default template argument for |
| 2453 | /// the given template type parameter. |
| 2454 | /// |
| 2455 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2456 | /// the substitution. |
| 2457 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2458 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2459 | /// for. |
| 2460 | /// |
| 2461 | /// \param TemplateLoc the location of the template name that started the |
| 2462 | /// template-id we are checking. |
| 2463 | /// |
| 2464 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2465 | /// terminates the template-id. |
| 2466 | /// |
| 2467 | /// \param Param the template template parameter whose default we are |
| 2468 | /// substituting into. |
| 2469 | /// |
| 2470 | /// \param Converted the list of template arguments provided for template |
| 2471 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2472 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2473 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2474 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2475 | TemplateDecl *Template, |
| 2476 | SourceLocation TemplateLoc, |
| 2477 | SourceLocation RAngleLoc, |
| 2478 | TemplateTypeParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2479 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2480 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2481 | |
| 2482 | // If the argument type is dependent, instantiate it now based |
| 2483 | // on the previously-computed template arguments. |
| 2484 | if (ArgType->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2485 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2486 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2487 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2488 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2489 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2490 | |
| 2491 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2492 | Template, Converted.data(), |
| 2493 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2494 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2495 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2496 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2497 | Param->getDefaultArgumentLoc(), |
| 2498 | Param->getDeclName()); |
| 2499 | } |
| 2500 | |
| 2501 | return ArgType; |
| 2502 | } |
| 2503 | |
| 2504 | /// \brief Substitute template arguments into the default template argument for |
| 2505 | /// the given non-type template parameter. |
| 2506 | /// |
| 2507 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2508 | /// the substitution. |
| 2509 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2510 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2511 | /// for. |
| 2512 | /// |
| 2513 | /// \param TemplateLoc the location of the template name that started the |
| 2514 | /// template-id we are checking. |
| 2515 | /// |
| 2516 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2517 | /// terminates the template-id. |
| 2518 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2519 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2520 | /// substituting into. |
| 2521 | /// |
| 2522 | /// \param Converted the list of template arguments provided for template |
| 2523 | /// parameters that precede \p Param in the template parameter list. |
| 2524 | /// |
| 2525 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2526 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2527 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2528 | TemplateDecl *Template, |
| 2529 | SourceLocation TemplateLoc, |
| 2530 | SourceLocation RAngleLoc, |
| 2531 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2532 | SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2533 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2534 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2535 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2536 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2537 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2538 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2539 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2540 | Template, Converted.data(), |
| 2541 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2542 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2543 | |
| 2544 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2545 | } |
| 2546 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2547 | /// \brief Substitute template arguments into the default template argument for |
| 2548 | /// the given template template parameter. |
| 2549 | /// |
| 2550 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2551 | /// the substitution. |
| 2552 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2553 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2554 | /// for. |
| 2555 | /// |
| 2556 | /// \param TemplateLoc the location of the template name that started the |
| 2557 | /// template-id we are checking. |
| 2558 | /// |
| 2559 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2560 | /// terminates the template-id. |
| 2561 | /// |
| 2562 | /// \param Param the template template parameter whose default we are |
| 2563 | /// substituting into. |
| 2564 | /// |
| 2565 | /// \param Converted the list of template arguments provided for template |
| 2566 | /// parameters that precede \p Param in the template parameter list. |
| 2567 | /// |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2568 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 2569 | /// source-location information) that precedes the template name. |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2570 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2571 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2572 | static TemplateName |
| 2573 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2574 | TemplateDecl *Template, |
| 2575 | SourceLocation TemplateLoc, |
| 2576 | SourceLocation RAngleLoc, |
| 2577 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2578 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2579 | NestedNameSpecifierLoc &QualifierLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2580 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2581 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2582 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2583 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2584 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2585 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2586 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2587 | Template, Converted.data(), |
| 2588 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2589 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2590 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2591 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2592 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2593 | if (QualifierLoc) { |
| 2594 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2595 | AllTemplateArgs); |
| 2596 | if (!QualifierLoc) |
| 2597 | return TemplateName(); |
| 2598 | } |
| 2599 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2600 | return SemaRef.SubstTemplateName(QualifierLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2601 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2602 | Param->getDefaultArgument().getTemplateNameLoc(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2603 | AllTemplateArgs); |
| 2604 | } |
| 2605 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2606 | /// \brief If the given template parameter has a default template |
| 2607 | /// argument, substitute into that default template argument and |
| 2608 | /// return the corresponding template argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2609 | TemplateArgumentLoc |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2610 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2611 | SourceLocation TemplateLoc, |
| 2612 | SourceLocation RAngleLoc, |
| 2613 | Decl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2614 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2615 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2616 | if (!TypeParm->hasDefaultArgument()) |
| 2617 | return TemplateArgumentLoc(); |
| 2618 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2619 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2620 | TemplateLoc, |
| 2621 | RAngleLoc, |
| 2622 | TypeParm, |
| 2623 | Converted); |
| 2624 | if (DI) |
| 2625 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2626 | |
| 2627 | return TemplateArgumentLoc(); |
| 2628 | } |
| 2629 | |
| 2630 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2631 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2632 | if (!NonTypeParm->hasDefaultArgument()) |
| 2633 | return TemplateArgumentLoc(); |
| 2634 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2635 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2636 | TemplateLoc, |
| 2637 | RAngleLoc, |
| 2638 | NonTypeParm, |
| 2639 | Converted); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2640 | if (Arg.isInvalid()) |
| 2641 | return TemplateArgumentLoc(); |
| 2642 | |
| 2643 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2644 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2645 | } |
| 2646 | |
| 2647 | TemplateTemplateParmDecl *TempTempParm |
| 2648 | = cast<TemplateTemplateParmDecl>(Param); |
| 2649 | if (!TempTempParm->hasDefaultArgument()) |
| 2650 | return TemplateArgumentLoc(); |
| 2651 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2652 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2653 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2654 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2655 | TemplateLoc, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2656 | RAngleLoc, |
| 2657 | TempTempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2658 | Converted, |
| 2659 | QualifierLoc); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2660 | if (TName.isNull()) |
| 2661 | return TemplateArgumentLoc(); |
| 2662 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2663 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2664 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2665 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2666 | } |
| 2667 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2668 | /// \brief Check that the given template argument corresponds to the given |
| 2669 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2670 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2671 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2672 | /// checked. |
| 2673 | /// |
| 2674 | /// \param Arg The template argument. |
| 2675 | /// |
| 2676 | /// \param Template The template in which the template argument resides. |
| 2677 | /// |
| 2678 | /// \param TemplateLoc The location of the template name for the template |
| 2679 | /// whose argument list we're matching. |
| 2680 | /// |
| 2681 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2682 | /// the template argument list. |
| 2683 | /// |
| 2684 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2685 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2686 | /// |
| 2687 | /// \param Converted The checked, converted argument will be added to the |
| 2688 | /// end of this small vector. |
| 2689 | /// |
| 2690 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2691 | /// explicitly written, deduced, etc. |
| 2692 | /// |
| 2693 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2694 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2695 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2696 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2697 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2698 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2699 | unsigned ArgumentPackIndex, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2700 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2701 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2702 | // Check template type parameters. |
| 2703 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2704 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2705 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2706 | // Check non-type template parameters. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2707 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2708 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2709 | // with the template arguments we've seen thus far. But if the |
| 2710 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2711 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2712 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2713 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2714 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2715 | if (NTTPType->isDependentType() && |
| 2716 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2717 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2718 | // Do substitution on the type of the non-type template parameter. |
| 2719 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2720 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2721 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2722 | |
| 2723 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2724 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2725 | NTTPType = SubstType(NTTPType, |
| 2726 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2727 | NTTP->getLocation(), |
| 2728 | NTTP->getDeclName()); |
| 2729 | // If that worked, check the non-type template parameter type |
| 2730 | // for validity. |
| 2731 | if (!NTTPType.isNull()) |
| 2732 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2733 | NTTP->getLocation()); |
| 2734 | if (NTTPType.isNull()) |
| 2735 | return true; |
| 2736 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2737 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2738 | switch (Arg.getArgument().getKind()) { |
| 2739 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2740 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2741 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2742 | case TemplateArgument::Expression: { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2743 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2744 | ExprResult Res = |
| 2745 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 2746 | Result, CTAK); |
| 2747 | if (Res.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2748 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2749 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2750 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2751 | break; |
| 2752 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2753 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2754 | case TemplateArgument::Declaration: |
| 2755 | case TemplateArgument::Integral: |
| 2756 | // We've already checked this template argument, so just copy |
| 2757 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2758 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2759 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2760 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2761 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2762 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2763 | // We were given a template template argument. It may not be ill-formed; |
| 2764 | // see below. |
| 2765 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2766 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2767 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2768 | // We have a template argument such as \c T::template X, which we |
| 2769 | // parsed as a template template argument. However, since we now |
| 2770 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2771 | // template name into an expression. |
| 2772 | |
| 2773 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2774 | Arg.getTemplateNameLoc()); |
| 2775 | |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2776 | CXXScopeSpec SS; |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2777 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2778 | // FIXME: the template-template arg was a DependentTemplateName, |
| 2779 | // so it was provided with a template keyword. However, its source |
| 2780 | // location is not stored in the template argument structure. |
| 2781 | SourceLocation TemplateKWLoc; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2782 | ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2783 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2784 | TemplateKWLoc, |
| 2785 | NameInfo, 0)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2786 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2787 | // If we parsed the template argument as a pack expansion, create a |
| 2788 | // pack expansion expression. |
| 2789 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2790 | E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc()); |
| 2791 | if (E.isInvalid()) |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2792 | return true; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2793 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2794 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2795 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2796 | E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result); |
| 2797 | if (E.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2798 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2799 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2800 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2801 | break; |
| 2802 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2803 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2804 | // We have a template argument that actually does refer to a class |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2805 | // template, alias template, or template template parameter, and |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2806 | // therefore cannot be a non-type template argument. |
| 2807 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2808 | << Arg.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2809 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2810 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2811 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2812 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2813 | case TemplateArgument::Type: { |
| 2814 | // We have a non-type template parameter but the template |
| 2815 | // argument is a type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2816 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2817 | // C++ [temp.arg]p2: |
| 2818 | // In a template-argument, an ambiguity between a type-id and |
| 2819 | // an expression is resolved to a type-id, regardless of the |
| 2820 | // form of the corresponding template-parameter. |
| 2821 | // |
| 2822 | // We warn specifically about this case, since it can be rather |
| 2823 | // confusing for users. |
| 2824 | QualType T = Arg.getArgument().getAsType(); |
| 2825 | SourceRange SR = Arg.getSourceRange(); |
| 2826 | if (T->isFunctionType()) |
| 2827 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2828 | else |
| 2829 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2830 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2831 | return true; |
| 2832 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2833 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2834 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2835 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2836 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2837 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2838 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2842 | // Check template template parameters. |
| 2843 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2844 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2845 | // Substitute into the template parameter list of the template |
| 2846 | // template parameter, since previously-supplied template arguments |
| 2847 | // may appear within the template template parameter. |
| 2848 | { |
| 2849 | // Set up a template instantiation context. |
| 2850 | LocalInstantiationScope Scope(*this); |
| 2851 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2852 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2853 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2854 | |
| 2855 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2856 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2857 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2858 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2859 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2860 | if (!TempParm) |
| 2861 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2862 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2863 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2864 | switch (Arg.getArgument().getKind()) { |
| 2865 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2866 | llvm_unreachable("Should never see a NULL template argument here"); |
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 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2869 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2870 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2871 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2872 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2873 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2874 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2875 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2876 | case TemplateArgument::Expression: |
| 2877 | case TemplateArgument::Type: |
| 2878 | // We have a template template parameter but the template |
| 2879 | // argument does not refer to a template. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2880 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2881 | << getLangOpts().CPlusPlus0x; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2882 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2883 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2884 | case TemplateArgument::Declaration: |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2885 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2886 | case TemplateArgument::Integral: |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2887 | llvm_unreachable("Integral argument with template template parameter"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2888 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2889 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2890 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2891 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2892 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2893 | return false; |
| 2894 | } |
| 2895 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2896 | /// \brief Diagnose an arity mismatch in the |
| 2897 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 2898 | SourceLocation TemplateLoc, |
| 2899 | TemplateArgumentListInfo &TemplateArgs) { |
| 2900 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2901 | unsigned NumParams = Params->size(); |
| 2902 | unsigned NumArgs = TemplateArgs.size(); |
| 2903 | |
| 2904 | SourceRange Range; |
| 2905 | if (NumArgs > NumParams) |
| 2906 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
| 2907 | TemplateArgs.getRAngleLoc()); |
| 2908 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2909 | << (NumArgs > NumParams) |
| 2910 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2911 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2912 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2913 | << Template << Range; |
| 2914 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2915 | << Params->getSourceRange(); |
| 2916 | return true; |
| 2917 | } |
| 2918 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2919 | /// \brief Check that the given template argument list is well-formed |
| 2920 | /// for specializing the given template. |
| 2921 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2922 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2923 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2924 | bool PartialTemplateArgs, |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2925 | SmallVectorImpl<TemplateArgument> &Converted, |
| 2926 | bool *ExpansionIntoFixedList) { |
| 2927 | if (ExpansionIntoFixedList) |
| 2928 | *ExpansionIntoFixedList = false; |
| 2929 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2930 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2931 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2932 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2933 | bool Invalid = false; |
| 2934 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2935 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2936 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2937 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2938 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2939 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2940 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2941 | // [...] The type and form of each template-argument specified in |
| 2942 | // a template-id shall match the type and form specified for the |
| 2943 | // corresponding parameter declared by the template in its |
| 2944 | // template-parameter-list. |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2945 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2946 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2947 | TemplateParameterList::iterator Param = Params->begin(), |
| 2948 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2949 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 2950 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2951 | bool SawPackExpansion = false; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2952 | while (Param != ParamEnd) { |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2953 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2954 | // If we have an expanded parameter pack, make sure we don't have too |
| 2955 | // many arguments. |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2956 | // FIXME: This really should fall out from the normal arity checking. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2957 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2958 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2959 | if (NTTP->isExpandedParameterPack() && |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2960 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 2961 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2962 | << true |
| 2963 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2964 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2965 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2966 | << Template; |
| 2967 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2968 | << Params->getSourceRange(); |
| 2969 | return true; |
| 2970 | } |
| 2971 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2972 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2973 | // Check the template argument we were given. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2974 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2975 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2976 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2977 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2978 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2979 | if ((*Param)->isTemplateParameterPack()) { |
| 2980 | // The template parameter was a template parameter pack, so take the |
| 2981 | // deduced argument and place it on the argument pack. Note that we |
| 2982 | // stay on the same template parameter so that we can deduce more |
| 2983 | // arguments. |
| 2984 | ArgumentPack.push_back(Converted.back()); |
| 2985 | Converted.pop_back(); |
| 2986 | } else { |
| 2987 | // Move to the next template parameter. |
| 2988 | ++Param; |
| 2989 | } |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2990 | |
| 2991 | // If this template argument is a pack expansion, record that fact |
| 2992 | // and break out; we can't actually check any more. |
| 2993 | if (TemplateArgs[ArgIdx].getArgument().isPackExpansion()) { |
| 2994 | SawPackExpansion = true; |
| 2995 | ++ArgIdx; |
| 2996 | break; |
| 2997 | } |
| 2998 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2999 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3000 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3001 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3002 | |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3003 | // If we're checking a partial template argument list, we're done. |
| 3004 | if (PartialTemplateArgs) { |
| 3005 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 3006 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3007 | ArgumentPack.data(), |
| 3008 | ArgumentPack.size())); |
| 3009 | |
| 3010 | return Invalid; |
| 3011 | } |
| 3012 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3013 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3014 | // arguments, just break out now and we'll fill in the argument pack below. |
| 3015 | if ((*Param)->isTemplateParameterPack()) |
| 3016 | break; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 3017 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3018 | // Check whether we have a default argument. |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3019 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3020 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3021 | // Retrieve the default template argument from the template |
| 3022 | // parameter. For each kind of template parameter, we substitute the |
| 3023 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3024 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3025 | // the default argument. |
| 3026 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3027 | if (!TTP->hasDefaultArgument()) |
| 3028 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3029 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3030 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3031 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3032 | Template, |
| 3033 | TemplateLoc, |
| 3034 | RAngleLoc, |
| 3035 | TTP, |
| 3036 | Converted); |
| 3037 | if (!ArgType) |
| 3038 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3039 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3040 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 3041 | ArgType); |
| 3042 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3043 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3044 | if (!NTTP->hasDefaultArgument()) |
| 3045 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3046 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3047 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3048 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3049 | TemplateLoc, |
| 3050 | RAngleLoc, |
| 3051 | NTTP, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3052 | Converted); |
| 3053 | if (E.isInvalid()) |
| 3054 | return true; |
| 3055 | |
| 3056 | Expr *Ex = E.takeAs<Expr>(); |
| 3057 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 3058 | } else { |
| 3059 | TemplateTemplateParmDecl *TempParm |
| 3060 | = cast<TemplateTemplateParmDecl>(*Param); |
| 3061 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3062 | if (!TempParm->hasDefaultArgument()) |
| 3063 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3064 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3065 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3066 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3067 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3068 | TemplateLoc, |
| 3069 | RAngleLoc, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3070 | TempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3071 | Converted, |
| 3072 | QualifierLoc); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3073 | if (Name.isNull()) |
| 3074 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3075 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3076 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3077 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3078 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3079 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3080 | // Introduce an instantiation record that describes where we are using |
| 3081 | // the default template argument. |
| 3082 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3083 | Converted.data(), Converted.size(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3084 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3085 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3086 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3087 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3088 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3089 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3090 | |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3091 | // Core issue 150 (assumed resolution): if this is a template template |
| 3092 | // parameter, keep track of the default template arguments from the |
| 3093 | // template definition. |
| 3094 | if (isTemplateTemplateParameter) |
| 3095 | TemplateArgs.addArgument(Arg); |
| 3096 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3097 | // Move to the next template parameter and argument. |
| 3098 | ++Param; |
| 3099 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3100 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3101 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3102 | // If we saw a pack expansion, then directly convert the remaining arguments, |
| 3103 | // because we don't know what parameters they'll match up with. |
| 3104 | if (SawPackExpansion) { |
| 3105 | bool AddToArgumentPack |
| 3106 | = Param != ParamEnd && (*Param)->isTemplateParameterPack(); |
| 3107 | while (ArgIdx < NumArgs) { |
| 3108 | if (AddToArgumentPack) |
| 3109 | ArgumentPack.push_back(TemplateArgs[ArgIdx].getArgument()); |
| 3110 | else |
| 3111 | Converted.push_back(TemplateArgs[ArgIdx].getArgument()); |
| 3112 | ++ArgIdx; |
| 3113 | } |
| 3114 | |
| 3115 | // Push the argument pack onto the list of converted arguments. |
| 3116 | if (AddToArgumentPack) { |
| 3117 | if (ArgumentPack.empty()) |
| 3118 | Converted.push_back(TemplateArgument(0, 0)); |
| 3119 | else { |
| 3120 | Converted.push_back( |
| 3121 | TemplateArgument::CreatePackCopy(Context, |
| 3122 | ArgumentPack.data(), |
| 3123 | ArgumentPack.size())); |
| 3124 | ArgumentPack.clear(); |
| 3125 | } |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3126 | } else if (ExpansionIntoFixedList) { |
| 3127 | // We have expanded a pack into a fixed list. |
| 3128 | *ExpansionIntoFixedList = true; |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
| 3131 | return Invalid; |
| 3132 | } |
| 3133 | |
| 3134 | // If we have any leftover arguments, then there were too many arguments. |
| 3135 | // Complain and fail. |
| 3136 | if (ArgIdx < NumArgs) |
| 3137 | return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs); |
| 3138 | |
| 3139 | // If we have an expanded parameter pack, make sure we don't have too |
| 3140 | // many arguments. |
| 3141 | // FIXME: This really should fall out from the normal arity checking. |
| 3142 | if (Param != ParamEnd) { |
| 3143 | if (NonTypeTemplateParmDecl *NTTP |
| 3144 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 3145 | if (NTTP->isExpandedParameterPack() && |
| 3146 | ArgumentPack.size() < NTTP->getNumExpansionTypes()) { |
| 3147 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3148 | << false |
| 3149 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3150 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3151 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3152 | << Template; |
| 3153 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3154 | << Params->getSourceRange(); |
| 3155 | return true; |
| 3156 | } |
| 3157 | } |
| 3158 | } |
| 3159 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3160 | // Form argument packs for each of the parameter packs remaining. |
| 3161 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3162 | // If we're checking a partial list of template arguments, don't fill |
| 3163 | // in arguments for non-template parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3164 | if ((*Param)->isTemplateParameterPack()) { |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 3165 | if (!HasParameterPack) |
| 3166 | return true; |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3167 | if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3168 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3169 | else { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3170 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3171 | ArgumentPack.data(), |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3172 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3173 | ArgumentPack.clear(); |
| 3174 | } |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3175 | } else if (!PartialTemplateArgs) |
| 3176 | return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3177 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3178 | ++Param; |
| 3179 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3180 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3181 | return Invalid; |
| 3182 | } |
| 3183 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3184 | namespace { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3185 | class UnnamedLocalNoLinkageFinder |
| 3186 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3187 | { |
| 3188 | Sema &S; |
| 3189 | SourceRange SR; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3190 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3191 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3192 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3193 | public: |
| 3194 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3195 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3196 | bool Visit(QualType T) { |
| 3197 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3198 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3199 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3200 | #define TYPE(Class, Parent) \ |
| 3201 | bool Visit##Class##Type(const Class##Type *); |
| 3202 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 3203 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3204 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 3205 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3206 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3207 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3208 | bool VisitTagDecl(const TagDecl *Tag); |
| 3209 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3210 | }; |
| 3211 | } |
| 3212 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3213 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3214 | return false; |
| 3215 | } |
| 3216 | |
| 3217 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3218 | return Visit(T->getElementType()); |
| 3219 | } |
| 3220 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3221 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3222 | return Visit(T->getPointeeType()); |
| 3223 | } |
| 3224 | |
| 3225 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3226 | const BlockPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3227 | return Visit(T->getPointeeType()); |
| 3228 | } |
| 3229 | |
| 3230 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3231 | const LValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3232 | return Visit(T->getPointeeType()); |
| 3233 | } |
| 3234 | |
| 3235 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3236 | const RValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3237 | return Visit(T->getPointeeType()); |
| 3238 | } |
| 3239 | |
| 3240 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3241 | const MemberPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3242 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3243 | } |
| 3244 | |
| 3245 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3246 | const ConstantArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3247 | return Visit(T->getElementType()); |
| 3248 | } |
| 3249 | |
| 3250 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3251 | const IncompleteArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3252 | return Visit(T->getElementType()); |
| 3253 | } |
| 3254 | |
| 3255 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3256 | const VariableArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3257 | return Visit(T->getElementType()); |
| 3258 | } |
| 3259 | |
| 3260 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3261 | const DependentSizedArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3262 | return Visit(T->getElementType()); |
| 3263 | } |
| 3264 | |
| 3265 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3266 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3267 | return Visit(T->getElementType()); |
| 3268 | } |
| 3269 | |
| 3270 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3271 | return Visit(T->getElementType()); |
| 3272 | } |
| 3273 | |
| 3274 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 3275 | return Visit(T->getElementType()); |
| 3276 | } |
| 3277 | |
| 3278 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 3279 | const FunctionProtoType* T) { |
| 3280 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3281 | AEnd = T->arg_type_end(); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3282 | A != AEnd; ++A) { |
| 3283 | if (Visit(*A)) |
| 3284 | return true; |
| 3285 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3286 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3287 | return Visit(T->getResultType()); |
| 3288 | } |
| 3289 | |
| 3290 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 3291 | const FunctionNoProtoType* T) { |
| 3292 | return Visit(T->getResultType()); |
| 3293 | } |
| 3294 | |
| 3295 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 3296 | const UnresolvedUsingType*) { |
| 3297 | return false; |
| 3298 | } |
| 3299 | |
| 3300 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 3301 | return false; |
| 3302 | } |
| 3303 | |
| 3304 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 3305 | return Visit(T->getUnderlyingType()); |
| 3306 | } |
| 3307 | |
| 3308 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 3309 | return false; |
| 3310 | } |
| 3311 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3312 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 3313 | const UnaryTransformType*) { |
| 3314 | return false; |
| 3315 | } |
| 3316 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3317 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 3318 | return Visit(T->getDeducedType()); |
| 3319 | } |
| 3320 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3321 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 3322 | return VisitTagDecl(T->getDecl()); |
| 3323 | } |
| 3324 | |
| 3325 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 3326 | return VisitTagDecl(T->getDecl()); |
| 3327 | } |
| 3328 | |
| 3329 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 3330 | const TemplateTypeParmType*) { |
| 3331 | return false; |
| 3332 | } |
| 3333 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3334 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 3335 | const SubstTemplateTypeParmPackType *) { |
| 3336 | return false; |
| 3337 | } |
| 3338 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3339 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 3340 | const TemplateSpecializationType*) { |
| 3341 | return false; |
| 3342 | } |
| 3343 | |
| 3344 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 3345 | const InjectedClassNameType* T) { |
| 3346 | return VisitTagDecl(T->getDecl()); |
| 3347 | } |
| 3348 | |
| 3349 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 3350 | const DependentNameType* T) { |
| 3351 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3352 | } |
| 3353 | |
| 3354 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 3355 | const DependentTemplateSpecializationType* T) { |
| 3356 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3357 | } |
| 3358 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3359 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 3360 | const PackExpansionType* T) { |
| 3361 | return Visit(T->getPattern()); |
| 3362 | } |
| 3363 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3364 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 3365 | return false; |
| 3366 | } |
| 3367 | |
| 3368 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 3369 | const ObjCInterfaceType *) { |
| 3370 | return false; |
| 3371 | } |
| 3372 | |
| 3373 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 3374 | const ObjCObjectPointerType *) { |
| 3375 | return false; |
| 3376 | } |
| 3377 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3378 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 3379 | return Visit(T->getValueType()); |
| 3380 | } |
| 3381 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3382 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 3383 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3384 | S.Diag(SR.getBegin(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3385 | S.getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3386 | diag::warn_cxx98_compat_template_arg_local_type : |
| 3387 | diag::ext_template_arg_local_type) |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3388 | << S.Context.getTypeDeclType(Tag) << SR; |
| 3389 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3390 | } |
| 3391 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3392 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3393 | S.Diag(SR.getBegin(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3394 | S.getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3395 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 3396 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3397 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 3398 | return true; |
| 3399 | } |
| 3400 | |
| 3401 | return false; |
| 3402 | } |
| 3403 | |
| 3404 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 3405 | NestedNameSpecifier *NNS) { |
| 3406 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 3407 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3408 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3409 | switch (NNS->getKind()) { |
| 3410 | case NestedNameSpecifier::Identifier: |
| 3411 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3412 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3413 | case NestedNameSpecifier::Global: |
| 3414 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3415 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3416 | case NestedNameSpecifier::TypeSpec: |
| 3417 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3418 | return Visit(QualType(NNS->getAsType(), 0)); |
| 3419 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3420 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
| 3423 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3424 | /// \brief Check a template argument against its corresponding |
| 3425 | /// template type parameter. |
| 3426 | /// |
| 3427 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 3428 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3429 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3430 | TypeSourceInfo *ArgInfo) { |
| 3431 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3432 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 3433 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 3434 | |
| 3435 | if (Arg->isVariablyModifiedType()) { |
| 3436 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3437 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3438 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3439 | } |
| 3440 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3441 | // C++03 [temp.arg.type]p2: |
| 3442 | // A local type, a type with no linkage, an unnamed type or a type |
| 3443 | // compounded from any of these types shall not be used as a |
| 3444 | // template-argument for a template type-parameter. |
| 3445 | // |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3446 | // 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] | 3447 | // a warning. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3448 | if (LangOpts.CPlusPlus0x ? |
| 3449 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 3450 | SR.getBegin()) != DiagnosticsEngine::Ignored || |
| 3451 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type, |
| 3452 | SR.getBegin()) != DiagnosticsEngine::Ignored : |
| 3453 | Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3454 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 3455 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 3456 | } |
| 3457 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3458 | return false; |
| 3459 | } |
| 3460 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3461 | /// \brief Checks whether the given template argument is the address |
| 3462 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3463 | static bool |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3464 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 3465 | NonTypeTemplateParmDecl *Param, |
| 3466 | QualType ParamType, |
| 3467 | Expr *ArgIn, |
| 3468 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3469 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3470 | Expr *Arg = ArgIn; |
| 3471 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3472 | |
| 3473 | // See through any implicit casts we added to fix the type. |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3474 | Arg = Arg->IgnoreImpCasts(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3475 | |
| 3476 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3477 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3478 | // A template-argument for a non-type, non-template |
| 3479 | // template-parameter shall be one of: [...] |
| 3480 | // |
| 3481 | // -- the address of an object or function with external |
| 3482 | // linkage, including function templates and function |
| 3483 | // template-ids but excluding non-static class members, |
| 3484 | // expressed as & id-expression where the & is optional if |
| 3485 | // the name refers to a function or array, or if the |
| 3486 | // corresponding template-parameter is a reference; or |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3487 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3488 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3489 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3490 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3491 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3492 | if (!Invalid && !ExtraParens) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3493 | S.Diag(Arg->getLocStart(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3494 | S.getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3495 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3496 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3497 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3498 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3499 | } |
| 3500 | |
| 3501 | Arg = Parens->getSubExpr(); |
| 3502 | } |
| 3503 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3504 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3505 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3506 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3507 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3508 | bool AddressTaken = false; |
| 3509 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3510 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3511 | if (UnOp->getOpcode() == UO_AddrOf) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3512 | Arg = UnOp->getSubExpr(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3513 | AddressTaken = true; |
| 3514 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 3515 | } |
Francois Pichet | a343a41 | 2011-04-29 09:08:14 +0000 | [diff] [blame] | 3516 | } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3517 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3518 | if (S.getLangOpts().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3519 | Converted = TemplateArgument(ArgIn); |
| 3520 | return false; |
| 3521 | } |
| 3522 | |
| 3523 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3524 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3525 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3526 | |
| 3527 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3528 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3529 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 3530 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3531 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3532 | return true; |
| 3533 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3534 | |
| 3535 | // Stop checking the precise nature of the argument if it is value dependent, |
| 3536 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3537 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3538 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3539 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3540 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3541 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3542 | if (!isa<ValueDecl>(DRE->getDecl())) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3543 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3544 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3545 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3546 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3547 | return true; |
| 3548 | } |
| 3549 | |
| 3550 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3551 | |
| 3552 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3553 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3554 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3555 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3556 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3557 | return true; |
| 3558 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3559 | |
| 3560 | // Cannot refer to non-static member functions |
| 3561 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3562 | if (!Method->isStatic()) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3563 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3564 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3565 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3566 | return true; |
| 3567 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3568 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3569 | // Functions must have external linkage. |
| 3570 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3571 | if (!isExternalLinkage(Func->getLinkage())) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3572 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3573 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3574 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3575 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3576 | << true; |
| 3577 | return true; |
| 3578 | } |
| 3579 | |
| 3580 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3581 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3582 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3583 | // If the template parameter has pointer type, the function decays. |
| 3584 | if (ParamType->isPointerType() && !AddressTaken) |
| 3585 | ArgType = S.Context.getPointerType(Func->getType()); |
| 3586 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 3587 | // If we originally had an address-of operator, but the |
| 3588 | // parameter has reference type, complain and (if things look |
| 3589 | // like they will work) drop the address-of operator. |
| 3590 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 3591 | ParamType.getNonReferenceType())) { |
| 3592 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3593 | << ParamType; |
| 3594 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3595 | return true; |
| 3596 | } |
| 3597 | |
| 3598 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3599 | << ParamType |
| 3600 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3601 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3602 | |
| 3603 | ArgType = Func->getType(); |
| 3604 | } |
| 3605 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3606 | if (!isExternalLinkage(Var->getLinkage())) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3607 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3608 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3609 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3610 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3611 | << true; |
| 3612 | return true; |
| 3613 | } |
| 3614 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3615 | // A value of reference type is not an object. |
| 3616 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3617 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3618 | diag::err_template_arg_reference_var) |
| 3619 | << Var->getType() << Arg->getSourceRange(); |
| 3620 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3621 | return true; |
| 3622 | } |
| 3623 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3624 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3625 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3626 | |
| 3627 | // If the template parameter has pointer type, we must have taken |
| 3628 | // the address of this object. |
| 3629 | if (ParamType->isReferenceType()) { |
| 3630 | if (AddressTaken) { |
| 3631 | // If we originally had an address-of operator, but the |
| 3632 | // parameter has reference type, complain and (if things look |
| 3633 | // like they will work) drop the address-of operator. |
| 3634 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3635 | ParamType.getNonReferenceType())) { |
| 3636 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3637 | << ParamType; |
| 3638 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3639 | return true; |
| 3640 | } |
| 3641 | |
| 3642 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3643 | << ParamType |
| 3644 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3645 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3646 | |
| 3647 | ArgType = Var->getType(); |
| 3648 | } |
| 3649 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3650 | if (Var->getType()->isArrayType()) { |
| 3651 | // Array-to-pointer decay. |
| 3652 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3653 | } else { |
| 3654 | // If the template parameter has pointer type but the address of |
| 3655 | // this object was not taken, complain and (possibly) recover by |
| 3656 | // taking the address of the entity. |
| 3657 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3658 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3659 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3660 | << ParamType; |
| 3661 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3662 | return true; |
| 3663 | } |
| 3664 | |
| 3665 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3666 | << ParamType |
| 3667 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3668 | |
| 3669 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3670 | } |
| 3671 | } |
| 3672 | } else { |
| 3673 | // We found something else, but we don't know specifically what it is. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3674 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3675 | diag::err_template_arg_not_object_or_func) |
| 3676 | << Arg->getSourceRange(); |
| 3677 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3678 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3679 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3680 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3681 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3682 | if (ParamType->isPointerType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3683 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3684 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 3685 | ObjCLifetimeConversion)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3686 | // For pointer-to-object types, qualification conversions are |
| 3687 | // permitted. |
| 3688 | } else { |
| 3689 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3690 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3691 | // C++ [temp.arg.nontype]p5b3: |
| 3692 | // For a non-type template-parameter of type reference to |
| 3693 | // object, no conversions apply. The type referred to by the |
| 3694 | // reference may be more cv-qualified than the (otherwise |
| 3695 | // identical) type of the template- argument. The |
| 3696 | // template-parameter is bound directly to the |
| 3697 | // template-argument, which shall be an lvalue. |
| 3698 | |
| 3699 | // FIXME: Other qualifiers? |
| 3700 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3701 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3702 | |
| 3703 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3704 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3705 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3706 | << ParamType << Arg->getType() |
| 3707 | << Arg->getSourceRange(); |
| 3708 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3709 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3710 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3711 | } |
| 3712 | } |
| 3713 | |
| 3714 | // At this point, the template argument refers to an object or |
| 3715 | // function with external linkage. We now need to check whether the |
| 3716 | // argument and parameter types are compatible. |
| 3717 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3718 | ParamType.getNonReferenceType())) { |
| 3719 | // We can't perform this conversion or binding. |
| 3720 | if (ParamType->isReferenceType()) |
| 3721 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3722 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3723 | else |
| 3724 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3725 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3726 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3727 | return true; |
| 3728 | } |
| 3729 | } |
| 3730 | |
| 3731 | // Create the template argument. |
| 3732 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3733 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3734 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3735 | } |
| 3736 | |
| 3737 | /// \brief Checks whether the given template argument is a pointer to |
| 3738 | /// member constant according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3739 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3740 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3741 | bool Invalid = false; |
| 3742 | |
| 3743 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3744 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3745 | Arg = Cast->getSubExpr(); |
| 3746 | |
| 3747 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3748 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3749 | // A template-argument for a non-type, non-template |
| 3750 | // template-parameter shall be one of: [...] |
| 3751 | // |
| 3752 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3753 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3754 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3755 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3756 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3757 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3758 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3759 | if (!Invalid && !ExtraParens) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3760 | Diag(Arg->getLocStart(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3761 | getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3762 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3763 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3764 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3765 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3766 | } |
| 3767 | |
| 3768 | Arg = Parens->getSubExpr(); |
| 3769 | } |
| 3770 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3771 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3772 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3773 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3774 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3775 | // A pointer-to-member constant written &Class::member. |
| 3776 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3777 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3778 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3779 | if (DRE && !DRE->getQualifier()) |
| 3780 | DRE = 0; |
| 3781 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3782 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3783 | // A constant of pointer-to-member type. |
| 3784 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3785 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3786 | if (VD->getType()->isMemberPointerType()) { |
| 3787 | if (isa<NonTypeTemplateParmDecl>(VD) || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3788 | (isa<VarDecl>(VD) && |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3789 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3790 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3791 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3792 | else |
| 3793 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3794 | return Invalid; |
| 3795 | } |
| 3796 | } |
| 3797 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3798 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3799 | DRE = 0; |
| 3800 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3801 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3802 | if (!DRE) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3803 | return Diag(Arg->getLocStart(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3804 | diag::err_template_arg_not_pointer_to_member_form) |
| 3805 | << Arg->getSourceRange(); |
| 3806 | |
| 3807 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3808 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3809 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3810 | "Only non-static member pointers can make it here"); |
| 3811 | |
| 3812 | // Okay: this is the address of a non-static member, and therefore |
| 3813 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3814 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3815 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3816 | else |
| 3817 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3818 | return Invalid; |
| 3819 | } |
| 3820 | |
| 3821 | // We found something else, but we don't know specifically what it is. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3822 | Diag(Arg->getLocStart(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3823 | diag::err_template_arg_not_pointer_to_member_form) |
| 3824 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3825 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3826 | diag::note_template_arg_refers_here); |
| 3827 | return true; |
| 3828 | } |
| 3829 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3830 | /// \brief Check a template argument against its corresponding |
| 3831 | /// non-type template parameter. |
| 3832 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3833 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3834 | /// If an error occurred, it returns ExprError(); otherwise, it |
| 3835 | /// returns the converted template argument. \p |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3836 | /// InstantiatedParamType is the type of the non-type template |
| 3837 | /// parameter after it has been instantiated. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3838 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 3839 | QualType InstantiatedParamType, Expr *Arg, |
| 3840 | TemplateArgument &Converted, |
| 3841 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3842 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3843 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3844 | // If either the parameter has a dependent type or the argument is |
| 3845 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3846 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3847 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3848 | Converted = TemplateArgument(Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3849 | return Owned(Arg); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3850 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3851 | |
| 3852 | // C++ [temp.arg.nontype]p5: |
| 3853 | // The following conversions are performed on each expression used |
| 3854 | // as a non-type template-argument. If a non-type |
| 3855 | // template-argument cannot be converted to the type of the |
| 3856 | // corresponding template-parameter then the program is |
| 3857 | // ill-formed. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3858 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3859 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 3860 | // C++11: |
| 3861 | // -- for a non-type template-parameter of integral or |
| 3862 | // enumeration type, conversions permitted in a converted |
| 3863 | // constant expression are applied. |
| 3864 | // |
| 3865 | // C++98: |
| 3866 | // -- for a non-type template-parameter of integral or |
| 3867 | // enumeration type, integral promotions (4.5) and integral |
| 3868 | // conversions (4.7) are applied. |
| 3869 | |
| 3870 | if (CTAK == CTAK_Deduced && |
| 3871 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 3872 | // C++ [temp.deduct.type]p17: |
| 3873 | // If, in the declaration of a function template with a non-type |
| 3874 | // template-parameter, the non-type template-parameter is used |
| 3875 | // in an expression in the function parameter-list and, if the |
| 3876 | // corresponding template-argument is deduced, the |
| 3877 | // template-argument type shall match the type of the |
| 3878 | // template-parameter exactly, except that a template-argument |
| 3879 | // deduced from an array bound may be of any integral type. |
| 3880 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3881 | << Arg->getType().getUnqualifiedType() |
| 3882 | << ParamType.getUnqualifiedType(); |
| 3883 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3884 | return ExprError(); |
| 3885 | } |
| 3886 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3887 | if (getLangOpts().CPlusPlus0x) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 3888 | // We can't check arbitrary value-dependent arguments. |
| 3889 | // FIXME: If there's no viable conversion to the template parameter type, |
| 3890 | // we should be able to diagnose that prior to instantiation. |
| 3891 | if (Arg->isValueDependent()) { |
| 3892 | Converted = TemplateArgument(Arg); |
| 3893 | return Owned(Arg); |
| 3894 | } |
| 3895 | |
| 3896 | // C++ [temp.arg.nontype]p1: |
| 3897 | // A template-argument for a non-type, non-template template-parameter |
| 3898 | // shall be one of: |
| 3899 | // |
| 3900 | // -- for a non-type template-parameter of integral or enumeration |
| 3901 | // type, a converted constant expression of the type of the |
| 3902 | // template-parameter; or |
| 3903 | llvm::APSInt Value; |
| 3904 | ExprResult ArgResult = |
| 3905 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 3906 | CCEK_TemplateArg); |
| 3907 | if (ArgResult.isInvalid()) |
| 3908 | return ExprError(); |
| 3909 | |
| 3910 | // Widen the argument value to sizeof(parameter type). This is almost |
| 3911 | // always a no-op, except when the parameter type is bool. In |
| 3912 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 3913 | QualType IntegerType = ParamType; |
| 3914 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 3915 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 3916 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 3917 | |
| 3918 | Converted = TemplateArgument(Value, Context.getCanonicalType(ParamType)); |
| 3919 | return ArgResult; |
| 3920 | } |
| 3921 | |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 3922 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 3923 | if (ArgResult.isInvalid()) |
| 3924 | return ExprError(); |
| 3925 | Arg = ArgResult.take(); |
| 3926 | |
| 3927 | QualType ArgType = Arg->getType(); |
| 3928 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3929 | // C++ [temp.arg.nontype]p1: |
| 3930 | // A template-argument for a non-type, non-template |
| 3931 | // template-parameter shall be one of: |
| 3932 | // |
| 3933 | // -- an integral constant-expression of integral or enumeration |
| 3934 | // type; or |
| 3935 | // -- the name of a non-type template-parameter; or |
| 3936 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3937 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3938 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3939 | Diag(Arg->getLocStart(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3940 | diag::err_template_arg_not_integral_or_enumeral) |
| 3941 | << ArgType << Arg->getSourceRange(); |
| 3942 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3943 | return ExprError(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3944 | } else if (!Arg->isValueDependent()) { |
| 3945 | Arg = VerifyIntegerConstantExpression(Arg, &Value, |
| 3946 | PDiag(diag::err_template_arg_not_ice) << ArgType, false).take(); |
| 3947 | if (!Arg) |
| 3948 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3949 | } |
| 3950 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3951 | // From here on out, all we care about are the unqualified forms |
| 3952 | // of the parameter and argument types. |
| 3953 | ParamType = ParamType.getUnqualifiedType(); |
| 3954 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3955 | |
| 3956 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3957 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3958 | // Okay: no conversion necessary |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3959 | } else if (ParamType->isBooleanType()) { |
| 3960 | // This is an integral-to-boolean conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3961 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3962 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3963 | !ParamType->isEnumeralType()) { |
| 3964 | // This is an integral promotion or conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3965 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3966 | } else { |
| 3967 | // We can't perform this conversion. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3968 | Diag(Arg->getLocStart(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3969 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3970 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3971 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3972 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3973 | } |
| 3974 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3975 | // Add the value of this argument to the list of converted |
| 3976 | // arguments. We use the bitwidth and signedness of the template |
| 3977 | // parameter. |
| 3978 | if (Arg->isValueDependent()) { |
| 3979 | // The argument is value-dependent. Create a new |
| 3980 | // TemplateArgument with the converted expression. |
| 3981 | Converted = TemplateArgument(Arg); |
| 3982 | return Owned(Arg); |
| 3983 | } |
| 3984 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3985 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3986 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3987 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3988 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3989 | if (ParamType->isBooleanType()) { |
| 3990 | // Value must be zero or one. |
| 3991 | Value = Value != 0; |
| 3992 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 3993 | if (Value.getBitWidth() != AllowedBits) |
| 3994 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3995 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3996 | } else { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3997 | llvm::APSInt OldValue = Value; |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3998 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3999 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4000 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 4001 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 4002 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4003 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4004 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4005 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4006 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4007 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4008 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4009 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4010 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 4011 | << Arg->getSourceRange(); |
| 4012 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4013 | } |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4014 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4015 | // Complain if we overflowed the template parameter's type. |
| 4016 | unsigned RequiredBits; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4017 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4018 | RequiredBits = OldValue.getActiveBits(); |
| 4019 | else if (OldValue.isUnsigned()) |
| 4020 | RequiredBits = OldValue.getActiveBits() + 1; |
| 4021 | else |
| 4022 | RequiredBits = OldValue.getMinSignedBits(); |
| 4023 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4024 | Diag(Arg->getLocStart(), |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4025 | diag::warn_template_arg_too_large) |
| 4026 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 4027 | << Arg->getSourceRange(); |
| 4028 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4029 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4030 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4031 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4032 | Converted = TemplateArgument(Value, |
Douglas Gregor | 6b63f55 | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 4033 | ParamType->isEnumeralType() |
| 4034 | ? Context.getCanonicalType(ParamType) |
| 4035 | : IntegerType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4036 | return Owned(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4037 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4038 | |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4039 | QualType ArgType = Arg->getType(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4040 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 4041 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4042 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 4043 | // from a template argument of type std::nullptr_t to a non-type |
| 4044 | // template parameter of type pointer to object, pointer to |
| 4045 | // function, or pointer-to-member, respectively. |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4046 | if (ArgType->isNullPtrType()) { |
| 4047 | if (ParamType->isPointerType() || ParamType->isMemberPointerType()) { |
| 4048 | Converted = TemplateArgument((NamedDecl *)0); |
| 4049 | return Owned(Arg); |
| 4050 | } |
| 4051 | |
| 4052 | if (ParamType->isNullPtrType()) { |
| 4053 | llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true); |
| 4054 | Converted = TemplateArgument(Zero, Context.NullPtrTy); |
| 4055 | return Owned(Arg); |
| 4056 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4057 | } |
| 4058 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4059 | // Handle pointer-to-function, reference-to-function, and |
| 4060 | // pointer-to-member-function all in (roughly) the same way. |
| 4061 | if (// -- For a non-type template-parameter of type pointer to |
| 4062 | // function, only the function-to-pointer conversion (4.3) is |
| 4063 | // applied. If the template-argument represents a set of |
| 4064 | // overloaded functions (or a pointer to such), the matching |
| 4065 | // function is selected from the set (13.4). |
| 4066 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4067 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4068 | // -- For a non-type template-parameter of type reference to |
| 4069 | // function, no conversions apply. If the template-argument |
| 4070 | // represents a set of overloaded functions, the matching |
| 4071 | // function is selected from the set (13.4). |
| 4072 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4073 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4074 | // -- For a non-type template-parameter of type pointer to |
| 4075 | // member function, no conversions apply. If the |
| 4076 | // template-argument represents a set of overloaded member |
| 4077 | // functions, the matching member function is selected from |
| 4078 | // the set (13.4). |
| 4079 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4080 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4081 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4082 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4083 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4084 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4085 | true, |
| 4086 | FoundResult)) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4087 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4088 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4089 | |
| 4090 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 4091 | ArgType = Arg->getType(); |
| 4092 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4093 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4094 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4095 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4096 | if (!ParamType->isMemberPointerType()) { |
| 4097 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4098 | ParamType, |
| 4099 | Arg, Converted)) |
| 4100 | return ExprError(); |
| 4101 | return Owned(Arg); |
| 4102 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4103 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4104 | bool ObjCLifetimeConversion; |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4105 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4106 | false, ObjCLifetimeConversion)) { |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 4107 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, |
| 4108 | Arg->getValueKind()).take(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4109 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 4110 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4111 | // We can't perform this conversion. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4112 | Diag(Arg->getLocStart(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4113 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4114 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4115 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4116 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4118 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4119 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 4120 | return ExprError(); |
| 4121 | return Owned(Arg); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 4124 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4125 | // -- for a non-type template-parameter of type pointer to |
| 4126 | // object, qualification conversions (4.4) and the |
| 4127 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4128 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4129 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4130 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4131 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4132 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4133 | ParamType, |
| 4134 | Arg, Converted)) |
| 4135 | return ExprError(); |
| 4136 | return Owned(Arg); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4137 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4138 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4139 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4140 | // -- For a non-type template-parameter of type reference to |
| 4141 | // object, no conversions apply. The type referred to by the |
| 4142 | // reference may be more cv-qualified than the (otherwise |
| 4143 | // identical) type of the template-argument. The |
| 4144 | // template-parameter is bound directly to the |
| 4145 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4146 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4147 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4148 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4149 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4150 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 4151 | ParamRefType->getPointeeType(), |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4152 | true, |
| 4153 | FoundResult)) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4154 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4155 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4156 | |
| 4157 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 4158 | ArgType = Arg->getType(); |
| 4159 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4160 | return ExprError(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4161 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4162 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4163 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4164 | ParamType, |
| 4165 | Arg, Converted)) |
| 4166 | return ExprError(); |
| 4167 | return Owned(Arg); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4168 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4169 | |
| 4170 | // -- For a non-type template-parameter of type pointer to data |
| 4171 | // member, qualification conversions (4.4) are applied. |
| 4172 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 4173 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4174 | bool ObjCLifetimeConversion; |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 4175 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4176 | // Types match exactly: nothing more to do here. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4177 | } else if (IsQualificationConversion(ArgType, ParamType, false, |
| 4178 | ObjCLifetimeConversion)) { |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 4179 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, |
| 4180 | Arg->getValueKind()).take(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4181 | } else { |
| 4182 | // We can't perform this conversion. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4183 | Diag(Arg->getLocStart(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4184 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4185 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4186 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4187 | return ExprError(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4188 | } |
| 4189 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4190 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 4191 | return ExprError(); |
| 4192 | return Owned(Arg); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4193 | } |
| 4194 | |
| 4195 | /// \brief Check a template argument against its corresponding |
| 4196 | /// template template parameter. |
| 4197 | /// |
| 4198 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 4199 | /// It returns true if an error occurred, and false otherwise. |
| 4200 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4201 | const TemplateArgumentLoc &Arg) { |
| 4202 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 4203 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 4204 | if (!Template) { |
| 4205 | // Any dependent template name is fine. |
| 4206 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 4207 | return false; |
| 4208 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4209 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4210 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4211 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4212 | // the name of a class template or an alias template, expressed as an |
| 4213 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4214 | // primary class templates are considered when matching the |
| 4215 | // template template argument with the corresponding parameter; |
| 4216 | // partial specializations are not considered even if their |
| 4217 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4218 | // |
| 4219 | // Note that we also allow template template parameters here, which |
| 4220 | // will happen when we are dealing with, e.g., class template |
| 4221 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4222 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4223 | !isa<TemplateTemplateParmDecl>(Template) && |
| 4224 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4225 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4226 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4227 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4228 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4229 | << Template; |
| 4230 | } |
| 4231 | |
| 4232 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 4233 | Param->getTemplateParameters(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4234 | true, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4235 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4236 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4237 | } |
| 4238 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4239 | /// \brief Given a non-type template argument that refers to a |
| 4240 | /// declaration and the type of its corresponding non-type template |
| 4241 | /// parameter, produce an expression that properly refers to that |
| 4242 | /// declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4243 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4244 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 4245 | QualType ParamType, |
| 4246 | SourceLocation Loc) { |
| 4247 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 4248 | "Only declaration template arguments permitted here"); |
| 4249 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 4250 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4251 | if (VD->getDeclContext()->isRecord() && |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4252 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 4253 | // If the value is a class member, we might have a pointer-to-member. |
| 4254 | // Determine whether the non-type template template parameter is of |
| 4255 | // pointer-to-member type. If so, we need to build an appropriate |
| 4256 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 4257 | // would refer to the member itself. |
| 4258 | if (ParamType->isMemberPointerType()) { |
| 4259 | QualType ClassType |
| 4260 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 4261 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4262 | = NestedNameSpecifier::Create(Context, 0, false, |
| 4263 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4264 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 4265 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4266 | |
| 4267 | // The actual value-ness of this is unimportant, but for |
| 4268 | // internal consistency's sake, references to instance methods |
| 4269 | // are r-values. |
| 4270 | ExprValueKind VK = VK_LValue; |
| 4271 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 4272 | VK = VK_RValue; |
| 4273 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4274 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4275 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4276 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4277 | Loc, |
| 4278 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4279 | if (RefExpr.isInvalid()) |
| 4280 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4281 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4282 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4283 | |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4284 | // We might need to perform a trailing qualification conversion, since |
| 4285 | // the element type on the parameter could be more qualified than the |
| 4286 | // element type in the expression we constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4287 | bool ObjCLifetimeConversion; |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4288 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4289 | ParamType.getUnqualifiedType(), false, |
| 4290 | ObjCLifetimeConversion)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4291 | RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4292 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4293 | assert(!RefExpr.isInvalid() && |
| 4294 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4295 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4296 | return move(RefExpr); |
| 4297 | } |
| 4298 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4299 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4300 | QualType T = VD->getType().getNonReferenceType(); |
| 4301 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4302 | // When the non-type template parameter is a pointer, take the |
| 4303 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4304 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4305 | if (RefExpr.isInvalid()) |
| 4306 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4307 | |
| 4308 | if (T->isFunctionType() || T->isArrayType()) { |
| 4309 | // Decay functions and arrays. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4310 | RefExpr = DefaultFunctionArrayConversion(RefExpr.take()); |
| 4311 | if (RefExpr.isInvalid()) |
| 4312 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4313 | |
| 4314 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4315 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4316 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4317 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4318 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4319 | } |
| 4320 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4321 | ExprValueKind VK = VK_RValue; |
| 4322 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4323 | // If the non-type template parameter has reference type, qualify the |
| 4324 | // resulting declaration reference with the extra qualifiers on the |
| 4325 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4326 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 4327 | VK = VK_LValue; |
| 4328 | T = Context.getQualifiedType(T, |
| 4329 | TargetRef->getPointeeType().getQualifiers()); |
| 4330 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4331 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4332 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
| 4335 | /// \brief Construct a new expression that refers to the given |
| 4336 | /// integral template argument with the given source-location |
| 4337 | /// information. |
| 4338 | /// |
| 4339 | /// This routine takes care of the mapping from an integral template |
| 4340 | /// argument (which may have any integral type) to the appropriate |
| 4341 | /// literal value. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4342 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4343 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 4344 | SourceLocation Loc) { |
| 4345 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4346 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4347 | QualType T = Arg.getIntegralType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4348 | if (T->isAnyCharacterType()) { |
| 4349 | CharacterLiteral::CharacterKind Kind; |
| 4350 | if (T->isWideCharType()) |
| 4351 | Kind = CharacterLiteral::Wide; |
| 4352 | else if (T->isChar16Type()) |
| 4353 | Kind = CharacterLiteral::UTF16; |
| 4354 | else if (T->isChar32Type()) |
| 4355 | Kind = CharacterLiteral::UTF32; |
| 4356 | else |
| 4357 | Kind = CharacterLiteral::Ascii; |
| 4358 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4359 | return Owned(new (Context) CharacterLiteral( |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4360 | Arg.getAsIntegral()->getZExtValue(), |
| 4361 | Kind, T, Loc)); |
| 4362 | } |
| 4363 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4364 | if (T->isBooleanType()) |
| 4365 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 4366 | Arg.getAsIntegral()->getBoolValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4367 | T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4368 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4369 | if (T->isNullPtrType()) |
| 4370 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 4371 | |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4372 | // If this is an enum type that we're instantiating, we need to use an integer |
| 4373 | // type the same size as the enumerator. We don't want to build an |
| 4374 | // IntegerLiteral with enum type. |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4375 | QualType BT; |
| 4376 | if (const EnumType *ET = T->getAs<EnumType>()) |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4377 | BT = ET->getDecl()->getIntegerType(); |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4378 | else |
| 4379 | BT = T; |
| 4380 | |
John McCall | 4e9272d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 4381 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
| 4382 | if (T->isEnumeralType()) { |
| 4383 | // FIXME: This is a hack. We need a better way to handle substituted |
| 4384 | // non-type template parameters. |
| 4385 | E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0, |
| 4386 | Context.getTrivialTypeSourceInfo(T, Loc), |
| 4387 | Loc, Loc); |
| 4388 | } |
| 4389 | |
| 4390 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4393 | /// \brief Match two template parameters within template parameter lists. |
| 4394 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 4395 | bool Complain, |
| 4396 | Sema::TemplateParameterListEqualKind Kind, |
| 4397 | SourceLocation TemplateArgLoc) { |
| 4398 | // Check the actual kind (type, non-type, template). |
| 4399 | if (Old->getKind() != New->getKind()) { |
| 4400 | if (Complain) { |
| 4401 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 4402 | if (TemplateArgLoc.isValid()) { |
| 4403 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4404 | NextDiag = diag::note_template_param_different_kind; |
| 4405 | } |
| 4406 | S.Diag(New->getLocation(), NextDiag) |
| 4407 | << (Kind != Sema::TPL_TemplateMatch); |
| 4408 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 4409 | << (Kind != Sema::TPL_TemplateMatch); |
| 4410 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4411 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4412 | return false; |
| 4413 | } |
| 4414 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4415 | // Check that both are parameter packs are neither are parameter packs. |
| 4416 | // However, if we are matching a template template argument to a |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4417 | // template template parameter, the template template parameter can have |
| 4418 | // a parameter pack where the template template argument does not. |
| 4419 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 4420 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4421 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4422 | if (Complain) { |
| 4423 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 4424 | if (TemplateArgLoc.isValid()) { |
| 4425 | S.Diag(TemplateArgLoc, |
| 4426 | diag::err_template_arg_template_params_mismatch); |
| 4427 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 4428 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4429 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4430 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 4431 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 4432 | : 2; |
| 4433 | S.Diag(New->getLocation(), NextDiag) |
| 4434 | << ParamKind << New->isParameterPack(); |
| 4435 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 4436 | << ParamKind << Old->isParameterPack(); |
| 4437 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4438 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4439 | return false; |
| 4440 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4441 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4442 | // For non-type template parameters, check the type of the parameter. |
| 4443 | if (NonTypeTemplateParmDecl *OldNTTP |
| 4444 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 4445 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4446 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4447 | // If we are matching a template template argument to a template |
| 4448 | // template parameter and one of the non-type template parameter types |
| 4449 | // is dependent, then we must wait until template instantiation time |
| 4450 | // to actually compare the arguments. |
| 4451 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4452 | (OldNTTP->getType()->isDependentType() || |
| 4453 | NewNTTP->getType()->isDependentType())) |
| 4454 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4455 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4456 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 4457 | if (Complain) { |
| 4458 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 4459 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4460 | S.Diag(TemplateArgLoc, |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4461 | diag::err_template_arg_template_params_mismatch); |
| 4462 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 4463 | } |
| 4464 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 4465 | << NewNTTP->getType() |
| 4466 | << (Kind != Sema::TPL_TemplateMatch); |
| 4467 | S.Diag(OldNTTP->getLocation(), |
| 4468 | diag::note_template_nontype_parm_prev_declaration) |
| 4469 | << OldNTTP->getType(); |
| 4470 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4471 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4472 | return false; |
| 4473 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4474 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4475 | return true; |
| 4476 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4477 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4478 | // For template template parameters, check the template parameter types. |
| 4479 | // The template parameter lists of template template |
| 4480 | // parameters must agree. |
| 4481 | if (TemplateTemplateParmDecl *OldTTP |
| 4482 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4483 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4484 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 4485 | OldTTP->getTemplateParameters(), |
| 4486 | Complain, |
| 4487 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4488 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4489 | : Kind), |
| 4490 | TemplateArgLoc); |
| 4491 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4492 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4493 | return true; |
| 4494 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4495 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4496 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 4497 | /// lists. |
| 4498 | static |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4499 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4500 | TemplateParameterList *New, |
| 4501 | TemplateParameterList *Old, |
| 4502 | Sema::TemplateParameterListEqualKind Kind, |
| 4503 | SourceLocation TemplateArgLoc) { |
| 4504 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 4505 | if (TemplateArgLoc.isValid()) { |
| 4506 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4507 | NextDiag = diag::note_template_param_list_different_arity; |
| 4508 | } |
| 4509 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 4510 | << (New->size() > Old->size()) |
| 4511 | << (Kind != Sema::TPL_TemplateMatch) |
| 4512 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 4513 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 4514 | << (Kind != Sema::TPL_TemplateMatch) |
| 4515 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 4516 | } |
| 4517 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4518 | /// \brief Determine whether the given template parameter lists are |
| 4519 | /// equivalent. |
| 4520 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4521 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4522 | /// source code as part of a new template declaration. |
| 4523 | /// |
| 4524 | /// \param Old The old template parameter list, typically found via |
| 4525 | /// name lookup of the template declared with this template parameter |
| 4526 | /// list. |
| 4527 | /// |
| 4528 | /// \param Complain If true, this routine will produce a diagnostic if |
| 4529 | /// the template parameter lists are not equivalent. |
| 4530 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4531 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4532 | /// |
| 4533 | /// \param TemplateArgLoc If this source location is valid, then we |
| 4534 | /// are actually checking the template parameter list of a template |
| 4535 | /// argument (New) against the template parameter list of its |
| 4536 | /// corresponding template template parameter (Old). We produce |
| 4537 | /// slightly different diagnostics in this scenario. |
| 4538 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4539 | /// \returns True if the template parameter lists are equal, false |
| 4540 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4541 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4542 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 4543 | TemplateParameterList *Old, |
| 4544 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4545 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4546 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4547 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 4548 | if (Complain) |
| 4549 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4550 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4551 | |
| 4552 | return false; |
| 4553 | } |
| 4554 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4555 | // C++0x [temp.arg.template]p3: |
| 4556 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4557 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4558 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4559 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4560 | // template-parameter-list of P. [...] |
| 4561 | TemplateParameterList::iterator NewParm = New->begin(); |
| 4562 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4563 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4564 | OldParmEnd = Old->end(); |
| 4565 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 4566 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 4567 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4568 | if (NewParm == NewParmEnd) { |
| 4569 | if (Complain) |
| 4570 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4571 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4572 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4573 | return false; |
| 4574 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4575 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4576 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4577 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4578 | return false; |
| 4579 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4580 | ++NewParm; |
| 4581 | continue; |
| 4582 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4583 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4584 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4585 | // [...] When P's template- parameter-list contains a template parameter |
| 4586 | // pack (14.5.3), the template parameter pack will match zero or more |
| 4587 | // template parameters or template parameter packs in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4588 | // template-parameter-list of A with the same type and form as the |
| 4589 | // template parameter pack in P (ignoring whether those template |
| 4590 | // parameters are template parameter packs). |
| 4591 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 4592 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4593 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4594 | return false; |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4595 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4596 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4597 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4598 | // Make sure we exhausted all of the arguments. |
| 4599 | if (NewParm != NewParmEnd) { |
| 4600 | if (Complain) |
| 4601 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4602 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4603 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4604 | return false; |
| 4605 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4606 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4607 | return true; |
| 4608 | } |
| 4609 | |
| 4610 | /// \brief Check whether a template can be declared within this scope. |
| 4611 | /// |
| 4612 | /// If the template declaration is valid in this scope, returns |
| 4613 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4614 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4615 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 4616 | if (!S) |
| 4617 | return false; |
| 4618 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4619 | // Find the nearest enclosing declaration scope. |
| 4620 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4621 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4622 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4623 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4624 | // C++ [temp]p2: |
| 4625 | // A template-declaration can appear only as a namespace scope or |
| 4626 | // class scope declaration. |
| 4627 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4628 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 4629 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4630 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4631 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4632 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4633 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4634 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4635 | |
| 4636 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 4637 | return false; |
| 4638 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4639 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4640 | diag::err_template_outside_namespace_or_class_scope) |
| 4641 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4642 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4643 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4644 | /// \brief Determine what kind of template specialization the given declaration |
| 4645 | /// is. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 4646 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4647 | if (!D) |
| 4648 | return TSK_Undeclared; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4649 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4650 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 4651 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4652 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 4653 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4654 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 4655 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4656 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4657 | return TSK_Undeclared; |
| 4658 | } |
| 4659 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4660 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4661 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4662 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4663 | /// This routine determines whether a template specialization can be declared |
| 4664 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4665 | /// |
| 4666 | /// \param S the semantic analysis object for which this check is being |
| 4667 | /// performed. |
| 4668 | /// |
| 4669 | /// \param Specialized the entity being specialized or instantiated, which |
| 4670 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4671 | /// a member of a class template (member function, static data member, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4672 | /// member class). |
| 4673 | /// |
| 4674 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 4675 | /// |
| 4676 | /// \param Loc the location of the explicit specialization or instantiation of |
| 4677 | /// this entity. |
| 4678 | /// |
| 4679 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 4680 | /// a class template. |
| 4681 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4682 | /// \returns true if there was an error that we cannot recover from, false |
| 4683 | /// otherwise. |
| 4684 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 4685 | NamedDecl *Specialized, |
| 4686 | NamedDecl *PrevDecl, |
| 4687 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4688 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4689 | // Keep these "kind" numbers in sync with the %select statements in the |
| 4690 | // various diagnostics emitted by this routine. |
| 4691 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4692 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4693 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4694 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4695 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4696 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4697 | EntityKind = 3; |
| 4698 | else if (isa<VarDecl>(Specialized)) |
| 4699 | EntityKind = 4; |
| 4700 | else if (isa<RecordDecl>(Specialized)) |
| 4701 | EntityKind = 5; |
| 4702 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4703 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 4704 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4705 | return true; |
| 4706 | } |
| 4707 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4708 | // C++ [temp.expl.spec]p2: |
| 4709 | // An explicit specialization shall be declared in the namespace |
| 4710 | // of which the template is a member, or, for member templates, in |
| 4711 | // the namespace of which the enclosing class or enclosing class |
| 4712 | // template is a member. An explicit specialization of a member |
| 4713 | // function, member class or static data member of a class |
| 4714 | // template shall be declared in the namespace of which the class |
| 4715 | // template is a member. Such a declaration may also be a |
| 4716 | // definition. If the declaration is not a definition, the |
| 4717 | // specialization may be defined later in the name- space in which |
| 4718 | // the explicit specialization was declared, or in a namespace |
| 4719 | // that encloses the one in which the explicit specialization was |
| 4720 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4721 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4722 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4723 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4724 | return true; |
| 4725 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4726 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4727 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4728 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4729 | // Do not warn for class scope explicit specialization during |
| 4730 | // instantiation, warning was already emitted during pattern |
| 4731 | // semantic analysis. |
| 4732 | if (!S.ActiveTemplateInstantiations.size()) |
| 4733 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 4734 | << Specialized; |
| 4735 | } else { |
| 4736 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4737 | << Specialized; |
| 4738 | return true; |
| 4739 | } |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4740 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4741 | |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4742 | if (S.CurContext->isRecord() && |
| 4743 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 4744 | // Make sure that we're specializing in the right record context. |
| 4745 | // Otherwise, things can go horribly wrong. |
| 4746 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4747 | << Specialized; |
| 4748 | return true; |
| 4749 | } |
| 4750 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4751 | // C++ [temp.class.spec]p6: |
| 4752 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4753 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4754 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4755 | bool ComplainedAboutScope = false; |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4756 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4757 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4758 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4759 | if ((!PrevDecl || |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4760 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4761 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4762 | // C++ [temp.exp.spec]p2: |
| 4763 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4764 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4765 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4766 | // An explicit specialization of a member function, member class or |
| 4767 | // static data member of a class template shall be declared in the |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4768 | // namespace of which the class template is a member. |
| 4769 | // |
| 4770 | // C++0x [temp.expl.spec]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4771 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4772 | // the specialized template. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4773 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
| 4774 | bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext); |
| 4775 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
| 4776 | assert(!IsCPlusPlus0xExtension && |
| 4777 | "DC encloses TU but isn't in enclosing namespace set"); |
| 4778 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4779 | << EntityKind << Specialized; |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4780 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 4781 | int Diag; |
| 4782 | if (!IsCPlusPlus0xExtension) |
| 4783 | Diag = diag::err_template_spec_decl_out_of_scope; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4784 | else if (!S.getLangOpts().CPlusPlus0x) |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4785 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 4786 | else |
| 4787 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 4788 | S.Diag(Loc, Diag) |
| 4789 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 4790 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4791 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4792 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4793 | ComplainedAboutScope = |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4794 | !(IsCPlusPlus0xExtension && S.getLangOpts().CPlusPlus0x); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4795 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4796 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4797 | |
| 4798 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4799 | // namespace. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4800 | // Note that HandleDeclarator() performs this check for explicit |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4801 | // specializations of function templates, static data members, and member |
| 4802 | // functions, so we skip the check here for those kinds of entities. |
| 4803 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4804 | // Should we refactor that check, so that it occurs later? |
| 4805 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4806 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 4807 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4808 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 4809 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 4810 | << EntityKind << Specialized; |
| 4811 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 4812 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 4813 | << EntityKind << Specialized |
| 4814 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4815 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4816 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4817 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4818 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4819 | // FIXME: check for specialization-after-instantiation errors and such. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4820 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4821 | return false; |
| 4822 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4823 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4824 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 4825 | /// that checks non-type template partial specialization arguments. |
| 4826 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 4827 | NonTypeTemplateParmDecl *Param, |
| 4828 | const TemplateArgument *Args, |
| 4829 | unsigned NumArgs) { |
| 4830 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4831 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 4832 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4833 | Args[I].pack_begin(), |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4834 | Args[I].pack_size())) |
| 4835 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4836 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4837 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4838 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4839 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4840 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4841 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4842 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4843 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4844 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4845 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4846 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4847 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4848 | |
| 4849 | // Strip off any implicit casts we added as part of type checking. |
| 4850 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4851 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4852 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4853 | // C++ [temp.class.spec]p8: |
| 4854 | // A non-type argument is non-specialized if it is the name of a |
| 4855 | // non-type parameter. All other non-type arguments are |
| 4856 | // specialized. |
| 4857 | // |
| 4858 | // Below, we check the two conditions that only apply to |
| 4859 | // specialized non-type arguments, so skip any non-specialized |
| 4860 | // arguments. |
| 4861 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4862 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4863 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4864 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4865 | // C++ [temp.class.spec]p9: |
| 4866 | // Within the argument list of a class template partial |
| 4867 | // specialization, the following restrictions apply: |
| 4868 | // -- A partially specialized non-type argument expression |
| 4869 | // shall not involve a template parameter of the partial |
| 4870 | // specialization except when the argument expression is a |
| 4871 | // simple identifier. |
| 4872 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4873 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4874 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4875 | << ArgExpr->getSourceRange(); |
| 4876 | return true; |
| 4877 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4878 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4879 | // -- The type of a template parameter corresponding to a |
| 4880 | // specialized non-type argument shall not be dependent on a |
| 4881 | // parameter of the specialization. |
| 4882 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4883 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4884 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 4885 | << Param->getType() |
| 4886 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4887 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4888 | return true; |
| 4889 | } |
| 4890 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4891 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4892 | return false; |
| 4893 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4894 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4895 | /// \brief Check the non-type template arguments of a class template |
| 4896 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 4897 | /// |
| 4898 | /// \param TemplateParams the template parameters of the primary class |
| 4899 | /// template. |
| 4900 | /// |
| 4901 | /// \param TemplateArg the template arguments of the class template |
| 4902 | /// partial specialization. |
| 4903 | /// |
| 4904 | /// \returns true if there was an error, false otherwise. |
| 4905 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 4906 | TemplateParameterList *TemplateParams, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4907 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4908 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 4909 | |
| 4910 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4911 | NonTypeTemplateParmDecl *Param |
| 4912 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 4913 | if (!Param) |
| 4914 | continue; |
| 4915 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4916 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4917 | &ArgList[I], 1)) |
| 4918 | return true; |
| 4919 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4920 | |
| 4921 | return false; |
| 4922 | } |
| 4923 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4924 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4925 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4926 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4927 | SourceLocation KWLoc, |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 4928 | SourceLocation ModulePrivateLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4929 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4930 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4931 | SourceLocation TemplateNameLoc, |
| 4932 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4933 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4934 | SourceLocation RAngleLoc, |
| 4935 | AttributeList *Attr, |
| 4936 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4937 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4938 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4939 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 4940 | // store the location of the outermost template keyword in the declaration. |
| 4941 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
| 4942 | ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation(); |
| 4943 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4944 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4945 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4946 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4947 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4948 | |
| 4949 | if (!ClassTemplate) { |
| 4950 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4951 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4952 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4953 | return true; |
| 4954 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4955 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4956 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4957 | bool isPartialSpecialization = false; |
| 4958 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4959 | // Check the validity of the template headers that introduce this |
| 4960 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4961 | // FIXME: We probably shouldn't complain about these headers for |
| 4962 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4963 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4964 | TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 4965 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, |
| 4966 | TemplateNameLoc, |
| 4967 | SS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4968 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4969 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4970 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4971 | isExplicitSpecialization, |
| 4972 | Invalid); |
| 4973 | if (Invalid) |
| 4974 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4975 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4976 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4977 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4978 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4979 | if (TUK == TUK_Friend) { |
| 4980 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4981 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4982 | return true; |
| 4983 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4984 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4985 | // C++ [temp.class.spec]p10: |
| 4986 | // The template parameter list of a specialization shall not |
| 4987 | // contain default template argument values. |
| 4988 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4989 | Decl *Param = TemplateParams->getParam(I); |
| 4990 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4991 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4992 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4993 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4994 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4995 | } |
| 4996 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4997 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4998 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4999 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5000 | diag::err_default_arg_in_partial_spec) |
| 5001 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 5002 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5003 | } |
| 5004 | } else { |
| 5005 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5006 | if (TTP->hasDefaultArgument()) { |
| 5007 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5008 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5009 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 5010 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5011 | } |
| 5012 | } |
| 5013 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 5014 | } else if (TemplateParams) { |
| 5015 | if (TUK == TUK_Friend) |
| 5016 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5017 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 5018 | SourceRange(TemplateParams->getTemplateLoc(), |
| 5019 | TemplateParams->getRAngleLoc())) |
| 5020 | << SourceRange(LAngleLoc, RAngleLoc); |
| 5021 | else |
| 5022 | isExplicitSpecialization = true; |
| 5023 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5024 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5025 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5026 | isExplicitSpecialization = true; |
| 5027 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5028 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5029 | // Check that the specialization uses the same tag kind as the |
| 5030 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5031 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5032 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5033 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 5034 | Kind, TUK == TUK_Definition, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5035 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5036 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5037 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5038 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5039 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5040 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5041 | diag::note_previous_use); |
| 5042 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5043 | } |
| 5044 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5045 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5046 | TemplateArgumentListInfo TemplateArgs; |
| 5047 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 5048 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5049 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5050 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 5051 | // Check for unexpanded parameter packs in any of the template arguments. |
| 5052 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5053 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 5054 | UPPC_PartialSpecialization)) |
| 5055 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5056 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5057 | // Check that the template argument list is well-formed for this |
| 5058 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5059 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5060 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5061 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5062 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5063 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5064 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5065 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5066 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5067 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5068 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5069 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5070 | return true; |
| 5071 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 5072 | bool InstantiationDependent; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5073 | if (!Name.isDependent() && |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5074 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5075 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 5076 | TemplateArgs.size(), |
| 5077 | InstantiationDependent)) { |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5078 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 5079 | << ClassTemplate->getDeclName(); |
| 5080 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5081 | } |
| 5082 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5083 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5084 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5085 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 5086 | |
| 5087 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5088 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5089 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5090 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 5091 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5092 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5093 | else |
| 5094 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5095 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5096 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5097 | |
| 5098 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5099 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5100 | // Check whether we can declare a class template specialization in |
| 5101 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5102 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5103 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 5104 | TemplateNameLoc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5105 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5106 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5107 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5108 | // The canonical type |
| 5109 | QualType CanonType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5110 | if (PrevDecl && |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5111 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5112 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5113 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5114 | // arguments was referenced but not declared, or we're only |
| 5115 | // referencing this specialization as a friend, reuse that |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5116 | // declaration node as our own, updating its source location and |
| 5117 | // the list of outer template parameters to reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5118 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5119 | Specialization->setLocation(TemplateNameLoc); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5120 | if (TemplateParameterLists.size() > 0) { |
| 5121 | Specialization->setTemplateParameterListsInfo(Context, |
| 5122 | TemplateParameterLists.size(), |
| 5123 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5124 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5125 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5126 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5127 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5128 | // Build the canonical type that describes the converted template |
| 5129 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5130 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 5131 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5132 | Converted.data(), |
| 5133 | Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5134 | |
| 5135 | if (Context.hasSameType(CanonType, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5136 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 5137 | // C++ [temp.class.spec]p9b3: |
| 5138 | // |
| 5139 | // -- The argument list of the specialization shall not be identical |
| 5140 | // to the implicit argument list of the primary template. |
| 5141 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 5142 | << (TUK == TUK_Definition) |
| 5143 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5144 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 5145 | ClassTemplate->getIdentifier(), |
| 5146 | TemplateNameLoc, |
| 5147 | Attr, |
| 5148 | TemplateParams, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 5149 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5150 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 5151 | (TemplateParameterList**) TemplateParameterLists.release()); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5152 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5153 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5154 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5155 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 5156 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5157 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5158 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5159 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5160 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5161 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5162 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 5163 | TemplateParams, |
| 5164 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5165 | Converted.data(), |
| 5166 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5167 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5168 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5169 | PrevPartial, |
| 5170 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5171 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5172 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5173 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5174 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5175 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5176 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5177 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5178 | if (!PrevPartial) |
| 5179 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5180 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5181 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5182 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5183 | // template specialization, make a note of that. |
| 5184 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 5185 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5186 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5187 | // Check that all of the template parameters of the class template |
| 5188 | // partial specialization are deducible from the template |
| 5189 | // arguments. If not, this class template partial specialization |
| 5190 | // will never be used. |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5191 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5192 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5193 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5194 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5195 | |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5196 | if (!DeducibleParams.all()) { |
| 5197 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5198 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 5199 | << (NumNonDeducible > 1) |
| 5200 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 5201 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 5202 | if (!DeducibleParams[I]) { |
| 5203 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 5204 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5205 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5206 | diag::note_partial_spec_unused_parameter) |
| 5207 | << Param->getDeclName(); |
| 5208 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5209 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5210 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 5211 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5212 | } |
| 5213 | } |
| 5214 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5215 | } else { |
| 5216 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5217 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5218 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5219 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5220 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5221 | KWLoc, TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5222 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5223 | Converted.data(), |
| 5224 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5225 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5226 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5227 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5228 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5229 | TemplateParameterLists.size(), |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5230 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5231 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5232 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5233 | if (!PrevDecl) |
| 5234 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5235 | |
| 5236 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5237 | } |
| 5238 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5239 | // C++ [temp.expl.spec]p6: |
| 5240 | // 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] | 5241 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5242 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5243 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5244 | // use occurs; no diagnostic is required. |
| 5245 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5246 | bool Okay = false; |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5247 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5248 | // Is there any previous explicit specialization declaration? |
| 5249 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5250 | Okay = true; |
| 5251 | break; |
| 5252 | } |
| 5253 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5254 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5255 | if (!Okay) { |
| 5256 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 5257 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 5258 | << Context.getTypeDeclType(Specialization) << Range; |
| 5259 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5260 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5261 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5262 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5263 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5264 | return true; |
| 5265 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5266 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5267 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5268 | // If this is not a friend, note that this is an explicit specialization. |
| 5269 | if (TUK != TUK_Friend) |
| 5270 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5271 | |
| 5272 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5273 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5274 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5275 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5276 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5277 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5278 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 5279 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5280 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5281 | } |
| 5282 | } |
| 5283 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 5284 | if (Attr) |
| 5285 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 5286 | |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5287 | if (ModulePrivateLoc.isValid()) |
| 5288 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 5289 | << (isPartialSpecialization? 1 : 0) |
| 5290 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 5291 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 5292 | // Build the fully-sugared type for this class template |
| 5293 | // specialization as the user wrote in the specialization |
| 5294 | // itself. This means that we'll pretty-print the type retrieved |
| 5295 | // from the specialization's declaration the way that the user |
| 5296 | // actually wrote the specialization, rather than formatting the |
| 5297 | // name based on the "canonical" representation used to store the |
| 5298 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5299 | TypeSourceInfo *WrittenTy |
| 5300 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5301 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5302 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5303 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5304 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5305 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5306 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5307 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5308 | // C++ [temp.expl.spec]p9: |
| 5309 | // A template explicit specialization is in the scope of the |
| 5310 | // namespace in which the template was defined. |
| 5311 | // |
| 5312 | // We actually implement this paragraph where we set the semantic |
| 5313 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 5314 | // but we also maintain the lexical context where the actual |
| 5315 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5316 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5317 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5318 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5319 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5320 | Specialization->startDefinition(); |
| 5321 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5322 | if (TUK == TUK_Friend) { |
| 5323 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 5324 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5325 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5326 | /*FIXME:*/KWLoc); |
| 5327 | Friend->setAccess(AS_public); |
| 5328 | CurContext->addDecl(Friend); |
| 5329 | } else { |
| 5330 | // Add the specialization into its lexical context, so that it can |
| 5331 | // be seen when iterating through the list of declarations in that |
| 5332 | // context. However, specializations are not found by name lookup. |
| 5333 | CurContext->addDecl(Specialization); |
| 5334 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5335 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5336 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5337 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5338 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5339 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5340 | Declarator &D) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5341 | return HandleDeclarator(S, D, move(TemplateParameterLists)); |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5342 | } |
| 5343 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5344 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5345 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5346 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5347 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5348 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5349 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5350 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5351 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5352 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5353 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5354 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5355 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 5356 | D.setFunctionDefinitionKind(FDK_Definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5357 | Decl *DP = HandleDeclarator(ParentScope, D, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5358 | move(TemplateParameterLists)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5359 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5360 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5361 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5362 | FunctionTemplate->getTemplatedDecl()); |
| 5363 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 5364 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 5365 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5366 | } |
| 5367 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5368 | /// \brief Strips various properties off an implicit instantiation |
| 5369 | /// that has just been explicitly specialized. |
| 5370 | static void StripImplicitInstantiation(NamedDecl *D) { |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 5371 | // 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] | 5372 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5373 | |
| 5374 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5375 | FD->setInlineSpecified(false); |
| 5376 | } |
| 5377 | } |
| 5378 | |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5379 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 5380 | // declaration or definition. |
| 5381 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5382 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5383 | // Explicit instantiations following a specialization have no effect and |
| 5384 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 5385 | // until a valid name loc is found. |
| 5386 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5387 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 5388 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5389 | PrevDiagLoc = Prev->getLocation(); |
| 5390 | } |
| 5391 | assert(PrevDiagLoc.isValid() && |
| 5392 | "Explicit instantiation without point of instantiation?"); |
| 5393 | return PrevDiagLoc; |
| 5394 | } |
| 5395 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5396 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5397 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5398 | /// for those cases where they are required and determining whether the |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5399 | /// new specialization/instantiation will have any effect. |
| 5400 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5401 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5402 | /// instantiation. |
| 5403 | /// |
| 5404 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 5405 | /// |
| 5406 | /// \param PrevDecl the previous declaration of the entity. |
| 5407 | /// |
| 5408 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 5409 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5410 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5411 | /// declaration was instantiated (either implicitly or explicitly). |
| 5412 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5413 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5414 | /// specialization or instantiation has no effect and should be ignored. |
| 5415 | /// |
| 5416 | /// \returns true if there was an error that should prevent the introduction of |
| 5417 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5418 | bool |
| 5419 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 5420 | TemplateSpecializationKind NewTSK, |
| 5421 | NamedDecl *PrevDecl, |
| 5422 | TemplateSpecializationKind PrevTSK, |
| 5423 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5424 | bool &HasNoEffect) { |
| 5425 | HasNoEffect = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5426 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5427 | switch (NewTSK) { |
| 5428 | case TSK_Undeclared: |
| 5429 | case TSK_ImplicitInstantiation: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5430 | llvm_unreachable("Don't check implicit instantiations here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5431 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5432 | case TSK_ExplicitSpecialization: |
| 5433 | switch (PrevTSK) { |
| 5434 | case TSK_Undeclared: |
| 5435 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5436 | // Okay, we're just specializing something that is either already |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5437 | // explicitly specialized or has merely been mentioned without any |
| 5438 | // instantiation. |
| 5439 | return false; |
| 5440 | |
| 5441 | case TSK_ImplicitInstantiation: |
| 5442 | if (PrevPointOfInstantiation.isInvalid()) { |
| 5443 | // The declaration itself has not actually been instantiated, so it is |
| 5444 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5445 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5446 | return false; |
| 5447 | } |
| 5448 | // Fall through |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5449 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5450 | case TSK_ExplicitInstantiationDeclaration: |
| 5451 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5452 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 5453 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5454 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5455 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5456 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5457 | // 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] | 5458 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5459 | // before the first use of that specialization that would cause an |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5460 | // implicit instantiation to take place, in every translation unit in |
| 5461 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5462 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5463 | // Is there any previous explicit specialization declaration? |
| 5464 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 5465 | return false; |
| 5466 | } |
| 5467 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5468 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5469 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5470 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5471 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5472 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5473 | return true; |
| 5474 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5475 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5476 | case TSK_ExplicitInstantiationDeclaration: |
| 5477 | switch (PrevTSK) { |
| 5478 | case TSK_ExplicitInstantiationDeclaration: |
| 5479 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5480 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5481 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5482 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5483 | case TSK_Undeclared: |
| 5484 | case TSK_ImplicitInstantiation: |
| 5485 | // We're explicitly instantiating something that may have already been |
| 5486 | // implicitly instantiated; that's fine. |
| 5487 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5488 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5489 | case TSK_ExplicitSpecialization: |
| 5490 | // C++0x [temp.explicit]p4: |
| 5491 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5492 | // of a template appears after a declaration of an explicit |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5493 | // specialization for that template, the explicit instantiation has no |
| 5494 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5495 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5496 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5497 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5498 | case TSK_ExplicitInstantiationDefinition: |
| 5499 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5500 | // If an entity is the subject of both an explicit instantiation |
| 5501 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5502 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5503 | Diag(NewLoc, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5504 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5505 | |
| 5506 | // Explicit instantiations following a specialization have no effect and |
| 5507 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 5508 | // until a valid name loc is found. |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5509 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 5510 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5511 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5512 | return false; |
| 5513 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5514 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5515 | case TSK_ExplicitInstantiationDefinition: |
| 5516 | switch (PrevTSK) { |
| 5517 | case TSK_Undeclared: |
| 5518 | case TSK_ImplicitInstantiation: |
| 5519 | // We're explicitly instantiating something that may have already been |
| 5520 | // implicitly instantiated; that's fine. |
| 5521 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5522 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5523 | case TSK_ExplicitSpecialization: |
| 5524 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 5525 | // For a given set of template parameters, if an explicit |
| 5526 | // instantiation of a template appears after a declaration of |
| 5527 | // an explicit specialization for that template, the explicit |
| 5528 | // instantiation has no effect. |
| 5529 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5530 | // 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] | 5531 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5532 | // has been explicitly specialized. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5533 | Diag(NewLoc, getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5534 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 5535 | diag::ext_explicit_instantiation_after_specialization) |
| 5536 | << PrevDecl; |
| 5537 | Diag(PrevDecl->getLocation(), |
| 5538 | diag::note_previous_template_specialization); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5539 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5540 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5541 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5542 | case TSK_ExplicitInstantiationDeclaration: |
| 5543 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5544 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5545 | |
| 5546 | // C++0x [temp.explicit]p4: |
| 5547 | // For a given set of template parameters, if an explicit instantiation |
| 5548 | // of a template appears after a declaration of an explicit |
| 5549 | // specialization for that template, the explicit instantiation has no |
| 5550 | // effect. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5551 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5552 | // Is there any previous explicit specialization declaration? |
| 5553 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5554 | HasNoEffect = true; |
| 5555 | break; |
| 5556 | } |
| 5557 | } |
| 5558 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5559 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5560 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5561 | case TSK_ExplicitInstantiationDefinition: |
| 5562 | // C++0x [temp.spec]p5: |
| 5563 | // For a given template and a given set of template-arguments, |
| 5564 | // - an explicit instantiation definition shall appear at most once |
| 5565 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5566 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5567 | << PrevDecl; |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5568 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5569 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5570 | HasNoEffect = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5571 | return false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5572 | } |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5573 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5574 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5575 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5576 | } |
| 5577 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5578 | /// \brief Perform semantic analysis for the given dependent function |
| 5579 | /// template specialization. The only possible way to get a dependent |
| 5580 | /// function template specialization is with a friend declaration, |
| 5581 | /// like so: |
| 5582 | /// |
| 5583 | /// template <class T> void foo(T); |
| 5584 | /// template <class T> class A { |
| 5585 | /// friend void foo<>(T); |
| 5586 | /// }; |
| 5587 | /// |
| 5588 | /// There really isn't any useful analysis we can do here, so we |
| 5589 | /// just store the information. |
| 5590 | bool |
| 5591 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 5592 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 5593 | LookupResult &Previous) { |
| 5594 | // Remove anything from Previous that isn't a function template in |
| 5595 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5596 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5597 | LookupResult::Filter F = Previous.makeFilter(); |
| 5598 | while (F.hasNext()) { |
| 5599 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 5600 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5601 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 5602 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5603 | F.erase(); |
| 5604 | } |
| 5605 | F.done(); |
| 5606 | |
| 5607 | // Should this be diagnosed here? |
| 5608 | if (Previous.empty()) return true; |
| 5609 | |
| 5610 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 5611 | ExplicitTemplateArgs); |
| 5612 | return false; |
| 5613 | } |
| 5614 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5615 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5616 | /// specialization. |
| 5617 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5618 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5619 | /// explicit function template specialization. On successful completion, |
| 5620 | /// the function declaration \p FD will become a function template |
| 5621 | /// specialization. |
| 5622 | /// |
| 5623 | /// \param FD the function declaration, which will be updated to become a |
| 5624 | /// function template specialization. |
| 5625 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5626 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 5627 | /// if any. Note that this may be valid info even when 0 arguments are |
| 5628 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 5629 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5630 | /// |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5631 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5632 | /// this function specialization. |
| 5633 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5634 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5635 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5636 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5637 | // The set of function template specializations that could match this |
| 5638 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5639 | UnresolvedSet<8> Candidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5640 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5641 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5642 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5643 | I != E; ++I) { |
| 5644 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 5645 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5646 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5647 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5648 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 5649 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5650 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5651 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5652 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5653 | // A trailing template-argument can be left unspecified in the |
| 5654 | // template-id naming an explicit function template specialization |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5655 | // provided it can be deduced from the function argument type. |
| 5656 | // Perform template argument deduction to determine whether we may be |
| 5657 | // specializing this template. |
| 5658 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5659 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5660 | FunctionDecl *Specialization = 0; |
| 5661 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5662 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5663 | FD->getType(), |
| 5664 | Specialization, |
| 5665 | Info)) { |
| 5666 | // FIXME: Template argument deduction failed; record why it failed, so |
| 5667 | // that we can provide nifty diagnostics. |
| 5668 | (void)TDK; |
| 5669 | continue; |
| 5670 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5671 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5672 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5673 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5674 | } |
| 5675 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5676 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5677 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5678 | UnresolvedSetIterator Result |
| 5679 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5680 | TPOC_Other, 0, FD->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5681 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5682 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5683 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5684 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5685 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5686 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5687 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5688 | |
| 5689 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 5690 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | abfb405 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 5691 | |
| 5692 | FunctionTemplateSpecializationInfo *SpecInfo |
| 5693 | = Specialization->getTemplateSpecializationInfo(); |
| 5694 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5695 | |
| 5696 | // Note: do not overwrite location info if previous template |
| 5697 | // specialization kind was explicit. |
| 5698 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 5699 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5700 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 5701 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 5702 | // function can differ from the template declaration with respect to |
| 5703 | // the constexpr specifier. |
| 5704 | Specialization->setConstexpr(FD->isConstexpr()); |
| 5705 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5706 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5707 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5708 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5709 | |
| 5710 | // If this is a friend declaration, then we're not really declaring |
| 5711 | // an explicit specialization. |
| 5712 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5713 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5714 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5715 | if (!isFriend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5716 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5717 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5718 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5719 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5720 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5721 | |
| 5722 | // C++ [temp.expl.spec]p6: |
| 5723 | // 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] | 5724 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5725 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5726 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5727 | // use occurs; no diagnostic is required. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5728 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5729 | if (!isFriend && |
| 5730 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5731 | TSK_ExplicitSpecialization, |
| 5732 | Specialization, |
| 5733 | SpecInfo->getTemplateSpecializationKind(), |
| 5734 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5735 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5736 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5737 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5738 | // Mark the prior declaration as an explicit specialization, so that later |
| 5739 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5740 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5741 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5742 | MarkUnusedFileScopedDecl(Specialization); |
| 5743 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5744 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5745 | // Turn the given function declaration into a function template |
| 5746 | // specialization, with the template arguments from the previous |
| 5747 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5748 | // Take copies of (semantic and syntactic) template argument lists. |
| 5749 | const TemplateArgumentList* TemplArgs = new (Context) |
| 5750 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5751 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5752 | TemplArgs, /*InsertPos=*/0, |
| 5753 | SpecInfo->getTemplateSpecializationKind(), |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 5754 | ExplicitTemplateArgs); |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5755 | FD->setStorageClass(Specialization->getStorageClass()); |
| 5756 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5757 | // The "previous declaration" for this function template specialization is |
| 5758 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5759 | Previous.clear(); |
| 5760 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5761 | return false; |
| 5762 | } |
| 5763 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5764 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5765 | /// specialization. |
| 5766 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5767 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5768 | /// explicit member function specialization. On successful completion, |
| 5769 | /// the function declaration \p FD will become a member function |
| 5770 | /// specialization. |
| 5771 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5772 | /// \param Member the member declaration, which will be updated to become a |
| 5773 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5774 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5775 | /// \param Previous the set of declarations, one of which may be specialized |
| 5776 | /// by this function specialization; the set will be modified to contain the |
| 5777 | /// redeclared member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5778 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5779 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5780 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5781 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5782 | // Try to find the member we are instantiating. |
| 5783 | NamedDecl *Instantiation = 0; |
| 5784 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5785 | MemberSpecializationInfo *MSInfo = 0; |
| 5786 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5787 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5788 | // Nowhere to look anyway. |
| 5789 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5790 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5791 | I != E; ++I) { |
| 5792 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5793 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5794 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 5795 | Instantiation = Method; |
| 5796 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5797 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5798 | break; |
| 5799 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5800 | } |
| 5801 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5802 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5803 | VarDecl *PrevVar; |
| 5804 | if (Previous.isSingleResult() && |
| 5805 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5806 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5807 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5808 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5809 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5810 | } |
| 5811 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5812 | CXXRecordDecl *PrevRecord; |
| 5813 | if (Previous.isSingleResult() && |
| 5814 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 5815 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5816 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5817 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5818 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5819 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5820 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5821 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5822 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5823 | // specializations are always out-of-line, the caller will complain about |
| 5824 | // this mismatch later. |
| 5825 | return false; |
| 5826 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5827 | |
| 5828 | // If this is a friend, just bail out here before we start turning |
| 5829 | // things into explicit specializations. |
| 5830 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 5831 | // Preserve instantiation information. |
| 5832 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 5833 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 5834 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5835 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5836 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 5837 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 5838 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5839 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5840 | } |
| 5841 | |
| 5842 | Previous.clear(); |
| 5843 | Previous.addDecl(Instantiation); |
| 5844 | return false; |
| 5845 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5846 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5847 | // Make sure that this is a specialization of a member. |
| 5848 | if (!InstantiatedFrom) { |
| 5849 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 5850 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5851 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 5852 | return true; |
| 5853 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5854 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5855 | // C++ [temp.expl.spec]p6: |
| 5856 | // 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] | 5857 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5858 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5859 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5860 | // use occurs; no diagnostic is required. |
| 5861 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5862 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5863 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5864 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 5865 | TSK_ExplicitSpecialization, |
| 5866 | Instantiation, |
| 5867 | MSInfo->getTemplateSpecializationKind(), |
| 5868 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5869 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5870 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5871 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5872 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5873 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5874 | InstantiatedFrom, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5875 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5876 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5877 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 5878 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5879 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5880 | // the original declaration to note that it is an explicit specialization |
| 5881 | // (if it was previously an implicit instantiation). This latter step |
| 5882 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5883 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5884 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 5885 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 5886 | TSK_ImplicitInstantiation) { |
| 5887 | InstantiationFunction->setTemplateSpecializationKind( |
| 5888 | TSK_ExplicitSpecialization); |
| 5889 | InstantiationFunction->setLocation(Member->getLocation()); |
| 5890 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5891 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5892 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 5893 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5894 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5895 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5896 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5897 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 5898 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 5899 | TSK_ImplicitInstantiation) { |
| 5900 | InstantiationVar->setTemplateSpecializationKind( |
| 5901 | TSK_ExplicitSpecialization); |
| 5902 | InstantiationVar->setLocation(Member->getLocation()); |
| 5903 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5904 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5905 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 5906 | cast<VarDecl>(InstantiatedFrom), |
| 5907 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5908 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5909 | } else { |
| 5910 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5911 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 5912 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 5913 | TSK_ImplicitInstantiation) { |
| 5914 | InstantiationClass->setTemplateSpecializationKind( |
| 5915 | TSK_ExplicitSpecialization); |
| 5916 | InstantiationClass->setLocation(Member->getLocation()); |
| 5917 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5918 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5919 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5920 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5921 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5922 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5923 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5924 | // Save the caller the trouble of having to figure out which declaration |
| 5925 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5926 | Previous.clear(); |
| 5927 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5928 | return false; |
| 5929 | } |
| 5930 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5931 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5932 | /// |
| 5933 | /// \returns true if a serious error occurs, false otherwise. |
| 5934 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5935 | SourceLocation InstLoc, |
| 5936 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5937 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5938 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5939 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5940 | if (CurContext->isRecord()) { |
| 5941 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5942 | << D; |
| 5943 | return true; |
| 5944 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5945 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5946 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5947 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5948 | // template. If the name declared in the explicit instantiation is an |
| 5949 | // unqualified name, the explicit instantiation shall appear in the |
| 5950 | // namespace where its template is declared or, if that namespace is inline |
| 5951 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5952 | // |
| 5953 | // 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] | 5954 | if (WasQualifiedName) { |
| 5955 | if (CurContext->Encloses(OrigContext)) |
| 5956 | return false; |
| 5957 | } else { |
| 5958 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 5959 | return false; |
| 5960 | } |
| 5961 | |
| 5962 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 5963 | if (WasQualifiedName) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5964 | S.Diag(InstLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5965 | S.getLangOpts().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5966 | diag::err_explicit_instantiation_out_of_scope : |
| 5967 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5968 | << D << NS; |
| 5969 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5970 | S.Diag(InstLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5971 | S.getLangOpts().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5972 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 5973 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 5974 | << D << NS; |
| 5975 | } else |
| 5976 | S.Diag(InstLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5977 | S.getLangOpts().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5978 | diag::err_explicit_instantiation_must_be_global : |
| 5979 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 5980 | << D; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5981 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5982 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5983 | } |
| 5984 | |
| 5985 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5986 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5987 | if (!SS.isSet()) |
| 5988 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5989 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5990 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5991 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5992 | // or a static data member of a class template specialization, the name of |
| 5993 | // the class template specialization in the qualified-id for the member |
| 5994 | // name shall be a simple-template-id. |
| 5995 | // |
| 5996 | // C++98 has the same restriction, just worded differently. |
| 5997 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5998 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5999 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6000 | if (isa<TemplateSpecializationType>(T)) |
| 6001 | return true; |
| 6002 | |
| 6003 | return false; |
| 6004 | } |
| 6005 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6006 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6007 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6008 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 6009 | SourceLocation ExternLoc, |
| 6010 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6011 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6012 | SourceLocation KWLoc, |
| 6013 | const CXXScopeSpec &SS, |
| 6014 | TemplateTy TemplateD, |
| 6015 | SourceLocation TemplateNameLoc, |
| 6016 | SourceLocation LAngleLoc, |
| 6017 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6018 | SourceLocation RAngleLoc, |
| 6019 | AttributeList *Attr) { |
| 6020 | // Find the class template we're specializing |
| 6021 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6022 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6023 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 6024 | |
| 6025 | // Check that the specialization uses the same tag kind as the |
| 6026 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6027 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6028 | assert(Kind != TTK_Enum && |
| 6029 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6030 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6031 | Kind, /*isDefinition*/false, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6032 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6033 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6034 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6035 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6036 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6037 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6038 | diag::note_previous_use); |
| 6039 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6040 | } |
| 6041 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6042 | // C++0x [temp.explicit]p2: |
| 6043 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6044 | // definition and an explicit instantiation declaration. An explicit |
| 6045 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6046 | TemplateSpecializationKind TSK |
| 6047 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6048 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6049 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6050 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6051 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 6052 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6053 | |
| 6054 | // Check that the template argument list is well-formed for this |
| 6055 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6056 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6057 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6058 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6059 | return true; |
| 6060 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6061 | // Find the class template specialization declaration that |
| 6062 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6063 | void *InsertPos = 0; |
| 6064 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6065 | = ClassTemplate->findSpecialization(Converted.data(), |
| 6066 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6067 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6068 | TemplateSpecializationKind PrevDecl_TSK |
| 6069 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 6070 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6071 | // C++0x [temp.explicit]p2: |
| 6072 | // [...] An explicit instantiation shall appear in an enclosing |
| 6073 | // namespace of its template. [...] |
| 6074 | // |
| 6075 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6076 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 6077 | SS.isSet())) |
| 6078 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6079 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6080 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 6081 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6082 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6083 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6084 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6085 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6086 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6087 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6088 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6089 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6090 | // Even though HasNoEffect == true means that this explicit instantiation |
| 6091 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 6092 | |
| 6093 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 6094 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6095 | // Since the only prior class template specialization with these |
| 6096 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6097 | // declaration node as our own, updating the source location |
| 6098 | // for the template name to reflect our new declaration. |
| 6099 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6100 | Specialization = PrevDecl; |
| 6101 | Specialization->setLocation(TemplateNameLoc); |
| 6102 | PrevDecl = 0; |
| 6103 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6104 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6105 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6106 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6107 | // Create a new class template specialization declaration node for |
| 6108 | // this explicit specialization. |
| 6109 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6110 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6111 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6112 | KWLoc, TemplateNameLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6113 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6114 | Converted.data(), |
| 6115 | Converted.size(), |
| 6116 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6117 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6118 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6119 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6120 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6121 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6122 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6123 | } |
| 6124 | |
| 6125 | // Build the fully-sugared type for this explicit instantiation as |
| 6126 | // the user wrote in the explicit instantiation itself. This means |
| 6127 | // that we'll pretty-print the type retrieved from the |
| 6128 | // specialization's declaration the way that the user actually wrote |
| 6129 | // the explicit instantiation, rather than formatting the name based |
| 6130 | // on the "canonical" representation used to store the template |
| 6131 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6132 | TypeSourceInfo *WrittenTy |
| 6133 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6134 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6135 | Context.getTypeDeclType(Specialization)); |
| 6136 | Specialization->setTypeAsWritten(WrittenTy); |
| 6137 | TemplateArgsIn.release(); |
| 6138 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6139 | // Set source locations for keywords. |
| 6140 | Specialization->setExternLoc(ExternLoc); |
| 6141 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 6142 | |
Rafael Espindola | 0257b7f | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 6143 | if (Attr) |
| 6144 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6145 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6146 | // Add the explicit instantiation into its lexical context. However, |
| 6147 | // since explicit instantiations are never found by name lookup, we |
| 6148 | // just put it into the declaration context directly. |
| 6149 | Specialization->setLexicalDeclContext(CurContext); |
| 6150 | CurContext->addDecl(Specialization); |
| 6151 | |
| 6152 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 6153 | if (HasNoEffect) { |
| 6154 | // Set the template specialization kind. |
| 6155 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6156 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 6157 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6158 | |
| 6159 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6160 | // A definition of a class template or class member template |
| 6161 | // shall be in scope at the point of the explicit instantiation of |
| 6162 | // the class template or class member template. |
| 6163 | // |
| 6164 | // This check comes when we actually try to perform the |
| 6165 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6166 | ClassTemplateSpecializationDecl *Def |
| 6167 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6168 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6169 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6170 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6171 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6172 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6173 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 6174 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6175 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6176 | // Instantiate the members of this class template specialization. |
| 6177 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6178 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6179 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 6180 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 6181 | |
| 6182 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 6183 | // TSK_ExplicitInstantiationDefinition |
| 6184 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 6185 | TSK == TSK_ExplicitInstantiationDefinition) |
| 6186 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6187 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6188 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6189 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6190 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6191 | // Set the template specialization kind. |
| 6192 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6193 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6194 | } |
| 6195 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6196 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6197 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6198 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 6199 | SourceLocation ExternLoc, |
| 6200 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6201 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6202 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6203 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6204 | IdentifierInfo *Name, |
| 6205 | SourceLocation NameLoc, |
| 6206 | AttributeList *Attr) { |
| 6207 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 6208 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6209 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6210 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6211 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6212 | /*ModulePrivateLoc=*/SourceLocation(), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6213 | MultiTemplateParamsArg(*this, 0, 0), |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 6214 | Owned, IsDependent, SourceLocation(), false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6215 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6216 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 6217 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6218 | if (!TagD) |
| 6219 | return true; |
| 6220 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6221 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6222 | if (Tag->isEnum()) { |
| 6223 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 6224 | << Context.getTypeDeclType(Tag); |
| 6225 | return true; |
| 6226 | } |
| 6227 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 6228 | if (Tag->isInvalidDecl()) |
| 6229 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6230 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6231 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 6232 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 6233 | if (!Pattern) { |
| 6234 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 6235 | << Context.getTypeDeclType(Record); |
| 6236 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 6237 | return true; |
| 6238 | } |
| 6239 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6240 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6241 | // If the explicit instantiation is for a class or member class, the |
| 6242 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6243 | // simple-template-id. |
| 6244 | // |
| 6245 | // C++98 has the same restriction, just worded differently. |
| 6246 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6247 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6248 | << Record << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6249 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6250 | // C++0x [temp.explicit]p2: |
| 6251 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6252 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6253 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 6254 | TemplateSpecializationKind TSK |
| 6255 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6256 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6257 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6258 | // C++0x [temp.explicit]p2: |
| 6259 | // [...] An explicit instantiation shall appear in an enclosing |
| 6260 | // namespace of its template. [...] |
| 6261 | // |
| 6262 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6263 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6264 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6265 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6266 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 6267 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6268 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6269 | PrevDecl = Record; |
| 6270 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6271 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6272 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6273 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6274 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6275 | PrevDecl, |
| 6276 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6277 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6278 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6279 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6280 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6281 | return TagD; |
| 6282 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6283 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6284 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6285 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6286 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6287 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6288 | // 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] | 6289 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6290 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6291 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6292 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 6293 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 6294 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6295 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 6296 | << Pattern; |
| 6297 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6298 | } else { |
| 6299 | if (InstantiateClass(NameLoc, Record, Def, |
| 6300 | getTemplateInstantiationArgs(Record), |
| 6301 | TSK)) |
| 6302 | return true; |
| 6303 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6304 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6305 | if (!RecordDef) |
| 6306 | return true; |
| 6307 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6308 | } |
| 6309 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6310 | // Instantiate all of the members of the class. |
| 6311 | InstantiateClassMembers(NameLoc, RecordDef, |
| 6312 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6313 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6314 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 6315 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 6316 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6317 | // FIXME: We don't have any representation for explicit instantiations of |
| 6318 | // member classes. Such a representation is not needed for compilation, but it |
| 6319 | // should be available for clients that want to see all of the declarations in |
| 6320 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6321 | return TagD; |
| 6322 | } |
| 6323 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6324 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 6325 | SourceLocation ExternLoc, |
| 6326 | SourceLocation TemplateLoc, |
| 6327 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6328 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6329 | // TODO: check if/when DNInfo should replace Name. |
| 6330 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6331 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6332 | if (!Name) { |
| 6333 | if (!D.isInvalidType()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6334 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6335 | diag::err_explicit_instantiation_requires_name) |
| 6336 | << D.getDeclSpec().getSourceRange() |
| 6337 | << D.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6338 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6339 | return true; |
| 6340 | } |
| 6341 | |
| 6342 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 6343 | // we find one that is. |
| 6344 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 6345 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 6346 | S = S->getParent(); |
| 6347 | |
| 6348 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6349 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 6350 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6351 | if (R.isNull()) |
| 6352 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6353 | |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6354 | // C++ [dcl.stc]p1: |
| 6355 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 6356 | // instantiation (14.7.2) directive. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6357 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6358 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 6359 | << Name; |
| 6360 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6361 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 6362 | != DeclSpec::SCS_unspecified) { |
| 6363 | // Complain about then remove the storage class specifier. |
| 6364 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 6365 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6366 | |
| 6367 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6368 | } |
| 6369 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6370 | // C++0x [temp.explicit]p1: |
| 6371 | // [...] An explicit instantiation of a function template shall not use the |
| 6372 | // inline or constexpr specifiers. |
| 6373 | // Presumably, this also applies to member functions of class templates as |
| 6374 | // well. |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6375 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6376 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6377 | getLangOpts().CPlusPlus0x ? |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6378 | diag::err_explicit_instantiation_inline : |
| 6379 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | fe6f648 | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 6380 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
| 6381 | if (D.getDeclSpec().isConstexprSpecified()) |
| 6382 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 6383 | // not already specified. |
| 6384 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 6385 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6386 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6387 | // C++0x [temp.explicit]p2: |
| 6388 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6389 | // definition and an explicit instantiation declaration. An explicit |
| 6390 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6391 | TemplateSpecializationKind TSK |
| 6392 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6393 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6394 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6395 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6396 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6397 | |
| 6398 | if (!R->isFunctionType()) { |
| 6399 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6400 | // A [...] static data member of a class template can be explicitly |
| 6401 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6402 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6403 | if (Previous.isAmbiguous()) |
| 6404 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6405 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6406 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6407 | if (!Prev || !Prev->isStaticDataMember()) { |
| 6408 | // We expect to see a data data member here. |
| 6409 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 6410 | << Name; |
| 6411 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6412 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6413 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6414 | return true; |
| 6415 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6416 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6417 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 6418 | // FIXME: Check for explicit specialization? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6419 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6420 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 6421 | << Prev; |
| 6422 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 6423 | // FIXME: Can we provide a note showing where this was declared? |
| 6424 | return true; |
| 6425 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6426 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6427 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6428 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6429 | // or a static data member of a class template specialization, the name of |
| 6430 | // the class template specialization in the qualified-id for the member |
| 6431 | // name shall be a simple-template-id. |
| 6432 | // |
| 6433 | // C++98 has the same restriction, just worded differently. |
| 6434 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6435 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6436 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6437 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6438 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6439 | // Check the scope of this explicit instantiation. |
| 6440 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6441 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6442 | // Verify that it is okay to explicitly instantiate here. |
| 6443 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 6444 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6445 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6446 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6447 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6448 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6449 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6450 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6451 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6452 | return (Decl*) 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6453 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6454 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6455 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6456 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6457 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6458 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6459 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6460 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6461 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6462 | |
| 6463 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6464 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6465 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6466 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6467 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 6468 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6469 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 6470 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6471 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 6472 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6473 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6474 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6475 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 6476 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6477 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6478 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6479 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6480 | // A [...] function [...] can be explicitly instantiated from its template. |
| 6481 | // A member function [...] of a class template can be explicitly |
| 6482 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6483 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6484 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6485 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6486 | P != PEnd; ++P) { |
| 6487 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6488 | if (!HasExplicitTemplateArgs) { |
| 6489 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 6490 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 6491 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6492 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6493 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6494 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 6495 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6496 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6497 | } |
| 6498 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6499 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6500 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 6501 | if (!FunTmpl) |
| 6502 | continue; |
| 6503 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6504 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6505 | FunctionDecl *Specialization = 0; |
| 6506 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6507 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6508 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6509 | R, Specialization, Info)) { |
| 6510 | // FIXME: Keep track of almost-matches? |
| 6511 | (void)TDK; |
| 6512 | continue; |
| 6513 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6514 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6515 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6516 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6517 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6518 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6519 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6520 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6521 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6522 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 6523 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 6524 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6525 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6526 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6527 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6528 | |
| 6529 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 6530 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6531 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6532 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6533 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6534 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 6535 | << Specialization |
| 6536 | << (Specialization->getTemplateSpecializationKind() == |
| 6537 | TSK_ExplicitSpecialization); |
| 6538 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 6539 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6540 | } |
| 6541 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 6542 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6543 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 6544 | PrevDecl = Specialization; |
| 6545 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6546 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6547 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6548 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6549 | PrevDecl, |
| 6550 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6551 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6552 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6553 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6554 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6555 | // FIXME: We may still want to build some representation of this |
| 6556 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6557 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6558 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6559 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 6560 | |
| 6561 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 256fc4d | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 6562 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 6563 | if (Attr) |
| 6564 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6565 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6566 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6567 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6568 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6569 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6570 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6571 | // or a static data member of a class template specialization, the name of |
| 6572 | // the class template specialization in the qualified-id for the member |
| 6573 | // name shall be a simple-template-id. |
| 6574 | // |
| 6575 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6576 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6577 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6578 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6579 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6580 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6581 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6582 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6583 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6584 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6585 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6586 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6587 | D.getIdentifierLoc(), |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6588 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6589 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6590 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6591 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6592 | } |
| 6593 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6594 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6595 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 6596 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 6597 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 6598 | // This has to hold, because SS is expected to be defined. |
| 6599 | assert(Name && "Expected a name in a dependent tag"); |
| 6600 | |
| 6601 | NestedNameSpecifier *NNS |
| 6602 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6603 | if (!NNS) |
| 6604 | return true; |
| 6605 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6606 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 6607 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6608 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 6609 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6610 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6611 | return true; |
| 6612 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6613 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6614 | // Create the resulting type. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6615 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6616 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 6617 | |
| 6618 | // Create type-source location information for this type. |
| 6619 | TypeLocBuilder TLB; |
| 6620 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6621 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6622 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6623 | TL.setNameLoc(NameLoc); |
| 6624 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6625 | } |
| 6626 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6627 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6628 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6629 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6630 | SourceLocation IdLoc) { |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6631 | if (SS.isInvalid()) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6632 | return true; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6633 | |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6634 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6635 | Diag(TypenameLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6636 | getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6637 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6638 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6639 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6640 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6641 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6642 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 6643 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 6644 | if (T.isNull()) |
| 6645 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6646 | |
| 6647 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6648 | if (isa<DependentNameType>(T)) { |
| 6649 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6650 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6651 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6652 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6653 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6654 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6655 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6656 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6657 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6658 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6659 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6660 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6661 | } |
| 6662 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6663 | TypeResult |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6664 | Sema::ActOnTypenameType(Scope *S, |
| 6665 | SourceLocation TypenameLoc, |
| 6666 | const CXXScopeSpec &SS, |
| 6667 | SourceLocation TemplateKWLoc, |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6668 | TemplateTy TemplateIn, |
| 6669 | SourceLocation TemplateNameLoc, |
| 6670 | SourceLocation LAngleLoc, |
| 6671 | ASTTemplateArgsPtr TemplateArgsIn, |
| 6672 | SourceLocation RAngleLoc) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6673 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6674 | Diag(TypenameLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6675 | getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6676 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6677 | diag::ext_typename_outside_of_template) |
| 6678 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6679 | |
| 6680 | // Translate the parser's template argument list in our AST format. |
| 6681 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 6682 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 6683 | |
| 6684 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6685 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 6686 | // Construct a dependent template specialization type. |
| 6687 | assert(DTN && "dependent template has non-dependent name?"); |
| 6688 | assert(DTN->getQualifier() |
| 6689 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 6690 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 6691 | DTN->getQualifier(), |
| 6692 | DTN->getIdentifier(), |
| 6693 | TemplateArgs); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6694 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6695 | // Create source-location information for this type. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6696 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6697 | DependentTemplateSpecializationTypeLoc SpecTL |
| 6698 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6699 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 6700 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 66581d4 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 6701 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6702 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6703 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6704 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6705 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6706 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6707 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 6708 | } |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6709 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6710 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 6711 | if (T.isNull()) |
| 6712 | return true; |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6713 | |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6714 | // Provide source-location information for the template specialization type. |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6715 | TypeLocBuilder Builder; |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6716 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6717 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6718 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 6719 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6720 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6721 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6722 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6723 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 6724 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6725 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 6726 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6727 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6728 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6729 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6730 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 6731 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 6732 | } |
| 6733 | |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6734 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6735 | /// \brief Build the type that describes a C++ typename specifier, |
| 6736 | /// e.g., "typename T::type". |
| 6737 | QualType |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6738 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 6739 | SourceLocation KeywordLoc, |
| 6740 | NestedNameSpecifierLoc QualifierLoc, |
| 6741 | const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6742 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6743 | CXXScopeSpec SS; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6744 | SS.Adopt(QualifierLoc); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6745 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6746 | DeclContext *Ctx = computeDeclContext(SS); |
| 6747 | if (!Ctx) { |
| 6748 | // If the nested-name-specifier is dependent and couldn't be |
| 6749 | // resolved to a type, build a typename type. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6750 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 6751 | return Context.getDependentNameType(Keyword, |
| 6752 | QualifierLoc.getNestedNameSpecifier(), |
| 6753 | &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 6754 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6755 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6756 | // If the nested-name-specifier refers to the current instantiation, |
| 6757 | // the "typename" keyword itself is superfluous. In C++03, the |
| 6758 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 6759 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 6760 | // 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] | 6761 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6762 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 6763 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6764 | |
| 6765 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6766 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6767 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6768 | unsigned DiagID = 0; |
| 6769 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6770 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6771 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6772 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6773 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6774 | |
| 6775 | case LookupResult::FoundUnresolvedValue: { |
| 6776 | // We found a using declaration that is a value. Most likely, the using |
| 6777 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6778 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6779 | IILoc); |
| 6780 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 6781 | << Name << Ctx << FullRange; |
| 6782 | if (UnresolvedUsingValueDecl *Using |
| 6783 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6784 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6785 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 6786 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 6787 | } |
| 6788 | } |
| 6789 | // Fall through to create a dependent typename type, from which we can recover |
| 6790 | // better. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6791 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 6792 | case LookupResult::NotFoundInCurrentInstantiation: |
| 6793 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6794 | return Context.getDependentNameType(Keyword, |
| 6795 | QualifierLoc.getNestedNameSpecifier(), |
| 6796 | &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6797 | |
| 6798 | case LookupResult::Found: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6799 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6800 | // We found a type. Build an ElaboratedType, since the |
| 6801 | // typename-specifier was just sugar. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6802 | return Context.getElaboratedType(ETK_Typename, |
| 6803 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6804 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6805 | } |
| 6806 | |
| 6807 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6808 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6809 | break; |
| 6810 | |
| 6811 | case LookupResult::FoundOverloaded: |
| 6812 | DiagID = diag::err_typename_nested_not_type; |
| 6813 | Referenced = *Result.begin(); |
| 6814 | break; |
| 6815 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 6816 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6817 | return QualType(); |
| 6818 | } |
| 6819 | |
| 6820 | // If we get here, it's because name lookup did not find a |
| 6821 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6822 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6823 | IILoc); |
| 6824 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6825 | if (Referenced) |
| 6826 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 6827 | << Name; |
| 6828 | return QualType(); |
| 6829 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6830 | |
| 6831 | namespace { |
| 6832 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 6833 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6834 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6835 | SourceLocation Loc; |
| 6836 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6837 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6838 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 6839 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6840 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6841 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6842 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6843 | DeclarationName Entity) |
| 6844 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6845 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6846 | |
| 6847 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6848 | /// transformed. |
| 6849 | /// |
| 6850 | /// For the purposes of type reconstruction, a type has already been |
| 6851 | /// transformed if it is NULL or if it is not dependent. |
| 6852 | bool AlreadyTransformed(QualType T) { |
| 6853 | return T.isNull() || !T->isDependentType(); |
| 6854 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6855 | |
| 6856 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6857 | /// rebuilt. |
| 6858 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6859 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6860 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 6861 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6862 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6863 | /// \brief Sets the "base" location and entity when that |
| 6864 | /// information is known based on another transformation. |
| 6865 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 6866 | this->Loc = Loc; |
| 6867 | this->Entity = Entity; |
| 6868 | } |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 6869 | |
| 6870 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 6871 | // Lambdas never need to be transformed. |
| 6872 | return E; |
| 6873 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6874 | }; |
| 6875 | } |
| 6876 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6877 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 6878 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6879 | /// 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] | 6880 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6881 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6882 | /// partial specialization thereof). This routine will rebuild that type now |
| 6883 | /// that we have entered the declarator's scope, which may produce different |
| 6884 | /// canonical types, e.g., |
| 6885 | /// |
| 6886 | /// \code |
| 6887 | /// template<typename T> |
| 6888 | /// struct X { |
| 6889 | /// typedef T* pointer; |
| 6890 | /// pointer data(); |
| 6891 | /// }; |
| 6892 | /// |
| 6893 | /// template<typename T> |
| 6894 | /// typename X<T>::pointer X<T>::data() { ... } |
| 6895 | /// \endcode |
| 6896 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 6897 | /// 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] | 6898 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 6899 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6900 | /// 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] | 6901 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 6902 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6903 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 6904 | SourceLocation Loc, |
| 6905 | DeclarationName Name) { |
| 6906 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6907 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6908 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6909 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 6910 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 6911 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6912 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6913 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6914 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 6915 | DeclarationName()); |
| 6916 | return Rebuilder.TransformExpr(E); |
| 6917 | } |
| 6918 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6919 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6920 | if (SS.isInvalid()) |
| 6921 | return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6922 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6923 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6924 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 6925 | DeclarationName()); |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6926 | NestedNameSpecifierLoc Rebuilt |
| 6927 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 6928 | if (!Rebuilt) |
| 6929 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6930 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6931 | SS.Adopt(Rebuilt); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6932 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6933 | } |
| 6934 | |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 6935 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 6936 | /// instantiation. |
| 6937 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 6938 | TemplateParameterList *Params) { |
| 6939 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 6940 | Decl *Param = Params->getParam(I); |
| 6941 | |
| 6942 | // There is nothing to rebuild in a type parameter. |
| 6943 | if (isa<TemplateTypeParmDecl>(Param)) |
| 6944 | continue; |
| 6945 | |
| 6946 | // Rebuild the template parameter list of a template template parameter. |
| 6947 | if (TemplateTemplateParmDecl *TTP |
| 6948 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 6949 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 6950 | TTP->getTemplateParameters())) |
| 6951 | return true; |
| 6952 | |
| 6953 | continue; |
| 6954 | } |
| 6955 | |
| 6956 | // Rebuild the type of a non-type template parameter. |
| 6957 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 6958 | TypeSourceInfo *NewTSI |
| 6959 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 6960 | NTTP->getLocation(), |
| 6961 | NTTP->getDeclName()); |
| 6962 | if (!NewTSI) |
| 6963 | return true; |
| 6964 | |
| 6965 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 6966 | NTTP->setTypeSourceInfo(NewTSI); |
| 6967 | NTTP->setType(NewTSI->getType()); |
| 6968 | } |
| 6969 | } |
| 6970 | |
| 6971 | return false; |
| 6972 | } |
| 6973 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6974 | /// \brief Produces a formatted string that describes the binding of |
| 6975 | /// template parameters to template arguments. |
| 6976 | std::string |
| 6977 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6978 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6979 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6980 | } |
| 6981 | |
| 6982 | std::string |
| 6983 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6984 | const TemplateArgument *Args, |
| 6985 | unsigned NumArgs) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6986 | SmallString<128> Str; |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6987 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6988 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6989 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6990 | return std::string(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6991 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6992 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6993 | if (I >= NumArgs) |
| 6994 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6995 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6996 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6997 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6998 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6999 | Out << ", "; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7000 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7001 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7002 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7003 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7004 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7005 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7006 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7007 | Out << " = "; |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7008 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7009 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7010 | |
| 7011 | Out << ']'; |
| 7012 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7013 | } |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 7014 | |
| 7015 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) { |
| 7016 | if (!FD) |
| 7017 | return; |
| 7018 | FD->setLateTemplateParsed(Flag); |
| 7019 | } |
| 7020 | |
| 7021 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 7022 | DeclContext *DC = CurContext; |
| 7023 | |
| 7024 | while (DC) { |
| 7025 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 7026 | const FunctionDecl *FD = RD->isLocalClass(); |
| 7027 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 7028 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 7029 | return false; |
| 7030 | |
| 7031 | DC = DC->getParent(); |
| 7032 | } |
| 7033 | return false; |
| 7034 | } |