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 | 8b0fa52 | 2012-03-30 16:20:47 +0000 | [diff] [blame] | 875 | // FIXME: Horrible, horrible hack! We can't currently represent this |
| 876 | // in the AST, and historically we have just ignored such friend |
| 877 | // class templates, so don't complain here. |
| 878 | if (TUK != TUK_Friend) |
| 879 | Diag(NameLoc, diag::err_template_qualified_declarator_no_match) |
| 880 | << SS.getScopeRep() << SS.getRange(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 881 | return true; |
| 882 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 883 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 884 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 885 | return true; |
| 886 | |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 887 | // If we're adding a template to a dependent context, we may need to |
| 888 | // rebuilding some of the types used within the template parameter list, |
| 889 | // now that we know what the current instantiation is. |
| 890 | if (SemanticContext->isDependentContext()) { |
| 891 | ContextRAII SavedContext(*this, SemanticContext); |
| 892 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 893 | Invalid = true; |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 894 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
| 895 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc); |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 896 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 897 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 898 | } else { |
| 899 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 900 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 901 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 903 | if (Previous.isAmbiguous()) |
| 904 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 905 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 906 | NamedDecl *PrevDecl = 0; |
| 907 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 908 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 909 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 910 | // If there is a previous declaration with the same name, check |
| 911 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 912 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 913 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 914 | |
| 915 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 916 | // class template partial specialization, or class template specialization. |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 917 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 918 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 919 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 920 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 921 | PrevClassTemplate |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 922 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 923 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 924 | PrevClassTemplate |
| 925 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 926 | ->getSpecializedTemplate(); |
| 927 | } |
| 928 | } |
| 929 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 930 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 931 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 932 | // [...] When looking for a prior declaration of a class or a function |
| 933 | // 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] | 934 | // function is neither a qualified name nor a template-id, scopes outside |
| 935 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 936 | if (!SS.isSet()) { |
| 937 | DeclContext *OutermostContext = CurContext; |
| 938 | while (!OutermostContext->isFileContext()) |
| 939 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 940 | |
Richard Smith | b2f245e | 2012-04-20 05:42:36 +0000 | [diff] [blame^] | 941 | if (PrevClassTemplate && |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 942 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 943 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 944 | SemanticContext = PrevDecl->getDeclContext(); |
| 945 | } else { |
| 946 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 947 | // context we computed is the semantic context for our new |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 948 | // declaration. |
| 949 | PrevDecl = PrevClassTemplate = 0; |
| 950 | SemanticContext = OutermostContext; |
| 951 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 952 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 953 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 954 | if (CurContext->isDependentContext()) { |
| 955 | // If this is a dependent context, we don't want to link the friend |
| 956 | // class template to the template in scope, because that would perform |
| 957 | // checking of the template parameter lists that can't be performed |
| 958 | // until the outer context is instantiated. |
| 959 | PrevDecl = PrevClassTemplate = 0; |
| 960 | } |
| 961 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 962 | PrevDecl = PrevClassTemplate = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 963 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 964 | if (PrevClassTemplate) { |
| 965 | // Ensure that the template parameter lists are compatible. |
| 966 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 967 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 968 | /*Complain=*/true, |
| 969 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 970 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 971 | |
| 972 | // C++ [temp.class]p4: |
| 973 | // In a redeclaration, partial specialization, explicit |
| 974 | // specialization or explicit instantiation of a class template, |
| 975 | // the class-key shall agree in kind with the original class |
| 976 | // template declaration (7.1.5.3). |
| 977 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 978 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
| 979 | TUK == TUK_Definition, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 980 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 981 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 982 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 983 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 984 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 987 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 988 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 989 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 990 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 991 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 992 | // FIXME: Would it make sense to try to "forget" the previous |
| 993 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 994 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 995 | } |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 996 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 997 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 998 | // Maybe we will complain about the shadowed template parameter. |
| 999 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 1000 | // Just pretend that we didn't see the previous declaration. |
| 1001 | PrevDecl = 0; |
| 1002 | } else if (PrevDecl) { |
| 1003 | // C++ [temp]p5: |
| 1004 | // A class template shall not have the same name as any other |
| 1005 | // template, class, function, object, enumeration, enumerator, |
| 1006 | // namespace, or type in the same scope (3.3), except as specified |
| 1007 | // in (14.5.4). |
| 1008 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1009 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1010 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1013 | // Check the template parameter list of this declaration, possibly |
| 1014 | // merging in the template parameter list from the previous class |
| 1015 | // template declaration. |
| 1016 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1017 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1018 | (SS.isSet() && SemanticContext && |
Douglas Gregor | 461bf2e | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 1019 | SemanticContext->isRecord() && |
| 1020 | SemanticContext->isDependentContext()) |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1021 | ? TPC_ClassTemplateMember |
| 1022 | : TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1023 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1024 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1025 | if (SS.isSet()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1026 | // 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] | 1027 | // template out-of-line. |
| 1028 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
Douglas Gregor | ea9f54a | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1029 | !(TUK == TUK_Friend && CurContext->isDependentContext())) { |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1030 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 1031 | << Name << SemanticContext << SS.getRange(); |
Douglas Gregor | ea9f54a | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1032 | Invalid = true; |
| 1033 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1036 | CXXRecordDecl *NewClass = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1037 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1038 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1039 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 1040 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1041 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1042 | if (NumOuterTemplateParamLists > 0) |
| 1043 | NewClass->setTemplateParameterListsInfo(Context, |
| 1044 | NumOuterTemplateParamLists, |
| 1045 | OuterTemplateParamLists); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1046 | |
Eli Friedman | 572ae0a | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1047 | // Add alignment attributes if necessary; these attributes are checked when |
| 1048 | // the ASTContext lays out the structure. |
| 1049 | AddAlignmentAttributesForRecord(NewClass); |
| 1050 | AddMsStructLayoutForRecord(NewClass); |
| 1051 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1052 | ClassTemplateDecl *NewTemplate |
| 1053 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1054 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1055 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1056 | NewClass->setDescribedClassTemplate(NewTemplate); |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1057 | |
Douglas Gregor | 2ccd89c | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1058 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1059 | NewTemplate->setModulePrivate(); |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1060 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1061 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1062 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1063 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1064 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1065 | (void)T; |
| 1066 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1067 | // 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] | 1068 | // class template, make a note of that. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1069 | if (PrevClassTemplate && |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1070 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1071 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1072 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1073 | // Set the access specifier. |
Douglas Gregor | 42acead | 2012-03-17 23:06:31 +0000 | [diff] [blame] | 1074 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1075 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1076 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1077 | // Set the lexical context of these templates |
| 1078 | NewClass->setLexicalDeclContext(CurContext); |
| 1079 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1080 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1081 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1082 | NewClass->startDefinition(); |
| 1083 | |
| 1084 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1085 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1086 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1087 | if (TUK != TUK_Friend) |
| 1088 | PushOnScopeChains(NewTemplate, S); |
| 1089 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1090 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1091 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1092 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1093 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1094 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1095 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1096 | PrevClassTemplate != NULL); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1097 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1098 | // Friend templates are visible in fairly strange ways. |
| 1099 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1100 | DeclContext *DC = SemanticContext->getRedeclContext(); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1101 | DC->makeDeclVisibleInContext(NewTemplate); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1102 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1103 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1104 | /* AddToContext = */ false); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1105 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1106 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1107 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1108 | NewClass->getLocation(), |
| 1109 | NewTemplate, |
| 1110 | /*FIXME:*/NewClass->getLocation()); |
| 1111 | Friend->setAccess(AS_public); |
| 1112 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1113 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1114 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1115 | if (Invalid) { |
| 1116 | NewTemplate->setInvalidDecl(); |
| 1117 | NewClass->setInvalidDecl(); |
| 1118 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1119 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1122 | /// \brief Diagnose the presence of a default template argument on a |
| 1123 | /// template parameter, which is ill-formed in certain contexts. |
| 1124 | /// |
| 1125 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1126 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1127 | Sema::TemplateParamListContext TPC, |
| 1128 | SourceLocation ParamLoc, |
| 1129 | SourceRange DefArgRange) { |
| 1130 | switch (TPC) { |
| 1131 | case Sema::TPC_ClassTemplate: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1132 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1133 | return false; |
| 1134 | |
| 1135 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1136 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1137 | // C++ [temp.param]p9: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1138 | // A default template-argument shall not be specified in a |
| 1139 | // function template declaration or a function template |
| 1140 | // definition [...] |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1141 | // If a friend function template declaration specifies a default |
| 1142 | // template-argument, that declaration shall be a definition and shall be |
| 1143 | // the only declaration of the function template in the translation unit. |
| 1144 | // (C++98/03 doesn't have this wording; see DR226). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1145 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1146 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1147 | : diag::ext_template_parameter_default_in_function_template) |
| 1148 | << DefArgRange; |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1149 | return false; |
| 1150 | |
| 1151 | case Sema::TPC_ClassTemplateMember: |
| 1152 | // C++0x [temp.param]p9: |
| 1153 | // A default template-argument shall not be specified in the |
| 1154 | // template-parameter-lists of the definition of a member of a |
| 1155 | // class template that appears outside of the member's class. |
| 1156 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1157 | << DefArgRange; |
| 1158 | return true; |
| 1159 | |
| 1160 | case Sema::TPC_FriendFunctionTemplate: |
| 1161 | // C++ [temp.param]p9: |
| 1162 | // A default template-argument shall not be specified in a |
| 1163 | // friend template declaration. |
| 1164 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1165 | << DefArgRange; |
| 1166 | return true; |
| 1167 | |
| 1168 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1169 | // for friend function templates if there is only a single |
| 1170 | // declaration (and it is a definition). Strange! |
| 1171 | } |
| 1172 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1173 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1176 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1177 | /// of a template template parameter, recursively. |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1178 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1179 | TemplateTemplateParmDecl *TTP) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1180 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1181 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1182 | NamedDecl *P = Params->getParam(I); |
| 1183 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1184 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1185 | NTTP->getTypeSourceInfo(), |
| 1186 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1187 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1188 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1189 | continue; |
| 1190 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1191 | |
| 1192 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1193 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1194 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1195 | return true; |
| 1196 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1197 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1198 | return false; |
| 1199 | } |
| 1200 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1201 | /// \brief Checks the validity of a template parameter list, possibly |
| 1202 | /// considering the template parameter list from a previous |
| 1203 | /// declaration. |
| 1204 | /// |
| 1205 | /// If an "old" template parameter list is provided, it must be |
| 1206 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1207 | /// template parameter list. |
| 1208 | /// |
| 1209 | /// \param NewParams Template parameter list for a new template |
| 1210 | /// declaration. This template parameter list will be updated with any |
| 1211 | /// default arguments that are carried through from the previous |
| 1212 | /// template parameter list. |
| 1213 | /// |
| 1214 | /// \param OldParams If provided, template parameter list from a |
| 1215 | /// previous declaration of the same template. Default template |
| 1216 | /// arguments will be merged from the old template parameter list to |
| 1217 | /// the new template parameter list. |
| 1218 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1219 | /// \param TPC Describes the context in which we are checking the given |
| 1220 | /// template parameter list. |
| 1221 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1222 | /// \returns true if an error occurred, false otherwise. |
| 1223 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1224 | TemplateParameterList *OldParams, |
| 1225 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1226 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1228 | // C++ [temp.param]p10: |
| 1229 | // The set of default template-arguments available for use with a |
| 1230 | // template declaration or definition is obtained by merging the |
| 1231 | // default arguments from the definition (if in scope) and all |
| 1232 | // declarations in scope in the same way default function |
| 1233 | // arguments are (8.3.6). |
| 1234 | bool SawDefaultArgument = false; |
| 1235 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1236 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1237 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1238 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1239 | if (OldParams) |
| 1240 | OldParam = OldParams->begin(); |
| 1241 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1242 | bool RemoveDefaultArguments = false; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1243 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1244 | NewParamEnd = NewParams->end(); |
| 1245 | NewParam != NewParamEnd; ++NewParam) { |
| 1246 | // Variables used to diagnose redundant default arguments |
| 1247 | bool RedundantDefaultArg = false; |
| 1248 | SourceLocation OldDefaultLoc; |
| 1249 | SourceLocation NewDefaultLoc; |
| 1250 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1251 | // Variable used to diagnose missing default arguments |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1252 | bool MissingDefaultArg = false; |
| 1253 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1254 | // Variable used to diagnose non-final parameter packs |
| 1255 | bool SawParameterPack = false; |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1256 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1257 | if (TemplateTypeParmDecl *NewTypeParm |
| 1258 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1259 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1260 | if (NewTypeParm->hasDefaultArgument() && |
| 1261 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1262 | NewTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1263 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1264 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1265 | NewTypeParm->removeDefaultArgument(); |
| 1266 | |
| 1267 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1268 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1269 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1271 | if (NewTypeParm->isParameterPack()) { |
| 1272 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1273 | "Parameter packs can't have a default argument!"); |
| 1274 | SawParameterPack = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1275 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1276 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1277 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1278 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1279 | SawDefaultArgument = true; |
| 1280 | RedundantDefaultArg = true; |
| 1281 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1282 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1283 | // Merge the default argument from the old declaration to the |
| 1284 | // new declaration. |
| 1285 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1286 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1287 | true); |
| 1288 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1289 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1290 | SawDefaultArgument = true; |
| 1291 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1292 | } else if (SawDefaultArgument) |
| 1293 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1294 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1295 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1296 | // Check for unexpanded parameter packs. |
| 1297 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1298 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1299 | UPPC_NonTypeTemplateParameterType)) { |
| 1300 | Invalid = true; |
| 1301 | continue; |
| 1302 | } |
| 1303 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1304 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1305 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1306 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1307 | NewNonTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1308 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1309 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1312 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1313 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1314 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1315 | if (NewNonTypeParm->isParameterPack()) { |
| 1316 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1317 | "Parameter packs can't have a default argument!"); |
| 1318 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1319 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1320 | NewNonTypeParm->hasDefaultArgument()) { |
| 1321 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1322 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1323 | SawDefaultArgument = true; |
| 1324 | RedundantDefaultArg = true; |
| 1325 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1326 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1327 | // Merge the default argument from the old declaration to the |
| 1328 | // new declaration. |
| 1329 | SawDefaultArgument = true; |
| 1330 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1331 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1332 | // parameter. |
| 1333 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1334 | OldNonTypeParm->getDefaultArgument(), |
| 1335 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1336 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1337 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1338 | SawDefaultArgument = true; |
| 1339 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1340 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1342 | } else { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1343 | TemplateTemplateParmDecl *NewTemplateParm |
| 1344 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1345 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1346 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1347 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1348 | Invalid = true; |
| 1349 | continue; |
| 1350 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1351 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1352 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1353 | if (NewTemplateParm->hasDefaultArgument() && |
| 1354 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1355 | NewTemplateParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1356 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1357 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1358 | |
| 1359 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1360 | TemplateTemplateParmDecl *OldTemplateParm |
| 1361 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1362 | if (NewTemplateParm->isParameterPack()) { |
| 1363 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1364 | "Parameter packs can't have a default argument!"); |
| 1365 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1366 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1367 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1368 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1369 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1370 | SawDefaultArgument = true; |
| 1371 | RedundantDefaultArg = true; |
| 1372 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1373 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1374 | // Merge the default argument from the old declaration to the |
| 1375 | // new declaration. |
| 1376 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1377 | // FIXME: We need to create a new kind of "default argument" expression |
| 1378 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1379 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1380 | OldTemplateParm->getDefaultArgument(), |
| 1381 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1382 | PreviousDefaultArgLoc |
| 1383 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1384 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1385 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1386 | PreviousDefaultArgLoc |
| 1387 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1388 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1389 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1392 | // C++0x [temp.param]p11: |
| 1393 | // If a template parameter of a primary class template or alias template |
| 1394 | // is a template parameter pack, it shall be the last template parameter. |
| 1395 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
| 1396 | (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) { |
| 1397 | Diag((*NewParam)->getLocation(), |
| 1398 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1399 | Invalid = true; |
| 1400 | } |
| 1401 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1402 | if (RedundantDefaultArg) { |
| 1403 | // C++ [temp.param]p12: |
| 1404 | // A template-parameter shall not be given default arguments |
| 1405 | // by two different declarations in the same scope. |
| 1406 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1407 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1408 | Invalid = true; |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1409 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1410 | // C++ [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1411 | // If a template-parameter of a class template has a default |
| 1412 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1413 | // have a default template-argument supplied or be a template parameter |
| 1414 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1416 | diag::err_template_param_default_arg_missing); |
| 1417 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1418 | Invalid = true; |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1419 | RemoveDefaultArguments = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | // If we have an old template parameter list that we're merging |
| 1423 | // in, move on to the next parameter. |
| 1424 | if (OldParams) |
| 1425 | ++OldParam; |
| 1426 | } |
| 1427 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1428 | // We were missing some default arguments at the end of the list, so remove |
| 1429 | // all of the default arguments. |
| 1430 | if (RemoveDefaultArguments) { |
| 1431 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1432 | NewParamEnd = NewParams->end(); |
| 1433 | NewParam != NewParamEnd; ++NewParam) { |
| 1434 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1435 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1436 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1437 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1438 | NTTP->removeDefaultArgument(); |
| 1439 | else |
| 1440 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1441 | } |
| 1442 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1443 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1444 | return Invalid; |
| 1445 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1446 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1447 | namespace { |
| 1448 | |
| 1449 | /// A class which looks for a use of a certain level of template |
| 1450 | /// parameter. |
| 1451 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1452 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1453 | |
| 1454 | unsigned Depth; |
| 1455 | bool Match; |
| 1456 | |
| 1457 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1458 | NamedDecl *ND = Params->getParam(0); |
| 1459 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1460 | Depth = PD->getDepth(); |
| 1461 | } else if (NonTypeTemplateParmDecl *PD = |
| 1462 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1463 | Depth = PD->getDepth(); |
| 1464 | } else { |
| 1465 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | bool Matches(unsigned ParmDepth) { |
| 1470 | if (ParmDepth >= Depth) { |
| 1471 | Match = true; |
| 1472 | return true; |
| 1473 | } |
| 1474 | return false; |
| 1475 | } |
| 1476 | |
| 1477 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1478 | return !Matches(T->getDepth()); |
| 1479 | } |
| 1480 | |
| 1481 | bool TraverseTemplateName(TemplateName N) { |
| 1482 | if (TemplateTemplateParmDecl *PD = |
| 1483 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1484 | if (Matches(PD->getDepth())) return false; |
| 1485 | return super::TraverseTemplateName(N); |
| 1486 | } |
| 1487 | |
| 1488 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1489 | if (NonTypeTemplateParmDecl *PD = |
| 1490 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1491 | if (PD->getDepth() == Depth) { |
| 1492 | Match = true; |
| 1493 | return false; |
| 1494 | } |
| 1495 | } |
| 1496 | return super::VisitDeclRefExpr(E); |
| 1497 | } |
Douglas Gregor | 18c8339 | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1498 | |
| 1499 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1500 | return TraverseType(T->getInjectedSpecializationType()); |
| 1501 | } |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1502 | }; |
| 1503 | } |
| 1504 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1505 | /// Determines whether a given type depends on the given parameter |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1506 | /// list. |
| 1507 | static bool |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1508 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1509 | DependencyChecker Checker(Params); |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1510 | Checker.TraverseType(T); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1511 | return Checker.Match; |
| 1512 | } |
| 1513 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1514 | // Find the source range corresponding to the named type in the given |
| 1515 | // nested-name-specifier, if any. |
| 1516 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1517 | QualType T, |
| 1518 | const CXXScopeSpec &SS) { |
| 1519 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1520 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1521 | if (const Type *CurType = NNS->getAsType()) { |
| 1522 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1523 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1524 | } else |
| 1525 | break; |
| 1526 | |
| 1527 | NNSLoc = NNSLoc.getPrefix(); |
| 1528 | } |
| 1529 | |
| 1530 | return SourceRange(); |
| 1531 | } |
| 1532 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1533 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1534 | /// specifier, returning the template parameter list that applies to the |
| 1535 | /// name. |
| 1536 | /// |
| 1537 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1538 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1539 | /// |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1540 | /// \param DeclLoc The location of the declaration itself. |
| 1541 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1542 | /// \param SS the scope specifier that will be matched to the given template |
| 1543 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1544 | /// being declared. |
| 1545 | /// |
| 1546 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1547 | /// innermost template parameter lists. |
| 1548 | /// |
| 1549 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1550 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1551 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1552 | /// matching template parameters to scope specifiers in friend |
| 1553 | /// declarations. |
| 1554 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1555 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1556 | /// declared is an explicit specialization, false otherwise. |
| 1557 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1559 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1560 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1562 | /// 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] | 1563 | /// itself a template). |
| 1564 | TemplateParameterList * |
| 1565 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1566 | SourceLocation DeclLoc, |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1567 | const CXXScopeSpec &SS, |
| 1568 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1569 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1570 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1571 | bool &IsExplicitSpecialization, |
| 1572 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1573 | IsExplicitSpecialization = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1574 | Invalid = false; |
| 1575 | |
| 1576 | // The sequence of nested types to which we will match up the template |
| 1577 | // parameter lists. We first build this list by starting with the type named |
| 1578 | // 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] | 1579 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1580 | QualType T; |
Douglas Gregor | 714c992 | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1581 | if (SS.getScopeRep()) { |
| 1582 | if (CXXRecordDecl *Record |
| 1583 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1584 | T = Context.getTypeDeclType(Record); |
| 1585 | else |
| 1586 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1587 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1588 | |
| 1589 | // If we found an explicit specialization that prevents us from needing |
| 1590 | // 'template<>' headers, this will be set to the location of that |
| 1591 | // explicit specialization. |
| 1592 | SourceLocation ExplicitSpecLoc; |
| 1593 | |
| 1594 | while (!T.isNull()) { |
| 1595 | NestedTypes.push_back(T); |
| 1596 | |
| 1597 | // Retrieve the parent of a record type. |
| 1598 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1599 | // If this type is an explicit specialization, we're done. |
| 1600 | if (ClassTemplateSpecializationDecl *Spec |
| 1601 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1602 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1603 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1604 | ExplicitSpecLoc = Spec->getLocation(); |
| 1605 | break; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1606 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1607 | } else if (Record->getTemplateSpecializationKind() |
| 1608 | == TSK_ExplicitSpecialization) { |
| 1609 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1610 | break; |
| 1611 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1612 | |
| 1613 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1614 | T = Context.getTypeDeclType(Parent); |
| 1615 | else |
| 1616 | T = QualType(); |
| 1617 | continue; |
| 1618 | } |
| 1619 | |
| 1620 | if (const TemplateSpecializationType *TST |
| 1621 | = T->getAs<TemplateSpecializationType>()) { |
| 1622 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1623 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1624 | T = Context.getTypeDeclType(Parent); |
| 1625 | else |
| 1626 | T = QualType(); |
| 1627 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1628 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | // Look one step prior in a dependent template specialization type. |
| 1632 | if (const DependentTemplateSpecializationType *DependentTST |
| 1633 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1634 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1635 | T = QualType(NNS->getAsType(), 0); |
| 1636 | else |
| 1637 | T = QualType(); |
| 1638 | continue; |
| 1639 | } |
| 1640 | |
| 1641 | // Look one step prior in a dependent name type. |
| 1642 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1643 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1644 | T = QualType(NNS->getAsType(), 0); |
| 1645 | else |
| 1646 | T = QualType(); |
| 1647 | continue; |
| 1648 | } |
| 1649 | |
| 1650 | // Retrieve the parent of an enumeration type. |
| 1651 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1652 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1653 | // check here. |
| 1654 | EnumDecl *Enum = EnumT->getDecl(); |
| 1655 | |
| 1656 | // Get to the parent type. |
| 1657 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1658 | T = Context.getTypeDeclType(Parent); |
| 1659 | else |
| 1660 | T = QualType(); |
| 1661 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1662 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1663 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1664 | T = QualType(); |
| 1665 | } |
| 1666 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1667 | // to the innermost while checking template-parameter-lists. |
| 1668 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1669 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1670 | // C++0x [temp.expl.spec]p17: |
| 1671 | // A member or a member template may be nested within many |
| 1672 | // enclosing class templates. In an explicit specialization for |
| 1673 | // such a member, the member declaration shall be preceded by a |
| 1674 | // template<> for each enclosing class template that is |
| 1675 | // explicitly specialized. |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1676 | bool SawNonEmptyTemplateParameterList = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1677 | unsigned ParamIdx = 0; |
| 1678 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1679 | ++TypeIdx) { |
| 1680 | T = NestedTypes[TypeIdx]; |
| 1681 | |
| 1682 | // Whether we expect a 'template<>' header. |
| 1683 | bool NeedEmptyTemplateHeader = false; |
| 1684 | |
| 1685 | // Whether we expect a template header with parameters. |
| 1686 | bool NeedNonemptyTemplateHeader = false; |
| 1687 | |
| 1688 | // For a dependent type, the set of template parameters that we |
| 1689 | // expect to see. |
| 1690 | TemplateParameterList *ExpectedTemplateParams = 0; |
| 1691 | |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1692 | // C++0x [temp.expl.spec]p15: |
| 1693 | // A member or a member template may be nested within many enclosing |
| 1694 | // class templates. In an explicit specialization for such a member, the |
| 1695 | // member declaration shall be preceded by a template<> for each |
| 1696 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1697 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1698 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1699 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1700 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1701 | NeedNonemptyTemplateHeader = true; |
| 1702 | } else if (Record->isDependentType()) { |
| 1703 | if (Record->getDescribedClassTemplate()) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1704 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1705 | ->getTemplateParameters(); |
| 1706 | NeedNonemptyTemplateHeader = true; |
| 1707 | } |
| 1708 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1709 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1710 | // C++0x [temp.expl.spec]p4: |
| 1711 | // Members of an explicitly specialized class template are defined |
| 1712 | // in the same manner as members of normal classes, and not using |
| 1713 | // the template<> syntax. |
| 1714 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1715 | NeedEmptyTemplateHeader = true; |
| 1716 | else |
Douglas Gregor | 95ea450 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1717 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1718 | } else if (Record->getTemplateSpecializationKind()) { |
| 1719 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1720 | != TSK_ExplicitSpecialization && |
| 1721 | TypeIdx == NumTypes - 1) |
| 1722 | IsExplicitSpecialization = true; |
| 1723 | |
| 1724 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1725 | } |
| 1726 | } else if (const TemplateSpecializationType *TST |
| 1727 | = T->getAs<TemplateSpecializationType>()) { |
| 1728 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1729 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1730 | NeedNonemptyTemplateHeader = true; |
| 1731 | } |
| 1732 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1733 | // FIXME: We actually could/should check the template arguments here |
| 1734 | // against the corresponding template parameter list. |
| 1735 | NeedNonemptyTemplateHeader = false; |
| 1736 | } |
| 1737 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1738 | // C++ [temp.expl.spec]p16: |
| 1739 | // In an explicit specialization declaration for a member of a class |
| 1740 | // template or a member template that ap- pears in namespace scope, the |
| 1741 | // member template and some of its enclosing class templates may remain |
| 1742 | // unspecialized, except that the declaration shall not explicitly |
| 1743 | // specialize a class member template if its en- closing class templates |
| 1744 | // are not explicitly specialized as well. |
| 1745 | if (ParamIdx < NumParamLists) { |
| 1746 | if (ParamLists[ParamIdx]->size() == 0) { |
| 1747 | if (SawNonEmptyTemplateParameterList) { |
| 1748 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1749 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1750 | Invalid = true; |
| 1751 | IsExplicitSpecialization = false; |
| 1752 | return 0; |
| 1753 | } |
| 1754 | } else |
| 1755 | SawNonEmptyTemplateParameterList = true; |
| 1756 | } |
| 1757 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1758 | if (NeedEmptyTemplateHeader) { |
| 1759 | // If we're on the last of the types, and we need a 'template<>' header |
| 1760 | // here, then it's an explicit specialization. |
| 1761 | if (TypeIdx == NumTypes - 1) |
| 1762 | IsExplicitSpecialization = true; |
| 1763 | |
| 1764 | if (ParamIdx < NumParamLists) { |
| 1765 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1766 | // The header has template parameters when it shouldn't. Complain. |
| 1767 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1768 | diag::err_template_param_list_matches_nontemplate) |
| 1769 | << T |
| 1770 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1771 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1772 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1773 | Invalid = true; |
| 1774 | return 0; |
| 1775 | } |
| 1776 | |
| 1777 | // Consume this template header. |
| 1778 | ++ParamIdx; |
| 1779 | continue; |
| 1780 | } |
| 1781 | |
| 1782 | if (!IsFriend) { |
| 1783 | // We don't have a template header, but we should. |
| 1784 | SourceLocation ExpectedTemplateLoc; |
| 1785 | if (NumParamLists > 0) |
| 1786 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1787 | else |
| 1788 | ExpectedTemplateLoc = DeclStartLoc; |
| 1789 | |
| 1790 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1791 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS) |
| 1792 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1793 | } |
| 1794 | |
| 1795 | continue; |
| 1796 | } |
| 1797 | |
| 1798 | if (NeedNonemptyTemplateHeader) { |
| 1799 | // In friend declarations we can have template-ids which don't |
| 1800 | // depend on the corresponding template parameter lists. But |
| 1801 | // assume that empty parameter lists are supposed to match this |
| 1802 | // template-id. |
| 1803 | if (IsFriend && T->isDependentType()) { |
| 1804 | if (ParamIdx < NumParamLists && |
| 1805 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
| 1806 | ExpectedTemplateParams = 0; |
| 1807 | else |
| 1808 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1809 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1810 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1811 | if (ParamIdx < NumParamLists) { |
| 1812 | // Check the template parameter list, if we can. |
| 1813 | if (ExpectedTemplateParams && |
| 1814 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1815 | ExpectedTemplateParams, |
| 1816 | true, TPL_TemplateMatch)) |
| 1817 | Invalid = true; |
| 1818 | |
| 1819 | if (!Invalid && |
| 1820 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1821 | TPC_ClassTemplateMember)) |
| 1822 | Invalid = true; |
| 1823 | |
| 1824 | ++ParamIdx; |
| 1825 | continue; |
| 1826 | } |
| 1827 | |
| 1828 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1829 | << T |
| 1830 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1831 | Invalid = true; |
| 1832 | continue; |
| 1833 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1834 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1835 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1836 | // If there were at least as many template-ids as there were template |
| 1837 | // parameter lists, then there are no template parameter lists remaining for |
| 1838 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1839 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1840 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1841 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1842 | // If there were too many template parameter lists, complain about that now. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1843 | if (ParamIdx < NumParamLists - 1) { |
| 1844 | bool HasAnyExplicitSpecHeader = false; |
| 1845 | bool AllExplicitSpecHeaders = true; |
| 1846 | for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) { |
| 1847 | if (ParamLists[I]->size() == 0) |
| 1848 | HasAnyExplicitSpecHeader = true; |
| 1849 | else |
| 1850 | AllExplicitSpecHeaders = false; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1851 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1852 | |
| 1853 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1854 | AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers |
| 1855 | : diag::err_template_spec_extra_headers) |
| 1856 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1857 | ParamLists[NumParamLists - 2]->getRAngleLoc()); |
| 1858 | |
| 1859 | // If there was a specialization somewhere, such that 'template<>' is |
| 1860 | // not required, and there were any 'template<>' headers, note where the |
| 1861 | // specialization occurred. |
| 1862 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1863 | Diag(ExplicitSpecLoc, |
| 1864 | diag::note_explicit_template_spec_does_not_need_header) |
| 1865 | << NestedTypes.back(); |
| 1866 | |
| 1867 | // We have a template parameter list with no corresponding scope, which |
| 1868 | // means that the resulting template declaration can't be instantiated |
| 1869 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1870 | if (!AllExplicitSpecHeaders) |
| 1871 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1872 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1873 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1874 | // C++ [temp.expl.spec]p16: |
| 1875 | // In an explicit specialization declaration for a member of a class |
| 1876 | // template or a member template that ap- pears in namespace scope, the |
| 1877 | // member template and some of its enclosing class templates may remain |
| 1878 | // unspecialized, except that the declaration shall not explicitly |
| 1879 | // specialize a class member template if its en- closing class templates |
| 1880 | // are not explicitly specialized as well. |
| 1881 | if (ParamLists[NumParamLists - 1]->size() == 0 && |
| 1882 | SawNonEmptyTemplateParameterList) { |
| 1883 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1884 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1885 | Invalid = true; |
| 1886 | IsExplicitSpecialization = false; |
| 1887 | return 0; |
| 1888 | } |
| 1889 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1890 | // Return the last template parameter list, which corresponds to the |
| 1891 | // entity being declared. |
| 1892 | return ParamLists[NumParamLists - 1]; |
| 1893 | } |
| 1894 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1895 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1896 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1897 | Diag(Template->getLocation(), diag::note_template_declared_here) |
| 1898 | << (isa<FunctionTemplateDecl>(Template)? 0 |
| 1899 | : isa<ClassTemplateDecl>(Template)? 1 |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1900 | : isa<TypeAliasTemplateDecl>(Template)? 2 |
| 1901 | : 3) |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1902 | << Template->getDeclName(); |
| 1903 | return; |
| 1904 | } |
| 1905 | |
| 1906 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1907 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1908 | IEnd = OST->end(); |
| 1909 | I != IEnd; ++I) |
| 1910 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1911 | << 0 << (*I)->getDeclName(); |
| 1912 | |
| 1913 | return; |
| 1914 | } |
| 1915 | } |
| 1916 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1917 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1918 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1919 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1920 | DependentTemplateName *DTN |
| 1921 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1922 | if (DTN && DTN->isIdentifier()) |
| 1923 | // When building a template-id where the template-name is dependent, |
| 1924 | // assume the template is a type template. Either our assumption is |
| 1925 | // correct, or the code is ill-formed and will be diagnosed when the |
| 1926 | // dependent name is substituted. |
| 1927 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 1928 | DTN->getQualifier(), |
| 1929 | DTN->getIdentifier(), |
| 1930 | TemplateArgs); |
| 1931 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1932 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1933 | if (!Template || isa<FunctionTemplateDecl>(Template)) { |
| 1934 | // We might have a substituted template template parameter pack. If so, |
| 1935 | // build a template specialization type for it. |
| 1936 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 1937 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1938 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1939 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 1940 | << Name; |
| 1941 | NoteAllFoundTemplates(Name); |
| 1942 | return QualType(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1943 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1944 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1945 | // Check that the template argument list is well-formed for this |
| 1946 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1947 | SmallVector<TemplateArgument, 4> Converted; |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1948 | bool ExpansionIntoFixedList = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1949 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1950 | false, Converted, &ExpansionIntoFixedList)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1951 | return QualType(); |
| 1952 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1953 | QualType CanonType; |
| 1954 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1955 | bool InstantiationDependent = false; |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1956 | TypeAliasTemplateDecl *AliasTemplate = 0; |
| 1957 | if (!ExpansionIntoFixedList && |
| 1958 | (AliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Template))) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1959 | // Find the canonical type for this type alias template specialization. |
| 1960 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 1961 | if (Pattern->isInvalidDecl()) |
| 1962 | return QualType(); |
| 1963 | |
| 1964 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1965 | Converted.data(), Converted.size()); |
| 1966 | |
| 1967 | // Only substitute for the innermost template argument list. |
| 1968 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 1804174 | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 1969 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | aff37b4 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 1970 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 1971 | for (unsigned I = 0; I < Depth; ++I) |
| 1972 | TemplateArgLists.addOuterTemplateArguments(0, 0); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1973 | |
| 1974 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
| 1975 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 1976 | TemplateArgLists, AliasTemplate->getLocation(), |
| 1977 | AliasTemplate->getDeclName()); |
| 1978 | if (CanonType.isNull()) |
| 1979 | return QualType(); |
| 1980 | } else if (Name.isDependent() || |
| 1981 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1982 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1983 | // This class template specialization is a dependent |
| 1984 | // type. Therefore, its canonical type is another class template |
| 1985 | // specialization type that contains all of the converted |
| 1986 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1987 | // A<T, T> have identical types when A is declared as: |
| 1988 | // |
| 1989 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1990 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1991 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1992 | Converted.data(), |
| 1993 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1994 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1995 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1996 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1997 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1998 | // build the canonical type and return that to us. |
| 1999 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2000 | |
| 2001 | // This might work out to be a current instantiation, in which |
| 2002 | // case the canonical type needs to be the InjectedClassNameType. |
| 2003 | // |
| 2004 | // TODO: in theory this could be a simple hashtable lookup; most |
| 2005 | // changes to CurContext don't change the set of current |
| 2006 | // instantiations. |
| 2007 | if (isa<ClassTemplateDecl>(Template)) { |
| 2008 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 2009 | // If we get out to a namespace, we're done. |
| 2010 | if (Ctx->isFileContext()) break; |
| 2011 | |
| 2012 | // If this isn't a record, keep looking. |
| 2013 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 2014 | if (!Record) continue; |
| 2015 | |
| 2016 | // Look for one of the two cases with InjectedClassNameTypes |
| 2017 | // and check whether it's the same template. |
| 2018 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 2019 | !Record->getDescribedClassTemplate()) |
| 2020 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2021 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2022 | // Fetch the injected class name type and check whether its |
| 2023 | // injected type is equal to the type we just built. |
| 2024 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2025 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2026 | ->getInjectedSpecializationType(); |
| 2027 | |
| 2028 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2029 | continue; |
| 2030 | |
| 2031 | // If so, the canonical type of this TST is the injected |
| 2032 | // class name type of the record we just found. |
| 2033 | assert(ICNT.isCanonical()); |
| 2034 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2035 | break; |
| 2036 | } |
| 2037 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2038 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2039 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2040 | // Find the class template specialization declaration that |
| 2041 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2042 | void *InsertPos = 0; |
| 2043 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2044 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2045 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2046 | if (!Decl) { |
| 2047 | // This is the first time we have referenced this class template |
| 2048 | // specialization. Create the canonical declaration and add it to |
| 2049 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2050 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2051 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2052 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 09d8212 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2053 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2054 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2055 | ClassTemplate, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2056 | Converted.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2057 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2058 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2059 | Decl->setLexicalDeclContext(CurContext); |
| 2060 | } |
| 2061 | |
| 2062 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2063 | assert(isa<RecordType>(CanonType) && |
| 2064 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2065 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2066 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2067 | // Build the fully-sugared type for this class template |
| 2068 | // specialization, which refers back to the class template |
| 2069 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2070 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2073 | TypeResult |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2074 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2075 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2076 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2077 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2078 | SourceLocation RAngleLoc, |
| 2079 | bool IsCtorOrDtorName) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2080 | if (SS.isInvalid()) |
| 2081 | return true; |
| 2082 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2083 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2084 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2085 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2086 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2087 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2088 | |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2089 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2090 | QualType T |
| 2091 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2092 | DTN->getQualifier(), |
| 2093 | DTN->getIdentifier(), |
| 2094 | TemplateArgs); |
| 2095 | // Build type-source information. |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2096 | TypeLocBuilder TLB; |
| 2097 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2098 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2099 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2100 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 66581d4 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2101 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2102 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2103 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2104 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2105 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2106 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2107 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2108 | } |
| 2109 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2110 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2111 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2112 | |
| 2113 | if (Result.isNull()) |
| 2114 | return true; |
| 2115 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2116 | // Build type-source information. |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2117 | TypeLocBuilder TLB; |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2118 | TemplateSpecializationTypeLoc SpecTL |
| 2119 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2120 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2121 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2122 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2123 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2124 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2125 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2126 | |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2127 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2128 | // constructor or destructor name (in such a case, the scope specifier |
| 2129 | // will be attached to the enclosing Decl or Expr node). |
| 2130 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2131 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2132 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2133 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2134 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2135 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2136 | } |
| 2137 | |
| 2138 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2139 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2140 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2141 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2142 | TypeSpecifierType TagSpec, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2143 | SourceLocation TagLoc, |
| 2144 | CXXScopeSpec &SS, |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2145 | SourceLocation TemplateKWLoc, |
| 2146 | TemplateTy TemplateD, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2147 | SourceLocation TemplateLoc, |
| 2148 | SourceLocation LAngleLoc, |
| 2149 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2150 | SourceLocation RAngleLoc) { |
| 2151 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
| 2152 | |
| 2153 | // Translate the parser's template argument list in our AST format. |
| 2154 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2155 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2156 | |
| 2157 | // Determine the tag kind |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2158 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2159 | ElaboratedTypeKeyword Keyword |
| 2160 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2161 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2162 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2163 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2164 | DTN->getQualifier(), |
| 2165 | DTN->getIdentifier(), |
| 2166 | TemplateArgs); |
| 2167 | |
| 2168 | // Build type-source information. |
| 2169 | TypeLocBuilder TLB; |
| 2170 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2171 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2172 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2173 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 66581d4 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2174 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2175 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2176 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2177 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2178 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2179 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2180 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2181 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2182 | |
| 2183 | if (TypeAliasTemplateDecl *TAT = |
| 2184 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2185 | // C++0x [dcl.type.elab]p2: |
| 2186 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2187 | // resolves to an alias template specialization, the |
| 2188 | // elaborated-type-specifier is ill-formed. |
| 2189 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2190 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2191 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2192 | |
| 2193 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2194 | if (Result.isNull()) |
Matt Beaumont-Gay | 3a51d41 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2195 | return TypeResult(true); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2196 | |
| 2197 | // Check the tag kind |
| 2198 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2199 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2200 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2201 | IdentifierInfo *Id = D->getIdentifier(); |
| 2202 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2203 | |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2204 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 2205 | TagLoc, *Id)) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2206 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2207 | << Result |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2208 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2209 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2210 | } |
| 2211 | } |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2212 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2213 | // Provide source-location information for the template specialization. |
| 2214 | TypeLocBuilder TLB; |
| 2215 | TemplateSpecializationTypeLoc SpecTL |
| 2216 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2217 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2218 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2219 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2220 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2221 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2222 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2223 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2224 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2225 | // and tag keyword. |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2226 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2227 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2228 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2229 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2230 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2233 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2234 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2235 | LookupResult &R, |
| 2236 | bool RequiresADL, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2237 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2238 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2239 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2240 | // 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] | 2241 | // though. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2242 | // foo<int> could identify a single function unambiguously |
| 2243 | // This approach does NOT work, since f<int>(1); |
| 2244 | // gets resolved prior to resorting to overload resolution |
| 2245 | // i.e., template<class T> void f(double); |
| 2246 | // vs template<class T, class U> void f(U); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2247 | |
| 2248 | // These should be filtered out by our callers. |
| 2249 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2250 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2251 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2252 | // We don't want lookup warnings at this point. |
| 2253 | R.suppressDiagnostics(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2254 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2255 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2256 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2257 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2258 | TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2259 | R.getLookupNameInfo(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2260 | RequiresADL, TemplateArgs, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2261 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2262 | |
| 2263 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2266 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2267 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2268 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2269 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2270 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2271 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2272 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2273 | DeclContext *DC; |
| 2274 | if (!(DC = computeDeclContext(SS, false)) || |
| 2275 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2276 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2277 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2278 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2279 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2280 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2281 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 2282 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2283 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2284 | if (R.isAmbiguous()) |
| 2285 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2286 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2287 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2288 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2289 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2290 | return ExprError(); |
| 2291 | } |
| 2292 | |
| 2293 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2294 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 2295 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 2296 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2297 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2298 | return ExprError(); |
| 2299 | } |
| 2300 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2301 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2304 | /// \brief Form a dependent template name. |
| 2305 | /// |
| 2306 | /// This action forms a dependent template name given the template |
| 2307 | /// name and its (presumably dependent) scope specifier. For |
| 2308 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2309 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2310 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2311 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2312 | CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2313 | SourceLocation TemplateKWLoc, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2314 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2315 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2316 | bool EnteringContext, |
| 2317 | TemplateTy &Result) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2318 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 2319 | Diag(TemplateKWLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2320 | getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2321 | diag::warn_cxx98_compat_template_outside_of_template : |
| 2322 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2323 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2324 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2325 | DeclContext *LookupCtx = 0; |
| 2326 | if (SS.isSet()) |
| 2327 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2328 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2329 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2330 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2331 | // C++0x [temp.names]p5: |
| 2332 | // If a name prefixed by the keyword template is not the name of |
| 2333 | // a template, the program is ill-formed. [Note: the keyword |
| 2334 | // template may not be applied to non-template members of class |
| 2335 | // templates. -end note ] [ Note: as is the case with the |
| 2336 | // typename prefix, the template prefix is allowed in cases |
| 2337 | // where it is not strictly necessary; i.e., when the |
| 2338 | // nested-name-specifier or the expression on the left of the -> |
| 2339 | // or . is not dependent on a template-parameter, or the use |
| 2340 | // does not appear in the scope of a template. -end note] |
| 2341 | // |
| 2342 | // Note: C++03 was more strict here, because it banned the use of |
| 2343 | // the "template" keyword prior to a template-name that was not a |
| 2344 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2345 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2346 | // 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] | 2347 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2348 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 2349 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2350 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2351 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2352 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | d078bd2 | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2353 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2354 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2355 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2356 | } else if (TNK == TNK_Non_template) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2357 | Diag(Name.getLocStart(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2358 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2359 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2360 | << Name.getSourceRange() |
| 2361 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2362 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2363 | } else { |
| 2364 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2365 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2366 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2369 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2370 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2371 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2372 | switch (Name.getKind()) { |
| 2373 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2374 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2375 | Name.Identifier)); |
| 2376 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2377 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2378 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2379 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2380 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2381 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2382 | |
| 2383 | case UnqualifiedId::IK_LiteralOperatorId: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2384 | llvm_unreachable( |
| 2385 | "We don't support these; Parse shouldn't have allowed propagation"); |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2386 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2387 | default: |
| 2388 | break; |
| 2389 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2390 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2391 | Diag(Name.getLocStart(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2392 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2393 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2394 | << Name.getSourceRange() |
| 2395 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2396 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2399 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2400 | const TemplateArgumentLoc &AL, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2401 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2402 | const TemplateArgument &Arg = AL.getArgument(); |
| 2403 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2404 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2405 | switch(Arg.getKind()) { |
| 2406 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2407 | // C++ [temp.arg.type]p1: |
| 2408 | // A template-argument for a template-parameter which is a |
| 2409 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2410 | break; |
| 2411 | case TemplateArgument::Template: { |
| 2412 | // We have a template type parameter but the template argument |
| 2413 | // is a template without any arguments. |
| 2414 | SourceRange SR = AL.getSourceRange(); |
| 2415 | TemplateName Name = Arg.getAsTemplate(); |
| 2416 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 2417 | << Name << SR; |
| 2418 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 2419 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2420 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2421 | return true; |
| 2422 | } |
| 2423 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2424 | // We have a template type parameter but the template argument |
| 2425 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 2426 | SourceRange SR = AL.getSourceRange(); |
| 2427 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2428 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2429 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2430 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2431 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2432 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2433 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2434 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2435 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2436 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2437 | // Add the converted template type argument. |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 2438 | QualType ArgType = Context.getCanonicalType(Arg.getAsType()); |
| 2439 | |
| 2440 | // Objective-C ARC: |
| 2441 | // If an explicitly-specified template argument type is a lifetime type |
| 2442 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2443 | if (getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 2444 | ArgType->isObjCLifetimeType() && |
| 2445 | !ArgType.getObjCLifetime()) { |
| 2446 | Qualifiers Qs; |
| 2447 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 2448 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 2449 | } |
| 2450 | |
| 2451 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2452 | return false; |
| 2453 | } |
| 2454 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2455 | /// \brief Substitute template arguments into the default template argument for |
| 2456 | /// the given template type parameter. |
| 2457 | /// |
| 2458 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2459 | /// the substitution. |
| 2460 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2461 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2462 | /// for. |
| 2463 | /// |
| 2464 | /// \param TemplateLoc the location of the template name that started the |
| 2465 | /// template-id we are checking. |
| 2466 | /// |
| 2467 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2468 | /// terminates the template-id. |
| 2469 | /// |
| 2470 | /// \param Param the template template parameter whose default we are |
| 2471 | /// substituting into. |
| 2472 | /// |
| 2473 | /// \param Converted the list of template arguments provided for template |
| 2474 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2475 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2476 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2477 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2478 | TemplateDecl *Template, |
| 2479 | SourceLocation TemplateLoc, |
| 2480 | SourceLocation RAngleLoc, |
| 2481 | TemplateTypeParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2482 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2483 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2484 | |
| 2485 | // If the argument type is dependent, instantiate it now based |
| 2486 | // on the previously-computed template arguments. |
| 2487 | if (ArgType->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2488 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2489 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2490 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2491 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2492 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2493 | |
| 2494 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2495 | Template, Converted.data(), |
| 2496 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2497 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2498 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2499 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2500 | Param->getDefaultArgumentLoc(), |
| 2501 | Param->getDeclName()); |
| 2502 | } |
| 2503 | |
| 2504 | return ArgType; |
| 2505 | } |
| 2506 | |
| 2507 | /// \brief Substitute template arguments into the default template argument for |
| 2508 | /// the given non-type template parameter. |
| 2509 | /// |
| 2510 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2511 | /// the substitution. |
| 2512 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2513 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2514 | /// for. |
| 2515 | /// |
| 2516 | /// \param TemplateLoc the location of the template name that started the |
| 2517 | /// template-id we are checking. |
| 2518 | /// |
| 2519 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2520 | /// terminates the template-id. |
| 2521 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2522 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2523 | /// substituting into. |
| 2524 | /// |
| 2525 | /// \param Converted the list of template arguments provided for template |
| 2526 | /// parameters that precede \p Param in the template parameter list. |
| 2527 | /// |
| 2528 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2529 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2530 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2531 | TemplateDecl *Template, |
| 2532 | SourceLocation TemplateLoc, |
| 2533 | SourceLocation RAngleLoc, |
| 2534 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2535 | SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2536 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2537 | Converted.data(), Converted.size()); |
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 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2540 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2541 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2542 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2543 | Template, Converted.data(), |
| 2544 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2545 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2546 | |
| 2547 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2548 | } |
| 2549 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2550 | /// \brief Substitute template arguments into the default template argument for |
| 2551 | /// the given template template parameter. |
| 2552 | /// |
| 2553 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2554 | /// the substitution. |
| 2555 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2556 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2557 | /// for. |
| 2558 | /// |
| 2559 | /// \param TemplateLoc the location of the template name that started the |
| 2560 | /// template-id we are checking. |
| 2561 | /// |
| 2562 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2563 | /// terminates the template-id. |
| 2564 | /// |
| 2565 | /// \param Param the template template parameter whose default we are |
| 2566 | /// substituting into. |
| 2567 | /// |
| 2568 | /// \param Converted the list of template arguments provided for template |
| 2569 | /// parameters that precede \p Param in the template parameter list. |
| 2570 | /// |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2571 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 2572 | /// source-location information) that precedes the template name. |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2573 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2574 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2575 | static TemplateName |
| 2576 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2577 | TemplateDecl *Template, |
| 2578 | SourceLocation TemplateLoc, |
| 2579 | SourceLocation RAngleLoc, |
| 2580 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2581 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2582 | NestedNameSpecifierLoc &QualifierLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2583 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2584 | Converted.data(), Converted.size()); |
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 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2587 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2588 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2589 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2590 | Template, Converted.data(), |
| 2591 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2592 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2593 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2594 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2595 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2596 | if (QualifierLoc) { |
| 2597 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2598 | AllTemplateArgs); |
| 2599 | if (!QualifierLoc) |
| 2600 | return TemplateName(); |
| 2601 | } |
| 2602 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2603 | return SemaRef.SubstTemplateName(QualifierLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2604 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2605 | Param->getDefaultArgument().getTemplateNameLoc(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2606 | AllTemplateArgs); |
| 2607 | } |
| 2608 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2609 | /// \brief If the given template parameter has a default template |
| 2610 | /// argument, substitute into that default template argument and |
| 2611 | /// return the corresponding template argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2612 | TemplateArgumentLoc |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2613 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2614 | SourceLocation TemplateLoc, |
| 2615 | SourceLocation RAngleLoc, |
| 2616 | Decl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2617 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2618 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2619 | if (!TypeParm->hasDefaultArgument()) |
| 2620 | return TemplateArgumentLoc(); |
| 2621 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2622 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2623 | TemplateLoc, |
| 2624 | RAngleLoc, |
| 2625 | TypeParm, |
| 2626 | Converted); |
| 2627 | if (DI) |
| 2628 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2629 | |
| 2630 | return TemplateArgumentLoc(); |
| 2631 | } |
| 2632 | |
| 2633 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2634 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2635 | if (!NonTypeParm->hasDefaultArgument()) |
| 2636 | return TemplateArgumentLoc(); |
| 2637 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2638 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2639 | TemplateLoc, |
| 2640 | RAngleLoc, |
| 2641 | NonTypeParm, |
| 2642 | Converted); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2643 | if (Arg.isInvalid()) |
| 2644 | return TemplateArgumentLoc(); |
| 2645 | |
| 2646 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2647 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2648 | } |
| 2649 | |
| 2650 | TemplateTemplateParmDecl *TempTempParm |
| 2651 | = cast<TemplateTemplateParmDecl>(Param); |
| 2652 | if (!TempTempParm->hasDefaultArgument()) |
| 2653 | return TemplateArgumentLoc(); |
| 2654 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2655 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2656 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2657 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2658 | TemplateLoc, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2659 | RAngleLoc, |
| 2660 | TempTempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2661 | Converted, |
| 2662 | QualifierLoc); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2663 | if (TName.isNull()) |
| 2664 | return TemplateArgumentLoc(); |
| 2665 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2666 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2667 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2668 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2669 | } |
| 2670 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2671 | /// \brief Check that the given template argument corresponds to the given |
| 2672 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2673 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2674 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2675 | /// checked. |
| 2676 | /// |
| 2677 | /// \param Arg The template argument. |
| 2678 | /// |
| 2679 | /// \param Template The template in which the template argument resides. |
| 2680 | /// |
| 2681 | /// \param TemplateLoc The location of the template name for the template |
| 2682 | /// whose argument list we're matching. |
| 2683 | /// |
| 2684 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2685 | /// the template argument list. |
| 2686 | /// |
| 2687 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2688 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2689 | /// |
| 2690 | /// \param Converted The checked, converted argument will be added to the |
| 2691 | /// end of this small vector. |
| 2692 | /// |
| 2693 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2694 | /// explicitly written, deduced, etc. |
| 2695 | /// |
| 2696 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2697 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2698 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2699 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2700 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2701 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2702 | unsigned ArgumentPackIndex, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2703 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2704 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2705 | // Check template type parameters. |
| 2706 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2707 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2708 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2709 | // Check non-type template parameters. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2710 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2711 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2712 | // with the template arguments we've seen thus far. But if the |
| 2713 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2714 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2715 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2716 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2717 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2718 | if (NTTPType->isDependentType() && |
| 2719 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2720 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2721 | // Do substitution on the type of the non-type template parameter. |
| 2722 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2723 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2724 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2725 | |
| 2726 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2727 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2728 | NTTPType = SubstType(NTTPType, |
| 2729 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2730 | NTTP->getLocation(), |
| 2731 | NTTP->getDeclName()); |
| 2732 | // If that worked, check the non-type template parameter type |
| 2733 | // for validity. |
| 2734 | if (!NTTPType.isNull()) |
| 2735 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2736 | NTTP->getLocation()); |
| 2737 | if (NTTPType.isNull()) |
| 2738 | return true; |
| 2739 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2740 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2741 | switch (Arg.getArgument().getKind()) { |
| 2742 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2743 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2744 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2745 | case TemplateArgument::Expression: { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2746 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2747 | ExprResult Res = |
| 2748 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 2749 | Result, CTAK); |
| 2750 | if (Res.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2751 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2752 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2753 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2754 | break; |
| 2755 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2756 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2757 | case TemplateArgument::Declaration: |
| 2758 | case TemplateArgument::Integral: |
| 2759 | // We've already checked this template argument, so just copy |
| 2760 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2761 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2762 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2763 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2764 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2765 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2766 | // We were given a template template argument. It may not be ill-formed; |
| 2767 | // see below. |
| 2768 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2769 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2770 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2771 | // We have a template argument such as \c T::template X, which we |
| 2772 | // parsed as a template template argument. However, since we now |
| 2773 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2774 | // template name into an expression. |
| 2775 | |
| 2776 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2777 | Arg.getTemplateNameLoc()); |
| 2778 | |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2779 | CXXScopeSpec SS; |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2780 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2781 | // FIXME: the template-template arg was a DependentTemplateName, |
| 2782 | // so it was provided with a template keyword. However, its source |
| 2783 | // location is not stored in the template argument structure. |
| 2784 | SourceLocation TemplateKWLoc; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2785 | ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2786 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2787 | TemplateKWLoc, |
| 2788 | NameInfo, 0)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2789 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2790 | // If we parsed the template argument as a pack expansion, create a |
| 2791 | // pack expansion expression. |
| 2792 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2793 | E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc()); |
| 2794 | if (E.isInvalid()) |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2795 | return true; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2796 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2797 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2798 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2799 | E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result); |
| 2800 | if (E.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2801 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2802 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2803 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2804 | break; |
| 2805 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2806 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2807 | // We have a template argument that actually does refer to a class |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2808 | // template, alias template, or template template parameter, and |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2809 | // therefore cannot be a non-type template argument. |
| 2810 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2811 | << Arg.getSourceRange(); |
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 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2814 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2815 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2816 | case TemplateArgument::Type: { |
| 2817 | // We have a non-type template parameter but the template |
| 2818 | // argument is a type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2819 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2820 | // C++ [temp.arg]p2: |
| 2821 | // In a template-argument, an ambiguity between a type-id and |
| 2822 | // an expression is resolved to a type-id, regardless of the |
| 2823 | // form of the corresponding template-parameter. |
| 2824 | // |
| 2825 | // We warn specifically about this case, since it can be rather |
| 2826 | // confusing for users. |
| 2827 | QualType T = Arg.getArgument().getAsType(); |
| 2828 | SourceRange SR = Arg.getSourceRange(); |
| 2829 | if (T->isFunctionType()) |
| 2830 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2831 | else |
| 2832 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2833 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2834 | return true; |
| 2835 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2836 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2837 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2838 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2839 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2840 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2841 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
| 2844 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2845 | // Check template template parameters. |
| 2846 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2847 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2848 | // Substitute into the template parameter list of the template |
| 2849 | // template parameter, since previously-supplied template arguments |
| 2850 | // may appear within the template template parameter. |
| 2851 | { |
| 2852 | // Set up a template instantiation context. |
| 2853 | LocalInstantiationScope Scope(*this); |
| 2854 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2855 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2856 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2857 | |
| 2858 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2859 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2860 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2861 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2862 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2863 | if (!TempParm) |
| 2864 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2865 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2866 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2867 | switch (Arg.getArgument().getKind()) { |
| 2868 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2869 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2870 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2871 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2872 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2873 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2874 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2875 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2876 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2877 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2878 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2879 | case TemplateArgument::Expression: |
| 2880 | case TemplateArgument::Type: |
| 2881 | // We have a template template parameter but the template |
| 2882 | // argument does not refer to a template. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2883 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2884 | << getLangOpts().CPlusPlus0x; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2885 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2886 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2887 | case TemplateArgument::Declaration: |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2888 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2889 | case TemplateArgument::Integral: |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2890 | llvm_unreachable("Integral argument with template template parameter"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2891 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2892 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2893 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2894 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2895 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2896 | return false; |
| 2897 | } |
| 2898 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2899 | /// \brief Diagnose an arity mismatch in the |
| 2900 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 2901 | SourceLocation TemplateLoc, |
| 2902 | TemplateArgumentListInfo &TemplateArgs) { |
| 2903 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2904 | unsigned NumParams = Params->size(); |
| 2905 | unsigned NumArgs = TemplateArgs.size(); |
| 2906 | |
| 2907 | SourceRange Range; |
| 2908 | if (NumArgs > NumParams) |
| 2909 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
| 2910 | TemplateArgs.getRAngleLoc()); |
| 2911 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2912 | << (NumArgs > NumParams) |
| 2913 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2914 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2915 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2916 | << Template << Range; |
| 2917 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2918 | << Params->getSourceRange(); |
| 2919 | return true; |
| 2920 | } |
| 2921 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2922 | /// \brief Check that the given template argument list is well-formed |
| 2923 | /// for specializing the given template. |
| 2924 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2925 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2926 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2927 | bool PartialTemplateArgs, |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2928 | SmallVectorImpl<TemplateArgument> &Converted, |
| 2929 | bool *ExpansionIntoFixedList) { |
| 2930 | if (ExpansionIntoFixedList) |
| 2931 | *ExpansionIntoFixedList = false; |
| 2932 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2933 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2934 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2935 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2936 | bool Invalid = false; |
| 2937 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2938 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2939 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2940 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2941 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2942 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2943 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2944 | // [...] The type and form of each template-argument specified in |
| 2945 | // a template-id shall match the type and form specified for the |
| 2946 | // corresponding parameter declared by the template in its |
| 2947 | // template-parameter-list. |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2948 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2949 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2950 | TemplateParameterList::iterator Param = Params->begin(), |
| 2951 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2952 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 2953 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2954 | bool SawPackExpansion = false; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2955 | while (Param != ParamEnd) { |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2956 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2957 | // If we have an expanded parameter pack, make sure we don't have too |
| 2958 | // many arguments. |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2959 | // FIXME: This really should fall out from the normal arity checking. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2960 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2961 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2962 | if (NTTP->isExpandedParameterPack() && |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2963 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 2964 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2965 | << true |
| 2966 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2967 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2968 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2969 | << Template; |
| 2970 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2971 | << Params->getSourceRange(); |
| 2972 | return true; |
| 2973 | } |
| 2974 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2975 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2976 | // Check the template argument we were given. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2977 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2978 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2979 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2980 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2981 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2982 | if ((*Param)->isTemplateParameterPack()) { |
| 2983 | // The template parameter was a template parameter pack, so take the |
| 2984 | // deduced argument and place it on the argument pack. Note that we |
| 2985 | // stay on the same template parameter so that we can deduce more |
| 2986 | // arguments. |
| 2987 | ArgumentPack.push_back(Converted.back()); |
| 2988 | Converted.pop_back(); |
| 2989 | } else { |
| 2990 | // Move to the next template parameter. |
| 2991 | ++Param; |
| 2992 | } |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2993 | |
| 2994 | // If this template argument is a pack expansion, record that fact |
| 2995 | // and break out; we can't actually check any more. |
| 2996 | if (TemplateArgs[ArgIdx].getArgument().isPackExpansion()) { |
| 2997 | SawPackExpansion = true; |
| 2998 | ++ArgIdx; |
| 2999 | break; |
| 3000 | } |
| 3001 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3002 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3003 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3004 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3005 | |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3006 | // If we're checking a partial template argument list, we're done. |
| 3007 | if (PartialTemplateArgs) { |
| 3008 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 3009 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3010 | ArgumentPack.data(), |
| 3011 | ArgumentPack.size())); |
| 3012 | |
| 3013 | return Invalid; |
| 3014 | } |
| 3015 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3016 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3017 | // arguments, just break out now and we'll fill in the argument pack below. |
| 3018 | if ((*Param)->isTemplateParameterPack()) |
| 3019 | break; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 3020 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3021 | // Check whether we have a default argument. |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3022 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3023 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3024 | // Retrieve the default template argument from the template |
| 3025 | // parameter. For each kind of template parameter, we substitute the |
| 3026 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3027 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3028 | // the default argument. |
| 3029 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3030 | if (!TTP->hasDefaultArgument()) |
| 3031 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3032 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3033 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3034 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3035 | Template, |
| 3036 | TemplateLoc, |
| 3037 | RAngleLoc, |
| 3038 | TTP, |
| 3039 | Converted); |
| 3040 | if (!ArgType) |
| 3041 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3042 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3043 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 3044 | ArgType); |
| 3045 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3046 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3047 | if (!NTTP->hasDefaultArgument()) |
| 3048 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3049 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3050 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3051 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3052 | TemplateLoc, |
| 3053 | RAngleLoc, |
| 3054 | NTTP, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3055 | Converted); |
| 3056 | if (E.isInvalid()) |
| 3057 | return true; |
| 3058 | |
| 3059 | Expr *Ex = E.takeAs<Expr>(); |
| 3060 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 3061 | } else { |
| 3062 | TemplateTemplateParmDecl *TempParm |
| 3063 | = cast<TemplateTemplateParmDecl>(*Param); |
| 3064 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3065 | if (!TempParm->hasDefaultArgument()) |
| 3066 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3067 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3068 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3069 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3070 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3071 | TemplateLoc, |
| 3072 | RAngleLoc, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3073 | TempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3074 | Converted, |
| 3075 | QualifierLoc); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3076 | if (Name.isNull()) |
| 3077 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3078 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3079 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3080 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3081 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3082 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3083 | // Introduce an instantiation record that describes where we are using |
| 3084 | // the default template argument. |
| 3085 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3086 | Converted.data(), Converted.size(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3087 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3088 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3089 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3090 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3091 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3092 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3093 | |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3094 | // Core issue 150 (assumed resolution): if this is a template template |
| 3095 | // parameter, keep track of the default template arguments from the |
| 3096 | // template definition. |
| 3097 | if (isTemplateTemplateParameter) |
| 3098 | TemplateArgs.addArgument(Arg); |
| 3099 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3100 | // Move to the next template parameter and argument. |
| 3101 | ++Param; |
| 3102 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3103 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3104 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3105 | // If we saw a pack expansion, then directly convert the remaining arguments, |
| 3106 | // because we don't know what parameters they'll match up with. |
| 3107 | if (SawPackExpansion) { |
| 3108 | bool AddToArgumentPack |
| 3109 | = Param != ParamEnd && (*Param)->isTemplateParameterPack(); |
| 3110 | while (ArgIdx < NumArgs) { |
| 3111 | if (AddToArgumentPack) |
| 3112 | ArgumentPack.push_back(TemplateArgs[ArgIdx].getArgument()); |
| 3113 | else |
| 3114 | Converted.push_back(TemplateArgs[ArgIdx].getArgument()); |
| 3115 | ++ArgIdx; |
| 3116 | } |
| 3117 | |
| 3118 | // Push the argument pack onto the list of converted arguments. |
| 3119 | if (AddToArgumentPack) { |
| 3120 | if (ArgumentPack.empty()) |
| 3121 | Converted.push_back(TemplateArgument(0, 0)); |
| 3122 | else { |
| 3123 | Converted.push_back( |
| 3124 | TemplateArgument::CreatePackCopy(Context, |
| 3125 | ArgumentPack.data(), |
| 3126 | ArgumentPack.size())); |
| 3127 | ArgumentPack.clear(); |
| 3128 | } |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3129 | } else if (ExpansionIntoFixedList) { |
| 3130 | // We have expanded a pack into a fixed list. |
| 3131 | *ExpansionIntoFixedList = true; |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
| 3134 | return Invalid; |
| 3135 | } |
| 3136 | |
| 3137 | // If we have any leftover arguments, then there were too many arguments. |
| 3138 | // Complain and fail. |
| 3139 | if (ArgIdx < NumArgs) |
| 3140 | return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs); |
| 3141 | |
| 3142 | // If we have an expanded parameter pack, make sure we don't have too |
| 3143 | // many arguments. |
| 3144 | // FIXME: This really should fall out from the normal arity checking. |
| 3145 | if (Param != ParamEnd) { |
| 3146 | if (NonTypeTemplateParmDecl *NTTP |
| 3147 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 3148 | if (NTTP->isExpandedParameterPack() && |
| 3149 | ArgumentPack.size() < NTTP->getNumExpansionTypes()) { |
| 3150 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3151 | << false |
| 3152 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3153 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3154 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3155 | << Template; |
| 3156 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3157 | << Params->getSourceRange(); |
| 3158 | return true; |
| 3159 | } |
| 3160 | } |
| 3161 | } |
| 3162 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3163 | // Form argument packs for each of the parameter packs remaining. |
| 3164 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3165 | // If we're checking a partial list of template arguments, don't fill |
| 3166 | // in arguments for non-template parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3167 | if ((*Param)->isTemplateParameterPack()) { |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 3168 | if (!HasParameterPack) |
| 3169 | return true; |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3170 | if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3171 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3172 | else { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3173 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3174 | ArgumentPack.data(), |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3175 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3176 | ArgumentPack.clear(); |
| 3177 | } |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3178 | } else if (!PartialTemplateArgs) |
| 3179 | return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3180 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3181 | ++Param; |
| 3182 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3183 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3184 | return Invalid; |
| 3185 | } |
| 3186 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3187 | namespace { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3188 | class UnnamedLocalNoLinkageFinder |
| 3189 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3190 | { |
| 3191 | Sema &S; |
| 3192 | SourceRange SR; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3193 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3194 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3195 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3196 | public: |
| 3197 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3198 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3199 | bool Visit(QualType T) { |
| 3200 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3201 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3202 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3203 | #define TYPE(Class, Parent) \ |
| 3204 | bool Visit##Class##Type(const Class##Type *); |
| 3205 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 3206 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3207 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 3208 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3209 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3210 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3211 | bool VisitTagDecl(const TagDecl *Tag); |
| 3212 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3213 | }; |
| 3214 | } |
| 3215 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3216 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3217 | return false; |
| 3218 | } |
| 3219 | |
| 3220 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3221 | return Visit(T->getElementType()); |
| 3222 | } |
| 3223 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3224 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3225 | return Visit(T->getPointeeType()); |
| 3226 | } |
| 3227 | |
| 3228 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3229 | const BlockPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3230 | return Visit(T->getPointeeType()); |
| 3231 | } |
| 3232 | |
| 3233 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3234 | const LValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3235 | return Visit(T->getPointeeType()); |
| 3236 | } |
| 3237 | |
| 3238 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3239 | const RValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3240 | return Visit(T->getPointeeType()); |
| 3241 | } |
| 3242 | |
| 3243 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3244 | const MemberPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3245 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3246 | } |
| 3247 | |
| 3248 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3249 | const ConstantArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3250 | return Visit(T->getElementType()); |
| 3251 | } |
| 3252 | |
| 3253 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3254 | const IncompleteArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3255 | return Visit(T->getElementType()); |
| 3256 | } |
| 3257 | |
| 3258 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3259 | const VariableArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3260 | return Visit(T->getElementType()); |
| 3261 | } |
| 3262 | |
| 3263 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3264 | const DependentSizedArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3265 | return Visit(T->getElementType()); |
| 3266 | } |
| 3267 | |
| 3268 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3269 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3270 | return Visit(T->getElementType()); |
| 3271 | } |
| 3272 | |
| 3273 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3274 | return Visit(T->getElementType()); |
| 3275 | } |
| 3276 | |
| 3277 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 3278 | return Visit(T->getElementType()); |
| 3279 | } |
| 3280 | |
| 3281 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 3282 | const FunctionProtoType* T) { |
| 3283 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3284 | AEnd = T->arg_type_end(); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3285 | A != AEnd; ++A) { |
| 3286 | if (Visit(*A)) |
| 3287 | return true; |
| 3288 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3289 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3290 | return Visit(T->getResultType()); |
| 3291 | } |
| 3292 | |
| 3293 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 3294 | const FunctionNoProtoType* T) { |
| 3295 | return Visit(T->getResultType()); |
| 3296 | } |
| 3297 | |
| 3298 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 3299 | const UnresolvedUsingType*) { |
| 3300 | return false; |
| 3301 | } |
| 3302 | |
| 3303 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 3304 | return false; |
| 3305 | } |
| 3306 | |
| 3307 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 3308 | return Visit(T->getUnderlyingType()); |
| 3309 | } |
| 3310 | |
| 3311 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 3312 | return false; |
| 3313 | } |
| 3314 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3315 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 3316 | const UnaryTransformType*) { |
| 3317 | return false; |
| 3318 | } |
| 3319 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3320 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 3321 | return Visit(T->getDeducedType()); |
| 3322 | } |
| 3323 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3324 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 3325 | return VisitTagDecl(T->getDecl()); |
| 3326 | } |
| 3327 | |
| 3328 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 3329 | return VisitTagDecl(T->getDecl()); |
| 3330 | } |
| 3331 | |
| 3332 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 3333 | const TemplateTypeParmType*) { |
| 3334 | return false; |
| 3335 | } |
| 3336 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3337 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 3338 | const SubstTemplateTypeParmPackType *) { |
| 3339 | return false; |
| 3340 | } |
| 3341 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3342 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 3343 | const TemplateSpecializationType*) { |
| 3344 | return false; |
| 3345 | } |
| 3346 | |
| 3347 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 3348 | const InjectedClassNameType* T) { |
| 3349 | return VisitTagDecl(T->getDecl()); |
| 3350 | } |
| 3351 | |
| 3352 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 3353 | const DependentNameType* T) { |
| 3354 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3355 | } |
| 3356 | |
| 3357 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 3358 | const DependentTemplateSpecializationType* T) { |
| 3359 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3360 | } |
| 3361 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3362 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 3363 | const PackExpansionType* T) { |
| 3364 | return Visit(T->getPattern()); |
| 3365 | } |
| 3366 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3367 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 3368 | return false; |
| 3369 | } |
| 3370 | |
| 3371 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 3372 | const ObjCInterfaceType *) { |
| 3373 | return false; |
| 3374 | } |
| 3375 | |
| 3376 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 3377 | const ObjCObjectPointerType *) { |
| 3378 | return false; |
| 3379 | } |
| 3380 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3381 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 3382 | return Visit(T->getValueType()); |
| 3383 | } |
| 3384 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3385 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 3386 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3387 | S.Diag(SR.getBegin(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3388 | S.getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3389 | diag::warn_cxx98_compat_template_arg_local_type : |
| 3390 | diag::ext_template_arg_local_type) |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3391 | << S.Context.getTypeDeclType(Tag) << SR; |
| 3392 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3393 | } |
| 3394 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3395 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3396 | S.Diag(SR.getBegin(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3397 | S.getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3398 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 3399 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3400 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 3401 | return true; |
| 3402 | } |
| 3403 | |
| 3404 | return false; |
| 3405 | } |
| 3406 | |
| 3407 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 3408 | NestedNameSpecifier *NNS) { |
| 3409 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 3410 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3411 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3412 | switch (NNS->getKind()) { |
| 3413 | case NestedNameSpecifier::Identifier: |
| 3414 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3415 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3416 | case NestedNameSpecifier::Global: |
| 3417 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3418 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3419 | case NestedNameSpecifier::TypeSpec: |
| 3420 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3421 | return Visit(QualType(NNS->getAsType(), 0)); |
| 3422 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3423 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
| 3426 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3427 | /// \brief Check a template argument against its corresponding |
| 3428 | /// template type parameter. |
| 3429 | /// |
| 3430 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 3431 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3432 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3433 | TypeSourceInfo *ArgInfo) { |
| 3434 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3435 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 3436 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 3437 | |
| 3438 | if (Arg->isVariablyModifiedType()) { |
| 3439 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3440 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3441 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3442 | } |
| 3443 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3444 | // C++03 [temp.arg.type]p2: |
| 3445 | // A local type, a type with no linkage, an unnamed type or a type |
| 3446 | // compounded from any of these types shall not be used as a |
| 3447 | // template-argument for a template type-parameter. |
| 3448 | // |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3449 | // 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] | 3450 | // a warning. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3451 | if (LangOpts.CPlusPlus0x ? |
| 3452 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 3453 | SR.getBegin()) != DiagnosticsEngine::Ignored || |
| 3454 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type, |
| 3455 | SR.getBegin()) != DiagnosticsEngine::Ignored : |
| 3456 | Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3457 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 3458 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 3459 | } |
| 3460 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3461 | return false; |
| 3462 | } |
| 3463 | |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3464 | enum NullPointerValueKind { |
| 3465 | NPV_NotNullPointer, |
| 3466 | NPV_NullPointer, |
| 3467 | NPV_Error |
| 3468 | }; |
| 3469 | |
| 3470 | /// \brief Determine whether the given template argument is a null pointer |
| 3471 | /// value of the appropriate type. |
| 3472 | static NullPointerValueKind |
| 3473 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 3474 | QualType ParamType, Expr *Arg) { |
| 3475 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 3476 | return NPV_NotNullPointer; |
| 3477 | |
| 3478 | if (!S.getLangOpts().CPlusPlus0x) |
| 3479 | return NPV_NotNullPointer; |
| 3480 | |
| 3481 | // Determine whether we have a constant expression. |
Douglas Gregor | 50fadd1 | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 3482 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 3483 | if (ArgRV.isInvalid()) |
| 3484 | return NPV_Error; |
| 3485 | Arg = ArgRV.take(); |
| 3486 | |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3487 | Expr::EvalResult EvalResult; |
Douglas Gregor | 50fadd1 | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 3488 | llvm::SmallVector<PartialDiagnosticAt, 8> Notes; |
| 3489 | EvalResult.Diag = &Notes; |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3490 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
Douglas Gregor | 50fadd1 | 2012-04-10 19:03:30 +0000 | [diff] [blame] | 3491 | EvalResult.HasSideEffects) { |
| 3492 | SourceLocation DiagLoc = Arg->getExprLoc(); |
| 3493 | |
| 3494 | // If our only note is the usual "invalid subexpression" note, just point |
| 3495 | // the caret at its location rather than producing an essentially |
| 3496 | // redundant note. |
| 3497 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 3498 | diag::note_invalid_subexpr_in_const_expr) { |
| 3499 | DiagLoc = Notes[0].first; |
| 3500 | Notes.clear(); |
| 3501 | } |
| 3502 | |
| 3503 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 3504 | << Arg->getType() << Arg->getSourceRange(); |
| 3505 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 3506 | S.Diag(Notes[I].first, Notes[I].second); |
| 3507 | |
| 3508 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3509 | return NPV_Error; |
| 3510 | } |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3511 | |
| 3512 | // C++11 [temp.arg.nontype]p1: |
| 3513 | // - an address constant expression of type std::nullptr_t |
| 3514 | if (Arg->getType()->isNullPtrType()) |
| 3515 | return NPV_NullPointer; |
| 3516 | |
| 3517 | // - a constant expression that evaluates to a null pointer value (4.10); or |
| 3518 | // - a constant expression that evaluates to a null member pointer value |
| 3519 | // (4.11); or |
| 3520 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 3521 | (EvalResult.Val.isMemberPointer() && |
| 3522 | !EvalResult.Val.getMemberPointerDecl())) { |
| 3523 | // If our expression has an appropriate type, we've succeeded. |
| 3524 | bool ObjCLifetimeConversion; |
| 3525 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 3526 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 3527 | ObjCLifetimeConversion)) |
| 3528 | return NPV_NullPointer; |
| 3529 | |
| 3530 | // The types didn't match, but we know we got a null pointer; complain, |
| 3531 | // then recover as if the types were correct. |
| 3532 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 3533 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 3534 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3535 | return NPV_NullPointer; |
| 3536 | } |
| 3537 | |
| 3538 | // If we don't have a null pointer value, but we do have a NULL pointer |
| 3539 | // constant, suggest a cast to the appropriate type. |
| 3540 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 3541 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 3542 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
| 3543 | << ParamType |
| 3544 | << FixItHint::CreateInsertion(Arg->getLocStart(), Code) |
| 3545 | << FixItHint::CreateInsertion(S.PP.getLocForEndOfToken(Arg->getLocEnd()), |
| 3546 | ")"); |
| 3547 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3548 | return NPV_NullPointer; |
| 3549 | } |
| 3550 | |
| 3551 | // FIXME: If we ever want to support general, address-constant expressions |
| 3552 | // as non-type template arguments, we should return the ExprResult here to |
| 3553 | // be interpreted by the caller. |
| 3554 | return NPV_NotNullPointer; |
| 3555 | } |
| 3556 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3557 | /// \brief Checks whether the given template argument is the address |
| 3558 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3559 | static bool |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3560 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 3561 | NonTypeTemplateParmDecl *Param, |
| 3562 | QualType ParamType, |
| 3563 | Expr *ArgIn, |
| 3564 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3565 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3566 | Expr *Arg = ArgIn; |
| 3567 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3568 | |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3569 | // If our parameter has pointer type, check for a null template value. |
| 3570 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 3571 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 3572 | case NPV_NullPointer: |
| 3573 | Converted = TemplateArgument((Decl *)0); |
| 3574 | return false; |
| 3575 | |
| 3576 | case NPV_Error: |
| 3577 | return true; |
| 3578 | |
| 3579 | case NPV_NotNullPointer: |
| 3580 | break; |
| 3581 | } |
| 3582 | } |
| 3583 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3584 | // See through any implicit casts we added to fix the type. |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3585 | Arg = Arg->IgnoreImpCasts(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3586 | |
| 3587 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3588 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3589 | // A template-argument for a non-type, non-template |
| 3590 | // template-parameter shall be one of: [...] |
| 3591 | // |
| 3592 | // -- the address of an object or function with external |
| 3593 | // linkage, including function templates and function |
| 3594 | // template-ids but excluding non-static class members, |
| 3595 | // expressed as & id-expression where the & is optional if |
| 3596 | // the name refers to a function or array, or if the |
| 3597 | // corresponding template-parameter is a reference; or |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3598 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3599 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3600 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3601 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3602 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3603 | if (!Invalid && !ExtraParens) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3604 | S.Diag(Arg->getLocStart(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3605 | S.getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3606 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3607 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3608 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3609 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3610 | } |
| 3611 | |
| 3612 | Arg = Parens->getSubExpr(); |
| 3613 | } |
| 3614 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3615 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3616 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3617 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3618 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3619 | bool AddressTaken = false; |
| 3620 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3621 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3622 | if (UnOp->getOpcode() == UO_AddrOf) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3623 | Arg = UnOp->getSubExpr(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3624 | AddressTaken = true; |
| 3625 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 3626 | } |
Francois Pichet | a343a41 | 2011-04-29 09:08:14 +0000 | [diff] [blame] | 3627 | } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3628 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3629 | if (S.getLangOpts().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3630 | Converted = TemplateArgument(ArgIn); |
| 3631 | return false; |
| 3632 | } |
| 3633 | |
| 3634 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3635 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3636 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3637 | |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3638 | // Stop checking the precise nature of the argument if it is value dependent, |
| 3639 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3640 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3641 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3642 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3643 | } |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 3644 | |
| 3645 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 3646 | if (!DRE) { |
| 3647 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 3648 | << Arg->getSourceRange(); |
| 3649 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3650 | return true; |
| 3651 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3652 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3653 | if (!isa<ValueDecl>(DRE->getDecl())) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3654 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3655 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3656 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3657 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3658 | return true; |
| 3659 | } |
| 3660 | |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3661 | NamedDecl *Entity = DRE->getDecl(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3662 | |
| 3663 | // Cannot refer to non-static data members |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3664 | if (FieldDecl *Field = dyn_cast<FieldDecl>(Entity)) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3665 | S.Diag(Arg->getLocStart(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3666 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3667 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3668 | return true; |
| 3669 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3670 | |
| 3671 | // Cannot refer to non-static member functions |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3672 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3673 | if (!Method->isStatic()) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3674 | S.Diag(Arg->getLocStart(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3675 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3676 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3677 | return true; |
| 3678 | } |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3679 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3680 | |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3681 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 3682 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3683 | |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3684 | // A non-type template argument must refer to an object or function. |
| 3685 | if (!Func && !Var) { |
| 3686 | // We found something, but we don't know specifically what it is. |
| 3687 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_object_or_func) |
| 3688 | << Arg->getSourceRange(); |
| 3689 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3690 | return true; |
| 3691 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3692 | |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3693 | // Address / reference template args must have external linkage in C++98. |
| 3694 | if (Entity->getLinkage() == InternalLinkage) { |
| 3695 | S.Diag(Arg->getLocStart(), S.getLangOpts().CPlusPlus0x ? |
| 3696 | diag::warn_cxx98_compat_template_arg_object_internal : |
| 3697 | diag::ext_template_arg_object_internal) |
| 3698 | << !Func << Entity << Arg->getSourceRange(); |
| 3699 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 3700 | << !Func; |
| 3701 | } else if (Entity->getLinkage() == NoLinkage) { |
| 3702 | S.Diag(Arg->getLocStart(), diag::err_template_arg_object_no_linkage) |
| 3703 | << !Func << Entity << Arg->getSourceRange(); |
| 3704 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 3705 | << !Func; |
| 3706 | return true; |
| 3707 | } |
| 3708 | |
| 3709 | if (Func) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3710 | // If the template parameter has pointer type, the function decays. |
| 3711 | if (ParamType->isPointerType() && !AddressTaken) |
| 3712 | ArgType = S.Context.getPointerType(Func->getType()); |
| 3713 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 3714 | // If we originally had an address-of operator, but the |
| 3715 | // parameter has reference type, complain and (if things look |
| 3716 | // like they will work) drop the address-of operator. |
| 3717 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 3718 | ParamType.getNonReferenceType())) { |
| 3719 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3720 | << ParamType; |
| 3721 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3722 | return true; |
| 3723 | } |
| 3724 | |
| 3725 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3726 | << ParamType |
| 3727 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3728 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3729 | |
| 3730 | ArgType = Func->getType(); |
| 3731 | } |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3732 | } else { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3733 | // A value of reference type is not an object. |
| 3734 | if (Var->getType()->isReferenceType()) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3735 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3736 | diag::err_template_arg_reference_var) |
| 3737 | << Var->getType() << Arg->getSourceRange(); |
| 3738 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3739 | return true; |
| 3740 | } |
| 3741 | |
Richard Smith | b4051e7 | 2012-04-04 21:11:30 +0000 | [diff] [blame] | 3742 | // A template argument must have static storage duration. |
| 3743 | // FIXME: Ensure this works for thread_local as well as __thread. |
| 3744 | if (Var->isThreadSpecified()) { |
| 3745 | S.Diag(Arg->getLocStart(), diag::err_template_arg_thread_local) |
| 3746 | << Arg->getSourceRange(); |
| 3747 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 3748 | return true; |
| 3749 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3750 | |
| 3751 | // If the template parameter has pointer type, we must have taken |
| 3752 | // the address of this object. |
| 3753 | if (ParamType->isReferenceType()) { |
| 3754 | if (AddressTaken) { |
| 3755 | // If we originally had an address-of operator, but the |
| 3756 | // parameter has reference type, complain and (if things look |
| 3757 | // like they will work) drop the address-of operator. |
| 3758 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3759 | ParamType.getNonReferenceType())) { |
| 3760 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3761 | << ParamType; |
| 3762 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3763 | return true; |
| 3764 | } |
| 3765 | |
| 3766 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3767 | << ParamType |
| 3768 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3769 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3770 | |
| 3771 | ArgType = Var->getType(); |
| 3772 | } |
| 3773 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3774 | if (Var->getType()->isArrayType()) { |
| 3775 | // Array-to-pointer decay. |
| 3776 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3777 | } else { |
| 3778 | // If the template parameter has pointer type but the address of |
| 3779 | // this object was not taken, complain and (possibly) recover by |
| 3780 | // taking the address of the entity. |
| 3781 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3782 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3783 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3784 | << ParamType; |
| 3785 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3786 | return true; |
| 3787 | } |
| 3788 | |
| 3789 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3790 | << ParamType |
| 3791 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3792 | |
| 3793 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3794 | } |
| 3795 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3796 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3797 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3798 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3799 | if (ParamType->isPointerType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3800 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3801 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 3802 | ObjCLifetimeConversion)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3803 | // For pointer-to-object types, qualification conversions are |
| 3804 | // permitted. |
| 3805 | } else { |
| 3806 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3807 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3808 | // C++ [temp.arg.nontype]p5b3: |
| 3809 | // For a non-type template-parameter of type reference to |
| 3810 | // object, no conversions apply. The type referred to by the |
| 3811 | // reference may be more cv-qualified than the (otherwise |
| 3812 | // identical) type of the template- argument. The |
| 3813 | // template-parameter is bound directly to the |
| 3814 | // template-argument, which shall be an lvalue. |
| 3815 | |
| 3816 | // FIXME: Other qualifiers? |
| 3817 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3818 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3819 | |
| 3820 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3821 | S.Diag(Arg->getLocStart(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3822 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3823 | << ParamType << Arg->getType() |
| 3824 | << Arg->getSourceRange(); |
| 3825 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3826 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3827 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3828 | } |
| 3829 | } |
| 3830 | |
| 3831 | // At this point, the template argument refers to an object or |
| 3832 | // function with external linkage. We now need to check whether the |
| 3833 | // argument and parameter types are compatible. |
| 3834 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3835 | ParamType.getNonReferenceType())) { |
| 3836 | // We can't perform this conversion or binding. |
| 3837 | if (ParamType->isReferenceType()) |
| 3838 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3839 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3840 | else |
| 3841 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3842 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3843 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3844 | return true; |
| 3845 | } |
| 3846 | } |
| 3847 | |
| 3848 | // Create the template argument. |
| 3849 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3850 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3851 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
| 3854 | /// \brief Checks whether the given template argument is a pointer to |
| 3855 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3856 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 3857 | NonTypeTemplateParmDecl *Param, |
| 3858 | QualType ParamType, |
| 3859 | Expr *&ResultArg, |
| 3860 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3861 | bool Invalid = false; |
| 3862 | |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3863 | // Check for a null pointer value. |
| 3864 | Expr *Arg = ResultArg; |
| 3865 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, Arg)) { |
| 3866 | case NPV_Error: |
| 3867 | return true; |
| 3868 | case NPV_NullPointer: |
| 3869 | Converted = TemplateArgument((Decl *)0); |
| 3870 | return false; |
| 3871 | case NPV_NotNullPointer: |
| 3872 | break; |
| 3873 | } |
| 3874 | |
| 3875 | bool ObjCLifetimeConversion; |
| 3876 | if (S.IsQualificationConversion(Arg->getType(), |
| 3877 | ParamType.getNonReferenceType(), |
| 3878 | false, ObjCLifetimeConversion)) { |
| 3879 | Arg = S.ImpCastExprToType(Arg, ParamType, CK_NoOp, |
| 3880 | Arg->getValueKind()).take(); |
| 3881 | ResultArg = Arg; |
| 3882 | } else if (!S.Context.hasSameUnqualifiedType(Arg->getType(), |
| 3883 | ParamType.getNonReferenceType())) { |
| 3884 | // We can't perform this conversion. |
| 3885 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 3886 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 3887 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3888 | return true; |
| 3889 | } |
| 3890 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3891 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3892 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3893 | Arg = Cast->getSubExpr(); |
| 3894 | |
| 3895 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3896 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3897 | // A template-argument for a non-type, non-template |
| 3898 | // template-parameter shall be one of: [...] |
| 3899 | // |
| 3900 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3901 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3902 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3903 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3904 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3905 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3906 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3907 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3908 | S.Diag(Arg->getLocStart(), |
| 3909 | S.getLangOpts().CPlusPlus0x ? |
| 3910 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3911 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3912 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3913 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
| 3916 | Arg = Parens->getSubExpr(); |
| 3917 | } |
| 3918 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3919 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3920 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3921 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3922 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3923 | // A pointer-to-member constant written &Class::member. |
| 3924 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3925 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3926 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3927 | if (DRE && !DRE->getQualifier()) |
| 3928 | DRE = 0; |
| 3929 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3930 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3931 | // A constant of pointer-to-member type. |
| 3932 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3933 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3934 | if (VD->getType()->isMemberPointerType()) { |
| 3935 | if (isa<NonTypeTemplateParmDecl>(VD) || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3936 | (isa<VarDecl>(VD) && |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3937 | S.Context.getCanonicalType(VD->getType()).isConstQualified())) { |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3938 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3939 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3940 | else |
| 3941 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3942 | return Invalid; |
| 3943 | } |
| 3944 | } |
| 3945 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3946 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3947 | DRE = 0; |
| 3948 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3949 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3950 | if (!DRE) |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3951 | return S.Diag(Arg->getLocStart(), |
| 3952 | diag::err_template_arg_not_pointer_to_member_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3953 | << Arg->getSourceRange(); |
| 3954 | |
| 3955 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3956 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3957 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3958 | "Only non-static member pointers can make it here"); |
| 3959 | |
| 3960 | // Okay: this is the address of a non-static member, and therefore |
| 3961 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3962 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3963 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3964 | else |
| 3965 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3966 | return Invalid; |
| 3967 | } |
| 3968 | |
| 3969 | // We found something else, but we don't know specifically what it is. |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 3970 | S.Diag(Arg->getLocStart(), |
| 3971 | diag::err_template_arg_not_pointer_to_member_form) |
| 3972 | << Arg->getSourceRange(); |
| 3973 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3974 | return true; |
| 3975 | } |
| 3976 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3977 | /// \brief Check a template argument against its corresponding |
| 3978 | /// non-type template parameter. |
| 3979 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3980 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3981 | /// If an error occurred, it returns ExprError(); otherwise, it |
| 3982 | /// returns the converted template argument. \p |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3983 | /// InstantiatedParamType is the type of the non-type template |
| 3984 | /// parameter after it has been instantiated. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3985 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 3986 | QualType InstantiatedParamType, Expr *Arg, |
| 3987 | TemplateArgument &Converted, |
| 3988 | CheckTemplateArgumentKind CTAK) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3989 | SourceLocation StartLoc = Arg->getLocStart(); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3990 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3991 | // If either the parameter has a dependent type or the argument is |
| 3992 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3993 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3994 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3995 | Converted = TemplateArgument(Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3996 | return Owned(Arg); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3997 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3998 | |
| 3999 | // C++ [temp.arg.nontype]p5: |
| 4000 | // The following conversions are performed on each expression used |
| 4001 | // as a non-type template-argument. If a non-type |
| 4002 | // template-argument cannot be converted to the type of the |
| 4003 | // corresponding template-parameter then the program is |
| 4004 | // ill-formed. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4005 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4006 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4007 | // C++11: |
| 4008 | // -- for a non-type template-parameter of integral or |
| 4009 | // enumeration type, conversions permitted in a converted |
| 4010 | // constant expression are applied. |
| 4011 | // |
| 4012 | // C++98: |
| 4013 | // -- for a non-type template-parameter of integral or |
| 4014 | // enumeration type, integral promotions (4.5) and integral |
| 4015 | // conversions (4.7) are applied. |
| 4016 | |
| 4017 | if (CTAK == CTAK_Deduced && |
| 4018 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 4019 | // C++ [temp.deduct.type]p17: |
| 4020 | // If, in the declaration of a function template with a non-type |
| 4021 | // template-parameter, the non-type template-parameter is used |
| 4022 | // in an expression in the function parameter-list and, if the |
| 4023 | // corresponding template-argument is deduced, the |
| 4024 | // template-argument type shall match the type of the |
| 4025 | // template-parameter exactly, except that a template-argument |
| 4026 | // deduced from an array bound may be of any integral type. |
| 4027 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 4028 | << Arg->getType().getUnqualifiedType() |
| 4029 | << ParamType.getUnqualifiedType(); |
| 4030 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4031 | return ExprError(); |
| 4032 | } |
| 4033 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4034 | if (getLangOpts().CPlusPlus0x) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 4035 | // We can't check arbitrary value-dependent arguments. |
| 4036 | // FIXME: If there's no viable conversion to the template parameter type, |
| 4037 | // we should be able to diagnose that prior to instantiation. |
| 4038 | if (Arg->isValueDependent()) { |
| 4039 | Converted = TemplateArgument(Arg); |
| 4040 | return Owned(Arg); |
| 4041 | } |
| 4042 | |
| 4043 | // C++ [temp.arg.nontype]p1: |
| 4044 | // A template-argument for a non-type, non-template template-parameter |
| 4045 | // shall be one of: |
| 4046 | // |
| 4047 | // -- for a non-type template-parameter of integral or enumeration |
| 4048 | // type, a converted constant expression of the type of the |
| 4049 | // template-parameter; or |
| 4050 | llvm::APSInt Value; |
| 4051 | ExprResult ArgResult = |
| 4052 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 4053 | CCEK_TemplateArg); |
| 4054 | if (ArgResult.isInvalid()) |
| 4055 | return ExprError(); |
| 4056 | |
| 4057 | // Widen the argument value to sizeof(parameter type). This is almost |
| 4058 | // always a no-op, except when the parameter type is bool. In |
| 4059 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 4060 | QualType IntegerType = ParamType; |
| 4061 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 4062 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 4063 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 4064 | |
| 4065 | Converted = TemplateArgument(Value, Context.getCanonicalType(ParamType)); |
| 4066 | return ArgResult; |
| 4067 | } |
| 4068 | |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4069 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 4070 | if (ArgResult.isInvalid()) |
| 4071 | return ExprError(); |
| 4072 | Arg = ArgResult.take(); |
| 4073 | |
| 4074 | QualType ArgType = Arg->getType(); |
| 4075 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4076 | // C++ [temp.arg.nontype]p1: |
| 4077 | // A template-argument for a non-type, non-template |
| 4078 | // template-parameter shall be one of: |
| 4079 | // |
| 4080 | // -- an integral constant-expression of integral or enumeration |
| 4081 | // type; or |
| 4082 | // -- the name of a non-type template-parameter; or |
| 4083 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4084 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4085 | if (!ArgType->isIntegralOrEnumerationType()) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4086 | Diag(Arg->getLocStart(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4087 | diag::err_template_arg_not_integral_or_enumeral) |
| 4088 | << ArgType << Arg->getSourceRange(); |
| 4089 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4090 | return ExprError(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 4091 | } else if (!Arg->isValueDependent()) { |
| 4092 | Arg = VerifyIntegerConstantExpression(Arg, &Value, |
| 4093 | PDiag(diag::err_template_arg_not_ice) << ArgType, false).take(); |
| 4094 | if (!Arg) |
| 4095 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4096 | } |
| 4097 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4098 | // From here on out, all we care about are the unqualified forms |
| 4099 | // of the parameter and argument types. |
| 4100 | ParamType = ParamType.getUnqualifiedType(); |
| 4101 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4102 | |
| 4103 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 4104 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4105 | // Okay: no conversion necessary |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4106 | } else if (ParamType->isBooleanType()) { |
| 4107 | // This is an integral-to-boolean conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4108 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4109 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 4110 | !ParamType->isEnumeralType()) { |
| 4111 | // This is an integral promotion or conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4112 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4113 | } else { |
| 4114 | // We can't perform this conversion. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4115 | Diag(Arg->getLocStart(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4116 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4117 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4118 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4119 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4122 | // Add the value of this argument to the list of converted |
| 4123 | // arguments. We use the bitwidth and signedness of the template |
| 4124 | // parameter. |
| 4125 | if (Arg->isValueDependent()) { |
| 4126 | // The argument is value-dependent. Create a new |
| 4127 | // TemplateArgument with the converted expression. |
| 4128 | Converted = TemplateArgument(Arg); |
| 4129 | return Owned(Arg); |
| 4130 | } |
| 4131 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4132 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4133 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 4134 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4135 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4136 | if (ParamType->isBooleanType()) { |
| 4137 | // Value must be zero or one. |
| 4138 | Value = Value != 0; |
| 4139 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 4140 | if (Value.getBitWidth() != AllowedBits) |
| 4141 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4142 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4143 | } else { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4144 | llvm::APSInt OldValue = Value; |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4145 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4146 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4147 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 4148 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 4149 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4150 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4151 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4152 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4153 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4154 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4155 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4156 | Diag(Arg->getLocStart(), diag::warn_template_arg_negative) |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4157 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 4158 | << Arg->getSourceRange(); |
| 4159 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4160 | } |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 4161 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4162 | // Complain if we overflowed the template parameter's type. |
| 4163 | unsigned RequiredBits; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4164 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4165 | RequiredBits = OldValue.getActiveBits(); |
| 4166 | else if (OldValue.isUnsigned()) |
| 4167 | RequiredBits = OldValue.getActiveBits() + 1; |
| 4168 | else |
| 4169 | RequiredBits = OldValue.getMinSignedBits(); |
| 4170 | if (RequiredBits > AllowedBits) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4171 | Diag(Arg->getLocStart(), |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4172 | diag::warn_template_arg_too_large) |
| 4173 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 4174 | << Arg->getSourceRange(); |
| 4175 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4176 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4177 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4178 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4179 | Converted = TemplateArgument(Value, |
Douglas Gregor | 6b63f55 | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 4180 | ParamType->isEnumeralType() |
| 4181 | ? Context.getCanonicalType(ParamType) |
| 4182 | : IntegerType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4183 | return Owned(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4184 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4185 | |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4186 | QualType ArgType = Arg->getType(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4187 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 4188 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4189 | // Handle pointer-to-function, reference-to-function, and |
| 4190 | // pointer-to-member-function all in (roughly) the same way. |
| 4191 | if (// -- For a non-type template-parameter of type pointer to |
| 4192 | // function, only the function-to-pointer conversion (4.3) is |
| 4193 | // applied. If the template-argument represents a set of |
| 4194 | // overloaded functions (or a pointer to such), the matching |
| 4195 | // function is selected from the set (13.4). |
| 4196 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4197 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4198 | // -- For a non-type template-parameter of type reference to |
| 4199 | // function, no conversions apply. If the template-argument |
| 4200 | // represents a set of overloaded functions, the matching |
| 4201 | // function is selected from the set (13.4). |
| 4202 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4203 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4204 | // -- For a non-type template-parameter of type pointer to |
| 4205 | // member function, no conversions apply. If the |
| 4206 | // template-argument represents a set of overloaded member |
| 4207 | // functions, the matching member function is selected from |
| 4208 | // the set (13.4). |
| 4209 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4210 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4211 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4212 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4213 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4214 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4215 | true, |
| 4216 | FoundResult)) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4217 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4218 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4219 | |
| 4220 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 4221 | ArgType = Arg->getType(); |
| 4222 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4223 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4224 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4225 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4226 | if (!ParamType->isMemberPointerType()) { |
| 4227 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4228 | ParamType, |
| 4229 | Arg, Converted)) |
| 4230 | return ExprError(); |
| 4231 | return Owned(Arg); |
| 4232 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4233 | |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4234 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 4235 | Converted)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4236 | return ExprError(); |
| 4237 | return Owned(Arg); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4238 | } |
| 4239 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 4240 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4241 | // -- for a non-type template-parameter of type pointer to |
| 4242 | // object, qualification conversions (4.4) and the |
| 4243 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4244 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4245 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4246 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4247 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4248 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4249 | ParamType, |
| 4250 | Arg, Converted)) |
| 4251 | return ExprError(); |
| 4252 | return Owned(Arg); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4253 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4254 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4255 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4256 | // -- For a non-type template-parameter of type reference to |
| 4257 | // object, no conversions apply. The type referred to by the |
| 4258 | // reference may be more cv-qualified than the (otherwise |
| 4259 | // identical) type of the template-argument. The |
| 4260 | // template-parameter is bound directly to the |
| 4261 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4262 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4263 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4264 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4265 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4266 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 4267 | ParamRefType->getPointeeType(), |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4268 | true, |
| 4269 | FoundResult)) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4270 | if (DiagnoseUseOfDecl(Fn, Arg->getLocStart())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4271 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4272 | |
| 4273 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 4274 | ArgType = Arg->getType(); |
| 4275 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4276 | return ExprError(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4277 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4278 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4279 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4280 | ParamType, |
| 4281 | Arg, Converted)) |
| 4282 | return ExprError(); |
| 4283 | return Owned(Arg); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4284 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4285 | |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4286 | // Deal with parameters of type std::nullptr_t. |
| 4287 | if (ParamType->isNullPtrType()) { |
| 4288 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 4289 | Converted = TemplateArgument(Arg); |
| 4290 | return Owned(Arg); |
| 4291 | } |
| 4292 | |
| 4293 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 4294 | case NPV_NotNullPointer: |
| 4295 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 4296 | << Arg->getType() << ParamType; |
| 4297 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4298 | return ExprError(); |
| 4299 | |
| 4300 | case NPV_Error: |
| 4301 | return ExprError(); |
| 4302 | |
| 4303 | case NPV_NullPointer: |
| 4304 | Converted = TemplateArgument((Decl *)0); |
| 4305 | return Owned(Arg);; |
| 4306 | } |
| 4307 | } |
| 4308 | |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4309 | // -- For a non-type template-parameter of type pointer to data |
| 4310 | // member, qualification conversions (4.4) are applied. |
| 4311 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 4312 | |
Douglas Gregor | 4296361 | 2012-04-10 17:08:25 +0000 | [diff] [blame] | 4313 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 4314 | Converted)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4315 | return ExprError(); |
| 4316 | return Owned(Arg); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4317 | } |
| 4318 | |
| 4319 | /// \brief Check a template argument against its corresponding |
| 4320 | /// template template parameter. |
| 4321 | /// |
| 4322 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 4323 | /// It returns true if an error occurred, and false otherwise. |
| 4324 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4325 | const TemplateArgumentLoc &Arg) { |
| 4326 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 4327 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 4328 | if (!Template) { |
| 4329 | // Any dependent template name is fine. |
| 4330 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 4331 | return false; |
| 4332 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4333 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4334 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4335 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4336 | // the name of a class template or an alias template, expressed as an |
| 4337 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4338 | // primary class templates are considered when matching the |
| 4339 | // template template argument with the corresponding parameter; |
| 4340 | // partial specializations are not considered even if their |
| 4341 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4342 | // |
| 4343 | // Note that we also allow template template parameters here, which |
| 4344 | // will happen when we are dealing with, e.g., class template |
| 4345 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4346 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4347 | !isa<TemplateTemplateParmDecl>(Template) && |
| 4348 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4349 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4350 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4351 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4352 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4353 | << Template; |
| 4354 | } |
| 4355 | |
| 4356 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 4357 | Param->getTemplateParameters(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4358 | true, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4359 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4360 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4363 | /// \brief Given a non-type template argument that refers to a |
| 4364 | /// declaration and the type of its corresponding non-type template |
| 4365 | /// parameter, produce an expression that properly refers to that |
| 4366 | /// declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4367 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4368 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 4369 | QualType ParamType, |
| 4370 | SourceLocation Loc) { |
| 4371 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 4372 | "Only declaration template arguments permitted here"); |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4373 | |
| 4374 | // For a NULL non-type template argument, return nullptr casted to the |
| 4375 | // parameter's type. |
| 4376 | if (!Arg.getAsDecl()) { |
| 4377 | return ImpCastExprToType( |
| 4378 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 4379 | ParamType, |
| 4380 | ParamType->getAs<MemberPointerType>() |
| 4381 | ? CK_NullToMemberPointer |
| 4382 | : CK_NullToPointer); |
| 4383 | } |
| 4384 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4385 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 4386 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4387 | if (VD->getDeclContext()->isRecord() && |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4388 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 4389 | // If the value is a class member, we might have a pointer-to-member. |
| 4390 | // Determine whether the non-type template template parameter is of |
| 4391 | // pointer-to-member type. If so, we need to build an appropriate |
| 4392 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 4393 | // would refer to the member itself. |
| 4394 | if (ParamType->isMemberPointerType()) { |
| 4395 | QualType ClassType |
| 4396 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 4397 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4398 | = NestedNameSpecifier::Create(Context, 0, false, |
| 4399 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4400 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 4401 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4402 | |
| 4403 | // The actual value-ness of this is unimportant, but for |
| 4404 | // internal consistency's sake, references to instance methods |
| 4405 | // are r-values. |
| 4406 | ExprValueKind VK = VK_LValue; |
| 4407 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 4408 | VK = VK_RValue; |
| 4409 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4410 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4411 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4412 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4413 | Loc, |
| 4414 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4415 | if (RefExpr.isInvalid()) |
| 4416 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4417 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4418 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4419 | |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4420 | // We might need to perform a trailing qualification conversion, since |
| 4421 | // the element type on the parameter could be more qualified than the |
| 4422 | // element type in the expression we constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4423 | bool ObjCLifetimeConversion; |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4424 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4425 | ParamType.getUnqualifiedType(), false, |
| 4426 | ObjCLifetimeConversion)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4427 | RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4428 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4429 | assert(!RefExpr.isInvalid() && |
| 4430 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4431 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4432 | return move(RefExpr); |
| 4433 | } |
| 4434 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4435 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4436 | QualType T = VD->getType().getNonReferenceType(); |
| 4437 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4438 | // When the non-type template parameter is a pointer, take the |
| 4439 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4440 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4441 | if (RefExpr.isInvalid()) |
| 4442 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4443 | |
| 4444 | if (T->isFunctionType() || T->isArrayType()) { |
| 4445 | // Decay functions and arrays. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4446 | RefExpr = DefaultFunctionArrayConversion(RefExpr.take()); |
| 4447 | if (RefExpr.isInvalid()) |
| 4448 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4449 | |
| 4450 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4451 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4452 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4453 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4454 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4455 | } |
| 4456 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4457 | ExprValueKind VK = VK_RValue; |
| 4458 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4459 | // If the non-type template parameter has reference type, qualify the |
| 4460 | // resulting declaration reference with the extra qualifiers on the |
| 4461 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4462 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 4463 | VK = VK_LValue; |
| 4464 | T = Context.getQualifiedType(T, |
| 4465 | TargetRef->getPointeeType().getQualifiers()); |
| 4466 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4467 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4468 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4469 | } |
| 4470 | |
| 4471 | /// \brief Construct a new expression that refers to the given |
| 4472 | /// integral template argument with the given source-location |
| 4473 | /// information. |
| 4474 | /// |
| 4475 | /// This routine takes care of the mapping from an integral template |
| 4476 | /// argument (which may have any integral type) to the appropriate |
| 4477 | /// literal value. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4478 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4479 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 4480 | SourceLocation Loc) { |
| 4481 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4482 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4483 | QualType T = Arg.getIntegralType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4484 | if (T->isAnyCharacterType()) { |
| 4485 | CharacterLiteral::CharacterKind Kind; |
| 4486 | if (T->isWideCharType()) |
| 4487 | Kind = CharacterLiteral::Wide; |
| 4488 | else if (T->isChar16Type()) |
| 4489 | Kind = CharacterLiteral::UTF16; |
| 4490 | else if (T->isChar32Type()) |
| 4491 | Kind = CharacterLiteral::UTF32; |
| 4492 | else |
| 4493 | Kind = CharacterLiteral::Ascii; |
| 4494 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4495 | return Owned(new (Context) CharacterLiteral( |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4496 | Arg.getAsIntegral()->getZExtValue(), |
| 4497 | Kind, T, Loc)); |
| 4498 | } |
| 4499 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4500 | if (T->isBooleanType()) |
| 4501 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 4502 | Arg.getAsIntegral()->getBoolValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4503 | T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4504 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4505 | if (T->isNullPtrType()) |
| 4506 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 4507 | |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4508 | // If this is an enum type that we're instantiating, we need to use an integer |
| 4509 | // type the same size as the enumerator. We don't want to build an |
| 4510 | // IntegerLiteral with enum type. |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4511 | QualType BT; |
| 4512 | if (const EnumType *ET = T->getAs<EnumType>()) |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4513 | BT = ET->getDecl()->getIntegerType(); |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4514 | else |
| 4515 | BT = T; |
| 4516 | |
John McCall | 4e9272d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 4517 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
| 4518 | if (T->isEnumeralType()) { |
| 4519 | // FIXME: This is a hack. We need a better way to handle substituted |
| 4520 | // non-type template parameters. |
| 4521 | E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0, |
| 4522 | Context.getTrivialTypeSourceInfo(T, Loc), |
| 4523 | Loc, Loc); |
| 4524 | } |
| 4525 | |
| 4526 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4527 | } |
| 4528 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4529 | /// \brief Match two template parameters within template parameter lists. |
| 4530 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 4531 | bool Complain, |
| 4532 | Sema::TemplateParameterListEqualKind Kind, |
| 4533 | SourceLocation TemplateArgLoc) { |
| 4534 | // Check the actual kind (type, non-type, template). |
| 4535 | if (Old->getKind() != New->getKind()) { |
| 4536 | if (Complain) { |
| 4537 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 4538 | if (TemplateArgLoc.isValid()) { |
| 4539 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4540 | NextDiag = diag::note_template_param_different_kind; |
| 4541 | } |
| 4542 | S.Diag(New->getLocation(), NextDiag) |
| 4543 | << (Kind != Sema::TPL_TemplateMatch); |
| 4544 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 4545 | << (Kind != Sema::TPL_TemplateMatch); |
| 4546 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4547 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4548 | return false; |
| 4549 | } |
| 4550 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4551 | // Check that both are parameter packs are neither are parameter packs. |
| 4552 | // However, if we are matching a template template argument to a |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4553 | // template template parameter, the template template parameter can have |
| 4554 | // a parameter pack where the template template argument does not. |
| 4555 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 4556 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4557 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4558 | if (Complain) { |
| 4559 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 4560 | if (TemplateArgLoc.isValid()) { |
| 4561 | S.Diag(TemplateArgLoc, |
| 4562 | diag::err_template_arg_template_params_mismatch); |
| 4563 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 4564 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4565 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4566 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 4567 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 4568 | : 2; |
| 4569 | S.Diag(New->getLocation(), NextDiag) |
| 4570 | << ParamKind << New->isParameterPack(); |
| 4571 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 4572 | << ParamKind << Old->isParameterPack(); |
| 4573 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4574 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4575 | return false; |
| 4576 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4577 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4578 | // For non-type template parameters, check the type of the parameter. |
| 4579 | if (NonTypeTemplateParmDecl *OldNTTP |
| 4580 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 4581 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4582 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4583 | // If we are matching a template template argument to a template |
| 4584 | // template parameter and one of the non-type template parameter types |
| 4585 | // is dependent, then we must wait until template instantiation time |
| 4586 | // to actually compare the arguments. |
| 4587 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4588 | (OldNTTP->getType()->isDependentType() || |
| 4589 | NewNTTP->getType()->isDependentType())) |
| 4590 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4591 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4592 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 4593 | if (Complain) { |
| 4594 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 4595 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4596 | S.Diag(TemplateArgLoc, |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4597 | diag::err_template_arg_template_params_mismatch); |
| 4598 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 4599 | } |
| 4600 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 4601 | << NewNTTP->getType() |
| 4602 | << (Kind != Sema::TPL_TemplateMatch); |
| 4603 | S.Diag(OldNTTP->getLocation(), |
| 4604 | diag::note_template_nontype_parm_prev_declaration) |
| 4605 | << OldNTTP->getType(); |
| 4606 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4607 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4608 | return false; |
| 4609 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4610 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4611 | return true; |
| 4612 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4613 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4614 | // For template template parameters, check the template parameter types. |
| 4615 | // The template parameter lists of template template |
| 4616 | // parameters must agree. |
| 4617 | if (TemplateTemplateParmDecl *OldTTP |
| 4618 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4619 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4620 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 4621 | OldTTP->getTemplateParameters(), |
| 4622 | Complain, |
| 4623 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4624 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4625 | : Kind), |
| 4626 | TemplateArgLoc); |
| 4627 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4628 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4629 | return true; |
| 4630 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4631 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4632 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 4633 | /// lists. |
| 4634 | static |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4635 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4636 | TemplateParameterList *New, |
| 4637 | TemplateParameterList *Old, |
| 4638 | Sema::TemplateParameterListEqualKind Kind, |
| 4639 | SourceLocation TemplateArgLoc) { |
| 4640 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 4641 | if (TemplateArgLoc.isValid()) { |
| 4642 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4643 | NextDiag = diag::note_template_param_list_different_arity; |
| 4644 | } |
| 4645 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 4646 | << (New->size() > Old->size()) |
| 4647 | << (Kind != Sema::TPL_TemplateMatch) |
| 4648 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 4649 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 4650 | << (Kind != Sema::TPL_TemplateMatch) |
| 4651 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 4652 | } |
| 4653 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4654 | /// \brief Determine whether the given template parameter lists are |
| 4655 | /// equivalent. |
| 4656 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4657 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4658 | /// source code as part of a new template declaration. |
| 4659 | /// |
| 4660 | /// \param Old The old template parameter list, typically found via |
| 4661 | /// name lookup of the template declared with this template parameter |
| 4662 | /// list. |
| 4663 | /// |
| 4664 | /// \param Complain If true, this routine will produce a diagnostic if |
| 4665 | /// the template parameter lists are not equivalent. |
| 4666 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4667 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4668 | /// |
| 4669 | /// \param TemplateArgLoc If this source location is valid, then we |
| 4670 | /// are actually checking the template parameter list of a template |
| 4671 | /// argument (New) against the template parameter list of its |
| 4672 | /// corresponding template template parameter (Old). We produce |
| 4673 | /// slightly different diagnostics in this scenario. |
| 4674 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4675 | /// \returns True if the template parameter lists are equal, false |
| 4676 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4677 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4678 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 4679 | TemplateParameterList *Old, |
| 4680 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4681 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4682 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4683 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 4684 | if (Complain) |
| 4685 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4686 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4687 | |
| 4688 | return false; |
| 4689 | } |
| 4690 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4691 | // C++0x [temp.arg.template]p3: |
| 4692 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4693 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4694 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4695 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4696 | // template-parameter-list of P. [...] |
| 4697 | TemplateParameterList::iterator NewParm = New->begin(); |
| 4698 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4699 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4700 | OldParmEnd = Old->end(); |
| 4701 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 4702 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 4703 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4704 | if (NewParm == NewParmEnd) { |
| 4705 | if (Complain) |
| 4706 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4707 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4708 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4709 | return false; |
| 4710 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4711 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4712 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4713 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4714 | return false; |
| 4715 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4716 | ++NewParm; |
| 4717 | continue; |
| 4718 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4719 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4720 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4721 | // [...] When P's template- parameter-list contains a template parameter |
| 4722 | // pack (14.5.3), the template parameter pack will match zero or more |
| 4723 | // template parameters or template parameter packs in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4724 | // template-parameter-list of A with the same type and form as the |
| 4725 | // template parameter pack in P (ignoring whether those template |
| 4726 | // parameters are template parameter packs). |
| 4727 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 4728 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4729 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4730 | return false; |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4731 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4732 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4733 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4734 | // Make sure we exhausted all of the arguments. |
| 4735 | if (NewParm != NewParmEnd) { |
| 4736 | if (Complain) |
| 4737 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4738 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4739 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4740 | return false; |
| 4741 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4742 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4743 | return true; |
| 4744 | } |
| 4745 | |
| 4746 | /// \brief Check whether a template can be declared within this scope. |
| 4747 | /// |
| 4748 | /// If the template declaration is valid in this scope, returns |
| 4749 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4750 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4751 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 4752 | if (!S) |
| 4753 | return false; |
| 4754 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4755 | // Find the nearest enclosing declaration scope. |
| 4756 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4757 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4758 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4759 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4760 | // C++ [temp]p2: |
| 4761 | // A template-declaration can appear only as a namespace scope or |
| 4762 | // class scope declaration. |
| 4763 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4764 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 4765 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4766 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4767 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4768 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4769 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4770 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4771 | |
| 4772 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 4773 | return false; |
| 4774 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4775 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4776 | diag::err_template_outside_namespace_or_class_scope) |
| 4777 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4778 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4779 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4780 | /// \brief Determine what kind of template specialization the given declaration |
| 4781 | /// is. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 4782 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4783 | if (!D) |
| 4784 | return TSK_Undeclared; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4785 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4786 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 4787 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4788 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 4789 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4790 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 4791 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4792 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4793 | return TSK_Undeclared; |
| 4794 | } |
| 4795 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4796 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4797 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4798 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4799 | /// This routine determines whether a template specialization can be declared |
| 4800 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4801 | /// |
| 4802 | /// \param S the semantic analysis object for which this check is being |
| 4803 | /// performed. |
| 4804 | /// |
| 4805 | /// \param Specialized the entity being specialized or instantiated, which |
| 4806 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4807 | /// a member of a class template (member function, static data member, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4808 | /// member class). |
| 4809 | /// |
| 4810 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 4811 | /// |
| 4812 | /// \param Loc the location of the explicit specialization or instantiation of |
| 4813 | /// this entity. |
| 4814 | /// |
| 4815 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 4816 | /// a class template. |
| 4817 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4818 | /// \returns true if there was an error that we cannot recover from, false |
| 4819 | /// otherwise. |
| 4820 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 4821 | NamedDecl *Specialized, |
| 4822 | NamedDecl *PrevDecl, |
| 4823 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4824 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4825 | // Keep these "kind" numbers in sync with the %select statements in the |
| 4826 | // various diagnostics emitted by this routine. |
| 4827 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4828 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4829 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4830 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4831 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4832 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4833 | EntityKind = 3; |
| 4834 | else if (isa<VarDecl>(Specialized)) |
| 4835 | EntityKind = 4; |
| 4836 | else if (isa<RecordDecl>(Specialized)) |
| 4837 | EntityKind = 5; |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4838 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus0x) |
| 4839 | EntityKind = 6; |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4840 | else { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4841 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
| 4842 | << S.getLangOpts().CPlusPlus0x; |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4843 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4844 | return true; |
| 4845 | } |
| 4846 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4847 | // C++ [temp.expl.spec]p2: |
| 4848 | // An explicit specialization shall be declared in the namespace |
| 4849 | // of which the template is a member, or, for member templates, in |
| 4850 | // the namespace of which the enclosing class or enclosing class |
| 4851 | // template is a member. An explicit specialization of a member |
| 4852 | // function, member class or static data member of a class |
| 4853 | // template shall be declared in the namespace of which the class |
| 4854 | // template is a member. Such a declaration may also be a |
| 4855 | // definition. If the declaration is not a definition, the |
| 4856 | // specialization may be defined later in the name- space in which |
| 4857 | // the explicit specialization was declared, or in a namespace |
| 4858 | // that encloses the one in which the explicit specialization was |
| 4859 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4860 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4861 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4862 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4863 | return true; |
| 4864 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4865 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4866 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4867 | if (S.getLangOpts().MicrosoftExt) { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4868 | // Do not warn for class scope explicit specialization during |
| 4869 | // instantiation, warning was already emitted during pattern |
| 4870 | // semantic analysis. |
| 4871 | if (!S.ActiveTemplateInstantiations.size()) |
| 4872 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 4873 | << Specialized; |
| 4874 | } else { |
| 4875 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4876 | << Specialized; |
| 4877 | return true; |
| 4878 | } |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4879 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4880 | |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4881 | if (S.CurContext->isRecord() && |
| 4882 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 4883 | // Make sure that we're specializing in the right record context. |
| 4884 | // Otherwise, things can go horribly wrong. |
| 4885 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4886 | << Specialized; |
| 4887 | return true; |
| 4888 | } |
| 4889 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4890 | // C++ [temp.class.spec]p6: |
| 4891 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4892 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4893 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4894 | bool ComplainedAboutScope = false; |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4895 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4896 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4897 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4898 | if ((!PrevDecl || |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4899 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4900 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4901 | // C++ [temp.exp.spec]p2: |
| 4902 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4903 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4904 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4905 | // An explicit specialization of a member function, member class or |
| 4906 | // static data member of a class template shall be declared in the |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4907 | // namespace of which the class template is a member. |
| 4908 | // |
| 4909 | // C++0x [temp.expl.spec]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4910 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4911 | // the specialized template. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4912 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
| 4913 | bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext); |
| 4914 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
| 4915 | assert(!IsCPlusPlus0xExtension && |
| 4916 | "DC encloses TU but isn't in enclosing namespace set"); |
| 4917 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4918 | << EntityKind << Specialized; |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4919 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 4920 | int Diag; |
| 4921 | if (!IsCPlusPlus0xExtension) |
| 4922 | Diag = diag::err_template_spec_decl_out_of_scope; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4923 | else if (!S.getLangOpts().CPlusPlus0x) |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4924 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 4925 | else |
| 4926 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 4927 | S.Diag(Loc, Diag) |
| 4928 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 4929 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4930 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4931 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4932 | ComplainedAboutScope = |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4933 | !(IsCPlusPlus0xExtension && S.getLangOpts().CPlusPlus0x); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4934 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4935 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4936 | |
| 4937 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4938 | // namespace. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4939 | // Note that HandleDeclarator() performs this check for explicit |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4940 | // specializations of function templates, static data members, and member |
| 4941 | // functions, so we skip the check here for those kinds of entities. |
| 4942 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4943 | // Should we refactor that check, so that it occurs later? |
| 4944 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4945 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 4946 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4947 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 4948 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 4949 | << EntityKind << Specialized; |
| 4950 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 4951 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 4952 | << EntityKind << Specialized |
| 4953 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4954 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4955 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4956 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4957 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4958 | // FIXME: check for specialization-after-instantiation errors and such. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4959 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4960 | return false; |
| 4961 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4962 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4963 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 4964 | /// that checks non-type template partial specialization arguments. |
| 4965 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 4966 | NonTypeTemplateParmDecl *Param, |
| 4967 | const TemplateArgument *Args, |
| 4968 | unsigned NumArgs) { |
| 4969 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4970 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 4971 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4972 | Args[I].pack_begin(), |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4973 | Args[I].pack_size())) |
| 4974 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4975 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4976 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4977 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4978 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4979 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4980 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4981 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4982 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4983 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4984 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4985 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4986 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4987 | |
| 4988 | // Strip off any implicit casts we added as part of type checking. |
| 4989 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4990 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4991 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4992 | // C++ [temp.class.spec]p8: |
| 4993 | // A non-type argument is non-specialized if it is the name of a |
| 4994 | // non-type parameter. All other non-type arguments are |
| 4995 | // specialized. |
| 4996 | // |
| 4997 | // Below, we check the two conditions that only apply to |
| 4998 | // specialized non-type arguments, so skip any non-specialized |
| 4999 | // arguments. |
| 5000 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 5001 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5002 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5003 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5004 | // C++ [temp.class.spec]p9: |
| 5005 | // Within the argument list of a class template partial |
| 5006 | // specialization, the following restrictions apply: |
| 5007 | // -- A partially specialized non-type argument expression |
| 5008 | // shall not involve a template parameter of the partial |
| 5009 | // specialization except when the argument expression is a |
| 5010 | // simple identifier. |
| 5011 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5012 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5013 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 5014 | << ArgExpr->getSourceRange(); |
| 5015 | return true; |
| 5016 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5017 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5018 | // -- The type of a template parameter corresponding to a |
| 5019 | // specialized non-type argument shall not be dependent on a |
| 5020 | // parameter of the specialization. |
| 5021 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5022 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5023 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 5024 | << Param->getType() |
| 5025 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5026 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5027 | return true; |
| 5028 | } |
| 5029 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5030 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5031 | return false; |
| 5032 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5033 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5034 | /// \brief Check the non-type template arguments of a class template |
| 5035 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 5036 | /// |
| 5037 | /// \param TemplateParams the template parameters of the primary class |
| 5038 | /// template. |
| 5039 | /// |
| 5040 | /// \param TemplateArg the template arguments of the class template |
| 5041 | /// partial specialization. |
| 5042 | /// |
| 5043 | /// \returns true if there was an error, false otherwise. |
| 5044 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 5045 | TemplateParameterList *TemplateParams, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5046 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5047 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 5048 | |
| 5049 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 5050 | NonTypeTemplateParmDecl *Param |
| 5051 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 5052 | if (!Param) |
| 5053 | continue; |
| 5054 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5055 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5056 | &ArgList[I], 1)) |
| 5057 | return true; |
| 5058 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5059 | |
| 5060 | return false; |
| 5061 | } |
| 5062 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5063 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5064 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 5065 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5066 | SourceLocation KWLoc, |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5067 | SourceLocation ModulePrivateLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5068 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5069 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5070 | SourceLocation TemplateNameLoc, |
| 5071 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5072 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5073 | SourceLocation RAngleLoc, |
| 5074 | AttributeList *Attr, |
| 5075 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5076 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 5077 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5078 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 5079 | // store the location of the outermost template keyword in the declaration. |
| 5080 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
| 5081 | ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation(); |
| 5082 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5083 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5084 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5085 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 5086 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5087 | |
| 5088 | if (!ClassTemplate) { |
| 5089 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5090 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 5091 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 5092 | return true; |
| 5093 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5094 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5095 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5096 | bool isPartialSpecialization = false; |
| 5097 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5098 | // Check the validity of the template headers that introduce this |
| 5099 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5100 | // FIXME: We probably shouldn't complain about these headers for |
| 5101 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 5102 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5103 | TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 5104 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, |
| 5105 | TemplateNameLoc, |
| 5106 | SS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5107 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5108 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5109 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 5110 | isExplicitSpecialization, |
| 5111 | Invalid); |
| 5112 | if (Invalid) |
| 5113 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5114 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5115 | if (TemplateParams && TemplateParams->size() > 0) { |
| 5116 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5117 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 5118 | if (TUK == TUK_Friend) { |
| 5119 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 5120 | << SourceRange(LAngleLoc, RAngleLoc); |
| 5121 | return true; |
| 5122 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5123 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5124 | // C++ [temp.class.spec]p10: |
| 5125 | // The template parameter list of a specialization shall not |
| 5126 | // contain default template argument values. |
| 5127 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 5128 | Decl *Param = TemplateParams->getParam(I); |
| 5129 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 5130 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5131 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5132 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5133 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5134 | } |
| 5135 | } else if (NonTypeTemplateParmDecl *NTTP |
| 5136 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 5137 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5138 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5139 | diag::err_default_arg_in_partial_spec) |
| 5140 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 5141 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5142 | } |
| 5143 | } else { |
| 5144 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5145 | if (TTP->hasDefaultArgument()) { |
| 5146 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5147 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5148 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 5149 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5150 | } |
| 5151 | } |
| 5152 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 5153 | } else if (TemplateParams) { |
| 5154 | if (TUK == TUK_Friend) |
| 5155 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5156 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 5157 | SourceRange(TemplateParams->getTemplateLoc(), |
| 5158 | TemplateParams->getRAngleLoc())) |
| 5159 | << SourceRange(LAngleLoc, RAngleLoc); |
| 5160 | else |
| 5161 | isExplicitSpecialization = true; |
| 5162 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5163 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5164 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5165 | isExplicitSpecialization = true; |
| 5166 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5167 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5168 | // Check that the specialization uses the same tag kind as the |
| 5169 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5170 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5171 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5172 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 5173 | Kind, TUK == TUK_Definition, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5174 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5175 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5176 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5177 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5178 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5179 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5180 | diag::note_previous_use); |
| 5181 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5182 | } |
| 5183 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5184 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5185 | TemplateArgumentListInfo TemplateArgs; |
| 5186 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 5187 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5188 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5189 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 5190 | // Check for unexpanded parameter packs in any of the template arguments. |
| 5191 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5192 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 5193 | UPPC_PartialSpecialization)) |
| 5194 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5195 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5196 | // Check that the template argument list is well-formed for this |
| 5197 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5198 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5199 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5200 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5201 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5202 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5203 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5204 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5205 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5206 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5207 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5208 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5209 | return true; |
| 5210 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 5211 | bool InstantiationDependent; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5212 | if (!Name.isDependent() && |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5213 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5214 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 5215 | TemplateArgs.size(), |
| 5216 | InstantiationDependent)) { |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5217 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 5218 | << ClassTemplate->getDeclName(); |
| 5219 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5220 | } |
| 5221 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5222 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5223 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5224 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 5225 | |
| 5226 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5227 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5228 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5229 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 5230 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5231 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5232 | else |
| 5233 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5234 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5235 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5236 | |
| 5237 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5238 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5239 | // Check whether we can declare a class template specialization in |
| 5240 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5241 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5242 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 5243 | TemplateNameLoc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5244 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5245 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5246 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5247 | // The canonical type |
| 5248 | QualType CanonType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5249 | if (PrevDecl && |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5250 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5251 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5252 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5253 | // arguments was referenced but not declared, or we're only |
| 5254 | // referencing this specialization as a friend, reuse that |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5255 | // declaration node as our own, updating its source location and |
| 5256 | // the list of outer template parameters to reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5257 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5258 | Specialization->setLocation(TemplateNameLoc); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5259 | if (TemplateParameterLists.size() > 0) { |
| 5260 | Specialization->setTemplateParameterListsInfo(Context, |
| 5261 | TemplateParameterLists.size(), |
| 5262 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5263 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5264 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5265 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5266 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5267 | // Build the canonical type that describes the converted template |
| 5268 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5269 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 5270 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5271 | Converted.data(), |
| 5272 | Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5273 | |
| 5274 | if (Context.hasSameType(CanonType, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5275 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 5276 | // C++ [temp.class.spec]p9b3: |
| 5277 | // |
| 5278 | // -- The argument list of the specialization shall not be identical |
| 5279 | // to the implicit argument list of the primary template. |
| 5280 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 5281 | << (TUK == TUK_Definition) |
| 5282 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5283 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 5284 | ClassTemplate->getIdentifier(), |
| 5285 | TemplateNameLoc, |
| 5286 | Attr, |
| 5287 | TemplateParams, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 5288 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5289 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 5290 | (TemplateParameterList**) TemplateParameterLists.release()); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5291 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5292 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5293 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5294 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 5295 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5296 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5297 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5298 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5299 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5300 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5301 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 5302 | TemplateParams, |
| 5303 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5304 | Converted.data(), |
| 5305 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5306 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5307 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5308 | PrevPartial, |
| 5309 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5310 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5311 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5312 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5313 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5314 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5315 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5316 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5317 | if (!PrevPartial) |
| 5318 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5319 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5320 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5321 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5322 | // template specialization, make a note of that. |
| 5323 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 5324 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5325 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5326 | // Check that all of the template parameters of the class template |
| 5327 | // partial specialization are deducible from the template |
| 5328 | // arguments. If not, this class template partial specialization |
| 5329 | // will never be used. |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5330 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5331 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5332 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5333 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5334 | |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5335 | if (!DeducibleParams.all()) { |
| 5336 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5337 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 5338 | << (NumNonDeducible > 1) |
| 5339 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 5340 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 5341 | if (!DeducibleParams[I]) { |
| 5342 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 5343 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5344 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5345 | diag::note_partial_spec_unused_parameter) |
| 5346 | << Param->getDeclName(); |
| 5347 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5348 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5349 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 5350 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5351 | } |
| 5352 | } |
| 5353 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5354 | } else { |
| 5355 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5356 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5357 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5358 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5359 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5360 | KWLoc, TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5361 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5362 | Converted.data(), |
| 5363 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5364 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5365 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5366 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5367 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5368 | TemplateParameterLists.size(), |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5369 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5370 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5371 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5372 | if (!PrevDecl) |
| 5373 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5374 | |
| 5375 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5376 | } |
| 5377 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5378 | // C++ [temp.expl.spec]p6: |
| 5379 | // 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] | 5380 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5381 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5382 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5383 | // use occurs; no diagnostic is required. |
| 5384 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5385 | bool Okay = false; |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5386 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5387 | // Is there any previous explicit specialization declaration? |
| 5388 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5389 | Okay = true; |
| 5390 | break; |
| 5391 | } |
| 5392 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5393 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5394 | if (!Okay) { |
| 5395 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 5396 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 5397 | << Context.getTypeDeclType(Specialization) << Range; |
| 5398 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5399 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5400 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5401 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5402 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5403 | return true; |
| 5404 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5405 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5406 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5407 | // If this is not a friend, note that this is an explicit specialization. |
| 5408 | if (TUK != TUK_Friend) |
| 5409 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5410 | |
| 5411 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5412 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5413 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5414 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5415 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5416 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5417 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 5418 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5419 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5420 | } |
| 5421 | } |
| 5422 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 5423 | if (Attr) |
| 5424 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 5425 | |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5426 | if (ModulePrivateLoc.isValid()) |
| 5427 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 5428 | << (isPartialSpecialization? 1 : 0) |
| 5429 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 5430 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 5431 | // Build the fully-sugared type for this class template |
| 5432 | // specialization as the user wrote in the specialization |
| 5433 | // itself. This means that we'll pretty-print the type retrieved |
| 5434 | // from the specialization's declaration the way that the user |
| 5435 | // actually wrote the specialization, rather than formatting the |
| 5436 | // name based on the "canonical" representation used to store the |
| 5437 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5438 | TypeSourceInfo *WrittenTy |
| 5439 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5440 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5441 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5442 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5443 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5444 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5445 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5446 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5447 | // C++ [temp.expl.spec]p9: |
| 5448 | // A template explicit specialization is in the scope of the |
| 5449 | // namespace in which the template was defined. |
| 5450 | // |
| 5451 | // We actually implement this paragraph where we set the semantic |
| 5452 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 5453 | // but we also maintain the lexical context where the actual |
| 5454 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5455 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5456 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5457 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5458 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5459 | Specialization->startDefinition(); |
| 5460 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5461 | if (TUK == TUK_Friend) { |
| 5462 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 5463 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5464 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5465 | /*FIXME:*/KWLoc); |
| 5466 | Friend->setAccess(AS_public); |
| 5467 | CurContext->addDecl(Friend); |
| 5468 | } else { |
| 5469 | // Add the specialization into its lexical context, so that it can |
| 5470 | // be seen when iterating through the list of declarations in that |
| 5471 | // context. However, specializations are not found by name lookup. |
| 5472 | CurContext->addDecl(Specialization); |
| 5473 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5474 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5475 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5476 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5477 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5478 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5479 | Declarator &D) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5480 | return HandleDeclarator(S, D, move(TemplateParameterLists)); |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5481 | } |
| 5482 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5483 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5484 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5485 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5486 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5487 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5488 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5489 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5490 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5491 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5492 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5493 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5494 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 5495 | D.setFunctionDefinitionKind(FDK_Definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5496 | Decl *DP = HandleDeclarator(ParentScope, D, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5497 | move(TemplateParameterLists)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5498 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5499 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5500 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5501 | FunctionTemplate->getTemplatedDecl()); |
| 5502 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 5503 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 5504 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5505 | } |
| 5506 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5507 | /// \brief Strips various properties off an implicit instantiation |
| 5508 | /// that has just been explicitly specialized. |
| 5509 | static void StripImplicitInstantiation(NamedDecl *D) { |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 5510 | // 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] | 5511 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5512 | |
| 5513 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5514 | FD->setInlineSpecified(false); |
| 5515 | } |
| 5516 | } |
| 5517 | |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5518 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 5519 | // declaration or definition. |
| 5520 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5521 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5522 | // Explicit instantiations following a specialization have no effect and |
| 5523 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 5524 | // until a valid name loc is found. |
| 5525 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5526 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 5527 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5528 | PrevDiagLoc = Prev->getLocation(); |
| 5529 | } |
| 5530 | assert(PrevDiagLoc.isValid() && |
| 5531 | "Explicit instantiation without point of instantiation?"); |
| 5532 | return PrevDiagLoc; |
| 5533 | } |
| 5534 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5535 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5536 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5537 | /// for those cases where they are required and determining whether the |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5538 | /// new specialization/instantiation will have any effect. |
| 5539 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5540 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5541 | /// instantiation. |
| 5542 | /// |
| 5543 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 5544 | /// |
| 5545 | /// \param PrevDecl the previous declaration of the entity. |
| 5546 | /// |
| 5547 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 5548 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5549 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5550 | /// declaration was instantiated (either implicitly or explicitly). |
| 5551 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5552 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5553 | /// specialization or instantiation has no effect and should be ignored. |
| 5554 | /// |
| 5555 | /// \returns true if there was an error that should prevent the introduction of |
| 5556 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5557 | bool |
| 5558 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 5559 | TemplateSpecializationKind NewTSK, |
| 5560 | NamedDecl *PrevDecl, |
| 5561 | TemplateSpecializationKind PrevTSK, |
| 5562 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5563 | bool &HasNoEffect) { |
| 5564 | HasNoEffect = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5565 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5566 | switch (NewTSK) { |
| 5567 | case TSK_Undeclared: |
| 5568 | case TSK_ImplicitInstantiation: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5569 | llvm_unreachable("Don't check implicit instantiations here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5570 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5571 | case TSK_ExplicitSpecialization: |
| 5572 | switch (PrevTSK) { |
| 5573 | case TSK_Undeclared: |
| 5574 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5575 | // Okay, we're just specializing something that is either already |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5576 | // explicitly specialized or has merely been mentioned without any |
| 5577 | // instantiation. |
| 5578 | return false; |
| 5579 | |
| 5580 | case TSK_ImplicitInstantiation: |
| 5581 | if (PrevPointOfInstantiation.isInvalid()) { |
| 5582 | // The declaration itself has not actually been instantiated, so it is |
| 5583 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5584 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5585 | return false; |
| 5586 | } |
| 5587 | // Fall through |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5588 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5589 | case TSK_ExplicitInstantiationDeclaration: |
| 5590 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5591 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 5592 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5593 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5594 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5595 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5596 | // 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] | 5597 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5598 | // before the first use of that specialization that would cause an |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5599 | // implicit instantiation to take place, in every translation unit in |
| 5600 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5601 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5602 | // Is there any previous explicit specialization declaration? |
| 5603 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 5604 | return false; |
| 5605 | } |
| 5606 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5607 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5608 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5609 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5610 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5611 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5612 | return true; |
| 5613 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5614 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5615 | case TSK_ExplicitInstantiationDeclaration: |
| 5616 | switch (PrevTSK) { |
| 5617 | case TSK_ExplicitInstantiationDeclaration: |
| 5618 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5619 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5620 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5621 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5622 | case TSK_Undeclared: |
| 5623 | case TSK_ImplicitInstantiation: |
| 5624 | // We're explicitly instantiating something that may have already been |
| 5625 | // implicitly instantiated; that's fine. |
| 5626 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5627 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5628 | case TSK_ExplicitSpecialization: |
| 5629 | // C++0x [temp.explicit]p4: |
| 5630 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5631 | // of a template appears after a declaration of an explicit |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5632 | // specialization for that template, the explicit instantiation has no |
| 5633 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5634 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5635 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5636 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5637 | case TSK_ExplicitInstantiationDefinition: |
| 5638 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5639 | // If an entity is the subject of both an explicit instantiation |
| 5640 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5641 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5642 | Diag(NewLoc, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5643 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5644 | |
| 5645 | // Explicit instantiations following a specialization have no effect and |
| 5646 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 5647 | // until a valid name loc is found. |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5648 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 5649 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5650 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5651 | return false; |
| 5652 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5653 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5654 | case TSK_ExplicitInstantiationDefinition: |
| 5655 | switch (PrevTSK) { |
| 5656 | case TSK_Undeclared: |
| 5657 | case TSK_ImplicitInstantiation: |
| 5658 | // We're explicitly instantiating something that may have already been |
| 5659 | // implicitly instantiated; that's fine. |
| 5660 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5661 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5662 | case TSK_ExplicitSpecialization: |
| 5663 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 5664 | // For a given set of template parameters, if an explicit |
| 5665 | // instantiation of a template appears after a declaration of |
| 5666 | // an explicit specialization for that template, the explicit |
| 5667 | // instantiation has no effect. |
| 5668 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5669 | // 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] | 5670 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5671 | // has been explicitly specialized. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5672 | Diag(NewLoc, getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5673 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 5674 | diag::ext_explicit_instantiation_after_specialization) |
| 5675 | << PrevDecl; |
| 5676 | Diag(PrevDecl->getLocation(), |
| 5677 | diag::note_previous_template_specialization); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5678 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5679 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5680 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5681 | case TSK_ExplicitInstantiationDeclaration: |
| 5682 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5683 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5684 | |
| 5685 | // C++0x [temp.explicit]p4: |
| 5686 | // For a given set of template parameters, if an explicit instantiation |
| 5687 | // of a template appears after a declaration of an explicit |
| 5688 | // specialization for that template, the explicit instantiation has no |
| 5689 | // effect. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5690 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5691 | // Is there any previous explicit specialization declaration? |
| 5692 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5693 | HasNoEffect = true; |
| 5694 | break; |
| 5695 | } |
| 5696 | } |
| 5697 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5698 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5699 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5700 | case TSK_ExplicitInstantiationDefinition: |
| 5701 | // C++0x [temp.spec]p5: |
| 5702 | // For a given template and a given set of template-arguments, |
| 5703 | // - an explicit instantiation definition shall appear at most once |
| 5704 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5705 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5706 | << PrevDecl; |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5707 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5708 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5709 | HasNoEffect = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5710 | return false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5711 | } |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5712 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5713 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5714 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5715 | } |
| 5716 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5717 | /// \brief Perform semantic analysis for the given dependent function |
| 5718 | /// template specialization. The only possible way to get a dependent |
| 5719 | /// function template specialization is with a friend declaration, |
| 5720 | /// like so: |
| 5721 | /// |
| 5722 | /// template <class T> void foo(T); |
| 5723 | /// template <class T> class A { |
| 5724 | /// friend void foo<>(T); |
| 5725 | /// }; |
| 5726 | /// |
| 5727 | /// There really isn't any useful analysis we can do here, so we |
| 5728 | /// just store the information. |
| 5729 | bool |
| 5730 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 5731 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 5732 | LookupResult &Previous) { |
| 5733 | // Remove anything from Previous that isn't a function template in |
| 5734 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5735 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5736 | LookupResult::Filter F = Previous.makeFilter(); |
| 5737 | while (F.hasNext()) { |
| 5738 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 5739 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5740 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 5741 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5742 | F.erase(); |
| 5743 | } |
| 5744 | F.done(); |
| 5745 | |
| 5746 | // Should this be diagnosed here? |
| 5747 | if (Previous.empty()) return true; |
| 5748 | |
| 5749 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 5750 | ExplicitTemplateArgs); |
| 5751 | return false; |
| 5752 | } |
| 5753 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5754 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5755 | /// specialization. |
| 5756 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5757 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5758 | /// explicit function template specialization. On successful completion, |
| 5759 | /// the function declaration \p FD will become a function template |
| 5760 | /// specialization. |
| 5761 | /// |
| 5762 | /// \param FD the function declaration, which will be updated to become a |
| 5763 | /// function template specialization. |
| 5764 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5765 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 5766 | /// if any. Note that this may be valid info even when 0 arguments are |
| 5767 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 5768 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5769 | /// |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5770 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5771 | /// this function specialization. |
| 5772 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5773 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5774 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5775 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5776 | // The set of function template specializations that could match this |
| 5777 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5778 | UnresolvedSet<8> Candidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5779 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5780 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5781 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5782 | I != E; ++I) { |
| 5783 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 5784 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5785 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5786 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5787 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 5788 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5789 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5790 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5791 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5792 | // A trailing template-argument can be left unspecified in the |
| 5793 | // template-id naming an explicit function template specialization |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5794 | // provided it can be deduced from the function argument type. |
| 5795 | // Perform template argument deduction to determine whether we may be |
| 5796 | // specializing this template. |
| 5797 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5798 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5799 | FunctionDecl *Specialization = 0; |
| 5800 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5801 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5802 | FD->getType(), |
| 5803 | Specialization, |
| 5804 | Info)) { |
| 5805 | // FIXME: Template argument deduction failed; record why it failed, so |
| 5806 | // that we can provide nifty diagnostics. |
| 5807 | (void)TDK; |
| 5808 | continue; |
| 5809 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5810 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5811 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5812 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5813 | } |
| 5814 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5815 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5816 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5817 | UnresolvedSetIterator Result |
| 5818 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5819 | TPOC_Other, 0, FD->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5820 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5821 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5822 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5823 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5824 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5825 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5826 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5827 | |
| 5828 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 5829 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | abfb405 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 5830 | |
| 5831 | FunctionTemplateSpecializationInfo *SpecInfo |
| 5832 | = Specialization->getTemplateSpecializationInfo(); |
| 5833 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5834 | |
| 5835 | // Note: do not overwrite location info if previous template |
| 5836 | // specialization kind was explicit. |
| 5837 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 5838 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5839 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 5840 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 5841 | // function can differ from the template declaration with respect to |
| 5842 | // the constexpr specifier. |
| 5843 | Specialization->setConstexpr(FD->isConstexpr()); |
| 5844 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5845 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5846 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5847 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5848 | |
| 5849 | // If this is a friend declaration, then we're not really declaring |
| 5850 | // an explicit specialization. |
| 5851 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5852 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5853 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5854 | if (!isFriend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5855 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5856 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5857 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5858 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5859 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5860 | |
| 5861 | // C++ [temp.expl.spec]p6: |
| 5862 | // 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] | 5863 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5864 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5865 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5866 | // use occurs; no diagnostic is required. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5867 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5868 | if (!isFriend && |
| 5869 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5870 | TSK_ExplicitSpecialization, |
| 5871 | Specialization, |
| 5872 | SpecInfo->getTemplateSpecializationKind(), |
| 5873 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5874 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5875 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5876 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5877 | // Mark the prior declaration as an explicit specialization, so that later |
| 5878 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5879 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5880 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5881 | MarkUnusedFileScopedDecl(Specialization); |
| 5882 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5883 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5884 | // Turn the given function declaration into a function template |
| 5885 | // specialization, with the template arguments from the previous |
| 5886 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5887 | // Take copies of (semantic and syntactic) template argument lists. |
| 5888 | const TemplateArgumentList* TemplArgs = new (Context) |
| 5889 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5890 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5891 | TemplArgs, /*InsertPos=*/0, |
| 5892 | SpecInfo->getTemplateSpecializationKind(), |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 5893 | ExplicitTemplateArgs); |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5894 | FD->setStorageClass(Specialization->getStorageClass()); |
| 5895 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5896 | // The "previous declaration" for this function template specialization is |
| 5897 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5898 | Previous.clear(); |
| 5899 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5900 | return false; |
| 5901 | } |
| 5902 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5903 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5904 | /// specialization. |
| 5905 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5906 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5907 | /// explicit member function specialization. On successful completion, |
| 5908 | /// the function declaration \p FD will become a member function |
| 5909 | /// specialization. |
| 5910 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5911 | /// \param Member the member declaration, which will be updated to become a |
| 5912 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5913 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5914 | /// \param Previous the set of declarations, one of which may be specialized |
| 5915 | /// by this function specialization; the set will be modified to contain the |
| 5916 | /// redeclared member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5917 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5918 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5919 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5920 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5921 | // Try to find the member we are instantiating. |
| 5922 | NamedDecl *Instantiation = 0; |
| 5923 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5924 | MemberSpecializationInfo *MSInfo = 0; |
| 5925 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5926 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5927 | // Nowhere to look anyway. |
| 5928 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5929 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5930 | I != E; ++I) { |
| 5931 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5932 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5933 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 5934 | Instantiation = Method; |
| 5935 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5936 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5937 | break; |
| 5938 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5939 | } |
| 5940 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5941 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5942 | VarDecl *PrevVar; |
| 5943 | if (Previous.isSingleResult() && |
| 5944 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5945 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5946 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5947 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5948 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5949 | } |
| 5950 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5951 | CXXRecordDecl *PrevRecord; |
| 5952 | if (Previous.isSingleResult() && |
| 5953 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 5954 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5955 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5956 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5957 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 5958 | } else if (isa<EnumDecl>(Member)) { |
| 5959 | EnumDecl *PrevEnum; |
| 5960 | if (Previous.isSingleResult() && |
| 5961 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
| 5962 | Instantiation = PrevEnum; |
| 5963 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 5964 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 5965 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5966 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5967 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5968 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5969 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5970 | // specializations are always out-of-line, the caller will complain about |
| 5971 | // this mismatch later. |
| 5972 | return false; |
| 5973 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5974 | |
| 5975 | // If this is a friend, just bail out here before we start turning |
| 5976 | // things into explicit specializations. |
| 5977 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 5978 | // Preserve instantiation information. |
| 5979 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 5980 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 5981 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5982 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5983 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 5984 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 5985 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5986 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5987 | } |
| 5988 | |
| 5989 | Previous.clear(); |
| 5990 | Previous.addDecl(Instantiation); |
| 5991 | return false; |
| 5992 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5993 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5994 | // Make sure that this is a specialization of a member. |
| 5995 | if (!InstantiatedFrom) { |
| 5996 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 5997 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5998 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 5999 | return true; |
| 6000 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6001 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6002 | // C++ [temp.expl.spec]p6: |
| 6003 | // 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] | 6004 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6005 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6006 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6007 | // use occurs; no diagnostic is required. |
| 6008 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6009 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6010 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 6011 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 6012 | TSK_ExplicitSpecialization, |
| 6013 | Instantiation, |
| 6014 | MSInfo->getTemplateSpecializationKind(), |
| 6015 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6016 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6017 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6018 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6019 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6020 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6021 | InstantiatedFrom, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6022 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 6023 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6024 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 6025 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6026 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6027 | // the original declaration to note that it is an explicit specialization |
| 6028 | // (if it was previously an implicit instantiation). This latter step |
| 6029 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6030 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6031 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 6032 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 6033 | TSK_ImplicitInstantiation) { |
| 6034 | InstantiationFunction->setTemplateSpecializationKind( |
| 6035 | TSK_ExplicitSpecialization); |
| 6036 | InstantiationFunction->setLocation(Member->getLocation()); |
| 6037 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6038 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6039 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 6040 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 6041 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6042 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6043 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6044 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 6045 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 6046 | TSK_ImplicitInstantiation) { |
| 6047 | InstantiationVar->setTemplateSpecializationKind( |
| 6048 | TSK_ExplicitSpecialization); |
| 6049 | InstantiationVar->setLocation(Member->getLocation()); |
| 6050 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6051 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6052 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 6053 | cast<VarDecl>(InstantiatedFrom), |
| 6054 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 6055 | MarkUnusedFileScopedDecl(InstantiationVar); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6056 | } else if (isa<CXXRecordDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6057 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 6058 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 6059 | TSK_ImplicitInstantiation) { |
| 6060 | InstantiationClass->setTemplateSpecializationKind( |
| 6061 | TSK_ExplicitSpecialization); |
| 6062 | InstantiationClass->setLocation(Member->getLocation()); |
| 6063 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6064 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6065 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 6066 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 6067 | TSK_ExplicitSpecialization); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6068 | } else { |
| 6069 | assert(isa<EnumDecl>(Member) && "Only member enums remain"); |
| 6070 | EnumDecl *InstantiationEnum = cast<EnumDecl>(Instantiation); |
| 6071 | if (InstantiationEnum->getTemplateSpecializationKind() == |
| 6072 | TSK_ImplicitInstantiation) { |
| 6073 | InstantiationEnum->setTemplateSpecializationKind( |
| 6074 | TSK_ExplicitSpecialization); |
| 6075 | InstantiationEnum->setLocation(Member->getLocation()); |
| 6076 | } |
| 6077 | |
| 6078 | cast<EnumDecl>(Member)->setInstantiationOfMemberEnum( |
| 6079 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 6080 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6081 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6082 | // Save the caller the trouble of having to figure out which declaration |
| 6083 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6084 | Previous.clear(); |
| 6085 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 6086 | return false; |
| 6087 | } |
| 6088 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6089 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6090 | /// |
| 6091 | /// \returns true if a serious error occurs, false otherwise. |
| 6092 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6093 | SourceLocation InstLoc, |
| 6094 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6095 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 6096 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6097 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6098 | if (CurContext->isRecord()) { |
| 6099 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 6100 | << D; |
| 6101 | return true; |
| 6102 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6103 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6104 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6105 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6106 | // template. If the name declared in the explicit instantiation is an |
| 6107 | // unqualified name, the explicit instantiation shall appear in the |
| 6108 | // namespace where its template is declared or, if that namespace is inline |
| 6109 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6110 | // |
| 6111 | // 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] | 6112 | if (WasQualifiedName) { |
| 6113 | if (CurContext->Encloses(OrigContext)) |
| 6114 | return false; |
| 6115 | } else { |
| 6116 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 6117 | return false; |
| 6118 | } |
| 6119 | |
| 6120 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 6121 | if (WasQualifiedName) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6122 | S.Diag(InstLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6123 | S.getLangOpts().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6124 | diag::err_explicit_instantiation_out_of_scope : |
| 6125 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6126 | << D << NS; |
| 6127 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6128 | S.Diag(InstLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6129 | S.getLangOpts().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6130 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 6131 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 6132 | << D << NS; |
| 6133 | } else |
| 6134 | S.Diag(InstLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6135 | S.getLangOpts().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6136 | diag::err_explicit_instantiation_must_be_global : |
| 6137 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 6138 | << D; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6139 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6140 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6141 | } |
| 6142 | |
| 6143 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 6144 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 6145 | if (!SS.isSet()) |
| 6146 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6147 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 6148 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6149 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6150 | // or a static data member of a class template specialization, the name of |
| 6151 | // the class template specialization in the qualified-id for the member |
| 6152 | // name shall be a simple-template-id. |
| 6153 | // |
| 6154 | // C++98 has the same restriction, just worded differently. |
| 6155 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 6156 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6157 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6158 | if (isa<TemplateSpecializationType>(T)) |
| 6159 | return true; |
| 6160 | |
| 6161 | return false; |
| 6162 | } |
| 6163 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6164 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6165 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6166 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 6167 | SourceLocation ExternLoc, |
| 6168 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6169 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6170 | SourceLocation KWLoc, |
| 6171 | const CXXScopeSpec &SS, |
| 6172 | TemplateTy TemplateD, |
| 6173 | SourceLocation TemplateNameLoc, |
| 6174 | SourceLocation LAngleLoc, |
| 6175 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6176 | SourceLocation RAngleLoc, |
| 6177 | AttributeList *Attr) { |
| 6178 | // Find the class template we're specializing |
| 6179 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6180 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6181 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 6182 | |
| 6183 | // Check that the specialization uses the same tag kind as the |
| 6184 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6185 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6186 | assert(Kind != TTK_Enum && |
| 6187 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6188 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6189 | Kind, /*isDefinition*/false, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6190 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6191 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6192 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6193 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6194 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6195 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6196 | diag::note_previous_use); |
| 6197 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6198 | } |
| 6199 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6200 | // C++0x [temp.explicit]p2: |
| 6201 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6202 | // definition and an explicit instantiation declaration. An explicit |
| 6203 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6204 | TemplateSpecializationKind TSK |
| 6205 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6206 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6207 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6208 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6209 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 6210 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6211 | |
| 6212 | // Check that the template argument list is well-formed for this |
| 6213 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6214 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6215 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6216 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6217 | return true; |
| 6218 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6219 | // Find the class template specialization declaration that |
| 6220 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6221 | void *InsertPos = 0; |
| 6222 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6223 | = ClassTemplate->findSpecialization(Converted.data(), |
| 6224 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6225 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6226 | TemplateSpecializationKind PrevDecl_TSK |
| 6227 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 6228 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6229 | // C++0x [temp.explicit]p2: |
| 6230 | // [...] An explicit instantiation shall appear in an enclosing |
| 6231 | // namespace of its template. [...] |
| 6232 | // |
| 6233 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6234 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 6235 | SS.isSet())) |
| 6236 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6237 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6238 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 6239 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6240 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6241 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6242 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6243 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6244 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6245 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6246 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6247 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6248 | // Even though HasNoEffect == true means that this explicit instantiation |
| 6249 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 6250 | |
| 6251 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 6252 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6253 | // Since the only prior class template specialization with these |
| 6254 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6255 | // declaration node as our own, updating the source location |
| 6256 | // for the template name to reflect our new declaration. |
| 6257 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6258 | Specialization = PrevDecl; |
| 6259 | Specialization->setLocation(TemplateNameLoc); |
| 6260 | PrevDecl = 0; |
| 6261 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6262 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6263 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6264 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6265 | // Create a new class template specialization declaration node for |
| 6266 | // this explicit specialization. |
| 6267 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6268 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6269 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6270 | KWLoc, TemplateNameLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6271 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6272 | Converted.data(), |
| 6273 | Converted.size(), |
| 6274 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6275 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6276 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6277 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6278 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6279 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6280 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6281 | } |
| 6282 | |
| 6283 | // Build the fully-sugared type for this explicit instantiation as |
| 6284 | // the user wrote in the explicit instantiation itself. This means |
| 6285 | // that we'll pretty-print the type retrieved from the |
| 6286 | // specialization's declaration the way that the user actually wrote |
| 6287 | // the explicit instantiation, rather than formatting the name based |
| 6288 | // on the "canonical" representation used to store the template |
| 6289 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6290 | TypeSourceInfo *WrittenTy |
| 6291 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6292 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6293 | Context.getTypeDeclType(Specialization)); |
| 6294 | Specialization->setTypeAsWritten(WrittenTy); |
| 6295 | TemplateArgsIn.release(); |
| 6296 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6297 | // Set source locations for keywords. |
| 6298 | Specialization->setExternLoc(ExternLoc); |
| 6299 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 6300 | |
Rafael Espindola | 0257b7f | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 6301 | if (Attr) |
| 6302 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6303 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6304 | // Add the explicit instantiation into its lexical context. However, |
| 6305 | // since explicit instantiations are never found by name lookup, we |
| 6306 | // just put it into the declaration context directly. |
| 6307 | Specialization->setLexicalDeclContext(CurContext); |
| 6308 | CurContext->addDecl(Specialization); |
| 6309 | |
| 6310 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 6311 | if (HasNoEffect) { |
| 6312 | // Set the template specialization kind. |
| 6313 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6314 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 6315 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6316 | |
| 6317 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6318 | // A definition of a class template or class member template |
| 6319 | // shall be in scope at the point of the explicit instantiation of |
| 6320 | // the class template or class member template. |
| 6321 | // |
| 6322 | // This check comes when we actually try to perform the |
| 6323 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6324 | ClassTemplateSpecializationDecl *Def |
| 6325 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6326 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6327 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6328 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6329 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6330 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6331 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 6332 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6333 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6334 | // Instantiate the members of this class template specialization. |
| 6335 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6336 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6337 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 6338 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 6339 | |
| 6340 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 6341 | // TSK_ExplicitInstantiationDefinition |
| 6342 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 6343 | TSK == TSK_ExplicitInstantiationDefinition) |
| 6344 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6345 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6346 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6347 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6348 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6349 | // Set the template specialization kind. |
| 6350 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6351 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6352 | } |
| 6353 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6354 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6355 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6356 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 6357 | SourceLocation ExternLoc, |
| 6358 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6359 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6360 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6361 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6362 | IdentifierInfo *Name, |
| 6363 | SourceLocation NameLoc, |
| 6364 | AttributeList *Attr) { |
| 6365 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 6366 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6367 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6368 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6369 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6370 | /*ModulePrivateLoc=*/SourceLocation(), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6371 | MultiTemplateParamsArg(*this, 0, 0), |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 6372 | Owned, IsDependent, SourceLocation(), false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6373 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6374 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 6375 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6376 | if (!TagD) |
| 6377 | return true; |
| 6378 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6379 | TagDecl *Tag = cast<TagDecl>(TagD); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 6380 | assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6381 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 6382 | if (Tag->isInvalidDecl()) |
| 6383 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6384 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6385 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 6386 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 6387 | if (!Pattern) { |
| 6388 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 6389 | << Context.getTypeDeclType(Record); |
| 6390 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 6391 | return true; |
| 6392 | } |
| 6393 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6394 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6395 | // If the explicit instantiation is for a class or member class, the |
| 6396 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6397 | // simple-template-id. |
| 6398 | // |
| 6399 | // C++98 has the same restriction, just worded differently. |
| 6400 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6401 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6402 | << Record << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6403 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6404 | // C++0x [temp.explicit]p2: |
| 6405 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6406 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6407 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 6408 | TemplateSpecializationKind TSK |
| 6409 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6410 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6411 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6412 | // C++0x [temp.explicit]p2: |
| 6413 | // [...] An explicit instantiation shall appear in an enclosing |
| 6414 | // namespace of its template. [...] |
| 6415 | // |
| 6416 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6417 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6418 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6419 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6420 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 6421 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6422 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6423 | PrevDecl = Record; |
| 6424 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6425 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6426 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6427 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6428 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6429 | PrevDecl, |
| 6430 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6431 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6432 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6433 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6434 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6435 | return TagD; |
| 6436 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6437 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6438 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6439 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6440 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6441 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6442 | // 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] | 6443 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6444 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6445 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6446 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 6447 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 6448 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6449 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 6450 | << Pattern; |
| 6451 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6452 | } else { |
| 6453 | if (InstantiateClass(NameLoc, Record, Def, |
| 6454 | getTemplateInstantiationArgs(Record), |
| 6455 | TSK)) |
| 6456 | return true; |
| 6457 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6458 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6459 | if (!RecordDef) |
| 6460 | return true; |
| 6461 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6462 | } |
| 6463 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6464 | // Instantiate all of the members of the class. |
| 6465 | InstantiateClassMembers(NameLoc, RecordDef, |
| 6466 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6467 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6468 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 6469 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 6470 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6471 | // FIXME: We don't have any representation for explicit instantiations of |
| 6472 | // member classes. Such a representation is not needed for compilation, but it |
| 6473 | // should be available for clients that want to see all of the declarations in |
| 6474 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6475 | return TagD; |
| 6476 | } |
| 6477 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6478 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 6479 | SourceLocation ExternLoc, |
| 6480 | SourceLocation TemplateLoc, |
| 6481 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6482 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6483 | // TODO: check if/when DNInfo should replace Name. |
| 6484 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6485 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6486 | if (!Name) { |
| 6487 | if (!D.isInvalidType()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6488 | Diag(D.getDeclSpec().getLocStart(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6489 | diag::err_explicit_instantiation_requires_name) |
| 6490 | << D.getDeclSpec().getSourceRange() |
| 6491 | << D.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6492 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6493 | return true; |
| 6494 | } |
| 6495 | |
| 6496 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 6497 | // we find one that is. |
| 6498 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 6499 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 6500 | S = S->getParent(); |
| 6501 | |
| 6502 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6503 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 6504 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6505 | if (R.isNull()) |
| 6506 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6507 | |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6508 | // C++ [dcl.stc]p1: |
| 6509 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 6510 | // instantiation (14.7.2) directive. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6511 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6512 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 6513 | << Name; |
| 6514 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6515 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 6516 | != DeclSpec::SCS_unspecified) { |
| 6517 | // Complain about then remove the storage class specifier. |
| 6518 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 6519 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6520 | |
| 6521 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6522 | } |
| 6523 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6524 | // C++0x [temp.explicit]p1: |
| 6525 | // [...] An explicit instantiation of a function template shall not use the |
| 6526 | // inline or constexpr specifiers. |
| 6527 | // Presumably, this also applies to member functions of class templates as |
| 6528 | // well. |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6529 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6530 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6531 | getLangOpts().CPlusPlus0x ? |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6532 | diag::err_explicit_instantiation_inline : |
| 6533 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | fe6f648 | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 6534 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
| 6535 | if (D.getDeclSpec().isConstexprSpecified()) |
| 6536 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 6537 | // not already specified. |
| 6538 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 6539 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6540 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6541 | // C++0x [temp.explicit]p2: |
| 6542 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6543 | // definition and an explicit instantiation declaration. An explicit |
| 6544 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6545 | TemplateSpecializationKind TSK |
| 6546 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6547 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6548 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6549 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6550 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6551 | |
| 6552 | if (!R->isFunctionType()) { |
| 6553 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6554 | // A [...] static data member of a class template can be explicitly |
| 6555 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6556 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6557 | if (Previous.isAmbiguous()) |
| 6558 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6559 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6560 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6561 | if (!Prev || !Prev->isStaticDataMember()) { |
| 6562 | // We expect to see a data data member here. |
| 6563 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 6564 | << Name; |
| 6565 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6566 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6567 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6568 | return true; |
| 6569 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6570 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6571 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 6572 | // FIXME: Check for explicit specialization? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6573 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6574 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 6575 | << Prev; |
| 6576 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 6577 | // FIXME: Can we provide a note showing where this was declared? |
| 6578 | return true; |
| 6579 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6580 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6581 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6582 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6583 | // or a static data member of a class template specialization, the name of |
| 6584 | // the class template specialization in the qualified-id for the member |
| 6585 | // name shall be a simple-template-id. |
| 6586 | // |
| 6587 | // C++98 has the same restriction, just worded differently. |
| 6588 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6589 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6590 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6591 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6592 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6593 | // Check the scope of this explicit instantiation. |
| 6594 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6595 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6596 | // Verify that it is okay to explicitly instantiate here. |
| 6597 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 6598 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6599 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6600 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6601 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6602 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6603 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6604 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6605 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6606 | return (Decl*) 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6607 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6608 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6609 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6610 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6611 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6612 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6613 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6614 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6615 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6616 | |
| 6617 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6618 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6619 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6620 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6621 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 6622 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6623 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 6624 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6625 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 6626 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6627 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6628 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6629 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 6630 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6631 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6632 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6633 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6634 | // A [...] function [...] can be explicitly instantiated from its template. |
| 6635 | // A member function [...] of a class template can be explicitly |
| 6636 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6637 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6638 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6639 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6640 | P != PEnd; ++P) { |
| 6641 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6642 | if (!HasExplicitTemplateArgs) { |
| 6643 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 6644 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 6645 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6646 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6647 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6648 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 6649 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6650 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6651 | } |
| 6652 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6653 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6654 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 6655 | if (!FunTmpl) |
| 6656 | continue; |
| 6657 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6658 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6659 | FunctionDecl *Specialization = 0; |
| 6660 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6661 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6662 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6663 | R, Specialization, Info)) { |
| 6664 | // FIXME: Keep track of almost-matches? |
| 6665 | (void)TDK; |
| 6666 | continue; |
| 6667 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6668 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6669 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6670 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6671 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6672 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6673 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6674 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6675 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6676 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 6677 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 6678 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6679 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6680 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6681 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6682 | |
| 6683 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 6684 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6685 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6686 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6687 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6688 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 6689 | << Specialization |
| 6690 | << (Specialization->getTemplateSpecializationKind() == |
| 6691 | TSK_ExplicitSpecialization); |
| 6692 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 6693 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6694 | } |
| 6695 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 6696 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6697 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 6698 | PrevDecl = Specialization; |
| 6699 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6700 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6701 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6702 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6703 | PrevDecl, |
| 6704 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6705 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6706 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6707 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6708 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6709 | // FIXME: We may still want to build some representation of this |
| 6710 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6711 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6712 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6713 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 6714 | |
| 6715 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 256fc4d | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 6716 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 6717 | if (Attr) |
| 6718 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6719 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6720 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6721 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6722 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6723 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6724 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6725 | // or a static data member of a class template specialization, the name of |
| 6726 | // the class template specialization in the qualified-id for the member |
| 6727 | // name shall be a simple-template-id. |
| 6728 | // |
| 6729 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6730 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6731 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6732 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6733 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6734 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6735 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6736 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6737 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6738 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6739 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6740 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6741 | D.getIdentifierLoc(), |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6742 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6743 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6744 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6745 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6746 | } |
| 6747 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6748 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6749 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 6750 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 6751 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 6752 | // This has to hold, because SS is expected to be defined. |
| 6753 | assert(Name && "Expected a name in a dependent tag"); |
| 6754 | |
| 6755 | NestedNameSpecifier *NNS |
| 6756 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6757 | if (!NNS) |
| 6758 | return true; |
| 6759 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6760 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 6761 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6762 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 6763 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6764 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6765 | return true; |
| 6766 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6767 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6768 | // Create the resulting type. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6769 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6770 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 6771 | |
| 6772 | // Create type-source location information for this type. |
| 6773 | TypeLocBuilder TLB; |
| 6774 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6775 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6776 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6777 | TL.setNameLoc(NameLoc); |
| 6778 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6779 | } |
| 6780 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6781 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6782 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6783 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6784 | SourceLocation IdLoc) { |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6785 | if (SS.isInvalid()) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6786 | return true; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6787 | |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6788 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6789 | Diag(TypenameLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6790 | getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6791 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6792 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6793 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6794 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6795 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6796 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 6797 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 6798 | if (T.isNull()) |
| 6799 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6800 | |
| 6801 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6802 | if (isa<DependentNameType>(T)) { |
| 6803 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6804 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6805 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6806 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6807 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6808 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6809 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6810 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6811 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6812 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6813 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6814 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6815 | } |
| 6816 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6817 | TypeResult |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6818 | Sema::ActOnTypenameType(Scope *S, |
| 6819 | SourceLocation TypenameLoc, |
| 6820 | const CXXScopeSpec &SS, |
| 6821 | SourceLocation TemplateKWLoc, |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6822 | TemplateTy TemplateIn, |
| 6823 | SourceLocation TemplateNameLoc, |
| 6824 | SourceLocation LAngleLoc, |
| 6825 | ASTTemplateArgsPtr TemplateArgsIn, |
| 6826 | SourceLocation RAngleLoc) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6827 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6828 | Diag(TypenameLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6829 | getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6830 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6831 | diag::ext_typename_outside_of_template) |
| 6832 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6833 | |
| 6834 | // Translate the parser's template argument list in our AST format. |
| 6835 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 6836 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 6837 | |
| 6838 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6839 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 6840 | // Construct a dependent template specialization type. |
| 6841 | assert(DTN && "dependent template has non-dependent name?"); |
| 6842 | assert(DTN->getQualifier() |
| 6843 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 6844 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 6845 | DTN->getQualifier(), |
| 6846 | DTN->getIdentifier(), |
| 6847 | TemplateArgs); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6848 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6849 | // Create source-location information for this type. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6850 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6851 | DependentTemplateSpecializationTypeLoc SpecTL |
| 6852 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6853 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 6854 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 66581d4 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 6855 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6856 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6857 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6858 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6859 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6860 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6861 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 6862 | } |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6863 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6864 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 6865 | if (T.isNull()) |
| 6866 | return true; |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6867 | |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6868 | // Provide source-location information for the template specialization type. |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6869 | TypeLocBuilder Builder; |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6870 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6871 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6872 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 6873 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6874 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6875 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6876 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6877 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 6878 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6879 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 6880 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6881 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6882 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6883 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6884 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 6885 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 6886 | } |
| 6887 | |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6888 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6889 | /// \brief Build the type that describes a C++ typename specifier, |
| 6890 | /// e.g., "typename T::type". |
| 6891 | QualType |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6892 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 6893 | SourceLocation KeywordLoc, |
| 6894 | NestedNameSpecifierLoc QualifierLoc, |
| 6895 | const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6896 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6897 | CXXScopeSpec SS; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6898 | SS.Adopt(QualifierLoc); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6899 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6900 | DeclContext *Ctx = computeDeclContext(SS); |
| 6901 | if (!Ctx) { |
| 6902 | // If the nested-name-specifier is dependent and couldn't be |
| 6903 | // resolved to a type, build a typename type. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6904 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 6905 | return Context.getDependentNameType(Keyword, |
| 6906 | QualifierLoc.getNestedNameSpecifier(), |
| 6907 | &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 6908 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6909 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6910 | // If the nested-name-specifier refers to the current instantiation, |
| 6911 | // the "typename" keyword itself is superfluous. In C++03, the |
| 6912 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 6913 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 6914 | // 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] | 6915 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6916 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 6917 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6918 | |
| 6919 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6920 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6921 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6922 | unsigned DiagID = 0; |
| 6923 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6924 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6925 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6926 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6927 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6928 | |
| 6929 | case LookupResult::FoundUnresolvedValue: { |
| 6930 | // We found a using declaration that is a value. Most likely, the using |
| 6931 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6932 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6933 | IILoc); |
| 6934 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 6935 | << Name << Ctx << FullRange; |
| 6936 | if (UnresolvedUsingValueDecl *Using |
| 6937 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6938 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6939 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 6940 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 6941 | } |
| 6942 | } |
| 6943 | // Fall through to create a dependent typename type, from which we can recover |
| 6944 | // better. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6945 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 6946 | case LookupResult::NotFoundInCurrentInstantiation: |
| 6947 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6948 | return Context.getDependentNameType(Keyword, |
| 6949 | QualifierLoc.getNestedNameSpecifier(), |
| 6950 | &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6951 | |
| 6952 | case LookupResult::Found: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6953 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6954 | // We found a type. Build an ElaboratedType, since the |
| 6955 | // typename-specifier was just sugar. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6956 | return Context.getElaboratedType(ETK_Typename, |
| 6957 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6958 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6959 | } |
| 6960 | |
| 6961 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6962 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6963 | break; |
| 6964 | |
| 6965 | case LookupResult::FoundOverloaded: |
| 6966 | DiagID = diag::err_typename_nested_not_type; |
| 6967 | Referenced = *Result.begin(); |
| 6968 | break; |
| 6969 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 6970 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6971 | return QualType(); |
| 6972 | } |
| 6973 | |
| 6974 | // If we get here, it's because name lookup did not find a |
| 6975 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6976 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6977 | IILoc); |
| 6978 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6979 | if (Referenced) |
| 6980 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 6981 | << Name; |
| 6982 | return QualType(); |
| 6983 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6984 | |
| 6985 | namespace { |
| 6986 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 6987 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6988 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6989 | SourceLocation Loc; |
| 6990 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6991 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6992 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 6993 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6994 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6995 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6996 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6997 | DeclarationName Entity) |
| 6998 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6999 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7000 | |
| 7001 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7002 | /// transformed. |
| 7003 | /// |
| 7004 | /// For the purposes of type reconstruction, a type has already been |
| 7005 | /// transformed if it is NULL or if it is not dependent. |
| 7006 | bool AlreadyTransformed(QualType T) { |
| 7007 | return T.isNull() || !T->isDependentType(); |
| 7008 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7009 | |
| 7010 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7011 | /// rebuilt. |
| 7012 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7013 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7014 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 7015 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7016 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 7017 | /// \brief Sets the "base" location and entity when that |
| 7018 | /// information is known based on another transformation. |
| 7019 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 7020 | this->Loc = Loc; |
| 7021 | this->Entity = Entity; |
| 7022 | } |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 7023 | |
| 7024 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 7025 | // Lambdas never need to be transformed. |
| 7026 | return E; |
| 7027 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7028 | }; |
| 7029 | } |
| 7030 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7031 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 7032 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7033 | /// 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] | 7034 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7035 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7036 | /// partial specialization thereof). This routine will rebuild that type now |
| 7037 | /// that we have entered the declarator's scope, which may produce different |
| 7038 | /// canonical types, e.g., |
| 7039 | /// |
| 7040 | /// \code |
| 7041 | /// template<typename T> |
| 7042 | /// struct X { |
| 7043 | /// typedef T* pointer; |
| 7044 | /// pointer data(); |
| 7045 | /// }; |
| 7046 | /// |
| 7047 | /// template<typename T> |
| 7048 | /// typename X<T>::pointer X<T>::data() { ... } |
| 7049 | /// \endcode |
| 7050 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 7051 | /// 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] | 7052 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 7053 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 7054 | /// 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] | 7055 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 7056 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7057 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 7058 | SourceLocation Loc, |
| 7059 | DeclarationName Name) { |
| 7060 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7061 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7062 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 7063 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 7064 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 7065 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7066 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7067 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7068 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 7069 | DeclarationName()); |
| 7070 | return Rebuilder.TransformExpr(E); |
| 7071 | } |
| 7072 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7073 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 7074 | if (SS.isInvalid()) |
| 7075 | return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 7076 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 7077 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 7078 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 7079 | DeclarationName()); |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 7080 | NestedNameSpecifierLoc Rebuilt |
| 7081 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 7082 | if (!Rebuilt) |
| 7083 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7084 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 7085 | SS.Adopt(Rebuilt); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 7086 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 7087 | } |
| 7088 | |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 7089 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 7090 | /// instantiation. |
| 7091 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 7092 | TemplateParameterList *Params) { |
| 7093 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 7094 | Decl *Param = Params->getParam(I); |
| 7095 | |
| 7096 | // There is nothing to rebuild in a type parameter. |
| 7097 | if (isa<TemplateTypeParmDecl>(Param)) |
| 7098 | continue; |
| 7099 | |
| 7100 | // Rebuild the template parameter list of a template template parameter. |
| 7101 | if (TemplateTemplateParmDecl *TTP |
| 7102 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 7103 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 7104 | TTP->getTemplateParameters())) |
| 7105 | return true; |
| 7106 | |
| 7107 | continue; |
| 7108 | } |
| 7109 | |
| 7110 | // Rebuild the type of a non-type template parameter. |
| 7111 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 7112 | TypeSourceInfo *NewTSI |
| 7113 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 7114 | NTTP->getLocation(), |
| 7115 | NTTP->getDeclName()); |
| 7116 | if (!NewTSI) |
| 7117 | return true; |
| 7118 | |
| 7119 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 7120 | NTTP->setTypeSourceInfo(NewTSI); |
| 7121 | NTTP->setType(NewTSI->getType()); |
| 7122 | } |
| 7123 | } |
| 7124 | |
| 7125 | return false; |
| 7126 | } |
| 7127 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7128 | /// \brief Produces a formatted string that describes the binding of |
| 7129 | /// template parameters to template arguments. |
| 7130 | std::string |
| 7131 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 7132 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 7133 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 7134 | } |
| 7135 | |
| 7136 | std::string |
| 7137 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 7138 | const TemplateArgument *Args, |
| 7139 | unsigned NumArgs) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 7140 | SmallString<128> Str; |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7141 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7142 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 7143 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7144 | return std::string(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7145 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7146 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 7147 | if (I >= NumArgs) |
| 7148 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7149 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7150 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7151 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7152 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7153 | Out << ", "; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7154 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7155 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7156 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7157 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7158 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7159 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7160 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7161 | Out << " = "; |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 7162 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7163 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 7164 | |
| 7165 | Out << ']'; |
| 7166 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 7167 | } |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 7168 | |
| 7169 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) { |
| 7170 | if (!FD) |
| 7171 | return; |
| 7172 | FD->setLateTemplateParsed(Flag); |
| 7173 | } |
| 7174 | |
| 7175 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 7176 | DeclContext *DC = CurContext; |
| 7177 | |
| 7178 | while (DC) { |
| 7179 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 7180 | const FunctionDecl *FD = RD->isLocalClass(); |
| 7181 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 7182 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 7183 | return false; |
| 7184 | |
| 7185 | DC = DC->getParent(); |
| 7186 | } |
| 7187 | return false; |
| 7188 | } |