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, |
| 47 | NamedDecl *Orig) { |
| 48 | NamedDecl *D = Orig->getUnderlyingDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 50 | if (isa<TemplateDecl>(D)) |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 51 | return Orig; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 53 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 54 | // C++ [temp.local]p1: |
| 55 | // Like normal (non-template) classes, class templates have an |
| 56 | // injected-class-name (Clause 9). The injected-class-name |
| 57 | // can be used with or without a template-argument-list. When |
| 58 | // it is used without a template-argument-list, it is |
| 59 | // equivalent to the injected-class-name followed by the |
| 60 | // template-parameters of the class template enclosed in |
| 61 | // <>. When it is used with a template-argument-list, it |
| 62 | // refers to the specified class template specialization, |
| 63 | // which could be the current specialization or another |
| 64 | // specialization. |
| 65 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 542b548 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 66 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 67 | if (Record->getDescribedClassTemplate()) |
| 68 | return Record->getDescribedClassTemplate(); |
| 69 | |
| 70 | if (ClassTemplateSpecializationDecl *Spec |
| 71 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 72 | return Spec->getSpecializedTemplate(); |
| 73 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 75 | return 0; |
| 76 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 81 | void Sema::FilterAcceptableTemplateNames(LookupResult &R) { |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 82 | // The set of class templates we've already seen. |
| 83 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 84 | LookupResult::Filter filter = R.makeFilter(); |
| 85 | while (filter.hasNext()) { |
| 86 | NamedDecl *Orig = filter.next(); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 87 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 88 | if (!Repl) |
| 89 | filter.erase(); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 90 | else if (Repl != Orig) { |
| 91 | |
| 92 | // C++ [temp.local]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 93 | // 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] | 94 | // 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] | 95 | // one base class). If all of the injected-class-names that are found |
| 96 | // refer to specializations of the same class template, and if the name |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 97 | // is used as a template-name, the reference refers to the class |
| 98 | // template itself and not a specialization thereof, and is not |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 99 | // ambiguous. |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 100 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
| 101 | if (!ClassTemplates.insert(ClassTmpl)) { |
| 102 | filter.erase(); |
| 103 | continue; |
| 104 | } |
John McCall | 8ba6691 | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 105 | |
| 106 | // FIXME: we promote access to public here as a workaround to |
| 107 | // the fact that LookupResult doesn't let us remember that we |
| 108 | // found this template through a particular injected class name, |
| 109 | // which means we end up doing nasty things to the invariants. |
| 110 | // Pretending that access is public is *much* safer. |
| 111 | filter.replace(Repl, AS_public); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 112 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 113 | } |
| 114 | filter.done(); |
| 115 | } |
| 116 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 117 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) { |
| 118 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
| 119 | if (isAcceptableTemplateName(Context, *I)) |
| 120 | return true; |
| 121 | |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 122 | return false; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 125 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 126 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 127 | bool hasTemplateKeyword, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 128 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 129 | ParsedType ObjectTypePtr, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 130 | bool EnteringContext, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 131 | TemplateTy &TemplateResult, |
| 132 | bool &MemberOfUnknownSpecialization) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 133 | assert(getLangOptions().CPlusPlus && "No template names in C!"); |
| 134 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 135 | DeclarationName TName; |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 136 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 137 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 138 | switch (Name.getKind()) { |
| 139 | case UnqualifiedId::IK_Identifier: |
| 140 | TName = DeclarationName(Name.Identifier); |
| 141 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 142 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 143 | case UnqualifiedId::IK_OperatorFunctionId: |
| 144 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 145 | Name.OperatorFunctionId.Operator); |
| 146 | break; |
| 147 | |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 148 | case UnqualifiedId::IK_LiteralOperatorId: |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 149 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 150 | break; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 151 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 152 | default: |
| 153 | return TNK_Non_template; |
| 154 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 156 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 158 | LookupResult R(*this, TName, Name.getSourceRange().getBegin(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 159 | LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 160 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 161 | MemberOfUnknownSpecialization); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 162 | if (R.empty()) return TNK_Non_template; |
| 163 | if (R.isAmbiguous()) { |
| 164 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 165 | R.suppressDiagnostics(); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 166 | |
| 167 | // FIXME: we might have ambiguous templates, in which case we |
| 168 | // should at least parse them properly! |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 169 | return TNK_Non_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 170 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 171 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 172 | TemplateName Template; |
| 173 | TemplateNameKind TemplateKind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 175 | unsigned ResultCount = R.end() - R.begin(); |
| 176 | if (ResultCount > 1) { |
| 177 | // We assume that we'll preserve the qualifier from a function |
| 178 | // template name in other ways. |
| 179 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 180 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 181 | |
| 182 | // We'll do this lookup again later. |
| 183 | R.suppressDiagnostics(); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 184 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 185 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 186 | |
| 187 | if (SS.isSet() && !SS.isInvalid()) { |
| 188 | NestedNameSpecifier *Qualifier |
| 189 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 190 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 191 | hasTemplateKeyword, TD); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 192 | } else { |
| 193 | Template = TemplateName(TD); |
| 194 | } |
| 195 | |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 196 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 197 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 198 | |
| 199 | // We'll do this lookup again later. |
| 200 | R.suppressDiagnostics(); |
| 201 | } else { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 202 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
| 203 | isa<TypeAliasTemplateDecl>(TD)); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 204 | TemplateKind = TNK_Type_template; |
| 205 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 206 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 207 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 208 | TemplateResult = TemplateTy::make(Template); |
| 209 | return TemplateKind; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 210 | } |
| 211 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 212 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 213 | SourceLocation IILoc, |
| 214 | Scope *S, |
| 215 | const CXXScopeSpec *SS, |
| 216 | TemplateTy &SuggestedTemplate, |
| 217 | TemplateNameKind &SuggestedKind) { |
| 218 | // We can't recover unless there's a dependent scope specifier preceding the |
| 219 | // template name. |
Douglas Gregor | d5ab9b0 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 220 | // FIXME: Typo correction? |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 221 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 222 | computeDeclContext(*SS)) |
| 223 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 224 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 225 | // The code is missing a 'template' keyword prior to the dependent template |
| 226 | // name. |
| 227 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 228 | Diag(IILoc, diag::err_template_kw_missing) |
| 229 | << Qualifier << II.getName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 230 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 231 | SuggestedTemplate |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 232 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 233 | SuggestedKind = TNK_Dependent_template_name; |
| 234 | return true; |
| 235 | } |
| 236 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 237 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 238 | Scope *S, CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 239 | QualType ObjectType, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 240 | bool EnteringContext, |
| 241 | bool &MemberOfUnknownSpecialization) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 242 | // Determine where to perform name lookup |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 243 | MemberOfUnknownSpecialization = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 244 | DeclContext *LookupCtx = 0; |
| 245 | bool isDependent = false; |
| 246 | if (!ObjectType.isNull()) { |
| 247 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 248 | // x->B::f, and we are looking into the type of the object. |
| 249 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 250 | LookupCtx = computeDeclContext(ObjectType); |
| 251 | isDependent = ObjectType->isDependentType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 252 | assert((isDependent || !ObjectType->isIncompleteType()) && |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 253 | "Caller should have completed object type"); |
Douglas Gregor | 1d7049a | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 254 | |
| 255 | // Template names cannot appear inside an Objective-C class or object type. |
| 256 | if (ObjectType->isObjCObjectOrInterfaceType()) { |
| 257 | Found.clear(); |
| 258 | return; |
| 259 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 260 | } else if (SS.isSet()) { |
| 261 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 262 | // so long into the context associated with the prior nested-name-specifier. |
| 263 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 264 | isDependent = isDependentScopeSpecifier(SS); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 265 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 266 | // The declaration context must be complete. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 267 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 268 | return; |
| 269 | } |
| 270 | |
| 271 | bool ObjectTypeSearchedInScope = false; |
| 272 | if (LookupCtx) { |
| 273 | // Perform "qualified" name lookup into the declaration context we |
| 274 | // computed, which is either the type of the base of a member access |
| 275 | // expression or the declaration context associated with a prior |
| 276 | // nested-name-specifier. |
| 277 | LookupQualifiedName(Found, LookupCtx); |
| 278 | |
| 279 | if (!ObjectType.isNull() && Found.empty()) { |
| 280 | // C++ [basic.lookup.classref]p1: |
| 281 | // In a class member access expression (5.2.5), if the . or -> token is |
| 282 | // immediately followed by an identifier followed by a <, the |
| 283 | // identifier must be looked up to determine whether the < is the |
| 284 | // beginning of a template argument list (14.2) or a less-than operator. |
| 285 | // The identifier is first looked up in the class of the object |
| 286 | // expression. If the identifier is not found, it is then looked up in |
| 287 | // the context of the entire postfix-expression and shall name a class |
| 288 | // or function template. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 289 | if (S) LookupName(Found, S); |
| 290 | ObjectTypeSearchedInScope = true; |
| 291 | } |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 292 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 293 | // We cannot look into a dependent object type or nested nme |
| 294 | // specifier. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 295 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 296 | return; |
| 297 | } else { |
| 298 | // Perform unqualified name lookup in the current scope. |
| 299 | LookupName(Found, S); |
| 300 | } |
| 301 | |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 302 | if (Found.empty() && !isDependent) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 303 | // If we did not find any names, attempt to correct any typos. |
| 304 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 305 | Found.clear(); |
Kaelyn Uhrain | f8ec8c9 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 306 | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
| 307 | CorrectionCandidateCallback FilterCCC; |
| 308 | FilterCCC.WantTypeSpecifiers = false; |
| 309 | FilterCCC.WantExpressionKeywords = false; |
| 310 | FilterCCC.WantRemainingKeywords = false; |
| 311 | FilterCCC.WantCXXNamedCasts = true; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 312 | if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(), |
| 313 | Found.getLookupKind(), S, &SS, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 314 | FilterCCC, LookupCtx)) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 315 | Found.setLookupName(Corrected.getCorrection()); |
| 316 | if (Corrected.getCorrectionDecl()) |
| 317 | Found.addDecl(Corrected.getCorrectionDecl()); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 318 | FilterAcceptableTemplateNames(Found); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 319 | if (!Found.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 320 | std::string CorrectedStr(Corrected.getAsString(getLangOptions())); |
| 321 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions())); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 322 | if (LookupCtx) |
| 323 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 324 | << Name << LookupCtx << CorrectedQuotedStr << SS.getRange() |
| 325 | << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 326 | else |
| 327 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 328 | << Name << CorrectedQuotedStr |
| 329 | << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 330 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) |
| 331 | Diag(Template->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 332 | << CorrectedQuotedStr; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 333 | } |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 334 | } else { |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 335 | Found.setLookupName(Name); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 339 | FilterAcceptableTemplateNames(Found); |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 340 | if (Found.empty()) { |
| 341 | if (isDependent) |
| 342 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 343 | return; |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 344 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 345 | |
| 346 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
| 347 | // C++ [basic.lookup.classref]p1: |
| 348 | // [...] If the lookup in the class of the object expression finds a |
| 349 | // template, the name is also looked up in the context of the entire |
| 350 | // postfix-expression and [...] |
| 351 | // |
| 352 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 353 | LookupOrdinaryName); |
| 354 | LookupName(FoundOuter, S); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 355 | FilterAcceptableTemplateNames(FoundOuter); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 356 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 357 | if (FoundOuter.empty()) { |
| 358 | // - if the name is not found, the name found in the class of the |
| 359 | // object expression is used, otherwise |
Douglas Gregor | a6d1e76 | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 360 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 361 | FoundOuter.isAmbiguous()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 362 | // - if the name is found in the context of the entire |
| 363 | // postfix-expression and does not name a class template, the name |
| 364 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | a6d1e76 | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 365 | FoundOuter.clear(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 366 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 367 | // - if the name found is a class template, it must refer to the same |
| 368 | // entity as the one found in the class of the object expression, |
| 369 | // otherwise the program is ill-formed. |
| 370 | if (!Found.isSingleResult() || |
| 371 | Found.getFoundDecl()->getCanonicalDecl() |
| 372 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 373 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 21d07e4 | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 374 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 375 | << Found.getLookupName() |
| 376 | << ObjectType; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 377 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 378 | diag::note_ambig_member_ref_object_type) |
| 379 | << ObjectType; |
| 380 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 381 | diag::note_ambig_member_ref_scope); |
| 382 | |
| 383 | // Recover by taking the template that we found in the object |
| 384 | // expression's type. |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 390 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 391 | /// was just parsed. This is only possible with an explicit scope |
| 392 | /// specifier naming a dependent type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 393 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 394 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 395 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 396 | const DeclarationNameInfo &NameInfo, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 397 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 398 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 399 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 400 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 401 | if (!isAddressOfOperand && |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 402 | isa<CXXMethodDecl>(DC) && |
| 403 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 404 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 405 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 406 | // Since the 'this' expression is synthesized, we don't need to |
| 407 | // perform the double-lookup check. |
| 408 | NamedDecl *FirstQualifierInScope = 0; |
| 409 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 410 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 411 | /*This*/ 0, ThisType, |
| 412 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 413 | /*Op*/ SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 414 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 415 | TemplateKWLoc, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 416 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 417 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 418 | TemplateArgs)); |
| 419 | } |
| 420 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 421 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 422 | } |
| 423 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 424 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 425 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 426 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 427 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 428 | const TemplateArgumentListInfo *TemplateArgs) { |
| 429 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 430 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 431 | TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 432 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 433 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 436 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 437 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 438 | /// declaration at location Loc. Returns true to indicate that this is |
| 439 | /// an error, and false otherwise. |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 440 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 441 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 442 | |
| 443 | // Microsoft Visual C++ permits template parameters to be shadowed. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 444 | if (getLangOptions().MicrosoftExt) |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 445 | return; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 446 | |
| 447 | // C++ [temp.local]p4: |
| 448 | // A template-parameter shall not be redeclared within its |
| 449 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 451 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 452 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 453 | return; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 456 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 457 | /// the parameter D to reference the templated declaration and return a pointer |
| 458 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 459 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 460 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 461 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 462 | return Temp; |
| 463 | } |
| 464 | return 0; |
| 465 | } |
| 466 | |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 467 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 468 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 469 | assert(Kind == Template && |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 470 | "Only template template arguments can be pack expansions here"); |
| 471 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 472 | "Template template argument pack expansion without packs"); |
| 473 | ParsedTemplateArgument Result(*this); |
| 474 | Result.EllipsisLoc = EllipsisLoc; |
| 475 | return Result; |
| 476 | } |
| 477 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 478 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 479 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 480 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 481 | switch (Arg.getKind()) { |
| 482 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 483 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 484 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 485 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 486 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 487 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 488 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 489 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 490 | case ParsedTemplateArgument::NonType: { |
| 491 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 492 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 493 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 494 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 495 | case ParsedTemplateArgument::Template: { |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 496 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 497 | TemplateArgument TArg; |
| 498 | if (Arg.getEllipsisLoc().isValid()) |
| 499 | TArg = TemplateArgument(Template, llvm::Optional<unsigned int>()); |
| 500 | else |
| 501 | TArg = Template; |
| 502 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 503 | Arg.getScopeSpec().getWithLocInContext( |
| 504 | SemaRef.Context), |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 505 | Arg.getLocation(), |
| 506 | Arg.getEllipsisLoc()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 509 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 510 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 511 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 512 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 513 | /// \brief Translates template arguments as provided by the parser |
| 514 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 515 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 516 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 517 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 518 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 519 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 520 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 521 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 522 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 523 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 524 | /// the keyword "typename" was used to declare the type parameter |
| 525 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 526 | /// "class" or "typename" keyword. ParamName is the name of the |
| 527 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 528 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 529 | /// If the type parameter has a default argument, it will be added |
| 530 | /// later via ActOnTypeParameterDefault. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 531 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
| 532 | SourceLocation EllipsisLoc, |
| 533 | SourceLocation KeyLoc, |
| 534 | IdentifierInfo *ParamName, |
| 535 | SourceLocation ParamNameLoc, |
| 536 | unsigned Depth, unsigned Position, |
| 537 | SourceLocation EqualLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 538 | ParsedType DefaultArg) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | assert(S->isTemplateParamScope() && |
| 540 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 541 | bool Invalid = false; |
| 542 | |
| 543 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 544 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 545 | LookupOrdinaryName, |
| 546 | ForRedeclaration); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 547 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 548 | DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl); |
| 549 | PrevDecl = 0; |
| 550 | } |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 553 | SourceLocation Loc = ParamNameLoc; |
| 554 | if (!ParamName) |
| 555 | Loc = KeyLoc; |
| 556 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 557 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 558 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 559 | KeyLoc, Loc, Depth, Position, ParamName, |
| 560 | Typename, Ellipsis); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 561 | Param->setAccess(AS_public); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 562 | if (Invalid) |
| 563 | Param->setInvalidDecl(); |
| 564 | |
| 565 | if (ParamName) { |
| 566 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 567 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 568 | IdResolver.AddDecl(Param); |
| 569 | } |
| 570 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 571 | // C++0x [temp.param]p9: |
| 572 | // A default template-argument may be specified for any kind of |
| 573 | // template-parameter that is not a template parameter pack. |
| 574 | if (DefaultArg && Ellipsis) { |
| 575 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 576 | DefaultArg = ParsedType(); |
| 577 | } |
| 578 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 579 | // Handle the default argument, if provided. |
| 580 | if (DefaultArg) { |
| 581 | TypeSourceInfo *DefaultTInfo; |
| 582 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 583 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 584 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 585 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 586 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 587 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 588 | UPPC_DefaultArgument)) |
| 589 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 590 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 591 | // Check the template argument itself. |
| 592 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 593 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 594 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 595 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 596 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 597 | Param->setDefaultArgument(DefaultTInfo, false); |
| 598 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 599 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 600 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 603 | /// \brief Check that the type of a non-type template parameter is |
| 604 | /// well-formed. |
| 605 | /// |
| 606 | /// \returns the (possibly-promoted) parameter type if valid; |
| 607 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 609 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 610 | // We don't allow variably-modified types as the type of non-type template |
| 611 | // parameters. |
| 612 | if (T->isVariablyModifiedType()) { |
| 613 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 614 | << T; |
| 615 | return QualType(); |
| 616 | } |
| 617 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 618 | // C++ [temp.param]p4: |
| 619 | // |
| 620 | // A non-type template-parameter shall have one of the following |
| 621 | // (optionally cv-qualified) types: |
| 622 | // |
| 623 | // -- integral or enumeration type, |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 624 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 625 | // -- pointer to object or pointer to function, |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 626 | T->isPointerType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 628 | T->isReferenceType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 629 | // -- pointer to member, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 630 | T->isMemberPointerType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 631 | // -- std::nullptr_t. |
| 632 | T->isNullPtrType() || |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 633 | // If T is a dependent type, we can't do the check now, so we |
| 634 | // assume that it is well-formed. |
| 635 | T->isDependentType()) |
| 636 | return T; |
| 637 | // C++ [temp.param]p8: |
| 638 | // |
| 639 | // A non-type template-parameter of type "array of T" or |
| 640 | // "function returning T" is adjusted to be of type "pointer to |
| 641 | // T" or "pointer to function returning T", respectively. |
| 642 | else if (T->isArrayType()) |
| 643 | // FIXME: Keep the type prior to promotion? |
| 644 | return Context.getArrayDecayedType(T); |
| 645 | else if (T->isFunctionType()) |
| 646 | // FIXME: Keep the type prior to promotion? |
| 647 | return Context.getPointerType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 648 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 649 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 650 | << T; |
| 651 | |
| 652 | return QualType(); |
| 653 | } |
| 654 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 655 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 656 | unsigned Depth, |
| 657 | unsigned Position, |
| 658 | SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 659 | Expr *Default) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 660 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 661 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 662 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 663 | assert(S->isTemplateParamScope() && |
| 664 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 665 | bool Invalid = false; |
| 666 | |
| 667 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 668 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 669 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 670 | LookupOrdinaryName, |
| 671 | ForRedeclaration); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 672 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 673 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 674 | PrevDecl = 0; |
| 675 | } |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 678 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 679 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 680 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 681 | Invalid = true; |
| 682 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 683 | |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 684 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 685 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 686 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 687 | D.getSourceRange().getBegin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 688 | D.getIdentifierLoc(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 689 | Depth, Position, ParamName, T, |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 690 | IsParameterPack, TInfo); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 691 | Param->setAccess(AS_public); |
| 692 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 693 | if (Invalid) |
| 694 | Param->setInvalidDecl(); |
| 695 | |
| 696 | if (D.getIdentifier()) { |
| 697 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 698 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 699 | IdResolver.AddDecl(Param); |
| 700 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 701 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 702 | // C++0x [temp.param]p9: |
| 703 | // A default template-argument may be specified for any kind of |
| 704 | // template-parameter that is not a template parameter pack. |
| 705 | if (Default && IsParameterPack) { |
| 706 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 707 | Default = 0; |
| 708 | } |
| 709 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 710 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 711 | if (Default) { |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 712 | // Check for unexpanded parameter packs. |
| 713 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 714 | return Param; |
| 715 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 716 | TemplateArgument Converted; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 717 | ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
| 718 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 719 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 720 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 721 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 722 | Default = DefaultRes.take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 723 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 724 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 725 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 726 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 727 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 728 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 729 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 730 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 731 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 732 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 733 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 734 | SourceLocation TmpLoc, |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 735 | TemplateParameterList *Params, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 736 | SourceLocation EllipsisLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 737 | IdentifierInfo *Name, |
| 738 | SourceLocation NameLoc, |
| 739 | unsigned Depth, |
| 740 | unsigned Position, |
| 741 | SourceLocation EqualLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 742 | ParsedTemplateArgument Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 743 | assert(S->isTemplateParamScope() && |
| 744 | "Template template parameter not in template parameter scope!"); |
| 745 | |
| 746 | // Construct the parameter object. |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 747 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 748 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 749 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 750 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 751 | Depth, Position, IsParameterPack, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 752 | Name, Params); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 753 | Param->setAccess(AS_public); |
| 754 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 755 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 756 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 757 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 758 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 759 | IdResolver.AddDecl(Param); |
| 760 | } |
| 761 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 762 | if (Params->size() == 0) { |
| 763 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 764 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 765 | Param->setInvalidDecl(); |
| 766 | } |
| 767 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 768 | // C++0x [temp.param]p9: |
| 769 | // A default template-argument may be specified for any kind of |
| 770 | // template-parameter that is not a template parameter pack. |
| 771 | if (IsParameterPack && !Default.isInvalid()) { |
| 772 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 773 | Default = ParsedTemplateArgument(); |
| 774 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 775 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 776 | if (!Default.isInvalid()) { |
| 777 | // Check only that we have a template template argument. We don't want to |
| 778 | // try to check well-formedness now, because our template template parameter |
| 779 | // might have dependent types in its template parameters, which we wouldn't |
| 780 | // be able to match now. |
| 781 | // |
| 782 | // If none of the template template parameter's template arguments mention |
| 783 | // other template parameters, we could actually perform more checking here. |
| 784 | // However, it isn't worth doing. |
| 785 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 786 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 787 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 788 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 789 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 790 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 791 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 792 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 793 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 794 | DefaultArg.getArgument().getAsTemplate(), |
| 795 | UPPC_DefaultArgument)) |
| 796 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 797 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 798 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 799 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 800 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 801 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 804 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 805 | /// contains the template parameters in Params/NumParams. |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 806 | TemplateParameterList * |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 807 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 808 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 809 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 810 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 811 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 812 | SourceLocation RAngleLoc) { |
| 813 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 814 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 815 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 816 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 817 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 818 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 819 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 820 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 821 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 822 | if (SS.isSet()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 823 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 824 | } |
| 825 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 826 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 827 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 828 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 829 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 830 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 831 | TemplateParameterList *TemplateParams, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 832 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 833 | unsigned NumOuterTemplateParamLists, |
| 834 | TemplateParameterList** OuterTemplateParamLists) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 836 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 837 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 838 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 839 | |
| 840 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 841 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 842 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 843 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 844 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 845 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 846 | |
| 847 | // There is no such thing as an unnamed class template. |
| 848 | if (!Name) { |
| 849 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 850 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 854 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 855 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 856 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 857 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 858 | SemanticContext = computeDeclContext(SS, true); |
| 859 | if (!SemanticContext) { |
| 860 | // FIXME: Produce a reasonable diagnostic here |
| 861 | return true; |
| 862 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 864 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 865 | return true; |
| 866 | |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 867 | // If we're adding a template to a dependent context, we may need to |
| 868 | // rebuilding some of the types used within the template parameter list, |
| 869 | // now that we know what the current instantiation is. |
| 870 | if (SemanticContext->isDependentContext()) { |
| 871 | ContextRAII SavedContext(*this, SemanticContext); |
| 872 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 873 | Invalid = true; |
| 874 | } |
| 875 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 876 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 877 | } else { |
| 878 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 879 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 880 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 881 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 882 | if (Previous.isAmbiguous()) |
| 883 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 884 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 885 | NamedDecl *PrevDecl = 0; |
| 886 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 887 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 888 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 889 | // If there is a previous declaration with the same name, check |
| 890 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 891 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 892 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 893 | |
| 894 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 895 | // class template partial specialization, or class template specialization. |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 896 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 897 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 898 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 899 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 900 | PrevClassTemplate |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 901 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 902 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 903 | PrevClassTemplate |
| 904 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 905 | ->getSpecializedTemplate(); |
| 906 | } |
| 907 | } |
| 908 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 909 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 910 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 911 | // [...] When looking for a prior declaration of a class or a function |
| 912 | // 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] | 913 | // function is neither a qualified name nor a template-id, scopes outside |
| 914 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 915 | if (!SS.isSet()) { |
| 916 | DeclContext *OutermostContext = CurContext; |
| 917 | while (!OutermostContext->isFileContext()) |
| 918 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 919 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 920 | if (PrevDecl && |
| 921 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 922 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 923 | SemanticContext = PrevDecl->getDeclContext(); |
| 924 | } else { |
| 925 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 926 | // context we computed is the semantic context for our new |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 927 | // declaration. |
| 928 | PrevDecl = PrevClassTemplate = 0; |
| 929 | SemanticContext = OutermostContext; |
| 930 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 931 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 932 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 933 | if (CurContext->isDependentContext()) { |
| 934 | // If this is a dependent context, we don't want to link the friend |
| 935 | // class template to the template in scope, because that would perform |
| 936 | // checking of the template parameter lists that can't be performed |
| 937 | // until the outer context is instantiated. |
| 938 | PrevDecl = PrevClassTemplate = 0; |
| 939 | } |
| 940 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 941 | PrevDecl = PrevClassTemplate = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 942 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 943 | if (PrevClassTemplate) { |
| 944 | // Ensure that the template parameter lists are compatible. |
| 945 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 946 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 947 | /*Complain=*/true, |
| 948 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 949 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 950 | |
| 951 | // C++ [temp.class]p4: |
| 952 | // In a redeclaration, partial specialization, explicit |
| 953 | // specialization or explicit instantiation of a class template, |
| 954 | // the class-key shall agree in kind with the original class |
| 955 | // template declaration (7.1.5.3). |
| 956 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 957 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
| 958 | TUK == TUK_Definition, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 960 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 961 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 962 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 963 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 966 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 967 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 968 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 969 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 970 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 971 | // FIXME: Would it make sense to try to "forget" the previous |
| 972 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 973 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 974 | } |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 975 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 976 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 977 | // Maybe we will complain about the shadowed template parameter. |
| 978 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 979 | // Just pretend that we didn't see the previous declaration. |
| 980 | PrevDecl = 0; |
| 981 | } else if (PrevDecl) { |
| 982 | // C++ [temp]p5: |
| 983 | // A class template shall not have the same name as any other |
| 984 | // template, class, function, object, enumeration, enumerator, |
| 985 | // namespace, or type in the same scope (3.3), except as specified |
| 986 | // in (14.5.4). |
| 987 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 988 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 989 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 992 | // Check the template parameter list of this declaration, possibly |
| 993 | // merging in the template parameter list from the previous class |
| 994 | // template declaration. |
| 995 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 996 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 997 | (SS.isSet() && SemanticContext && |
Douglas Gregor | 461bf2e | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 998 | SemanticContext->isRecord() && |
| 999 | SemanticContext->isDependentContext()) |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1000 | ? TPC_ClassTemplateMember |
| 1001 | : TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1002 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1003 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1004 | if (SS.isSet()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1005 | // 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] | 1006 | // template out-of-line. |
| 1007 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
Douglas Gregor | ea9f54a | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1008 | !(TUK == TUK_Friend && CurContext->isDependentContext())) { |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1009 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 1010 | << Name << SemanticContext << SS.getRange(); |
Douglas Gregor | ea9f54a | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1011 | Invalid = true; |
| 1012 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1015 | CXXRecordDecl *NewClass = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1016 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1017 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1018 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 1019 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1020 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1021 | if (NumOuterTemplateParamLists > 0) |
| 1022 | NewClass->setTemplateParameterListsInfo(Context, |
| 1023 | NumOuterTemplateParamLists, |
| 1024 | OuterTemplateParamLists); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1025 | |
| 1026 | ClassTemplateDecl *NewTemplate |
| 1027 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1028 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1029 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1030 | NewClass->setDescribedClassTemplate(NewTemplate); |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1031 | |
Douglas Gregor | 2ccd89c | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1032 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1033 | NewTemplate->setModulePrivate(); |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1034 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1035 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1036 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1037 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1038 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1039 | (void)T; |
| 1040 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1041 | // 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] | 1042 | // class template, make a note of that. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1043 | if (PrevClassTemplate && |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1044 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1045 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1046 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1047 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1048 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1049 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1051 | // Set the lexical context of these templates |
| 1052 | NewClass->setLexicalDeclContext(CurContext); |
| 1053 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1054 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1055 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1056 | NewClass->startDefinition(); |
| 1057 | |
| 1058 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1059 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1060 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1061 | if (TUK != TUK_Friend) |
| 1062 | PushOnScopeChains(NewTemplate, S); |
| 1063 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1064 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1065 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1066 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1067 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1068 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1069 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1070 | PrevClassTemplate != NULL); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1071 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1072 | // Friend templates are visible in fairly strange ways. |
| 1073 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1074 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1075 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 1076 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1077 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1078 | /* AddToContext = */ false); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1079 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1080 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1081 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1082 | NewClass->getLocation(), |
| 1083 | NewTemplate, |
| 1084 | /*FIXME:*/NewClass->getLocation()); |
| 1085 | Friend->setAccess(AS_public); |
| 1086 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1087 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1088 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1089 | if (Invalid) { |
| 1090 | NewTemplate->setInvalidDecl(); |
| 1091 | NewClass->setInvalidDecl(); |
| 1092 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1093 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1096 | /// \brief Diagnose the presence of a default template argument on a |
| 1097 | /// template parameter, which is ill-formed in certain contexts. |
| 1098 | /// |
| 1099 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1100 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1101 | Sema::TemplateParamListContext TPC, |
| 1102 | SourceLocation ParamLoc, |
| 1103 | SourceRange DefArgRange) { |
| 1104 | switch (TPC) { |
| 1105 | case Sema::TPC_ClassTemplate: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1106 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1107 | return false; |
| 1108 | |
| 1109 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1110 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1111 | // C++ [temp.param]p9: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1112 | // A default template-argument shall not be specified in a |
| 1113 | // function template declaration or a function template |
| 1114 | // definition [...] |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1115 | // If a friend function template declaration specifies a default |
| 1116 | // template-argument, that declaration shall be a definition and shall be |
| 1117 | // the only declaration of the function template in the translation unit. |
| 1118 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1119 | S.Diag(ParamLoc, S.getLangOptions().CPlusPlus0x ? |
| 1120 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1121 | : diag::ext_template_parameter_default_in_function_template) |
| 1122 | << DefArgRange; |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1123 | return false; |
| 1124 | |
| 1125 | case Sema::TPC_ClassTemplateMember: |
| 1126 | // C++0x [temp.param]p9: |
| 1127 | // A default template-argument shall not be specified in the |
| 1128 | // template-parameter-lists of the definition of a member of a |
| 1129 | // class template that appears outside of the member's class. |
| 1130 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1131 | << DefArgRange; |
| 1132 | return true; |
| 1133 | |
| 1134 | case Sema::TPC_FriendFunctionTemplate: |
| 1135 | // C++ [temp.param]p9: |
| 1136 | // A default template-argument shall not be specified in a |
| 1137 | // friend template declaration. |
| 1138 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1139 | << DefArgRange; |
| 1140 | return true; |
| 1141 | |
| 1142 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1143 | // for friend function templates if there is only a single |
| 1144 | // declaration (and it is a definition). Strange! |
| 1145 | } |
| 1146 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1147 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1150 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1151 | /// of a template template parameter, recursively. |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1152 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1153 | TemplateTemplateParmDecl *TTP) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1154 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1155 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1156 | NamedDecl *P = Params->getParam(I); |
| 1157 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1158 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1159 | NTTP->getTypeSourceInfo(), |
| 1160 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1161 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1162 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1163 | continue; |
| 1164 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1165 | |
| 1166 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1167 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1168 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1169 | return true; |
| 1170 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1171 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1172 | return false; |
| 1173 | } |
| 1174 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1175 | /// \brief Checks the validity of a template parameter list, possibly |
| 1176 | /// considering the template parameter list from a previous |
| 1177 | /// declaration. |
| 1178 | /// |
| 1179 | /// If an "old" template parameter list is provided, it must be |
| 1180 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1181 | /// template parameter list. |
| 1182 | /// |
| 1183 | /// \param NewParams Template parameter list for a new template |
| 1184 | /// declaration. This template parameter list will be updated with any |
| 1185 | /// default arguments that are carried through from the previous |
| 1186 | /// template parameter list. |
| 1187 | /// |
| 1188 | /// \param OldParams If provided, template parameter list from a |
| 1189 | /// previous declaration of the same template. Default template |
| 1190 | /// arguments will be merged from the old template parameter list to |
| 1191 | /// the new template parameter list. |
| 1192 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1193 | /// \param TPC Describes the context in which we are checking the given |
| 1194 | /// template parameter list. |
| 1195 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1196 | /// \returns true if an error occurred, false otherwise. |
| 1197 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1198 | TemplateParameterList *OldParams, |
| 1199 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1200 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1201 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1202 | // C++ [temp.param]p10: |
| 1203 | // The set of default template-arguments available for use with a |
| 1204 | // template declaration or definition is obtained by merging the |
| 1205 | // default arguments from the definition (if in scope) and all |
| 1206 | // declarations in scope in the same way default function |
| 1207 | // arguments are (8.3.6). |
| 1208 | bool SawDefaultArgument = false; |
| 1209 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1210 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1211 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1212 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1213 | if (OldParams) |
| 1214 | OldParam = OldParams->begin(); |
| 1215 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1216 | bool RemoveDefaultArguments = false; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1217 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1218 | NewParamEnd = NewParams->end(); |
| 1219 | NewParam != NewParamEnd; ++NewParam) { |
| 1220 | // Variables used to diagnose redundant default arguments |
| 1221 | bool RedundantDefaultArg = false; |
| 1222 | SourceLocation OldDefaultLoc; |
| 1223 | SourceLocation NewDefaultLoc; |
| 1224 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1225 | // Variable used to diagnose missing default arguments |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1226 | bool MissingDefaultArg = false; |
| 1227 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1228 | // Variable used to diagnose non-final parameter packs |
| 1229 | bool SawParameterPack = false; |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1230 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1231 | if (TemplateTypeParmDecl *NewTypeParm |
| 1232 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1233 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1234 | if (NewTypeParm->hasDefaultArgument() && |
| 1235 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1236 | NewTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1237 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1238 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1239 | NewTypeParm->removeDefaultArgument(); |
| 1240 | |
| 1241 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1242 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1243 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1244 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1245 | if (NewTypeParm->isParameterPack()) { |
| 1246 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1247 | "Parameter packs can't have a default argument!"); |
| 1248 | SawParameterPack = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1250 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1251 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1252 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1253 | SawDefaultArgument = true; |
| 1254 | RedundantDefaultArg = true; |
| 1255 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1256 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1257 | // Merge the default argument from the old declaration to the |
| 1258 | // new declaration. |
| 1259 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1260 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1261 | true); |
| 1262 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1263 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1264 | SawDefaultArgument = true; |
| 1265 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1266 | } else if (SawDefaultArgument) |
| 1267 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1268 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1269 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1270 | // Check for unexpanded parameter packs. |
| 1271 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1272 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1273 | UPPC_NonTypeTemplateParameterType)) { |
| 1274 | Invalid = true; |
| 1275 | continue; |
| 1276 | } |
| 1277 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1278 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1279 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1280 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1281 | NewNonTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1282 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1283 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1286 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1287 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1288 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1289 | if (NewNonTypeParm->isParameterPack()) { |
| 1290 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1291 | "Parameter packs can't have a default argument!"); |
| 1292 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1293 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1294 | NewNonTypeParm->hasDefaultArgument()) { |
| 1295 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1296 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1297 | SawDefaultArgument = true; |
| 1298 | RedundantDefaultArg = true; |
| 1299 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1300 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1301 | // Merge the default argument from the old declaration to the |
| 1302 | // new declaration. |
| 1303 | SawDefaultArgument = true; |
| 1304 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1305 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1306 | // parameter. |
| 1307 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1308 | OldNonTypeParm->getDefaultArgument(), |
| 1309 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1310 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1311 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1312 | SawDefaultArgument = true; |
| 1313 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1314 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1316 | } else { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1317 | TemplateTemplateParmDecl *NewTemplateParm |
| 1318 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1319 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1320 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1321 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1322 | Invalid = true; |
| 1323 | continue; |
| 1324 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1325 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1326 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1327 | if (NewTemplateParm->hasDefaultArgument() && |
| 1328 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1329 | NewTemplateParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1330 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1331 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1332 | |
| 1333 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1334 | TemplateTemplateParmDecl *OldTemplateParm |
| 1335 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1336 | if (NewTemplateParm->isParameterPack()) { |
| 1337 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1338 | "Parameter packs can't have a default argument!"); |
| 1339 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1340 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1341 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1342 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1343 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1344 | SawDefaultArgument = true; |
| 1345 | RedundantDefaultArg = true; |
| 1346 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1347 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1348 | // Merge the default argument from the old declaration to the |
| 1349 | // new declaration. |
| 1350 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1351 | // FIXME: We need to create a new kind of "default argument" expression |
| 1352 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1353 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1354 | OldTemplateParm->getDefaultArgument(), |
| 1355 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1356 | PreviousDefaultArgLoc |
| 1357 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1358 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1359 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1360 | PreviousDefaultArgLoc |
| 1361 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1362 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1366 | // C++0x [temp.param]p11: |
| 1367 | // If a template parameter of a primary class template or alias template |
| 1368 | // is a template parameter pack, it shall be the last template parameter. |
| 1369 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
| 1370 | (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) { |
| 1371 | Diag((*NewParam)->getLocation(), |
| 1372 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1373 | Invalid = true; |
| 1374 | } |
| 1375 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1376 | if (RedundantDefaultArg) { |
| 1377 | // C++ [temp.param]p12: |
| 1378 | // A template-parameter shall not be given default arguments |
| 1379 | // by two different declarations in the same scope. |
| 1380 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1381 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1382 | Invalid = true; |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1383 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1384 | // C++ [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1385 | // If a template-parameter of a class template has a default |
| 1386 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1387 | // have a default template-argument supplied or be a template parameter |
| 1388 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1389 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1390 | diag::err_template_param_default_arg_missing); |
| 1391 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1392 | Invalid = true; |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1393 | RemoveDefaultArguments = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | // If we have an old template parameter list that we're merging |
| 1397 | // in, move on to the next parameter. |
| 1398 | if (OldParams) |
| 1399 | ++OldParam; |
| 1400 | } |
| 1401 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1402 | // We were missing some default arguments at the end of the list, so remove |
| 1403 | // all of the default arguments. |
| 1404 | if (RemoveDefaultArguments) { |
| 1405 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1406 | NewParamEnd = NewParams->end(); |
| 1407 | NewParam != NewParamEnd; ++NewParam) { |
| 1408 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1409 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1410 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1411 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1412 | NTTP->removeDefaultArgument(); |
| 1413 | else |
| 1414 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1415 | } |
| 1416 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1417 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1418 | return Invalid; |
| 1419 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1420 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1421 | namespace { |
| 1422 | |
| 1423 | /// A class which looks for a use of a certain level of template |
| 1424 | /// parameter. |
| 1425 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1426 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1427 | |
| 1428 | unsigned Depth; |
| 1429 | bool Match; |
| 1430 | |
| 1431 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1432 | NamedDecl *ND = Params->getParam(0); |
| 1433 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1434 | Depth = PD->getDepth(); |
| 1435 | } else if (NonTypeTemplateParmDecl *PD = |
| 1436 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1437 | Depth = PD->getDepth(); |
| 1438 | } else { |
| 1439 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | bool Matches(unsigned ParmDepth) { |
| 1444 | if (ParmDepth >= Depth) { |
| 1445 | Match = true; |
| 1446 | return true; |
| 1447 | } |
| 1448 | return false; |
| 1449 | } |
| 1450 | |
| 1451 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1452 | return !Matches(T->getDepth()); |
| 1453 | } |
| 1454 | |
| 1455 | bool TraverseTemplateName(TemplateName N) { |
| 1456 | if (TemplateTemplateParmDecl *PD = |
| 1457 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1458 | if (Matches(PD->getDepth())) return false; |
| 1459 | return super::TraverseTemplateName(N); |
| 1460 | } |
| 1461 | |
| 1462 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1463 | if (NonTypeTemplateParmDecl *PD = |
| 1464 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1465 | if (PD->getDepth() == Depth) { |
| 1466 | Match = true; |
| 1467 | return false; |
| 1468 | } |
| 1469 | } |
| 1470 | return super::VisitDeclRefExpr(E); |
| 1471 | } |
Douglas Gregor | 18c8339 | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1472 | |
| 1473 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1474 | return TraverseType(T->getInjectedSpecializationType()); |
| 1475 | } |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1476 | }; |
| 1477 | } |
| 1478 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1479 | /// Determines whether a given type depends on the given parameter |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1480 | /// list. |
| 1481 | static bool |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1482 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1483 | DependencyChecker Checker(Params); |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1484 | Checker.TraverseType(T); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1485 | return Checker.Match; |
| 1486 | } |
| 1487 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1488 | // Find the source range corresponding to the named type in the given |
| 1489 | // nested-name-specifier, if any. |
| 1490 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1491 | QualType T, |
| 1492 | const CXXScopeSpec &SS) { |
| 1493 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1494 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1495 | if (const Type *CurType = NNS->getAsType()) { |
| 1496 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1497 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1498 | } else |
| 1499 | break; |
| 1500 | |
| 1501 | NNSLoc = NNSLoc.getPrefix(); |
| 1502 | } |
| 1503 | |
| 1504 | return SourceRange(); |
| 1505 | } |
| 1506 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1507 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1508 | /// specifier, returning the template parameter list that applies to the |
| 1509 | /// name. |
| 1510 | /// |
| 1511 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1512 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1513 | /// |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1514 | /// \param DeclLoc The location of the declaration itself. |
| 1515 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1516 | /// \param SS the scope specifier that will be matched to the given template |
| 1517 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1518 | /// being declared. |
| 1519 | /// |
| 1520 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1521 | /// innermost template parameter lists. |
| 1522 | /// |
| 1523 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1524 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1525 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1526 | /// matching template parameters to scope specifiers in friend |
| 1527 | /// declarations. |
| 1528 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1529 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1530 | /// declared is an explicit specialization, false otherwise. |
| 1531 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1533 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1534 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1536 | /// 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] | 1537 | /// itself a template). |
| 1538 | TemplateParameterList * |
| 1539 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1540 | SourceLocation DeclLoc, |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1541 | const CXXScopeSpec &SS, |
| 1542 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1543 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1544 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1545 | bool &IsExplicitSpecialization, |
| 1546 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1547 | IsExplicitSpecialization = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1548 | Invalid = false; |
| 1549 | |
| 1550 | // The sequence of nested types to which we will match up the template |
| 1551 | // parameter lists. We first build this list by starting with the type named |
| 1552 | // 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] | 1553 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1554 | QualType T; |
Douglas Gregor | 714c992 | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1555 | if (SS.getScopeRep()) { |
| 1556 | if (CXXRecordDecl *Record |
| 1557 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1558 | T = Context.getTypeDeclType(Record); |
| 1559 | else |
| 1560 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1561 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1562 | |
| 1563 | // If we found an explicit specialization that prevents us from needing |
| 1564 | // 'template<>' headers, this will be set to the location of that |
| 1565 | // explicit specialization. |
| 1566 | SourceLocation ExplicitSpecLoc; |
| 1567 | |
| 1568 | while (!T.isNull()) { |
| 1569 | NestedTypes.push_back(T); |
| 1570 | |
| 1571 | // Retrieve the parent of a record type. |
| 1572 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1573 | // If this type is an explicit specialization, we're done. |
| 1574 | if (ClassTemplateSpecializationDecl *Spec |
| 1575 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1576 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1577 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1578 | ExplicitSpecLoc = Spec->getLocation(); |
| 1579 | break; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1580 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1581 | } else if (Record->getTemplateSpecializationKind() |
| 1582 | == TSK_ExplicitSpecialization) { |
| 1583 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1584 | break; |
| 1585 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1586 | |
| 1587 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1588 | T = Context.getTypeDeclType(Parent); |
| 1589 | else |
| 1590 | T = QualType(); |
| 1591 | continue; |
| 1592 | } |
| 1593 | |
| 1594 | if (const TemplateSpecializationType *TST |
| 1595 | = T->getAs<TemplateSpecializationType>()) { |
| 1596 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1597 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1598 | T = Context.getTypeDeclType(Parent); |
| 1599 | else |
| 1600 | T = QualType(); |
| 1601 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1602 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | // Look one step prior in a dependent template specialization type. |
| 1606 | if (const DependentTemplateSpecializationType *DependentTST |
| 1607 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1608 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1609 | T = QualType(NNS->getAsType(), 0); |
| 1610 | else |
| 1611 | T = QualType(); |
| 1612 | continue; |
| 1613 | } |
| 1614 | |
| 1615 | // Look one step prior in a dependent name type. |
| 1616 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1617 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1618 | T = QualType(NNS->getAsType(), 0); |
| 1619 | else |
| 1620 | T = QualType(); |
| 1621 | continue; |
| 1622 | } |
| 1623 | |
| 1624 | // Retrieve the parent of an enumeration type. |
| 1625 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1626 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1627 | // check here. |
| 1628 | EnumDecl *Enum = EnumT->getDecl(); |
| 1629 | |
| 1630 | // Get to the parent type. |
| 1631 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1632 | T = Context.getTypeDeclType(Parent); |
| 1633 | else |
| 1634 | T = QualType(); |
| 1635 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1636 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1637 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1638 | T = QualType(); |
| 1639 | } |
| 1640 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1641 | // to the innermost while checking template-parameter-lists. |
| 1642 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1643 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1644 | // C++0x [temp.expl.spec]p17: |
| 1645 | // A member or a member template may be nested within many |
| 1646 | // enclosing class templates. In an explicit specialization for |
| 1647 | // such a member, the member declaration shall be preceded by a |
| 1648 | // template<> for each enclosing class template that is |
| 1649 | // explicitly specialized. |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1650 | bool SawNonEmptyTemplateParameterList = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1651 | unsigned ParamIdx = 0; |
| 1652 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1653 | ++TypeIdx) { |
| 1654 | T = NestedTypes[TypeIdx]; |
| 1655 | |
| 1656 | // Whether we expect a 'template<>' header. |
| 1657 | bool NeedEmptyTemplateHeader = false; |
| 1658 | |
| 1659 | // Whether we expect a template header with parameters. |
| 1660 | bool NeedNonemptyTemplateHeader = false; |
| 1661 | |
| 1662 | // For a dependent type, the set of template parameters that we |
| 1663 | // expect to see. |
| 1664 | TemplateParameterList *ExpectedTemplateParams = 0; |
| 1665 | |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1666 | // C++0x [temp.expl.spec]p15: |
| 1667 | // A member or a member template may be nested within many enclosing |
| 1668 | // class templates. In an explicit specialization for such a member, the |
| 1669 | // member declaration shall be preceded by a template<> for each |
| 1670 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1671 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1672 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1673 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1674 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1675 | NeedNonemptyTemplateHeader = true; |
| 1676 | } else if (Record->isDependentType()) { |
| 1677 | if (Record->getDescribedClassTemplate()) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1678 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1679 | ->getTemplateParameters(); |
| 1680 | NeedNonemptyTemplateHeader = true; |
| 1681 | } |
| 1682 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1683 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1684 | // C++0x [temp.expl.spec]p4: |
| 1685 | // Members of an explicitly specialized class template are defined |
| 1686 | // in the same manner as members of normal classes, and not using |
| 1687 | // the template<> syntax. |
| 1688 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1689 | NeedEmptyTemplateHeader = true; |
| 1690 | else |
Douglas Gregor | 95ea450 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1691 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1692 | } else if (Record->getTemplateSpecializationKind()) { |
| 1693 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1694 | != TSK_ExplicitSpecialization && |
| 1695 | TypeIdx == NumTypes - 1) |
| 1696 | IsExplicitSpecialization = true; |
| 1697 | |
| 1698 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1699 | } |
| 1700 | } else if (const TemplateSpecializationType *TST |
| 1701 | = T->getAs<TemplateSpecializationType>()) { |
| 1702 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1703 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1704 | NeedNonemptyTemplateHeader = true; |
| 1705 | } |
| 1706 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1707 | // FIXME: We actually could/should check the template arguments here |
| 1708 | // against the corresponding template parameter list. |
| 1709 | NeedNonemptyTemplateHeader = false; |
| 1710 | } |
| 1711 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1712 | // C++ [temp.expl.spec]p16: |
| 1713 | // In an explicit specialization declaration for a member of a class |
| 1714 | // template or a member template that ap- pears in namespace scope, the |
| 1715 | // member template and some of its enclosing class templates may remain |
| 1716 | // unspecialized, except that the declaration shall not explicitly |
| 1717 | // specialize a class member template if its en- closing class templates |
| 1718 | // are not explicitly specialized as well. |
| 1719 | if (ParamIdx < NumParamLists) { |
| 1720 | if (ParamLists[ParamIdx]->size() == 0) { |
| 1721 | if (SawNonEmptyTemplateParameterList) { |
| 1722 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1723 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1724 | Invalid = true; |
| 1725 | IsExplicitSpecialization = false; |
| 1726 | return 0; |
| 1727 | } |
| 1728 | } else |
| 1729 | SawNonEmptyTemplateParameterList = true; |
| 1730 | } |
| 1731 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1732 | if (NeedEmptyTemplateHeader) { |
| 1733 | // If we're on the last of the types, and we need a 'template<>' header |
| 1734 | // here, then it's an explicit specialization. |
| 1735 | if (TypeIdx == NumTypes - 1) |
| 1736 | IsExplicitSpecialization = true; |
| 1737 | |
| 1738 | if (ParamIdx < NumParamLists) { |
| 1739 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1740 | // The header has template parameters when it shouldn't. Complain. |
| 1741 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1742 | diag::err_template_param_list_matches_nontemplate) |
| 1743 | << T |
| 1744 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1745 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1746 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1747 | Invalid = true; |
| 1748 | return 0; |
| 1749 | } |
| 1750 | |
| 1751 | // Consume this template header. |
| 1752 | ++ParamIdx; |
| 1753 | continue; |
| 1754 | } |
| 1755 | |
| 1756 | if (!IsFriend) { |
| 1757 | // We don't have a template header, but we should. |
| 1758 | SourceLocation ExpectedTemplateLoc; |
| 1759 | if (NumParamLists > 0) |
| 1760 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1761 | else |
| 1762 | ExpectedTemplateLoc = DeclStartLoc; |
| 1763 | |
| 1764 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1765 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS) |
| 1766 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1767 | } |
| 1768 | |
| 1769 | continue; |
| 1770 | } |
| 1771 | |
| 1772 | if (NeedNonemptyTemplateHeader) { |
| 1773 | // In friend declarations we can have template-ids which don't |
| 1774 | // depend on the corresponding template parameter lists. But |
| 1775 | // assume that empty parameter lists are supposed to match this |
| 1776 | // template-id. |
| 1777 | if (IsFriend && T->isDependentType()) { |
| 1778 | if (ParamIdx < NumParamLists && |
| 1779 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
| 1780 | ExpectedTemplateParams = 0; |
| 1781 | else |
| 1782 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1783 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1784 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1785 | if (ParamIdx < NumParamLists) { |
| 1786 | // Check the template parameter list, if we can. |
| 1787 | if (ExpectedTemplateParams && |
| 1788 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1789 | ExpectedTemplateParams, |
| 1790 | true, TPL_TemplateMatch)) |
| 1791 | Invalid = true; |
| 1792 | |
| 1793 | if (!Invalid && |
| 1794 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1795 | TPC_ClassTemplateMember)) |
| 1796 | Invalid = true; |
| 1797 | |
| 1798 | ++ParamIdx; |
| 1799 | continue; |
| 1800 | } |
| 1801 | |
| 1802 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1803 | << T |
| 1804 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1805 | Invalid = true; |
| 1806 | continue; |
| 1807 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1808 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1809 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1810 | // If there were at least as many template-ids as there were template |
| 1811 | // parameter lists, then there are no template parameter lists remaining for |
| 1812 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1813 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1814 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1815 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1816 | // If there were too many template parameter lists, complain about that now. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1817 | if (ParamIdx < NumParamLists - 1) { |
| 1818 | bool HasAnyExplicitSpecHeader = false; |
| 1819 | bool AllExplicitSpecHeaders = true; |
| 1820 | for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) { |
| 1821 | if (ParamLists[I]->size() == 0) |
| 1822 | HasAnyExplicitSpecHeader = true; |
| 1823 | else |
| 1824 | AllExplicitSpecHeaders = false; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1825 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1826 | |
| 1827 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1828 | AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers |
| 1829 | : diag::err_template_spec_extra_headers) |
| 1830 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1831 | ParamLists[NumParamLists - 2]->getRAngleLoc()); |
| 1832 | |
| 1833 | // If there was a specialization somewhere, such that 'template<>' is |
| 1834 | // not required, and there were any 'template<>' headers, note where the |
| 1835 | // specialization occurred. |
| 1836 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1837 | Diag(ExplicitSpecLoc, |
| 1838 | diag::note_explicit_template_spec_does_not_need_header) |
| 1839 | << NestedTypes.back(); |
| 1840 | |
| 1841 | // We have a template parameter list with no corresponding scope, which |
| 1842 | // means that the resulting template declaration can't be instantiated |
| 1843 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1844 | if (!AllExplicitSpecHeaders) |
| 1845 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1846 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1847 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1848 | // C++ [temp.expl.spec]p16: |
| 1849 | // In an explicit specialization declaration for a member of a class |
| 1850 | // template or a member template that ap- pears in namespace scope, the |
| 1851 | // member template and some of its enclosing class templates may remain |
| 1852 | // unspecialized, except that the declaration shall not explicitly |
| 1853 | // specialize a class member template if its en- closing class templates |
| 1854 | // are not explicitly specialized as well. |
| 1855 | if (ParamLists[NumParamLists - 1]->size() == 0 && |
| 1856 | SawNonEmptyTemplateParameterList) { |
| 1857 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1858 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1859 | Invalid = true; |
| 1860 | IsExplicitSpecialization = false; |
| 1861 | return 0; |
| 1862 | } |
| 1863 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1864 | // Return the last template parameter list, which corresponds to the |
| 1865 | // entity being declared. |
| 1866 | return ParamLists[NumParamLists - 1]; |
| 1867 | } |
| 1868 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1869 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1870 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1871 | Diag(Template->getLocation(), diag::note_template_declared_here) |
| 1872 | << (isa<FunctionTemplateDecl>(Template)? 0 |
| 1873 | : isa<ClassTemplateDecl>(Template)? 1 |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1874 | : isa<TypeAliasTemplateDecl>(Template)? 2 |
| 1875 | : 3) |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1876 | << Template->getDeclName(); |
| 1877 | return; |
| 1878 | } |
| 1879 | |
| 1880 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1881 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1882 | IEnd = OST->end(); |
| 1883 | I != IEnd; ++I) |
| 1884 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1885 | << 0 << (*I)->getDeclName(); |
| 1886 | |
| 1887 | return; |
| 1888 | } |
| 1889 | } |
| 1890 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1891 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1892 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1893 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1894 | DependentTemplateName *DTN |
| 1895 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1896 | if (DTN && DTN->isIdentifier()) |
| 1897 | // When building a template-id where the template-name is dependent, |
| 1898 | // assume the template is a type template. Either our assumption is |
| 1899 | // correct, or the code is ill-formed and will be diagnosed when the |
| 1900 | // dependent name is substituted. |
| 1901 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 1902 | DTN->getQualifier(), |
| 1903 | DTN->getIdentifier(), |
| 1904 | TemplateArgs); |
| 1905 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1906 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1907 | if (!Template || isa<FunctionTemplateDecl>(Template)) { |
| 1908 | // We might have a substituted template template parameter pack. If so, |
| 1909 | // build a template specialization type for it. |
| 1910 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 1911 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1912 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1913 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 1914 | << Name; |
| 1915 | NoteAllFoundTemplates(Name); |
| 1916 | return QualType(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1917 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1918 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1919 | // Check that the template argument list is well-formed for this |
| 1920 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1921 | SmallVector<TemplateArgument, 4> Converted; |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1922 | bool ExpansionIntoFixedList = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1923 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1924 | false, Converted, &ExpansionIntoFixedList)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1925 | return QualType(); |
| 1926 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1927 | QualType CanonType; |
| 1928 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1929 | bool InstantiationDependent = false; |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1930 | TypeAliasTemplateDecl *AliasTemplate = 0; |
| 1931 | if (!ExpansionIntoFixedList && |
| 1932 | (AliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Template))) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1933 | // Find the canonical type for this type alias template specialization. |
| 1934 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 1935 | if (Pattern->isInvalidDecl()) |
| 1936 | return QualType(); |
| 1937 | |
| 1938 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1939 | Converted.data(), Converted.size()); |
| 1940 | |
| 1941 | // Only substitute for the innermost template argument list. |
| 1942 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 1804174 | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 1943 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | aff37b4 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 1944 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 1945 | for (unsigned I = 0; I < Depth; ++I) |
| 1946 | TemplateArgLists.addOuterTemplateArguments(0, 0); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1947 | |
| 1948 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
| 1949 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 1950 | TemplateArgLists, AliasTemplate->getLocation(), |
| 1951 | AliasTemplate->getDeclName()); |
| 1952 | if (CanonType.isNull()) |
| 1953 | return QualType(); |
| 1954 | } else if (Name.isDependent() || |
| 1955 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1956 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1957 | // This class template specialization is a dependent |
| 1958 | // type. Therefore, its canonical type is another class template |
| 1959 | // specialization type that contains all of the converted |
| 1960 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1961 | // A<T, T> have identical types when A is declared as: |
| 1962 | // |
| 1963 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1964 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1965 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1966 | Converted.data(), |
| 1967 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1968 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1969 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1970 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1971 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1972 | // build the canonical type and return that to us. |
| 1973 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1974 | |
| 1975 | // This might work out to be a current instantiation, in which |
| 1976 | // case the canonical type needs to be the InjectedClassNameType. |
| 1977 | // |
| 1978 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1979 | // changes to CurContext don't change the set of current |
| 1980 | // instantiations. |
| 1981 | if (isa<ClassTemplateDecl>(Template)) { |
| 1982 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1983 | // If we get out to a namespace, we're done. |
| 1984 | if (Ctx->isFileContext()) break; |
| 1985 | |
| 1986 | // If this isn't a record, keep looking. |
| 1987 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1988 | if (!Record) continue; |
| 1989 | |
| 1990 | // Look for one of the two cases with InjectedClassNameTypes |
| 1991 | // and check whether it's the same template. |
| 1992 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1993 | !Record->getDescribedClassTemplate()) |
| 1994 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1995 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1996 | // Fetch the injected class name type and check whether its |
| 1997 | // injected type is equal to the type we just built. |
| 1998 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1999 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2000 | ->getInjectedSpecializationType(); |
| 2001 | |
| 2002 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2003 | continue; |
| 2004 | |
| 2005 | // If so, the canonical type of this TST is the injected |
| 2006 | // class name type of the record we just found. |
| 2007 | assert(ICNT.isCanonical()); |
| 2008 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2009 | break; |
| 2010 | } |
| 2011 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2012 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2013 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2014 | // Find the class template specialization declaration that |
| 2015 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2016 | void *InsertPos = 0; |
| 2017 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2018 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2019 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2020 | if (!Decl) { |
| 2021 | // This is the first time we have referenced this class template |
| 2022 | // specialization. Create the canonical declaration and add it to |
| 2023 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2024 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2025 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2026 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 09d8212 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2027 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2028 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2029 | ClassTemplate, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2030 | Converted.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2031 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2032 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2033 | Decl->setLexicalDeclContext(CurContext); |
| 2034 | } |
| 2035 | |
| 2036 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2037 | assert(isa<RecordType>(CanonType) && |
| 2038 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2039 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2040 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2041 | // Build the fully-sugared type for this class template |
| 2042 | // specialization, which refers back to the class template |
| 2043 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2044 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2047 | TypeResult |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2048 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2049 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2050 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2051 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2052 | SourceLocation RAngleLoc, |
| 2053 | bool IsCtorOrDtorName) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2054 | if (SS.isInvalid()) |
| 2055 | return true; |
| 2056 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2057 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2058 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2059 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2060 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2061 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2062 | |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2063 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2064 | QualType T |
| 2065 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2066 | DTN->getQualifier(), |
| 2067 | DTN->getIdentifier(), |
| 2068 | TemplateArgs); |
| 2069 | // Build type-source information. |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2070 | TypeLocBuilder TLB; |
| 2071 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2072 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2073 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2074 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2075 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2076 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2077 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2078 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2079 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2080 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2081 | } |
| 2082 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2083 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2084 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2085 | |
| 2086 | if (Result.isNull()) |
| 2087 | return true; |
| 2088 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2089 | // Build type-source information. |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2090 | TypeLocBuilder TLB; |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2091 | TemplateSpecializationTypeLoc SpecTL |
| 2092 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2093 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2094 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2095 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2096 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2097 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2098 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2099 | |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2100 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2101 | // constructor or destructor name (in such a case, the scope specifier |
| 2102 | // will be attached to the enclosing Decl or Expr node). |
| 2103 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2104 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2105 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2106 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame^] | 2107 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2108 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2109 | } |
| 2110 | |
| 2111 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2112 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2113 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2114 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2115 | TypeSpecifierType TagSpec, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2116 | SourceLocation TagLoc, |
| 2117 | CXXScopeSpec &SS, |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2118 | SourceLocation TemplateKWLoc, |
| 2119 | TemplateTy TemplateD, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2120 | SourceLocation TemplateLoc, |
| 2121 | SourceLocation LAngleLoc, |
| 2122 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2123 | SourceLocation RAngleLoc) { |
| 2124 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
| 2125 | |
| 2126 | // Translate the parser's template argument list in our AST format. |
| 2127 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2128 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2129 | |
| 2130 | // Determine the tag kind |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2131 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2132 | ElaboratedTypeKeyword Keyword |
| 2133 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2134 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2135 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2136 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2137 | DTN->getQualifier(), |
| 2138 | DTN->getIdentifier(), |
| 2139 | TemplateArgs); |
| 2140 | |
| 2141 | // Build type-source information. |
| 2142 | TypeLocBuilder TLB; |
| 2143 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2144 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2145 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2146 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2147 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2148 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2149 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2150 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2151 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2152 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2153 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2154 | |
| 2155 | if (TypeAliasTemplateDecl *TAT = |
| 2156 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2157 | // C++0x [dcl.type.elab]p2: |
| 2158 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2159 | // resolves to an alias template specialization, the |
| 2160 | // elaborated-type-specifier is ill-formed. |
| 2161 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2162 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2163 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2164 | |
| 2165 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2166 | if (Result.isNull()) |
Matt Beaumont-Gay | 3a51d41 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2167 | return TypeResult(true); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2168 | |
| 2169 | // Check the tag kind |
| 2170 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2171 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2172 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2173 | IdentifierInfo *Id = D->getIdentifier(); |
| 2174 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2175 | |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2176 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 2177 | TagLoc, *Id)) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2178 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2179 | << Result |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2180 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2181 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2182 | } |
| 2183 | } |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2184 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2185 | // Provide source-location information for the template specialization. |
| 2186 | TypeLocBuilder TLB; |
| 2187 | TemplateSpecializationTypeLoc SpecTL |
| 2188 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2189 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2190 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2191 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2192 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2193 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2194 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2195 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2196 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2197 | // and tag keyword. |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2198 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2199 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame^] | 2200 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2201 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2202 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2205 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2206 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2207 | LookupResult &R, |
| 2208 | bool RequiresADL, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2209 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2210 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2211 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2212 | // 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] | 2213 | // though. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2214 | // foo<int> could identify a single function unambiguously |
| 2215 | // This approach does NOT work, since f<int>(1); |
| 2216 | // gets resolved prior to resorting to overload resolution |
| 2217 | // i.e., template<class T> void f(double); |
| 2218 | // vs template<class T, class U> void f(U); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2219 | |
| 2220 | // These should be filtered out by our callers. |
| 2221 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2222 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2223 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2224 | // We don't want lookup warnings at this point. |
| 2225 | R.suppressDiagnostics(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2226 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2227 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2228 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2229 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2230 | TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2231 | R.getLookupNameInfo(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2232 | RequiresADL, TemplateArgs, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2233 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2234 | |
| 2235 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2238 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2239 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2240 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2241 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2242 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2243 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2244 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2245 | DeclContext *DC; |
| 2246 | if (!(DC = computeDeclContext(SS, false)) || |
| 2247 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2248 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2249 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2250 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2251 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2252 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2253 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 2254 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2255 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2256 | if (R.isAmbiguous()) |
| 2257 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2258 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2259 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2260 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2261 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2262 | return ExprError(); |
| 2263 | } |
| 2264 | |
| 2265 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2266 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 2267 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 2268 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2269 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2270 | return ExprError(); |
| 2271 | } |
| 2272 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2273 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2276 | /// \brief Form a dependent template name. |
| 2277 | /// |
| 2278 | /// This action forms a dependent template name given the template |
| 2279 | /// name and its (presumably dependent) scope specifier. For |
| 2280 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2281 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2282 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2283 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2284 | CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2285 | SourceLocation TemplateKWLoc, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2286 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2287 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2288 | bool EnteringContext, |
| 2289 | TemplateTy &Result) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2290 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 2291 | Diag(TemplateKWLoc, |
| 2292 | getLangOptions().CPlusPlus0x ? |
| 2293 | diag::warn_cxx98_compat_template_outside_of_template : |
| 2294 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2295 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2296 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2297 | DeclContext *LookupCtx = 0; |
| 2298 | if (SS.isSet()) |
| 2299 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2300 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2301 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2302 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2303 | // C++0x [temp.names]p5: |
| 2304 | // If a name prefixed by the keyword template is not the name of |
| 2305 | // a template, the program is ill-formed. [Note: the keyword |
| 2306 | // template may not be applied to non-template members of class |
| 2307 | // templates. -end note ] [ Note: as is the case with the |
| 2308 | // typename prefix, the template prefix is allowed in cases |
| 2309 | // where it is not strictly necessary; i.e., when the |
| 2310 | // nested-name-specifier or the expression on the left of the -> |
| 2311 | // or . is not dependent on a template-parameter, or the use |
| 2312 | // does not appear in the scope of a template. -end note] |
| 2313 | // |
| 2314 | // Note: C++03 was more strict here, because it banned the use of |
| 2315 | // the "template" keyword prior to a template-name that was not a |
| 2316 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2317 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2318 | // 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] | 2319 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2320 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 2321 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2322 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2323 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2324 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | d078bd2 | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2325 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2326 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2327 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2328 | } else if (TNK == TNK_Non_template) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2329 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2330 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2331 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2332 | << Name.getSourceRange() |
| 2333 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2334 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2335 | } else { |
| 2336 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2337 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2338 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2341 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2342 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2343 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2344 | switch (Name.getKind()) { |
| 2345 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2346 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2347 | Name.Identifier)); |
| 2348 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2349 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2350 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2351 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2352 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2353 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2354 | |
| 2355 | case UnqualifiedId::IK_LiteralOperatorId: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2356 | llvm_unreachable( |
| 2357 | "We don't support these; Parse shouldn't have allowed propagation"); |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2358 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2359 | default: |
| 2360 | break; |
| 2361 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2362 | |
| 2363 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2364 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2365 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2366 | << Name.getSourceRange() |
| 2367 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2368 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2371 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2372 | const TemplateArgumentLoc &AL, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2373 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2374 | const TemplateArgument &Arg = AL.getArgument(); |
| 2375 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2376 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2377 | switch(Arg.getKind()) { |
| 2378 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2379 | // C++ [temp.arg.type]p1: |
| 2380 | // A template-argument for a template-parameter which is a |
| 2381 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2382 | break; |
| 2383 | case TemplateArgument::Template: { |
| 2384 | // We have a template type parameter but the template argument |
| 2385 | // is a template without any arguments. |
| 2386 | SourceRange SR = AL.getSourceRange(); |
| 2387 | TemplateName Name = Arg.getAsTemplate(); |
| 2388 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 2389 | << Name << SR; |
| 2390 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 2391 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2392 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2393 | return true; |
| 2394 | } |
| 2395 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2396 | // We have a template type parameter but the template argument |
| 2397 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 2398 | SourceRange SR = AL.getSourceRange(); |
| 2399 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2400 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2401 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2402 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2403 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2404 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2405 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2406 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2407 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2408 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2409 | // Add the converted template type argument. |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 2410 | QualType ArgType = Context.getCanonicalType(Arg.getAsType()); |
| 2411 | |
| 2412 | // Objective-C ARC: |
| 2413 | // If an explicitly-specified template argument type is a lifetime type |
| 2414 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
| 2415 | if (getLangOptions().ObjCAutoRefCount && |
| 2416 | ArgType->isObjCLifetimeType() && |
| 2417 | !ArgType.getObjCLifetime()) { |
| 2418 | Qualifiers Qs; |
| 2419 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 2420 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 2421 | } |
| 2422 | |
| 2423 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2424 | return false; |
| 2425 | } |
| 2426 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2427 | /// \brief Substitute template arguments into the default template argument for |
| 2428 | /// the given template type parameter. |
| 2429 | /// |
| 2430 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2431 | /// the substitution. |
| 2432 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2433 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2434 | /// for. |
| 2435 | /// |
| 2436 | /// \param TemplateLoc the location of the template name that started the |
| 2437 | /// template-id we are checking. |
| 2438 | /// |
| 2439 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2440 | /// terminates the template-id. |
| 2441 | /// |
| 2442 | /// \param Param the template template parameter whose default we are |
| 2443 | /// substituting into. |
| 2444 | /// |
| 2445 | /// \param Converted the list of template arguments provided for template |
| 2446 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2447 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2448 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2449 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2450 | TemplateDecl *Template, |
| 2451 | SourceLocation TemplateLoc, |
| 2452 | SourceLocation RAngleLoc, |
| 2453 | TemplateTypeParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2454 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2455 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2456 | |
| 2457 | // If the argument type is dependent, instantiate it now based |
| 2458 | // on the previously-computed template arguments. |
| 2459 | if (ArgType->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2460 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2461 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2462 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2463 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2464 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2465 | |
| 2466 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2467 | Template, Converted.data(), |
| 2468 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2469 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2470 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2471 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2472 | Param->getDefaultArgumentLoc(), |
| 2473 | Param->getDeclName()); |
| 2474 | } |
| 2475 | |
| 2476 | return ArgType; |
| 2477 | } |
| 2478 | |
| 2479 | /// \brief Substitute template arguments into the default template argument for |
| 2480 | /// the given non-type template parameter. |
| 2481 | /// |
| 2482 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2483 | /// the substitution. |
| 2484 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2485 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2486 | /// for. |
| 2487 | /// |
| 2488 | /// \param TemplateLoc the location of the template name that started the |
| 2489 | /// template-id we are checking. |
| 2490 | /// |
| 2491 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2492 | /// terminates the template-id. |
| 2493 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2494 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2495 | /// substituting into. |
| 2496 | /// |
| 2497 | /// \param Converted the list of template arguments provided for template |
| 2498 | /// parameters that precede \p Param in the template parameter list. |
| 2499 | /// |
| 2500 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2501 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2502 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2503 | TemplateDecl *Template, |
| 2504 | SourceLocation TemplateLoc, |
| 2505 | SourceLocation RAngleLoc, |
| 2506 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2507 | SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2508 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2509 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2510 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2511 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2512 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2513 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2514 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2515 | Template, Converted.data(), |
| 2516 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2517 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2518 | |
| 2519 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2520 | } |
| 2521 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2522 | /// \brief Substitute template arguments into the default template argument for |
| 2523 | /// the given template template parameter. |
| 2524 | /// |
| 2525 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2526 | /// the substitution. |
| 2527 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2528 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2529 | /// for. |
| 2530 | /// |
| 2531 | /// \param TemplateLoc the location of the template name that started the |
| 2532 | /// template-id we are checking. |
| 2533 | /// |
| 2534 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2535 | /// terminates the template-id. |
| 2536 | /// |
| 2537 | /// \param Param the template template parameter whose default we are |
| 2538 | /// substituting into. |
| 2539 | /// |
| 2540 | /// \param Converted the list of template arguments provided for template |
| 2541 | /// parameters that precede \p Param in the template parameter list. |
| 2542 | /// |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2543 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 2544 | /// source-location information) that precedes the template name. |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2545 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2546 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2547 | static TemplateName |
| 2548 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2549 | TemplateDecl *Template, |
| 2550 | SourceLocation TemplateLoc, |
| 2551 | SourceLocation RAngleLoc, |
| 2552 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2553 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2554 | NestedNameSpecifierLoc &QualifierLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2555 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2556 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2557 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2558 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2559 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2560 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2561 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2562 | Template, Converted.data(), |
| 2563 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2564 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2565 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2566 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2567 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2568 | if (QualifierLoc) { |
| 2569 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2570 | AllTemplateArgs); |
| 2571 | if (!QualifierLoc) |
| 2572 | return TemplateName(); |
| 2573 | } |
| 2574 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2575 | return SemaRef.SubstTemplateName(QualifierLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2576 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2577 | Param->getDefaultArgument().getTemplateNameLoc(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2578 | AllTemplateArgs); |
| 2579 | } |
| 2580 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2581 | /// \brief If the given template parameter has a default template |
| 2582 | /// argument, substitute into that default template argument and |
| 2583 | /// return the corresponding template argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2584 | TemplateArgumentLoc |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2585 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2586 | SourceLocation TemplateLoc, |
| 2587 | SourceLocation RAngleLoc, |
| 2588 | Decl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2589 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2590 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2591 | if (!TypeParm->hasDefaultArgument()) |
| 2592 | return TemplateArgumentLoc(); |
| 2593 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2594 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2595 | TemplateLoc, |
| 2596 | RAngleLoc, |
| 2597 | TypeParm, |
| 2598 | Converted); |
| 2599 | if (DI) |
| 2600 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2601 | |
| 2602 | return TemplateArgumentLoc(); |
| 2603 | } |
| 2604 | |
| 2605 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2606 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2607 | if (!NonTypeParm->hasDefaultArgument()) |
| 2608 | return TemplateArgumentLoc(); |
| 2609 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2610 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2611 | TemplateLoc, |
| 2612 | RAngleLoc, |
| 2613 | NonTypeParm, |
| 2614 | Converted); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2615 | if (Arg.isInvalid()) |
| 2616 | return TemplateArgumentLoc(); |
| 2617 | |
| 2618 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2619 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2620 | } |
| 2621 | |
| 2622 | TemplateTemplateParmDecl *TempTempParm |
| 2623 | = cast<TemplateTemplateParmDecl>(Param); |
| 2624 | if (!TempTempParm->hasDefaultArgument()) |
| 2625 | return TemplateArgumentLoc(); |
| 2626 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2627 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2628 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2629 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2630 | TemplateLoc, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2631 | RAngleLoc, |
| 2632 | TempTempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2633 | Converted, |
| 2634 | QualifierLoc); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2635 | if (TName.isNull()) |
| 2636 | return TemplateArgumentLoc(); |
| 2637 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2638 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2639 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2640 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2641 | } |
| 2642 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2643 | /// \brief Check that the given template argument corresponds to the given |
| 2644 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2645 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2646 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2647 | /// checked. |
| 2648 | /// |
| 2649 | /// \param Arg The template argument. |
| 2650 | /// |
| 2651 | /// \param Template The template in which the template argument resides. |
| 2652 | /// |
| 2653 | /// \param TemplateLoc The location of the template name for the template |
| 2654 | /// whose argument list we're matching. |
| 2655 | /// |
| 2656 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2657 | /// the template argument list. |
| 2658 | /// |
| 2659 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2660 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2661 | /// |
| 2662 | /// \param Converted The checked, converted argument will be added to the |
| 2663 | /// end of this small vector. |
| 2664 | /// |
| 2665 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2666 | /// explicitly written, deduced, etc. |
| 2667 | /// |
| 2668 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2669 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2670 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2671 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2672 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2673 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2674 | unsigned ArgumentPackIndex, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2675 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2676 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2677 | // Check template type parameters. |
| 2678 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2679 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2680 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2681 | // Check non-type template parameters. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2682 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2683 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2684 | // with the template arguments we've seen thus far. But if the |
| 2685 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2686 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2687 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2688 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2689 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2690 | if (NTTPType->isDependentType() && |
| 2691 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2692 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2693 | // Do substitution on the type of the non-type template parameter. |
| 2694 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2695 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2696 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2697 | |
| 2698 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2699 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2700 | NTTPType = SubstType(NTTPType, |
| 2701 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2702 | NTTP->getLocation(), |
| 2703 | NTTP->getDeclName()); |
| 2704 | // If that worked, check the non-type template parameter type |
| 2705 | // for validity. |
| 2706 | if (!NTTPType.isNull()) |
| 2707 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2708 | NTTP->getLocation()); |
| 2709 | if (NTTPType.isNull()) |
| 2710 | return true; |
| 2711 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2712 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2713 | switch (Arg.getArgument().getKind()) { |
| 2714 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2715 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2716 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2717 | case TemplateArgument::Expression: { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2718 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2719 | ExprResult Res = |
| 2720 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 2721 | Result, CTAK); |
| 2722 | if (Res.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2723 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2724 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2725 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2726 | break; |
| 2727 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2728 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2729 | case TemplateArgument::Declaration: |
| 2730 | case TemplateArgument::Integral: |
| 2731 | // We've already checked this template argument, so just copy |
| 2732 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2733 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2734 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2735 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2736 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2737 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2738 | // We were given a template template argument. It may not be ill-formed; |
| 2739 | // see below. |
| 2740 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2741 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2742 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2743 | // We have a template argument such as \c T::template X, which we |
| 2744 | // parsed as a template template argument. However, since we now |
| 2745 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2746 | // template name into an expression. |
| 2747 | |
| 2748 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2749 | Arg.getTemplateNameLoc()); |
| 2750 | |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2751 | CXXScopeSpec SS; |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2752 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2753 | // FIXME: the template-template arg was a DependentTemplateName, |
| 2754 | // so it was provided with a template keyword. However, its source |
| 2755 | // location is not stored in the template argument structure. |
| 2756 | SourceLocation TemplateKWLoc; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2757 | ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2758 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2759 | TemplateKWLoc, |
| 2760 | NameInfo, 0)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2761 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2762 | // If we parsed the template argument as a pack expansion, create a |
| 2763 | // pack expansion expression. |
| 2764 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2765 | E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc()); |
| 2766 | if (E.isInvalid()) |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2767 | return true; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2768 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2769 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2770 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2771 | E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result); |
| 2772 | if (E.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2773 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2774 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2775 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2776 | break; |
| 2777 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2778 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2779 | // We have a template argument that actually does refer to a class |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2780 | // template, alias template, or template template parameter, and |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2781 | // therefore cannot be a non-type template argument. |
| 2782 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2783 | << Arg.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2784 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2785 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2786 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2787 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2788 | case TemplateArgument::Type: { |
| 2789 | // We have a non-type template parameter but the template |
| 2790 | // argument is a type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2791 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2792 | // C++ [temp.arg]p2: |
| 2793 | // In a template-argument, an ambiguity between a type-id and |
| 2794 | // an expression is resolved to a type-id, regardless of the |
| 2795 | // form of the corresponding template-parameter. |
| 2796 | // |
| 2797 | // We warn specifically about this case, since it can be rather |
| 2798 | // confusing for users. |
| 2799 | QualType T = Arg.getArgument().getAsType(); |
| 2800 | SourceRange SR = Arg.getSourceRange(); |
| 2801 | if (T->isFunctionType()) |
| 2802 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2803 | else |
| 2804 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2805 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2806 | return true; |
| 2807 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2808 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2809 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2810 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2811 | } |
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 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2814 | } |
| 2815 | |
| 2816 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2817 | // Check template template parameters. |
| 2818 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
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 | // Substitute into the template parameter list of the template |
| 2821 | // template parameter, since previously-supplied template arguments |
| 2822 | // may appear within the template template parameter. |
| 2823 | { |
| 2824 | // Set up a template instantiation context. |
| 2825 | LocalInstantiationScope Scope(*this); |
| 2826 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2827 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2828 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2829 | |
| 2830 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2831 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2832 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2833 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2834 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2835 | if (!TempParm) |
| 2836 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2837 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2838 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2839 | switch (Arg.getArgument().getKind()) { |
| 2840 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2841 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2842 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2843 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2844 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2845 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2846 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2847 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2848 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2849 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2850 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2851 | case TemplateArgument::Expression: |
| 2852 | case TemplateArgument::Type: |
| 2853 | // We have a template template parameter but the template |
| 2854 | // argument does not refer to a template. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2855 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
| 2856 | << getLangOptions().CPlusPlus0x; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2857 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2858 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2859 | case TemplateArgument::Declaration: |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2860 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2861 | case TemplateArgument::Integral: |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2862 | llvm_unreachable("Integral argument with template template parameter"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2863 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2864 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2865 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2866 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2867 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2868 | return false; |
| 2869 | } |
| 2870 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2871 | /// \brief Diagnose an arity mismatch in the |
| 2872 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 2873 | SourceLocation TemplateLoc, |
| 2874 | TemplateArgumentListInfo &TemplateArgs) { |
| 2875 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2876 | unsigned NumParams = Params->size(); |
| 2877 | unsigned NumArgs = TemplateArgs.size(); |
| 2878 | |
| 2879 | SourceRange Range; |
| 2880 | if (NumArgs > NumParams) |
| 2881 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
| 2882 | TemplateArgs.getRAngleLoc()); |
| 2883 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2884 | << (NumArgs > NumParams) |
| 2885 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2886 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2887 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2888 | << Template << Range; |
| 2889 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2890 | << Params->getSourceRange(); |
| 2891 | return true; |
| 2892 | } |
| 2893 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2894 | /// \brief Check that the given template argument list is well-formed |
| 2895 | /// for specializing the given template. |
| 2896 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2897 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2898 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2899 | bool PartialTemplateArgs, |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2900 | SmallVectorImpl<TemplateArgument> &Converted, |
| 2901 | bool *ExpansionIntoFixedList) { |
| 2902 | if (ExpansionIntoFixedList) |
| 2903 | *ExpansionIntoFixedList = false; |
| 2904 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2905 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2906 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2907 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2908 | bool Invalid = false; |
| 2909 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2910 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2911 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2912 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2913 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2914 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2915 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2916 | // [...] The type and form of each template-argument specified in |
| 2917 | // a template-id shall match the type and form specified for the |
| 2918 | // corresponding parameter declared by the template in its |
| 2919 | // template-parameter-list. |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2920 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2921 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2922 | TemplateParameterList::iterator Param = Params->begin(), |
| 2923 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2924 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 2925 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2926 | bool SawPackExpansion = false; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2927 | while (Param != ParamEnd) { |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2928 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2929 | // If we have an expanded parameter pack, make sure we don't have too |
| 2930 | // many arguments. |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2931 | // FIXME: This really should fall out from the normal arity checking. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2932 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2933 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2934 | if (NTTP->isExpandedParameterPack() && |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2935 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 2936 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2937 | << true |
| 2938 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2939 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2940 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2941 | << Template; |
| 2942 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2943 | << Params->getSourceRange(); |
| 2944 | return true; |
| 2945 | } |
| 2946 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2947 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2948 | // Check the template argument we were given. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2949 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2950 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2951 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2952 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2953 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2954 | if ((*Param)->isTemplateParameterPack()) { |
| 2955 | // The template parameter was a template parameter pack, so take the |
| 2956 | // deduced argument and place it on the argument pack. Note that we |
| 2957 | // stay on the same template parameter so that we can deduce more |
| 2958 | // arguments. |
| 2959 | ArgumentPack.push_back(Converted.back()); |
| 2960 | Converted.pop_back(); |
| 2961 | } else { |
| 2962 | // Move to the next template parameter. |
| 2963 | ++Param; |
| 2964 | } |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2965 | |
| 2966 | // If this template argument is a pack expansion, record that fact |
| 2967 | // and break out; we can't actually check any more. |
| 2968 | if (TemplateArgs[ArgIdx].getArgument().isPackExpansion()) { |
| 2969 | SawPackExpansion = true; |
| 2970 | ++ArgIdx; |
| 2971 | break; |
| 2972 | } |
| 2973 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2974 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2975 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2976 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2977 | |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2978 | // If we're checking a partial template argument list, we're done. |
| 2979 | if (PartialTemplateArgs) { |
| 2980 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 2981 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 2982 | ArgumentPack.data(), |
| 2983 | ArgumentPack.size())); |
| 2984 | |
| 2985 | return Invalid; |
| 2986 | } |
| 2987 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2988 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2989 | // arguments, just break out now and we'll fill in the argument pack below. |
| 2990 | if ((*Param)->isTemplateParameterPack()) |
| 2991 | break; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2992 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2993 | // Check whether we have a default argument. |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2994 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2995 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2996 | // Retrieve the default template argument from the template |
| 2997 | // parameter. For each kind of template parameter, we substitute the |
| 2998 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2999 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3000 | // the default argument. |
| 3001 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3002 | if (!TTP->hasDefaultArgument()) |
| 3003 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3004 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3005 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3006 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3007 | Template, |
| 3008 | TemplateLoc, |
| 3009 | RAngleLoc, |
| 3010 | TTP, |
| 3011 | Converted); |
| 3012 | if (!ArgType) |
| 3013 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3014 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3015 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 3016 | ArgType); |
| 3017 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3018 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3019 | if (!NTTP->hasDefaultArgument()) |
| 3020 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3021 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3022 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3023 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3024 | TemplateLoc, |
| 3025 | RAngleLoc, |
| 3026 | NTTP, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3027 | Converted); |
| 3028 | if (E.isInvalid()) |
| 3029 | return true; |
| 3030 | |
| 3031 | Expr *Ex = E.takeAs<Expr>(); |
| 3032 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 3033 | } else { |
| 3034 | TemplateTemplateParmDecl *TempParm |
| 3035 | = cast<TemplateTemplateParmDecl>(*Param); |
| 3036 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3037 | if (!TempParm->hasDefaultArgument()) |
| 3038 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3039 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3040 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3041 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3042 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3043 | TemplateLoc, |
| 3044 | RAngleLoc, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3045 | TempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3046 | Converted, |
| 3047 | QualifierLoc); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3048 | if (Name.isNull()) |
| 3049 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3050 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3051 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3052 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3053 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3054 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3055 | // Introduce an instantiation record that describes where we are using |
| 3056 | // the default template argument. |
| 3057 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3058 | Converted.data(), Converted.size(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3059 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3060 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3061 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3062 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3063 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3064 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3065 | |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3066 | // Core issue 150 (assumed resolution): if this is a template template |
| 3067 | // parameter, keep track of the default template arguments from the |
| 3068 | // template definition. |
| 3069 | if (isTemplateTemplateParameter) |
| 3070 | TemplateArgs.addArgument(Arg); |
| 3071 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3072 | // Move to the next template parameter and argument. |
| 3073 | ++Param; |
| 3074 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3075 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3076 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3077 | // If we saw a pack expansion, then directly convert the remaining arguments, |
| 3078 | // because we don't know what parameters they'll match up with. |
| 3079 | if (SawPackExpansion) { |
| 3080 | bool AddToArgumentPack |
| 3081 | = Param != ParamEnd && (*Param)->isTemplateParameterPack(); |
| 3082 | while (ArgIdx < NumArgs) { |
| 3083 | if (AddToArgumentPack) |
| 3084 | ArgumentPack.push_back(TemplateArgs[ArgIdx].getArgument()); |
| 3085 | else |
| 3086 | Converted.push_back(TemplateArgs[ArgIdx].getArgument()); |
| 3087 | ++ArgIdx; |
| 3088 | } |
| 3089 | |
| 3090 | // Push the argument pack onto the list of converted arguments. |
| 3091 | if (AddToArgumentPack) { |
| 3092 | if (ArgumentPack.empty()) |
| 3093 | Converted.push_back(TemplateArgument(0, 0)); |
| 3094 | else { |
| 3095 | Converted.push_back( |
| 3096 | TemplateArgument::CreatePackCopy(Context, |
| 3097 | ArgumentPack.data(), |
| 3098 | ArgumentPack.size())); |
| 3099 | ArgumentPack.clear(); |
| 3100 | } |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3101 | } else if (ExpansionIntoFixedList) { |
| 3102 | // We have expanded a pack into a fixed list. |
| 3103 | *ExpansionIntoFixedList = true; |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
| 3106 | return Invalid; |
| 3107 | } |
| 3108 | |
| 3109 | // If we have any leftover arguments, then there were too many arguments. |
| 3110 | // Complain and fail. |
| 3111 | if (ArgIdx < NumArgs) |
| 3112 | return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs); |
| 3113 | |
| 3114 | // If we have an expanded parameter pack, make sure we don't have too |
| 3115 | // many arguments. |
| 3116 | // FIXME: This really should fall out from the normal arity checking. |
| 3117 | if (Param != ParamEnd) { |
| 3118 | if (NonTypeTemplateParmDecl *NTTP |
| 3119 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 3120 | if (NTTP->isExpandedParameterPack() && |
| 3121 | ArgumentPack.size() < NTTP->getNumExpansionTypes()) { |
| 3122 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3123 | << false |
| 3124 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3125 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3126 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3127 | << Template; |
| 3128 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3129 | << Params->getSourceRange(); |
| 3130 | return true; |
| 3131 | } |
| 3132 | } |
| 3133 | } |
| 3134 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3135 | // Form argument packs for each of the parameter packs remaining. |
| 3136 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3137 | // If we're checking a partial list of template arguments, don't fill |
| 3138 | // in arguments for non-template parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3139 | if ((*Param)->isTemplateParameterPack()) { |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 3140 | if (!HasParameterPack) |
| 3141 | return true; |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3142 | if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3143 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3144 | else { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3145 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3146 | ArgumentPack.data(), |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3147 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3148 | ArgumentPack.clear(); |
| 3149 | } |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3150 | } else if (!PartialTemplateArgs) |
| 3151 | return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3152 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3153 | ++Param; |
| 3154 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3155 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3156 | return Invalid; |
| 3157 | } |
| 3158 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3159 | namespace { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3160 | class UnnamedLocalNoLinkageFinder |
| 3161 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3162 | { |
| 3163 | Sema &S; |
| 3164 | SourceRange SR; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3165 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3166 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3167 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3168 | public: |
| 3169 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3170 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3171 | bool Visit(QualType T) { |
| 3172 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3173 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3174 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3175 | #define TYPE(Class, Parent) \ |
| 3176 | bool Visit##Class##Type(const Class##Type *); |
| 3177 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 3178 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3179 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 3180 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3181 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3182 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3183 | bool VisitTagDecl(const TagDecl *Tag); |
| 3184 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3185 | }; |
| 3186 | } |
| 3187 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3188 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3189 | return false; |
| 3190 | } |
| 3191 | |
| 3192 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3193 | return Visit(T->getElementType()); |
| 3194 | } |
| 3195 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3196 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3197 | return Visit(T->getPointeeType()); |
| 3198 | } |
| 3199 | |
| 3200 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3201 | const BlockPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3202 | return Visit(T->getPointeeType()); |
| 3203 | } |
| 3204 | |
| 3205 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3206 | const LValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3207 | return Visit(T->getPointeeType()); |
| 3208 | } |
| 3209 | |
| 3210 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3211 | const RValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3212 | return Visit(T->getPointeeType()); |
| 3213 | } |
| 3214 | |
| 3215 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3216 | const MemberPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3217 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3218 | } |
| 3219 | |
| 3220 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3221 | const ConstantArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3222 | return Visit(T->getElementType()); |
| 3223 | } |
| 3224 | |
| 3225 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3226 | const IncompleteArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3227 | return Visit(T->getElementType()); |
| 3228 | } |
| 3229 | |
| 3230 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3231 | const VariableArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3232 | return Visit(T->getElementType()); |
| 3233 | } |
| 3234 | |
| 3235 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3236 | const DependentSizedArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3237 | return Visit(T->getElementType()); |
| 3238 | } |
| 3239 | |
| 3240 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3241 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3242 | return Visit(T->getElementType()); |
| 3243 | } |
| 3244 | |
| 3245 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3246 | return Visit(T->getElementType()); |
| 3247 | } |
| 3248 | |
| 3249 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 3250 | return Visit(T->getElementType()); |
| 3251 | } |
| 3252 | |
| 3253 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 3254 | const FunctionProtoType* T) { |
| 3255 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3256 | AEnd = T->arg_type_end(); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3257 | A != AEnd; ++A) { |
| 3258 | if (Visit(*A)) |
| 3259 | return true; |
| 3260 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3261 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3262 | return Visit(T->getResultType()); |
| 3263 | } |
| 3264 | |
| 3265 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 3266 | const FunctionNoProtoType* T) { |
| 3267 | return Visit(T->getResultType()); |
| 3268 | } |
| 3269 | |
| 3270 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 3271 | const UnresolvedUsingType*) { |
| 3272 | return false; |
| 3273 | } |
| 3274 | |
| 3275 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 3276 | return false; |
| 3277 | } |
| 3278 | |
| 3279 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 3280 | return Visit(T->getUnderlyingType()); |
| 3281 | } |
| 3282 | |
| 3283 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 3284 | return false; |
| 3285 | } |
| 3286 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3287 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 3288 | const UnaryTransformType*) { |
| 3289 | return false; |
| 3290 | } |
| 3291 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3292 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 3293 | return Visit(T->getDeducedType()); |
| 3294 | } |
| 3295 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3296 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 3297 | return VisitTagDecl(T->getDecl()); |
| 3298 | } |
| 3299 | |
| 3300 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 3301 | return VisitTagDecl(T->getDecl()); |
| 3302 | } |
| 3303 | |
| 3304 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 3305 | const TemplateTypeParmType*) { |
| 3306 | return false; |
| 3307 | } |
| 3308 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3309 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 3310 | const SubstTemplateTypeParmPackType *) { |
| 3311 | return false; |
| 3312 | } |
| 3313 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3314 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 3315 | const TemplateSpecializationType*) { |
| 3316 | return false; |
| 3317 | } |
| 3318 | |
| 3319 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 3320 | const InjectedClassNameType* T) { |
| 3321 | return VisitTagDecl(T->getDecl()); |
| 3322 | } |
| 3323 | |
| 3324 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 3325 | const DependentNameType* T) { |
| 3326 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3327 | } |
| 3328 | |
| 3329 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 3330 | const DependentTemplateSpecializationType* T) { |
| 3331 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3332 | } |
| 3333 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3334 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 3335 | const PackExpansionType* T) { |
| 3336 | return Visit(T->getPattern()); |
| 3337 | } |
| 3338 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3339 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 3340 | return false; |
| 3341 | } |
| 3342 | |
| 3343 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 3344 | const ObjCInterfaceType *) { |
| 3345 | return false; |
| 3346 | } |
| 3347 | |
| 3348 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 3349 | const ObjCObjectPointerType *) { |
| 3350 | return false; |
| 3351 | } |
| 3352 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3353 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 3354 | return Visit(T->getValueType()); |
| 3355 | } |
| 3356 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3357 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 3358 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3359 | S.Diag(SR.getBegin(), |
| 3360 | S.getLangOptions().CPlusPlus0x ? |
| 3361 | diag::warn_cxx98_compat_template_arg_local_type : |
| 3362 | diag::ext_template_arg_local_type) |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3363 | << S.Context.getTypeDeclType(Tag) << SR; |
| 3364 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3365 | } |
| 3366 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3367 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3368 | S.Diag(SR.getBegin(), |
| 3369 | S.getLangOptions().CPlusPlus0x ? |
| 3370 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 3371 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3372 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 3373 | return true; |
| 3374 | } |
| 3375 | |
| 3376 | return false; |
| 3377 | } |
| 3378 | |
| 3379 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 3380 | NestedNameSpecifier *NNS) { |
| 3381 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 3382 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3383 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3384 | switch (NNS->getKind()) { |
| 3385 | case NestedNameSpecifier::Identifier: |
| 3386 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3387 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3388 | case NestedNameSpecifier::Global: |
| 3389 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3390 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3391 | case NestedNameSpecifier::TypeSpec: |
| 3392 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3393 | return Visit(QualType(NNS->getAsType(), 0)); |
| 3394 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3395 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
| 3398 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3399 | /// \brief Check a template argument against its corresponding |
| 3400 | /// template type parameter. |
| 3401 | /// |
| 3402 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 3403 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3404 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3405 | TypeSourceInfo *ArgInfo) { |
| 3406 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3407 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 3408 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 3409 | |
| 3410 | if (Arg->isVariablyModifiedType()) { |
| 3411 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3412 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3413 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3414 | } |
| 3415 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3416 | // C++03 [temp.arg.type]p2: |
| 3417 | // A local type, a type with no linkage, an unnamed type or a type |
| 3418 | // compounded from any of these types shall not be used as a |
| 3419 | // template-argument for a template type-parameter. |
| 3420 | // |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3421 | // 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] | 3422 | // a warning. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3423 | if (LangOpts.CPlusPlus0x ? |
| 3424 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 3425 | SR.getBegin()) != DiagnosticsEngine::Ignored || |
| 3426 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type, |
| 3427 | SR.getBegin()) != DiagnosticsEngine::Ignored : |
| 3428 | Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3429 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 3430 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 3431 | } |
| 3432 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3433 | return false; |
| 3434 | } |
| 3435 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3436 | /// \brief Checks whether the given template argument is the address |
| 3437 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3438 | static bool |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3439 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 3440 | NonTypeTemplateParmDecl *Param, |
| 3441 | QualType ParamType, |
| 3442 | Expr *ArgIn, |
| 3443 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3444 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3445 | Expr *Arg = ArgIn; |
| 3446 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3447 | |
| 3448 | // See through any implicit casts we added to fix the type. |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3449 | Arg = Arg->IgnoreImpCasts(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3450 | |
| 3451 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3452 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3453 | // A template-argument for a non-type, non-template |
| 3454 | // template-parameter shall be one of: [...] |
| 3455 | // |
| 3456 | // -- the address of an object or function with external |
| 3457 | // linkage, including function templates and function |
| 3458 | // template-ids but excluding non-static class members, |
| 3459 | // expressed as & id-expression where the & is optional if |
| 3460 | // the name refers to a function or array, or if the |
| 3461 | // corresponding template-parameter is a reference; or |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3462 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3463 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3464 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3465 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3466 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3467 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3468 | S.Diag(Arg->getSourceRange().getBegin(), |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3469 | S.getLangOptions().CPlusPlus0x ? |
| 3470 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3471 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3472 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3473 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3474 | } |
| 3475 | |
| 3476 | Arg = Parens->getSubExpr(); |
| 3477 | } |
| 3478 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3479 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3480 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3481 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3482 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3483 | bool AddressTaken = false; |
| 3484 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3485 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3486 | if (UnOp->getOpcode() == UO_AddrOf) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3487 | Arg = UnOp->getSubExpr(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3488 | AddressTaken = true; |
| 3489 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 3490 | } |
Francois Pichet | a343a41 | 2011-04-29 09:08:14 +0000 | [diff] [blame] | 3491 | } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3492 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3493 | if (S.getLangOptions().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3494 | Converted = TemplateArgument(ArgIn); |
| 3495 | return false; |
| 3496 | } |
| 3497 | |
| 3498 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3499 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3500 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3501 | |
| 3502 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3503 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3504 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 3505 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3506 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3507 | return true; |
| 3508 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3509 | |
| 3510 | // Stop checking the precise nature of the argument if it is value dependent, |
| 3511 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3512 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3513 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3514 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3515 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3516 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3517 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 3518 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3519 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3520 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3521 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3522 | return true; |
| 3523 | } |
| 3524 | |
| 3525 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3526 | |
| 3527 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3528 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 3529 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3530 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3531 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3532 | return true; |
| 3533 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3534 | |
| 3535 | // Cannot refer to non-static member functions |
| 3536 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3537 | if (!Method->isStatic()) { |
| 3538 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3539 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3540 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3541 | return true; |
| 3542 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3543 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3544 | // Functions must have external linkage. |
| 3545 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3546 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3547 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3548 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3549 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3550 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3551 | << true; |
| 3552 | return true; |
| 3553 | } |
| 3554 | |
| 3555 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3556 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3557 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3558 | // If the template parameter has pointer type, the function decays. |
| 3559 | if (ParamType->isPointerType() && !AddressTaken) |
| 3560 | ArgType = S.Context.getPointerType(Func->getType()); |
| 3561 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 3562 | // If we originally had an address-of operator, but the |
| 3563 | // parameter has reference type, complain and (if things look |
| 3564 | // like they will work) drop the address-of operator. |
| 3565 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 3566 | ParamType.getNonReferenceType())) { |
| 3567 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3568 | << ParamType; |
| 3569 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3570 | return true; |
| 3571 | } |
| 3572 | |
| 3573 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3574 | << ParamType |
| 3575 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3576 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3577 | |
| 3578 | ArgType = Func->getType(); |
| 3579 | } |
| 3580 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3581 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3582 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3583 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3584 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3585 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3586 | << true; |
| 3587 | return true; |
| 3588 | } |
| 3589 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3590 | // A value of reference type is not an object. |
| 3591 | if (Var->getType()->isReferenceType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3592 | S.Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3593 | diag::err_template_arg_reference_var) |
| 3594 | << Var->getType() << Arg->getSourceRange(); |
| 3595 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3596 | return true; |
| 3597 | } |
| 3598 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3599 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3600 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3601 | |
| 3602 | // If the template parameter has pointer type, we must have taken |
| 3603 | // the address of this object. |
| 3604 | if (ParamType->isReferenceType()) { |
| 3605 | if (AddressTaken) { |
| 3606 | // If we originally had an address-of operator, but the |
| 3607 | // parameter has reference type, complain and (if things look |
| 3608 | // like they will work) drop the address-of operator. |
| 3609 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3610 | ParamType.getNonReferenceType())) { |
| 3611 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3612 | << ParamType; |
| 3613 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3614 | return true; |
| 3615 | } |
| 3616 | |
| 3617 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3618 | << ParamType |
| 3619 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3620 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3621 | |
| 3622 | ArgType = Var->getType(); |
| 3623 | } |
| 3624 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3625 | if (Var->getType()->isArrayType()) { |
| 3626 | // Array-to-pointer decay. |
| 3627 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3628 | } else { |
| 3629 | // If the template parameter has pointer type but the address of |
| 3630 | // this object was not taken, complain and (possibly) recover by |
| 3631 | // taking the address of the entity. |
| 3632 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3633 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3634 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3635 | << ParamType; |
| 3636 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3637 | return true; |
| 3638 | } |
| 3639 | |
| 3640 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3641 | << ParamType |
| 3642 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3643 | |
| 3644 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3645 | } |
| 3646 | } |
| 3647 | } else { |
| 3648 | // We found something else, but we don't know specifically what it is. |
| 3649 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3650 | diag::err_template_arg_not_object_or_func) |
| 3651 | << Arg->getSourceRange(); |
| 3652 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3653 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3654 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3655 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3656 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3657 | if (ParamType->isPointerType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3658 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3659 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 3660 | ObjCLifetimeConversion)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3661 | // For pointer-to-object types, qualification conversions are |
| 3662 | // permitted. |
| 3663 | } else { |
| 3664 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3665 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3666 | // C++ [temp.arg.nontype]p5b3: |
| 3667 | // For a non-type template-parameter of type reference to |
| 3668 | // object, no conversions apply. The type referred to by the |
| 3669 | // reference may be more cv-qualified than the (otherwise |
| 3670 | // identical) type of the template- argument. The |
| 3671 | // template-parameter is bound directly to the |
| 3672 | // template-argument, which shall be an lvalue. |
| 3673 | |
| 3674 | // FIXME: Other qualifiers? |
| 3675 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3676 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3677 | |
| 3678 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 3679 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3680 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3681 | << ParamType << Arg->getType() |
| 3682 | << Arg->getSourceRange(); |
| 3683 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3684 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3685 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3686 | } |
| 3687 | } |
| 3688 | |
| 3689 | // At this point, the template argument refers to an object or |
| 3690 | // function with external linkage. We now need to check whether the |
| 3691 | // argument and parameter types are compatible. |
| 3692 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3693 | ParamType.getNonReferenceType())) { |
| 3694 | // We can't perform this conversion or binding. |
| 3695 | if (ParamType->isReferenceType()) |
| 3696 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3697 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3698 | else |
| 3699 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3700 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3701 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3702 | return true; |
| 3703 | } |
| 3704 | } |
| 3705 | |
| 3706 | // Create the template argument. |
| 3707 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3708 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3709 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
| 3712 | /// \brief Checks whether the given template argument is a pointer to |
| 3713 | /// member constant according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3714 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3715 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3716 | bool Invalid = false; |
| 3717 | |
| 3718 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3719 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3720 | Arg = Cast->getSubExpr(); |
| 3721 | |
| 3722 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3723 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3724 | // A template-argument for a non-type, non-template |
| 3725 | // template-parameter shall be one of: [...] |
| 3726 | // |
| 3727 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3728 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3729 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3730 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3731 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3732 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3733 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3734 | if (!Invalid && !ExtraParens) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3735 | Diag(Arg->getSourceRange().getBegin(), |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3736 | getLangOptions().CPlusPlus0x ? |
| 3737 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3738 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3739 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3740 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | Arg = Parens->getSubExpr(); |
| 3744 | } |
| 3745 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3746 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3747 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3748 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3749 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3750 | // A pointer-to-member constant written &Class::member. |
| 3751 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3752 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3753 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3754 | if (DRE && !DRE->getQualifier()) |
| 3755 | DRE = 0; |
| 3756 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3757 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3758 | // A constant of pointer-to-member type. |
| 3759 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3760 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3761 | if (VD->getType()->isMemberPointerType()) { |
| 3762 | if (isa<NonTypeTemplateParmDecl>(VD) || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3763 | (isa<VarDecl>(VD) && |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3764 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3765 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3766 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3767 | else |
| 3768 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3769 | return Invalid; |
| 3770 | } |
| 3771 | } |
| 3772 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3773 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3774 | DRE = 0; |
| 3775 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3776 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3777 | if (!DRE) |
| 3778 | return Diag(Arg->getSourceRange().getBegin(), |
| 3779 | diag::err_template_arg_not_pointer_to_member_form) |
| 3780 | << Arg->getSourceRange(); |
| 3781 | |
| 3782 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3783 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3784 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3785 | "Only non-static member pointers can make it here"); |
| 3786 | |
| 3787 | // Okay: this is the address of a non-static member, and therefore |
| 3788 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3789 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3790 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3791 | else |
| 3792 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3793 | return Invalid; |
| 3794 | } |
| 3795 | |
| 3796 | // We found something else, but we don't know specifically what it is. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3797 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3798 | diag::err_template_arg_not_pointer_to_member_form) |
| 3799 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3800 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3801 | diag::note_template_arg_refers_here); |
| 3802 | return true; |
| 3803 | } |
| 3804 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3805 | /// \brief Check a template argument against its corresponding |
| 3806 | /// non-type template parameter. |
| 3807 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3808 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3809 | /// If an error occurred, it returns ExprError(); otherwise, it |
| 3810 | /// returns the converted template argument. \p |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3811 | /// InstantiatedParamType is the type of the non-type template |
| 3812 | /// parameter after it has been instantiated. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3813 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 3814 | QualType InstantiatedParamType, Expr *Arg, |
| 3815 | TemplateArgument &Converted, |
| 3816 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3817 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3818 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3819 | // If either the parameter has a dependent type or the argument is |
| 3820 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3821 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3822 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3823 | Converted = TemplateArgument(Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3824 | return Owned(Arg); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3825 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3826 | |
| 3827 | // C++ [temp.arg.nontype]p5: |
| 3828 | // The following conversions are performed on each expression used |
| 3829 | // as a non-type template-argument. If a non-type |
| 3830 | // template-argument cannot be converted to the type of the |
| 3831 | // corresponding template-parameter then the program is |
| 3832 | // ill-formed. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3833 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3834 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 3835 | // C++11: |
| 3836 | // -- for a non-type template-parameter of integral or |
| 3837 | // enumeration type, conversions permitted in a converted |
| 3838 | // constant expression are applied. |
| 3839 | // |
| 3840 | // C++98: |
| 3841 | // -- for a non-type template-parameter of integral or |
| 3842 | // enumeration type, integral promotions (4.5) and integral |
| 3843 | // conversions (4.7) are applied. |
| 3844 | |
| 3845 | if (CTAK == CTAK_Deduced && |
| 3846 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 3847 | // C++ [temp.deduct.type]p17: |
| 3848 | // If, in the declaration of a function template with a non-type |
| 3849 | // template-parameter, the non-type template-parameter is used |
| 3850 | // in an expression in the function parameter-list and, if the |
| 3851 | // corresponding template-argument is deduced, the |
| 3852 | // template-argument type shall match the type of the |
| 3853 | // template-parameter exactly, except that a template-argument |
| 3854 | // deduced from an array bound may be of any integral type. |
| 3855 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3856 | << Arg->getType().getUnqualifiedType() |
| 3857 | << ParamType.getUnqualifiedType(); |
| 3858 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3859 | return ExprError(); |
| 3860 | } |
| 3861 | |
| 3862 | if (getLangOptions().CPlusPlus0x) { |
| 3863 | // We can't check arbitrary value-dependent arguments. |
| 3864 | // FIXME: If there's no viable conversion to the template parameter type, |
| 3865 | // we should be able to diagnose that prior to instantiation. |
| 3866 | if (Arg->isValueDependent()) { |
| 3867 | Converted = TemplateArgument(Arg); |
| 3868 | return Owned(Arg); |
| 3869 | } |
| 3870 | |
| 3871 | // C++ [temp.arg.nontype]p1: |
| 3872 | // A template-argument for a non-type, non-template template-parameter |
| 3873 | // shall be one of: |
| 3874 | // |
| 3875 | // -- for a non-type template-parameter of integral or enumeration |
| 3876 | // type, a converted constant expression of the type of the |
| 3877 | // template-parameter; or |
| 3878 | llvm::APSInt Value; |
| 3879 | ExprResult ArgResult = |
| 3880 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 3881 | CCEK_TemplateArg); |
| 3882 | if (ArgResult.isInvalid()) |
| 3883 | return ExprError(); |
| 3884 | |
| 3885 | // Widen the argument value to sizeof(parameter type). This is almost |
| 3886 | // always a no-op, except when the parameter type is bool. In |
| 3887 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 3888 | QualType IntegerType = ParamType; |
| 3889 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 3890 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 3891 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 3892 | |
| 3893 | Converted = TemplateArgument(Value, Context.getCanonicalType(ParamType)); |
| 3894 | return ArgResult; |
| 3895 | } |
| 3896 | |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 3897 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 3898 | if (ArgResult.isInvalid()) |
| 3899 | return ExprError(); |
| 3900 | Arg = ArgResult.take(); |
| 3901 | |
| 3902 | QualType ArgType = Arg->getType(); |
| 3903 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3904 | // C++ [temp.arg.nontype]p1: |
| 3905 | // A template-argument for a non-type, non-template |
| 3906 | // template-parameter shall be one of: |
| 3907 | // |
| 3908 | // -- an integral constant-expression of integral or enumeration |
| 3909 | // type; or |
| 3910 | // -- the name of a non-type template-parameter; or |
| 3911 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3912 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3913 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3914 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3915 | diag::err_template_arg_not_integral_or_enumeral) |
| 3916 | << ArgType << Arg->getSourceRange(); |
| 3917 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3918 | return ExprError(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3919 | } else if (!Arg->isValueDependent()) { |
| 3920 | Arg = VerifyIntegerConstantExpression(Arg, &Value, |
| 3921 | PDiag(diag::err_template_arg_not_ice) << ArgType, false).take(); |
| 3922 | if (!Arg) |
| 3923 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3926 | // From here on out, all we care about are the unqualified forms |
| 3927 | // of the parameter and argument types. |
| 3928 | ParamType = ParamType.getUnqualifiedType(); |
| 3929 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3930 | |
| 3931 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3932 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3933 | // Okay: no conversion necessary |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3934 | } else if (ParamType->isBooleanType()) { |
| 3935 | // This is an integral-to-boolean conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3936 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3937 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3938 | !ParamType->isEnumeralType()) { |
| 3939 | // This is an integral promotion or conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3940 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3941 | } else { |
| 3942 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3943 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3944 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3945 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3946 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3947 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3950 | // Add the value of this argument to the list of converted |
| 3951 | // arguments. We use the bitwidth and signedness of the template |
| 3952 | // parameter. |
| 3953 | if (Arg->isValueDependent()) { |
| 3954 | // The argument is value-dependent. Create a new |
| 3955 | // TemplateArgument with the converted expression. |
| 3956 | Converted = TemplateArgument(Arg); |
| 3957 | return Owned(Arg); |
| 3958 | } |
| 3959 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3960 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3961 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3962 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3963 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3964 | if (ParamType->isBooleanType()) { |
| 3965 | // Value must be zero or one. |
| 3966 | Value = Value != 0; |
| 3967 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 3968 | if (Value.getBitWidth() != AllowedBits) |
| 3969 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3970 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3971 | } else { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3972 | llvm::APSInt OldValue = Value; |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3973 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3974 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3975 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3976 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3977 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3978 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3979 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3980 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3981 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3982 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3983 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3984 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3985 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3986 | << Arg->getSourceRange(); |
| 3987 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3988 | } |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3989 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3990 | // Complain if we overflowed the template parameter's type. |
| 3991 | unsigned RequiredBits; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3992 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3993 | RequiredBits = OldValue.getActiveBits(); |
| 3994 | else if (OldValue.isUnsigned()) |
| 3995 | RequiredBits = OldValue.getActiveBits() + 1; |
| 3996 | else |
| 3997 | RequiredBits = OldValue.getMinSignedBits(); |
| 3998 | if (RequiredBits > AllowedBits) { |
| 3999 | Diag(Arg->getSourceRange().getBegin(), |
| 4000 | diag::warn_template_arg_too_large) |
| 4001 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 4002 | << Arg->getSourceRange(); |
| 4003 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4004 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4005 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4006 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4007 | Converted = TemplateArgument(Value, |
Douglas Gregor | 6b63f55 | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 4008 | ParamType->isEnumeralType() |
| 4009 | ? Context.getCanonicalType(ParamType) |
| 4010 | : IntegerType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4011 | return Owned(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4012 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4013 | |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4014 | QualType ArgType = Arg->getType(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4015 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 4016 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4017 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 4018 | // from a template argument of type std::nullptr_t to a non-type |
| 4019 | // template parameter of type pointer to object, pointer to |
| 4020 | // function, or pointer-to-member, respectively. |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4021 | if (ArgType->isNullPtrType()) { |
| 4022 | if (ParamType->isPointerType() || ParamType->isMemberPointerType()) { |
| 4023 | Converted = TemplateArgument((NamedDecl *)0); |
| 4024 | return Owned(Arg); |
| 4025 | } |
| 4026 | |
| 4027 | if (ParamType->isNullPtrType()) { |
| 4028 | llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true); |
| 4029 | Converted = TemplateArgument(Zero, Context.NullPtrTy); |
| 4030 | return Owned(Arg); |
| 4031 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4032 | } |
| 4033 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4034 | // Handle pointer-to-function, reference-to-function, and |
| 4035 | // pointer-to-member-function all in (roughly) the same way. |
| 4036 | if (// -- For a non-type template-parameter of type pointer to |
| 4037 | // function, only the function-to-pointer conversion (4.3) is |
| 4038 | // applied. If the template-argument represents a set of |
| 4039 | // overloaded functions (or a pointer to such), the matching |
| 4040 | // function is selected from the set (13.4). |
| 4041 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4042 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4043 | // -- For a non-type template-parameter of type reference to |
| 4044 | // function, no conversions apply. If the template-argument |
| 4045 | // represents a set of overloaded functions, the matching |
| 4046 | // function is selected from the set (13.4). |
| 4047 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4048 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4049 | // -- For a non-type template-parameter of type pointer to |
| 4050 | // member function, no conversions apply. If the |
| 4051 | // template-argument represents a set of overloaded member |
| 4052 | // functions, the matching member function is selected from |
| 4053 | // the set (13.4). |
| 4054 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4055 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4056 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4057 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4058 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4059 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4060 | true, |
| 4061 | FoundResult)) { |
| 4062 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4063 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4064 | |
| 4065 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 4066 | ArgType = Arg->getType(); |
| 4067 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4068 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4069 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4070 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4071 | if (!ParamType->isMemberPointerType()) { |
| 4072 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4073 | ParamType, |
| 4074 | Arg, Converted)) |
| 4075 | return ExprError(); |
| 4076 | return Owned(Arg); |
| 4077 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4078 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4079 | bool ObjCLifetimeConversion; |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4080 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4081 | false, ObjCLifetimeConversion)) { |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 4082 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, |
| 4083 | Arg->getValueKind()).take(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4084 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 4085 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4086 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4087 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4088 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4089 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4090 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4091 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4092 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4093 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4094 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 4095 | return ExprError(); |
| 4096 | return Owned(Arg); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 4099 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4100 | // -- for a non-type template-parameter of type pointer to |
| 4101 | // object, qualification conversions (4.4) and the |
| 4102 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4103 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4104 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4105 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4106 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4107 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4108 | ParamType, |
| 4109 | Arg, Converted)) |
| 4110 | return ExprError(); |
| 4111 | return Owned(Arg); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4112 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4113 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4114 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4115 | // -- For a non-type template-parameter of type reference to |
| 4116 | // object, no conversions apply. The type referred to by the |
| 4117 | // reference may be more cv-qualified than the (otherwise |
| 4118 | // identical) type of the template-argument. The |
| 4119 | // template-parameter is bound directly to the |
| 4120 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4121 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4122 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4123 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4124 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4125 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 4126 | ParamRefType->getPointeeType(), |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4127 | true, |
| 4128 | FoundResult)) { |
| 4129 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4130 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4131 | |
| 4132 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 4133 | ArgType = Arg->getType(); |
| 4134 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4135 | return ExprError(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4136 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4137 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4138 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4139 | ParamType, |
| 4140 | Arg, Converted)) |
| 4141 | return ExprError(); |
| 4142 | return Owned(Arg); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4143 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4144 | |
| 4145 | // -- For a non-type template-parameter of type pointer to data |
| 4146 | // member, qualification conversions (4.4) are applied. |
| 4147 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 4148 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4149 | bool ObjCLifetimeConversion; |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 4150 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4151 | // Types match exactly: nothing more to do here. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4152 | } else if (IsQualificationConversion(ArgType, ParamType, false, |
| 4153 | ObjCLifetimeConversion)) { |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 4154 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, |
| 4155 | Arg->getValueKind()).take(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4156 | } else { |
| 4157 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4158 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4159 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4160 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4161 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4162 | return ExprError(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4165 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 4166 | return ExprError(); |
| 4167 | return Owned(Arg); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | /// \brief Check a template argument against its corresponding |
| 4171 | /// template template parameter. |
| 4172 | /// |
| 4173 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 4174 | /// It returns true if an error occurred, and false otherwise. |
| 4175 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4176 | const TemplateArgumentLoc &Arg) { |
| 4177 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 4178 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 4179 | if (!Template) { |
| 4180 | // Any dependent template name is fine. |
| 4181 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 4182 | return false; |
| 4183 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4184 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4185 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4186 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4187 | // the name of a class template or an alias template, expressed as an |
| 4188 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4189 | // primary class templates are considered when matching the |
| 4190 | // template template argument with the corresponding parameter; |
| 4191 | // partial specializations are not considered even if their |
| 4192 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4193 | // |
| 4194 | // Note that we also allow template template parameters here, which |
| 4195 | // will happen when we are dealing with, e.g., class template |
| 4196 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4197 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4198 | !isa<TemplateTemplateParmDecl>(Template) && |
| 4199 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4200 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4201 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4202 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4203 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4204 | << Template; |
| 4205 | } |
| 4206 | |
| 4207 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 4208 | Param->getTemplateParameters(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4209 | true, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4210 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4211 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4214 | /// \brief Given a non-type template argument that refers to a |
| 4215 | /// declaration and the type of its corresponding non-type template |
| 4216 | /// parameter, produce an expression that properly refers to that |
| 4217 | /// declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4218 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4219 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 4220 | QualType ParamType, |
| 4221 | SourceLocation Loc) { |
| 4222 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 4223 | "Only declaration template arguments permitted here"); |
| 4224 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 4225 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4226 | if (VD->getDeclContext()->isRecord() && |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4227 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 4228 | // If the value is a class member, we might have a pointer-to-member. |
| 4229 | // Determine whether the non-type template template parameter is of |
| 4230 | // pointer-to-member type. If so, we need to build an appropriate |
| 4231 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 4232 | // would refer to the member itself. |
| 4233 | if (ParamType->isMemberPointerType()) { |
| 4234 | QualType ClassType |
| 4235 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 4236 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4237 | = NestedNameSpecifier::Create(Context, 0, false, |
| 4238 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4239 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 4240 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4241 | |
| 4242 | // The actual value-ness of this is unimportant, but for |
| 4243 | // internal consistency's sake, references to instance methods |
| 4244 | // are r-values. |
| 4245 | ExprValueKind VK = VK_LValue; |
| 4246 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 4247 | VK = VK_RValue; |
| 4248 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4249 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4250 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4251 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4252 | Loc, |
| 4253 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4254 | if (RefExpr.isInvalid()) |
| 4255 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4256 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4257 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4258 | |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4259 | // We might need to perform a trailing qualification conversion, since |
| 4260 | // the element type on the parameter could be more qualified than the |
| 4261 | // element type in the expression we constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4262 | bool ObjCLifetimeConversion; |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4263 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4264 | ParamType.getUnqualifiedType(), false, |
| 4265 | ObjCLifetimeConversion)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4266 | RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4267 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4268 | assert(!RefExpr.isInvalid() && |
| 4269 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4270 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4271 | return move(RefExpr); |
| 4272 | } |
| 4273 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4274 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4275 | QualType T = VD->getType().getNonReferenceType(); |
| 4276 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4277 | // When the non-type template parameter is a pointer, take the |
| 4278 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4279 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4280 | if (RefExpr.isInvalid()) |
| 4281 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4282 | |
| 4283 | if (T->isFunctionType() || T->isArrayType()) { |
| 4284 | // Decay functions and arrays. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4285 | RefExpr = DefaultFunctionArrayConversion(RefExpr.take()); |
| 4286 | if (RefExpr.isInvalid()) |
| 4287 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4288 | |
| 4289 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4290 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4291 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4292 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4293 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4294 | } |
| 4295 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4296 | ExprValueKind VK = VK_RValue; |
| 4297 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4298 | // If the non-type template parameter has reference type, qualify the |
| 4299 | // resulting declaration reference with the extra qualifiers on the |
| 4300 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4301 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 4302 | VK = VK_LValue; |
| 4303 | T = Context.getQualifiedType(T, |
| 4304 | TargetRef->getPointeeType().getQualifiers()); |
| 4305 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4306 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4307 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
| 4310 | /// \brief Construct a new expression that refers to the given |
| 4311 | /// integral template argument with the given source-location |
| 4312 | /// information. |
| 4313 | /// |
| 4314 | /// This routine takes care of the mapping from an integral template |
| 4315 | /// argument (which may have any integral type) to the appropriate |
| 4316 | /// literal value. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4317 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4318 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 4319 | SourceLocation Loc) { |
| 4320 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4321 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4322 | QualType T = Arg.getIntegralType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4323 | if (T->isAnyCharacterType()) { |
| 4324 | CharacterLiteral::CharacterKind Kind; |
| 4325 | if (T->isWideCharType()) |
| 4326 | Kind = CharacterLiteral::Wide; |
| 4327 | else if (T->isChar16Type()) |
| 4328 | Kind = CharacterLiteral::UTF16; |
| 4329 | else if (T->isChar32Type()) |
| 4330 | Kind = CharacterLiteral::UTF32; |
| 4331 | else |
| 4332 | Kind = CharacterLiteral::Ascii; |
| 4333 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4334 | return Owned(new (Context) CharacterLiteral( |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4335 | Arg.getAsIntegral()->getZExtValue(), |
| 4336 | Kind, T, Loc)); |
| 4337 | } |
| 4338 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4339 | if (T->isBooleanType()) |
| 4340 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 4341 | Arg.getAsIntegral()->getBoolValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4342 | T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4343 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4344 | if (T->isNullPtrType()) |
| 4345 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 4346 | |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4347 | // If this is an enum type that we're instantiating, we need to use an integer |
| 4348 | // type the same size as the enumerator. We don't want to build an |
| 4349 | // IntegerLiteral with enum type. |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4350 | QualType BT; |
| 4351 | if (const EnumType *ET = T->getAs<EnumType>()) |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4352 | BT = ET->getDecl()->getIntegerType(); |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4353 | else |
| 4354 | BT = T; |
| 4355 | |
John McCall | 4e9272d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 4356 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
| 4357 | if (T->isEnumeralType()) { |
| 4358 | // FIXME: This is a hack. We need a better way to handle substituted |
| 4359 | // non-type template parameters. |
| 4360 | E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0, |
| 4361 | Context.getTrivialTypeSourceInfo(T, Loc), |
| 4362 | Loc, Loc); |
| 4363 | } |
| 4364 | |
| 4365 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4366 | } |
| 4367 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4368 | /// \brief Match two template parameters within template parameter lists. |
| 4369 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 4370 | bool Complain, |
| 4371 | Sema::TemplateParameterListEqualKind Kind, |
| 4372 | SourceLocation TemplateArgLoc) { |
| 4373 | // Check the actual kind (type, non-type, template). |
| 4374 | if (Old->getKind() != New->getKind()) { |
| 4375 | if (Complain) { |
| 4376 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 4377 | if (TemplateArgLoc.isValid()) { |
| 4378 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4379 | NextDiag = diag::note_template_param_different_kind; |
| 4380 | } |
| 4381 | S.Diag(New->getLocation(), NextDiag) |
| 4382 | << (Kind != Sema::TPL_TemplateMatch); |
| 4383 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 4384 | << (Kind != Sema::TPL_TemplateMatch); |
| 4385 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4386 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4387 | return false; |
| 4388 | } |
| 4389 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4390 | // Check that both are parameter packs are neither are parameter packs. |
| 4391 | // However, if we are matching a template template argument to a |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4392 | // template template parameter, the template template parameter can have |
| 4393 | // a parameter pack where the template template argument does not. |
| 4394 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 4395 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4396 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4397 | if (Complain) { |
| 4398 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 4399 | if (TemplateArgLoc.isValid()) { |
| 4400 | S.Diag(TemplateArgLoc, |
| 4401 | diag::err_template_arg_template_params_mismatch); |
| 4402 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 4403 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4404 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4405 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 4406 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 4407 | : 2; |
| 4408 | S.Diag(New->getLocation(), NextDiag) |
| 4409 | << ParamKind << New->isParameterPack(); |
| 4410 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 4411 | << ParamKind << Old->isParameterPack(); |
| 4412 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4413 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4414 | return false; |
| 4415 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4416 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4417 | // For non-type template parameters, check the type of the parameter. |
| 4418 | if (NonTypeTemplateParmDecl *OldNTTP |
| 4419 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 4420 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4421 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4422 | // If we are matching a template template argument to a template |
| 4423 | // template parameter and one of the non-type template parameter types |
| 4424 | // is dependent, then we must wait until template instantiation time |
| 4425 | // to actually compare the arguments. |
| 4426 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4427 | (OldNTTP->getType()->isDependentType() || |
| 4428 | NewNTTP->getType()->isDependentType())) |
| 4429 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4430 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4431 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 4432 | if (Complain) { |
| 4433 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 4434 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4435 | S.Diag(TemplateArgLoc, |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4436 | diag::err_template_arg_template_params_mismatch); |
| 4437 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 4438 | } |
| 4439 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 4440 | << NewNTTP->getType() |
| 4441 | << (Kind != Sema::TPL_TemplateMatch); |
| 4442 | S.Diag(OldNTTP->getLocation(), |
| 4443 | diag::note_template_nontype_parm_prev_declaration) |
| 4444 | << OldNTTP->getType(); |
| 4445 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4446 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4447 | return false; |
| 4448 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4449 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4450 | return true; |
| 4451 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4452 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4453 | // For template template parameters, check the template parameter types. |
| 4454 | // The template parameter lists of template template |
| 4455 | // parameters must agree. |
| 4456 | if (TemplateTemplateParmDecl *OldTTP |
| 4457 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4458 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4459 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 4460 | OldTTP->getTemplateParameters(), |
| 4461 | Complain, |
| 4462 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4463 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4464 | : Kind), |
| 4465 | TemplateArgLoc); |
| 4466 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4467 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4468 | return true; |
| 4469 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4470 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4471 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 4472 | /// lists. |
| 4473 | static |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4474 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4475 | TemplateParameterList *New, |
| 4476 | TemplateParameterList *Old, |
| 4477 | Sema::TemplateParameterListEqualKind Kind, |
| 4478 | SourceLocation TemplateArgLoc) { |
| 4479 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 4480 | if (TemplateArgLoc.isValid()) { |
| 4481 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4482 | NextDiag = diag::note_template_param_list_different_arity; |
| 4483 | } |
| 4484 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 4485 | << (New->size() > Old->size()) |
| 4486 | << (Kind != Sema::TPL_TemplateMatch) |
| 4487 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 4488 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 4489 | << (Kind != Sema::TPL_TemplateMatch) |
| 4490 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 4491 | } |
| 4492 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4493 | /// \brief Determine whether the given template parameter lists are |
| 4494 | /// equivalent. |
| 4495 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4496 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4497 | /// source code as part of a new template declaration. |
| 4498 | /// |
| 4499 | /// \param Old The old template parameter list, typically found via |
| 4500 | /// name lookup of the template declared with this template parameter |
| 4501 | /// list. |
| 4502 | /// |
| 4503 | /// \param Complain If true, this routine will produce a diagnostic if |
| 4504 | /// the template parameter lists are not equivalent. |
| 4505 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4506 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4507 | /// |
| 4508 | /// \param TemplateArgLoc If this source location is valid, then we |
| 4509 | /// are actually checking the template parameter list of a template |
| 4510 | /// argument (New) against the template parameter list of its |
| 4511 | /// corresponding template template parameter (Old). We produce |
| 4512 | /// slightly different diagnostics in this scenario. |
| 4513 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4514 | /// \returns True if the template parameter lists are equal, false |
| 4515 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4516 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4517 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 4518 | TemplateParameterList *Old, |
| 4519 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4520 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4521 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4522 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 4523 | if (Complain) |
| 4524 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4525 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4526 | |
| 4527 | return false; |
| 4528 | } |
| 4529 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4530 | // C++0x [temp.arg.template]p3: |
| 4531 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4532 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4533 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4534 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4535 | // template-parameter-list of P. [...] |
| 4536 | TemplateParameterList::iterator NewParm = New->begin(); |
| 4537 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4538 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4539 | OldParmEnd = Old->end(); |
| 4540 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 4541 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 4542 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4543 | if (NewParm == NewParmEnd) { |
| 4544 | if (Complain) |
| 4545 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4546 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4547 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4548 | return false; |
| 4549 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4550 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4551 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4552 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4553 | return false; |
| 4554 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4555 | ++NewParm; |
| 4556 | continue; |
| 4557 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4558 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4559 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4560 | // [...] When P's template- parameter-list contains a template parameter |
| 4561 | // pack (14.5.3), the template parameter pack will match zero or more |
| 4562 | // template parameters or template parameter packs in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4563 | // template-parameter-list of A with the same type and form as the |
| 4564 | // template parameter pack in P (ignoring whether those template |
| 4565 | // parameters are template parameter packs). |
| 4566 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 4567 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4568 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4569 | return false; |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4570 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4571 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4572 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4573 | // Make sure we exhausted all of the arguments. |
| 4574 | if (NewParm != NewParmEnd) { |
| 4575 | if (Complain) |
| 4576 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4577 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4578 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4579 | return false; |
| 4580 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4581 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4582 | return true; |
| 4583 | } |
| 4584 | |
| 4585 | /// \brief Check whether a template can be declared within this scope. |
| 4586 | /// |
| 4587 | /// If the template declaration is valid in this scope, returns |
| 4588 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4589 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4590 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 4591 | if (!S) |
| 4592 | return false; |
| 4593 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4594 | // Find the nearest enclosing declaration scope. |
| 4595 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4596 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4597 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4598 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4599 | // C++ [temp]p2: |
| 4600 | // A template-declaration can appear only as a namespace scope or |
| 4601 | // class scope declaration. |
| 4602 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4603 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 4604 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4605 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4606 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4607 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4608 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4609 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4610 | |
| 4611 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 4612 | return false; |
| 4613 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4614 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4615 | diag::err_template_outside_namespace_or_class_scope) |
| 4616 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4617 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4618 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4619 | /// \brief Determine what kind of template specialization the given declaration |
| 4620 | /// is. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 4621 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4622 | if (!D) |
| 4623 | return TSK_Undeclared; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4624 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4625 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 4626 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4627 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 4628 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4629 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 4630 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4631 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4632 | return TSK_Undeclared; |
| 4633 | } |
| 4634 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4635 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4636 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4637 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4638 | /// This routine determines whether a template specialization can be declared |
| 4639 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4640 | /// |
| 4641 | /// \param S the semantic analysis object for which this check is being |
| 4642 | /// performed. |
| 4643 | /// |
| 4644 | /// \param Specialized the entity being specialized or instantiated, which |
| 4645 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4646 | /// a member of a class template (member function, static data member, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4647 | /// member class). |
| 4648 | /// |
| 4649 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 4650 | /// |
| 4651 | /// \param Loc the location of the explicit specialization or instantiation of |
| 4652 | /// this entity. |
| 4653 | /// |
| 4654 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 4655 | /// a class template. |
| 4656 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4657 | /// \returns true if there was an error that we cannot recover from, false |
| 4658 | /// otherwise. |
| 4659 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 4660 | NamedDecl *Specialized, |
| 4661 | NamedDecl *PrevDecl, |
| 4662 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4663 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4664 | // Keep these "kind" numbers in sync with the %select statements in the |
| 4665 | // various diagnostics emitted by this routine. |
| 4666 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4667 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4668 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4669 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4670 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4671 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4672 | EntityKind = 3; |
| 4673 | else if (isa<VarDecl>(Specialized)) |
| 4674 | EntityKind = 4; |
| 4675 | else if (isa<RecordDecl>(Specialized)) |
| 4676 | EntityKind = 5; |
| 4677 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4678 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 4679 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4680 | return true; |
| 4681 | } |
| 4682 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4683 | // C++ [temp.expl.spec]p2: |
| 4684 | // An explicit specialization shall be declared in the namespace |
| 4685 | // of which the template is a member, or, for member templates, in |
| 4686 | // the namespace of which the enclosing class or enclosing class |
| 4687 | // template is a member. An explicit specialization of a member |
| 4688 | // function, member class or static data member of a class |
| 4689 | // template shall be declared in the namespace of which the class |
| 4690 | // template is a member. Such a declaration may also be a |
| 4691 | // definition. If the declaration is not a definition, the |
| 4692 | // specialization may be defined later in the name- space in which |
| 4693 | // the explicit specialization was declared, or in a namespace |
| 4694 | // that encloses the one in which the explicit specialization was |
| 4695 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4696 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4697 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4698 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4699 | return true; |
| 4700 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4701 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4702 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4703 | if (S.getLangOptions().MicrosoftExt) { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4704 | // Do not warn for class scope explicit specialization during |
| 4705 | // instantiation, warning was already emitted during pattern |
| 4706 | // semantic analysis. |
| 4707 | if (!S.ActiveTemplateInstantiations.size()) |
| 4708 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 4709 | << Specialized; |
| 4710 | } else { |
| 4711 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4712 | << Specialized; |
| 4713 | return true; |
| 4714 | } |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4715 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4716 | |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4717 | if (S.CurContext->isRecord() && |
| 4718 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 4719 | // Make sure that we're specializing in the right record context. |
| 4720 | // Otherwise, things can go horribly wrong. |
| 4721 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4722 | << Specialized; |
| 4723 | return true; |
| 4724 | } |
| 4725 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4726 | // C++ [temp.class.spec]p6: |
| 4727 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4728 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4729 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4730 | bool ComplainedAboutScope = false; |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4731 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4732 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4733 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4734 | if ((!PrevDecl || |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4735 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4736 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4737 | // C++ [temp.exp.spec]p2: |
| 4738 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4739 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4740 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4741 | // An explicit specialization of a member function, member class or |
| 4742 | // static data member of a class template shall be declared in the |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4743 | // namespace of which the class template is a member. |
| 4744 | // |
| 4745 | // C++0x [temp.expl.spec]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4746 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4747 | // the specialized template. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4748 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
| 4749 | bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext); |
| 4750 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
| 4751 | assert(!IsCPlusPlus0xExtension && |
| 4752 | "DC encloses TU but isn't in enclosing namespace set"); |
| 4753 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4754 | << EntityKind << Specialized; |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4755 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 4756 | int Diag; |
| 4757 | if (!IsCPlusPlus0xExtension) |
| 4758 | Diag = diag::err_template_spec_decl_out_of_scope; |
| 4759 | else if (!S.getLangOptions().CPlusPlus0x) |
| 4760 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 4761 | else |
| 4762 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 4763 | S.Diag(Loc, Diag) |
| 4764 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 4765 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4766 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4767 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4768 | ComplainedAboutScope = |
| 4769 | !(IsCPlusPlus0xExtension && S.getLangOptions().CPlusPlus0x); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4770 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4771 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4772 | |
| 4773 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4774 | // namespace. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4775 | // Note that HandleDeclarator() performs this check for explicit |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4776 | // specializations of function templates, static data members, and member |
| 4777 | // functions, so we skip the check here for those kinds of entities. |
| 4778 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4779 | // Should we refactor that check, so that it occurs later? |
| 4780 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4781 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 4782 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4783 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 4784 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 4785 | << EntityKind << Specialized; |
| 4786 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 4787 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 4788 | << EntityKind << Specialized |
| 4789 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4790 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4791 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4792 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4793 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4794 | // FIXME: check for specialization-after-instantiation errors and such. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4795 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4796 | return false; |
| 4797 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4798 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4799 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 4800 | /// that checks non-type template partial specialization arguments. |
| 4801 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 4802 | NonTypeTemplateParmDecl *Param, |
| 4803 | const TemplateArgument *Args, |
| 4804 | unsigned NumArgs) { |
| 4805 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4806 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 4807 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4808 | Args[I].pack_begin(), |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4809 | Args[I].pack_size())) |
| 4810 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4811 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4812 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4813 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4814 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4815 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4816 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4817 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4818 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4819 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4820 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4821 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4822 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4823 | |
| 4824 | // Strip off any implicit casts we added as part of type checking. |
| 4825 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4826 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4827 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4828 | // C++ [temp.class.spec]p8: |
| 4829 | // A non-type argument is non-specialized if it is the name of a |
| 4830 | // non-type parameter. All other non-type arguments are |
| 4831 | // specialized. |
| 4832 | // |
| 4833 | // Below, we check the two conditions that only apply to |
| 4834 | // specialized non-type arguments, so skip any non-specialized |
| 4835 | // arguments. |
| 4836 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4837 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4838 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4839 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4840 | // C++ [temp.class.spec]p9: |
| 4841 | // Within the argument list of a class template partial |
| 4842 | // specialization, the following restrictions apply: |
| 4843 | // -- A partially specialized non-type argument expression |
| 4844 | // shall not involve a template parameter of the partial |
| 4845 | // specialization except when the argument expression is a |
| 4846 | // simple identifier. |
| 4847 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4848 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4849 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4850 | << ArgExpr->getSourceRange(); |
| 4851 | return true; |
| 4852 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4853 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4854 | // -- The type of a template parameter corresponding to a |
| 4855 | // specialized non-type argument shall not be dependent on a |
| 4856 | // parameter of the specialization. |
| 4857 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4858 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4859 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 4860 | << Param->getType() |
| 4861 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4862 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4863 | return true; |
| 4864 | } |
| 4865 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4866 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4867 | return false; |
| 4868 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4869 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4870 | /// \brief Check the non-type template arguments of a class template |
| 4871 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 4872 | /// |
| 4873 | /// \param TemplateParams the template parameters of the primary class |
| 4874 | /// template. |
| 4875 | /// |
| 4876 | /// \param TemplateArg the template arguments of the class template |
| 4877 | /// partial specialization. |
| 4878 | /// |
| 4879 | /// \returns true if there was an error, false otherwise. |
| 4880 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 4881 | TemplateParameterList *TemplateParams, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4882 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4883 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 4884 | |
| 4885 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4886 | NonTypeTemplateParmDecl *Param |
| 4887 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 4888 | if (!Param) |
| 4889 | continue; |
| 4890 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4891 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4892 | &ArgList[I], 1)) |
| 4893 | return true; |
| 4894 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4895 | |
| 4896 | return false; |
| 4897 | } |
| 4898 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4899 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4900 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4901 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4902 | SourceLocation KWLoc, |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 4903 | SourceLocation ModulePrivateLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4904 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4905 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4906 | SourceLocation TemplateNameLoc, |
| 4907 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4908 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4909 | SourceLocation RAngleLoc, |
| 4910 | AttributeList *Attr, |
| 4911 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4912 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4913 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4914 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 4915 | // store the location of the outermost template keyword in the declaration. |
| 4916 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
| 4917 | ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation(); |
| 4918 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4919 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4920 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4921 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4922 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4923 | |
| 4924 | if (!ClassTemplate) { |
| 4925 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4926 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4927 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4928 | return true; |
| 4929 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4930 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4931 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4932 | bool isPartialSpecialization = false; |
| 4933 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4934 | // Check the validity of the template headers that introduce this |
| 4935 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4936 | // FIXME: We probably shouldn't complain about these headers for |
| 4937 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4938 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4939 | TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 4940 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, |
| 4941 | TemplateNameLoc, |
| 4942 | SS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4943 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4944 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4945 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4946 | isExplicitSpecialization, |
| 4947 | Invalid); |
| 4948 | if (Invalid) |
| 4949 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4950 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4951 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4952 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4953 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4954 | if (TUK == TUK_Friend) { |
| 4955 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4956 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4957 | return true; |
| 4958 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4959 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4960 | // C++ [temp.class.spec]p10: |
| 4961 | // The template parameter list of a specialization shall not |
| 4962 | // contain default template argument values. |
| 4963 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4964 | Decl *Param = TemplateParams->getParam(I); |
| 4965 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4966 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4967 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4968 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4969 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4970 | } |
| 4971 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4972 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4973 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4974 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4975 | diag::err_default_arg_in_partial_spec) |
| 4976 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4977 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4978 | } |
| 4979 | } else { |
| 4980 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4981 | if (TTP->hasDefaultArgument()) { |
| 4982 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4983 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4984 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4985 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4986 | } |
| 4987 | } |
| 4988 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4989 | } else if (TemplateParams) { |
| 4990 | if (TUK == TUK_Friend) |
| 4991 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4992 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4993 | SourceRange(TemplateParams->getTemplateLoc(), |
| 4994 | TemplateParams->getRAngleLoc())) |
| 4995 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4996 | else |
| 4997 | isExplicitSpecialization = true; |
| 4998 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4999 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5000 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5001 | isExplicitSpecialization = true; |
| 5002 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5003 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5004 | // Check that the specialization uses the same tag kind as the |
| 5005 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5006 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5007 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5008 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 5009 | Kind, TUK == TUK_Definition, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5010 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5011 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5012 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5013 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5014 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5015 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5016 | diag::note_previous_use); |
| 5017 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5018 | } |
| 5019 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5020 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5021 | TemplateArgumentListInfo TemplateArgs; |
| 5022 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 5023 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5024 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5025 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 5026 | // Check for unexpanded parameter packs in any of the template arguments. |
| 5027 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5028 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 5029 | UPPC_PartialSpecialization)) |
| 5030 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5031 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5032 | // Check that the template argument list is well-formed for this |
| 5033 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5034 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5035 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5036 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5037 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5038 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5039 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5040 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5041 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5042 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5043 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5044 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5045 | return true; |
| 5046 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 5047 | bool InstantiationDependent; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5048 | if (!Name.isDependent() && |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5049 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5050 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 5051 | TemplateArgs.size(), |
| 5052 | InstantiationDependent)) { |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5053 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 5054 | << ClassTemplate->getDeclName(); |
| 5055 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5056 | } |
| 5057 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5058 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5059 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5060 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 5061 | |
| 5062 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5063 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5064 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5065 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 5066 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5067 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5068 | else |
| 5069 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5070 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5071 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5072 | |
| 5073 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5074 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5075 | // Check whether we can declare a class template specialization in |
| 5076 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5077 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5078 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 5079 | TemplateNameLoc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5080 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5081 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5082 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5083 | // The canonical type |
| 5084 | QualType CanonType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5085 | if (PrevDecl && |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5086 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5087 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5088 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5089 | // arguments was referenced but not declared, or we're only |
| 5090 | // referencing this specialization as a friend, reuse that |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5091 | // declaration node as our own, updating its source location and |
| 5092 | // the list of outer template parameters to reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5093 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5094 | Specialization->setLocation(TemplateNameLoc); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5095 | if (TemplateParameterLists.size() > 0) { |
| 5096 | Specialization->setTemplateParameterListsInfo(Context, |
| 5097 | TemplateParameterLists.size(), |
| 5098 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5099 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5100 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5101 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5102 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5103 | // Build the canonical type that describes the converted template |
| 5104 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5105 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 5106 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5107 | Converted.data(), |
| 5108 | Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5109 | |
| 5110 | if (Context.hasSameType(CanonType, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5111 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 5112 | // C++ [temp.class.spec]p9b3: |
| 5113 | // |
| 5114 | // -- The argument list of the specialization shall not be identical |
| 5115 | // to the implicit argument list of the primary template. |
| 5116 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 5117 | << (TUK == TUK_Definition) |
| 5118 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5119 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 5120 | ClassTemplate->getIdentifier(), |
| 5121 | TemplateNameLoc, |
| 5122 | Attr, |
| 5123 | TemplateParams, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 5124 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5125 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 5126 | (TemplateParameterList**) TemplateParameterLists.release()); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5127 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5128 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5129 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5130 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 5131 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5132 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5133 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5134 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5135 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5136 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5137 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 5138 | TemplateParams, |
| 5139 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5140 | Converted.data(), |
| 5141 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5142 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5143 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5144 | PrevPartial, |
| 5145 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5146 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5147 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5148 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5149 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5150 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5151 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5152 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5153 | if (!PrevPartial) |
| 5154 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5155 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5156 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5157 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5158 | // template specialization, make a note of that. |
| 5159 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 5160 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5161 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5162 | // Check that all of the template parameters of the class template |
| 5163 | // partial specialization are deducible from the template |
| 5164 | // arguments. If not, this class template partial specialization |
| 5165 | // will never be used. |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5166 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5167 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5168 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5169 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5170 | |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5171 | if (!DeducibleParams.all()) { |
| 5172 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5173 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 5174 | << (NumNonDeducible > 1) |
| 5175 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 5176 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 5177 | if (!DeducibleParams[I]) { |
| 5178 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 5179 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5180 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5181 | diag::note_partial_spec_unused_parameter) |
| 5182 | << Param->getDeclName(); |
| 5183 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5184 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5185 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 5186 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5187 | } |
| 5188 | } |
| 5189 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5190 | } else { |
| 5191 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5192 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5193 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5194 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5195 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5196 | KWLoc, TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5197 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5198 | Converted.data(), |
| 5199 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5200 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5201 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5202 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5203 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5204 | TemplateParameterLists.size(), |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5205 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5206 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5207 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5208 | if (!PrevDecl) |
| 5209 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5210 | |
| 5211 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5212 | } |
| 5213 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5214 | // C++ [temp.expl.spec]p6: |
| 5215 | // 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] | 5216 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5217 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5218 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5219 | // use occurs; no diagnostic is required. |
| 5220 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5221 | bool Okay = false; |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5222 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5223 | // Is there any previous explicit specialization declaration? |
| 5224 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5225 | Okay = true; |
| 5226 | break; |
| 5227 | } |
| 5228 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5229 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5230 | if (!Okay) { |
| 5231 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 5232 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 5233 | << Context.getTypeDeclType(Specialization) << Range; |
| 5234 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5235 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5236 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5237 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5238 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5239 | return true; |
| 5240 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5241 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5242 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5243 | // If this is not a friend, note that this is an explicit specialization. |
| 5244 | if (TUK != TUK_Friend) |
| 5245 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5246 | |
| 5247 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5248 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5249 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5250 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5251 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5252 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5253 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 5254 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5255 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5256 | } |
| 5257 | } |
| 5258 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 5259 | if (Attr) |
| 5260 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 5261 | |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5262 | if (ModulePrivateLoc.isValid()) |
| 5263 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 5264 | << (isPartialSpecialization? 1 : 0) |
| 5265 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 5266 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 5267 | // Build the fully-sugared type for this class template |
| 5268 | // specialization as the user wrote in the specialization |
| 5269 | // itself. This means that we'll pretty-print the type retrieved |
| 5270 | // from the specialization's declaration the way that the user |
| 5271 | // actually wrote the specialization, rather than formatting the |
| 5272 | // name based on the "canonical" representation used to store the |
| 5273 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5274 | TypeSourceInfo *WrittenTy |
| 5275 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5276 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5277 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5278 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5279 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5280 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5281 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5282 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5283 | // C++ [temp.expl.spec]p9: |
| 5284 | // A template explicit specialization is in the scope of the |
| 5285 | // namespace in which the template was defined. |
| 5286 | // |
| 5287 | // We actually implement this paragraph where we set the semantic |
| 5288 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 5289 | // but we also maintain the lexical context where the actual |
| 5290 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5291 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5292 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5293 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5294 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5295 | Specialization->startDefinition(); |
| 5296 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5297 | if (TUK == TUK_Friend) { |
| 5298 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 5299 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5300 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5301 | /*FIXME:*/KWLoc); |
| 5302 | Friend->setAccess(AS_public); |
| 5303 | CurContext->addDecl(Friend); |
| 5304 | } else { |
| 5305 | // Add the specialization into its lexical context, so that it can |
| 5306 | // be seen when iterating through the list of declarations in that |
| 5307 | // context. However, specializations are not found by name lookup. |
| 5308 | CurContext->addDecl(Specialization); |
| 5309 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5310 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5311 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5312 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5313 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5314 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5315 | Declarator &D) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5316 | return HandleDeclarator(S, D, move(TemplateParameterLists)); |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5317 | } |
| 5318 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5319 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5320 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5321 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5322 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5323 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5324 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5325 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5326 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5327 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5328 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5329 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5330 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 5331 | D.setFunctionDefinitionKind(FDK_Definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5332 | Decl *DP = HandleDeclarator(ParentScope, D, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5333 | move(TemplateParameterLists)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5334 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5335 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5336 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5337 | FunctionTemplate->getTemplatedDecl()); |
| 5338 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 5339 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 5340 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5341 | } |
| 5342 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5343 | /// \brief Strips various properties off an implicit instantiation |
| 5344 | /// that has just been explicitly specialized. |
| 5345 | static void StripImplicitInstantiation(NamedDecl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5346 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5347 | |
| 5348 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5349 | FD->setInlineSpecified(false); |
| 5350 | } |
| 5351 | } |
| 5352 | |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5353 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 5354 | // declaration or definition. |
| 5355 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5356 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5357 | // Explicit instantiations following a specialization have no effect and |
| 5358 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 5359 | // until a valid name loc is found. |
| 5360 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5361 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 5362 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5363 | PrevDiagLoc = Prev->getLocation(); |
| 5364 | } |
| 5365 | assert(PrevDiagLoc.isValid() && |
| 5366 | "Explicit instantiation without point of instantiation?"); |
| 5367 | return PrevDiagLoc; |
| 5368 | } |
| 5369 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5370 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5371 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5372 | /// for those cases where they are required and determining whether the |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5373 | /// new specialization/instantiation will have any effect. |
| 5374 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5375 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5376 | /// instantiation. |
| 5377 | /// |
| 5378 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 5379 | /// |
| 5380 | /// \param PrevDecl the previous declaration of the entity. |
| 5381 | /// |
| 5382 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 5383 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5384 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5385 | /// declaration was instantiated (either implicitly or explicitly). |
| 5386 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5387 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5388 | /// specialization or instantiation has no effect and should be ignored. |
| 5389 | /// |
| 5390 | /// \returns true if there was an error that should prevent the introduction of |
| 5391 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5392 | bool |
| 5393 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 5394 | TemplateSpecializationKind NewTSK, |
| 5395 | NamedDecl *PrevDecl, |
| 5396 | TemplateSpecializationKind PrevTSK, |
| 5397 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5398 | bool &HasNoEffect) { |
| 5399 | HasNoEffect = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5400 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5401 | switch (NewTSK) { |
| 5402 | case TSK_Undeclared: |
| 5403 | case TSK_ImplicitInstantiation: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5404 | llvm_unreachable("Don't check implicit instantiations here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5405 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5406 | case TSK_ExplicitSpecialization: |
| 5407 | switch (PrevTSK) { |
| 5408 | case TSK_Undeclared: |
| 5409 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5410 | // Okay, we're just specializing something that is either already |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5411 | // explicitly specialized or has merely been mentioned without any |
| 5412 | // instantiation. |
| 5413 | return false; |
| 5414 | |
| 5415 | case TSK_ImplicitInstantiation: |
| 5416 | if (PrevPointOfInstantiation.isInvalid()) { |
| 5417 | // The declaration itself has not actually been instantiated, so it is |
| 5418 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5419 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5420 | return false; |
| 5421 | } |
| 5422 | // Fall through |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5423 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5424 | case TSK_ExplicitInstantiationDeclaration: |
| 5425 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5426 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 5427 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5428 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5429 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5430 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5431 | // 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] | 5432 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5433 | // before the first use of that specialization that would cause an |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5434 | // implicit instantiation to take place, in every translation unit in |
| 5435 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5436 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5437 | // Is there any previous explicit specialization declaration? |
| 5438 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 5439 | return false; |
| 5440 | } |
| 5441 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5442 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5443 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5444 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5445 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5446 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5447 | return true; |
| 5448 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5449 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5450 | case TSK_ExplicitInstantiationDeclaration: |
| 5451 | switch (PrevTSK) { |
| 5452 | case TSK_ExplicitInstantiationDeclaration: |
| 5453 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5454 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5455 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5456 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5457 | case TSK_Undeclared: |
| 5458 | case TSK_ImplicitInstantiation: |
| 5459 | // We're explicitly instantiating something that may have already been |
| 5460 | // implicitly instantiated; that's fine. |
| 5461 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5462 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5463 | case TSK_ExplicitSpecialization: |
| 5464 | // C++0x [temp.explicit]p4: |
| 5465 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5466 | // of a template appears after a declaration of an explicit |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5467 | // specialization for that template, the explicit instantiation has no |
| 5468 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5469 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5470 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5471 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5472 | case TSK_ExplicitInstantiationDefinition: |
| 5473 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5474 | // If an entity is the subject of both an explicit instantiation |
| 5475 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5476 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5477 | Diag(NewLoc, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5478 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5479 | |
| 5480 | // Explicit instantiations following a specialization have no effect and |
| 5481 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 5482 | // until a valid name loc is found. |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5483 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 5484 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5485 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5486 | return false; |
| 5487 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5488 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5489 | case TSK_ExplicitInstantiationDefinition: |
| 5490 | switch (PrevTSK) { |
| 5491 | case TSK_Undeclared: |
| 5492 | case TSK_ImplicitInstantiation: |
| 5493 | // We're explicitly instantiating something that may have already been |
| 5494 | // implicitly instantiated; that's fine. |
| 5495 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5496 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5497 | case TSK_ExplicitSpecialization: |
| 5498 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 5499 | // For a given set of template parameters, if an explicit |
| 5500 | // instantiation of a template appears after a declaration of |
| 5501 | // an explicit specialization for that template, the explicit |
| 5502 | // instantiation has no effect. |
| 5503 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5504 | // 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] | 5505 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5506 | // has been explicitly specialized. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5507 | Diag(NewLoc, getLangOptions().CPlusPlus0x ? |
| 5508 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 5509 | diag::ext_explicit_instantiation_after_specialization) |
| 5510 | << PrevDecl; |
| 5511 | Diag(PrevDecl->getLocation(), |
| 5512 | diag::note_previous_template_specialization); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5513 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5514 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5515 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5516 | case TSK_ExplicitInstantiationDeclaration: |
| 5517 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5518 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5519 | |
| 5520 | // C++0x [temp.explicit]p4: |
| 5521 | // For a given set of template parameters, if an explicit instantiation |
| 5522 | // of a template appears after a declaration of an explicit |
| 5523 | // specialization for that template, the explicit instantiation has no |
| 5524 | // effect. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5525 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5526 | // Is there any previous explicit specialization declaration? |
| 5527 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5528 | HasNoEffect = true; |
| 5529 | break; |
| 5530 | } |
| 5531 | } |
| 5532 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5533 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5534 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5535 | case TSK_ExplicitInstantiationDefinition: |
| 5536 | // C++0x [temp.spec]p5: |
| 5537 | // For a given template and a given set of template-arguments, |
| 5538 | // - an explicit instantiation definition shall appear at most once |
| 5539 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5540 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5541 | << PrevDecl; |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5542 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5543 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5544 | HasNoEffect = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5545 | return false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5546 | } |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5547 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5548 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5549 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5550 | } |
| 5551 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5552 | /// \brief Perform semantic analysis for the given dependent function |
| 5553 | /// template specialization. The only possible way to get a dependent |
| 5554 | /// function template specialization is with a friend declaration, |
| 5555 | /// like so: |
| 5556 | /// |
| 5557 | /// template <class T> void foo(T); |
| 5558 | /// template <class T> class A { |
| 5559 | /// friend void foo<>(T); |
| 5560 | /// }; |
| 5561 | /// |
| 5562 | /// There really isn't any useful analysis we can do here, so we |
| 5563 | /// just store the information. |
| 5564 | bool |
| 5565 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 5566 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 5567 | LookupResult &Previous) { |
| 5568 | // Remove anything from Previous that isn't a function template in |
| 5569 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5570 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5571 | LookupResult::Filter F = Previous.makeFilter(); |
| 5572 | while (F.hasNext()) { |
| 5573 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 5574 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5575 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 5576 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5577 | F.erase(); |
| 5578 | } |
| 5579 | F.done(); |
| 5580 | |
| 5581 | // Should this be diagnosed here? |
| 5582 | if (Previous.empty()) return true; |
| 5583 | |
| 5584 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 5585 | ExplicitTemplateArgs); |
| 5586 | return false; |
| 5587 | } |
| 5588 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5589 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5590 | /// specialization. |
| 5591 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5592 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5593 | /// explicit function template specialization. On successful completion, |
| 5594 | /// the function declaration \p FD will become a function template |
| 5595 | /// specialization. |
| 5596 | /// |
| 5597 | /// \param FD the function declaration, which will be updated to become a |
| 5598 | /// function template specialization. |
| 5599 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5600 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 5601 | /// if any. Note that this may be valid info even when 0 arguments are |
| 5602 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 5603 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5604 | /// |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5605 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5606 | /// this function specialization. |
| 5607 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5608 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5609 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5610 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5611 | // The set of function template specializations that could match this |
| 5612 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5613 | UnresolvedSet<8> Candidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5614 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5615 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5616 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5617 | I != E; ++I) { |
| 5618 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 5619 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5620 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5621 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5622 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 5623 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5624 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5625 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5626 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5627 | // A trailing template-argument can be left unspecified in the |
| 5628 | // template-id naming an explicit function template specialization |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5629 | // provided it can be deduced from the function argument type. |
| 5630 | // Perform template argument deduction to determine whether we may be |
| 5631 | // specializing this template. |
| 5632 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5633 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5634 | FunctionDecl *Specialization = 0; |
| 5635 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5636 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5637 | FD->getType(), |
| 5638 | Specialization, |
| 5639 | Info)) { |
| 5640 | // FIXME: Template argument deduction failed; record why it failed, so |
| 5641 | // that we can provide nifty diagnostics. |
| 5642 | (void)TDK; |
| 5643 | continue; |
| 5644 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5645 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5646 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5647 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5648 | } |
| 5649 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5650 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5651 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5652 | UnresolvedSetIterator Result |
| 5653 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5654 | TPOC_Other, 0, FD->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5655 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5656 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5657 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5658 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5659 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5660 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5661 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5662 | |
| 5663 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 5664 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | abfb405 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 5665 | |
| 5666 | FunctionTemplateSpecializationInfo *SpecInfo |
| 5667 | = Specialization->getTemplateSpecializationInfo(); |
| 5668 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5669 | |
| 5670 | // Note: do not overwrite location info if previous template |
| 5671 | // specialization kind was explicit. |
| 5672 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
| 5673 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) |
| 5674 | Specialization->setLocation(FD->getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5675 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5676 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5677 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5678 | |
| 5679 | // If this is a friend declaration, then we're not really declaring |
| 5680 | // an explicit specialization. |
| 5681 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5682 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5683 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5684 | if (!isFriend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5685 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5686 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5687 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5688 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5689 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5690 | |
| 5691 | // C++ [temp.expl.spec]p6: |
| 5692 | // 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] | 5693 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5694 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5695 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5696 | // use occurs; no diagnostic is required. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5697 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5698 | if (!isFriend && |
| 5699 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5700 | TSK_ExplicitSpecialization, |
| 5701 | Specialization, |
| 5702 | SpecInfo->getTemplateSpecializationKind(), |
| 5703 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5704 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5705 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5706 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5707 | // Mark the prior declaration as an explicit specialization, so that later |
| 5708 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5709 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5710 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5711 | MarkUnusedFileScopedDecl(Specialization); |
| 5712 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5713 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5714 | // Turn the given function declaration into a function template |
| 5715 | // specialization, with the template arguments from the previous |
| 5716 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5717 | // Take copies of (semantic and syntactic) template argument lists. |
| 5718 | const TemplateArgumentList* TemplArgs = new (Context) |
| 5719 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5720 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5721 | TemplArgs, /*InsertPos=*/0, |
| 5722 | SpecInfo->getTemplateSpecializationKind(), |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 5723 | ExplicitTemplateArgs); |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5724 | FD->setStorageClass(Specialization->getStorageClass()); |
| 5725 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5726 | // The "previous declaration" for this function template specialization is |
| 5727 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5728 | Previous.clear(); |
| 5729 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5730 | return false; |
| 5731 | } |
| 5732 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5733 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5734 | /// specialization. |
| 5735 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5736 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5737 | /// explicit member function specialization. On successful completion, |
| 5738 | /// the function declaration \p FD will become a member function |
| 5739 | /// specialization. |
| 5740 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5741 | /// \param Member the member declaration, which will be updated to become a |
| 5742 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5743 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5744 | /// \param Previous the set of declarations, one of which may be specialized |
| 5745 | /// by this function specialization; the set will be modified to contain the |
| 5746 | /// redeclared member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5747 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5748 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5749 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5750 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5751 | // Try to find the member we are instantiating. |
| 5752 | NamedDecl *Instantiation = 0; |
| 5753 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5754 | MemberSpecializationInfo *MSInfo = 0; |
| 5755 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5756 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5757 | // Nowhere to look anyway. |
| 5758 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5759 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5760 | I != E; ++I) { |
| 5761 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5762 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5763 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 5764 | Instantiation = Method; |
| 5765 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5766 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5767 | break; |
| 5768 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5769 | } |
| 5770 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5771 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5772 | VarDecl *PrevVar; |
| 5773 | if (Previous.isSingleResult() && |
| 5774 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5775 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5776 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5777 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5778 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5779 | } |
| 5780 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5781 | CXXRecordDecl *PrevRecord; |
| 5782 | if (Previous.isSingleResult() && |
| 5783 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 5784 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5785 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5786 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5787 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5788 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5789 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5790 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5791 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5792 | // specializations are always out-of-line, the caller will complain about |
| 5793 | // this mismatch later. |
| 5794 | return false; |
| 5795 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5796 | |
| 5797 | // If this is a friend, just bail out here before we start turning |
| 5798 | // things into explicit specializations. |
| 5799 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 5800 | // Preserve instantiation information. |
| 5801 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 5802 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 5803 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5804 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5805 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 5806 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 5807 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5808 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5809 | } |
| 5810 | |
| 5811 | Previous.clear(); |
| 5812 | Previous.addDecl(Instantiation); |
| 5813 | return false; |
| 5814 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5815 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5816 | // Make sure that this is a specialization of a member. |
| 5817 | if (!InstantiatedFrom) { |
| 5818 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 5819 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5820 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 5821 | return true; |
| 5822 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5823 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5824 | // C++ [temp.expl.spec]p6: |
| 5825 | // 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] | 5826 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5827 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5828 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5829 | // use occurs; no diagnostic is required. |
| 5830 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5831 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5832 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5833 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 5834 | TSK_ExplicitSpecialization, |
| 5835 | Instantiation, |
| 5836 | MSInfo->getTemplateSpecializationKind(), |
| 5837 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5838 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5839 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5840 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5841 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5842 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5843 | InstantiatedFrom, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5844 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5845 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5846 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 5847 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5848 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5849 | // the original declaration to note that it is an explicit specialization |
| 5850 | // (if it was previously an implicit instantiation). This latter step |
| 5851 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5852 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5853 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 5854 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 5855 | TSK_ImplicitInstantiation) { |
| 5856 | InstantiationFunction->setTemplateSpecializationKind( |
| 5857 | TSK_ExplicitSpecialization); |
| 5858 | InstantiationFunction->setLocation(Member->getLocation()); |
| 5859 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5860 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5861 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 5862 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5863 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5864 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5865 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5866 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 5867 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 5868 | TSK_ImplicitInstantiation) { |
| 5869 | InstantiationVar->setTemplateSpecializationKind( |
| 5870 | TSK_ExplicitSpecialization); |
| 5871 | InstantiationVar->setLocation(Member->getLocation()); |
| 5872 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5873 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5874 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 5875 | cast<VarDecl>(InstantiatedFrom), |
| 5876 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5877 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5878 | } else { |
| 5879 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5880 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 5881 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 5882 | TSK_ImplicitInstantiation) { |
| 5883 | InstantiationClass->setTemplateSpecializationKind( |
| 5884 | TSK_ExplicitSpecialization); |
| 5885 | InstantiationClass->setLocation(Member->getLocation()); |
| 5886 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5887 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5888 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5889 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5890 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5891 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5892 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5893 | // Save the caller the trouble of having to figure out which declaration |
| 5894 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5895 | Previous.clear(); |
| 5896 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5897 | return false; |
| 5898 | } |
| 5899 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5900 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5901 | /// |
| 5902 | /// \returns true if a serious error occurs, false otherwise. |
| 5903 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5904 | SourceLocation InstLoc, |
| 5905 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5906 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5907 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5908 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5909 | if (CurContext->isRecord()) { |
| 5910 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5911 | << D; |
| 5912 | return true; |
| 5913 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5914 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5915 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5916 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5917 | // template. If the name declared in the explicit instantiation is an |
| 5918 | // unqualified name, the explicit instantiation shall appear in the |
| 5919 | // namespace where its template is declared or, if that namespace is inline |
| 5920 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5921 | // |
| 5922 | // 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] | 5923 | if (WasQualifiedName) { |
| 5924 | if (CurContext->Encloses(OrigContext)) |
| 5925 | return false; |
| 5926 | } else { |
| 5927 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 5928 | return false; |
| 5929 | } |
| 5930 | |
| 5931 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 5932 | if (WasQualifiedName) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5933 | S.Diag(InstLoc, |
| 5934 | S.getLangOptions().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5935 | diag::err_explicit_instantiation_out_of_scope : |
| 5936 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5937 | << D << NS; |
| 5938 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5939 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5940 | S.getLangOptions().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5941 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 5942 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 5943 | << D << NS; |
| 5944 | } else |
| 5945 | S.Diag(InstLoc, |
| 5946 | S.getLangOptions().CPlusPlus0x? |
| 5947 | diag::err_explicit_instantiation_must_be_global : |
| 5948 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 5949 | << D; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5950 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5951 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5952 | } |
| 5953 | |
| 5954 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5955 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5956 | if (!SS.isSet()) |
| 5957 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5958 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5959 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5960 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5961 | // or a static data member of a class template specialization, the name of |
| 5962 | // the class template specialization in the qualified-id for the member |
| 5963 | // name shall be a simple-template-id. |
| 5964 | // |
| 5965 | // C++98 has the same restriction, just worded differently. |
| 5966 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5967 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5968 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5969 | if (isa<TemplateSpecializationType>(T)) |
| 5970 | return true; |
| 5971 | |
| 5972 | return false; |
| 5973 | } |
| 5974 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5975 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5976 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5977 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5978 | SourceLocation ExternLoc, |
| 5979 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5980 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5981 | SourceLocation KWLoc, |
| 5982 | const CXXScopeSpec &SS, |
| 5983 | TemplateTy TemplateD, |
| 5984 | SourceLocation TemplateNameLoc, |
| 5985 | SourceLocation LAngleLoc, |
| 5986 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5987 | SourceLocation RAngleLoc, |
| 5988 | AttributeList *Attr) { |
| 5989 | // Find the class template we're specializing |
| 5990 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5991 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5992 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5993 | |
| 5994 | // Check that the specialization uses the same tag kind as the |
| 5995 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5996 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5997 | assert(Kind != TTK_Enum && |
| 5998 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5999 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6000 | Kind, /*isDefinition*/false, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6001 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6002 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6003 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6004 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6005 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6006 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6007 | diag::note_previous_use); |
| 6008 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6009 | } |
| 6010 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6011 | // C++0x [temp.explicit]p2: |
| 6012 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6013 | // definition and an explicit instantiation declaration. An explicit |
| 6014 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6015 | TemplateSpecializationKind TSK |
| 6016 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6017 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6018 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6019 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6020 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 6021 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6022 | |
| 6023 | // Check that the template argument list is well-formed for this |
| 6024 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6025 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6026 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6027 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6028 | return true; |
| 6029 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6030 | // Find the class template specialization declaration that |
| 6031 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6032 | void *InsertPos = 0; |
| 6033 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6034 | = ClassTemplate->findSpecialization(Converted.data(), |
| 6035 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6036 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6037 | TemplateSpecializationKind PrevDecl_TSK |
| 6038 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 6039 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6040 | // C++0x [temp.explicit]p2: |
| 6041 | // [...] An explicit instantiation shall appear in an enclosing |
| 6042 | // namespace of its template. [...] |
| 6043 | // |
| 6044 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6045 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 6046 | SS.isSet())) |
| 6047 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6048 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6049 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 6050 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6051 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6052 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6053 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6054 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6055 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6056 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6057 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6058 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6059 | // Even though HasNoEffect == true means that this explicit instantiation |
| 6060 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 6061 | |
| 6062 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 6063 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6064 | // Since the only prior class template specialization with these |
| 6065 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6066 | // declaration node as our own, updating the source location |
| 6067 | // for the template name to reflect our new declaration. |
| 6068 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6069 | Specialization = PrevDecl; |
| 6070 | Specialization->setLocation(TemplateNameLoc); |
| 6071 | PrevDecl = 0; |
| 6072 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6073 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6074 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6075 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6076 | // Create a new class template specialization declaration node for |
| 6077 | // this explicit specialization. |
| 6078 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6079 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6080 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6081 | KWLoc, TemplateNameLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6082 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6083 | Converted.data(), |
| 6084 | Converted.size(), |
| 6085 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6086 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6087 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6088 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6089 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6090 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6091 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6092 | } |
| 6093 | |
| 6094 | // Build the fully-sugared type for this explicit instantiation as |
| 6095 | // the user wrote in the explicit instantiation itself. This means |
| 6096 | // that we'll pretty-print the type retrieved from the |
| 6097 | // specialization's declaration the way that the user actually wrote |
| 6098 | // the explicit instantiation, rather than formatting the name based |
| 6099 | // on the "canonical" representation used to store the template |
| 6100 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6101 | TypeSourceInfo *WrittenTy |
| 6102 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6103 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6104 | Context.getTypeDeclType(Specialization)); |
| 6105 | Specialization->setTypeAsWritten(WrittenTy); |
| 6106 | TemplateArgsIn.release(); |
| 6107 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6108 | // Set source locations for keywords. |
| 6109 | Specialization->setExternLoc(ExternLoc); |
| 6110 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 6111 | |
Rafael Espindola | 0257b7f | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 6112 | if (Attr) |
| 6113 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6114 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6115 | // Add the explicit instantiation into its lexical context. However, |
| 6116 | // since explicit instantiations are never found by name lookup, we |
| 6117 | // just put it into the declaration context directly. |
| 6118 | Specialization->setLexicalDeclContext(CurContext); |
| 6119 | CurContext->addDecl(Specialization); |
| 6120 | |
| 6121 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 6122 | if (HasNoEffect) { |
| 6123 | // Set the template specialization kind. |
| 6124 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6125 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 6126 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6127 | |
| 6128 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6129 | // A definition of a class template or class member template |
| 6130 | // shall be in scope at the point of the explicit instantiation of |
| 6131 | // the class template or class member template. |
| 6132 | // |
| 6133 | // This check comes when we actually try to perform the |
| 6134 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6135 | ClassTemplateSpecializationDecl *Def |
| 6136 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6137 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6138 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6139 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6140 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6141 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6142 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 6143 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6144 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6145 | // Instantiate the members of this class template specialization. |
| 6146 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6147 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6148 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 6149 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 6150 | |
| 6151 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 6152 | // TSK_ExplicitInstantiationDefinition |
| 6153 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 6154 | TSK == TSK_ExplicitInstantiationDefinition) |
| 6155 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6156 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6157 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6158 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6159 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6160 | // Set the template specialization kind. |
| 6161 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6162 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6163 | } |
| 6164 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6165 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6166 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6167 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 6168 | SourceLocation ExternLoc, |
| 6169 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6170 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6171 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6172 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6173 | IdentifierInfo *Name, |
| 6174 | SourceLocation NameLoc, |
| 6175 | AttributeList *Attr) { |
| 6176 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 6177 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6178 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6179 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6180 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6181 | /*ModulePrivateLoc=*/SourceLocation(), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6182 | MultiTemplateParamsArg(*this, 0, 0), |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 6183 | Owned, IsDependent, SourceLocation(), false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6184 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6185 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 6186 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6187 | if (!TagD) |
| 6188 | return true; |
| 6189 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6190 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6191 | if (Tag->isEnum()) { |
| 6192 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 6193 | << Context.getTypeDeclType(Tag); |
| 6194 | return true; |
| 6195 | } |
| 6196 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 6197 | if (Tag->isInvalidDecl()) |
| 6198 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6199 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6200 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 6201 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 6202 | if (!Pattern) { |
| 6203 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 6204 | << Context.getTypeDeclType(Record); |
| 6205 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 6206 | return true; |
| 6207 | } |
| 6208 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6209 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6210 | // If the explicit instantiation is for a class or member class, the |
| 6211 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6212 | // simple-template-id. |
| 6213 | // |
| 6214 | // C++98 has the same restriction, just worded differently. |
| 6215 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6216 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6217 | << Record << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6218 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6219 | // C++0x [temp.explicit]p2: |
| 6220 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6221 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6222 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 6223 | TemplateSpecializationKind TSK |
| 6224 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6225 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6226 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6227 | // C++0x [temp.explicit]p2: |
| 6228 | // [...] An explicit instantiation shall appear in an enclosing |
| 6229 | // namespace of its template. [...] |
| 6230 | // |
| 6231 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6232 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6233 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6234 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6235 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 6236 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6237 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6238 | PrevDecl = Record; |
| 6239 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6240 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6241 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6242 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6243 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6244 | PrevDecl, |
| 6245 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6246 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6247 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6248 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6249 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6250 | return TagD; |
| 6251 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6252 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6253 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6254 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6255 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6256 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6257 | // 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] | 6258 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6259 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6260 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6261 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 6262 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 6263 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6264 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 6265 | << Pattern; |
| 6266 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6267 | } else { |
| 6268 | if (InstantiateClass(NameLoc, Record, Def, |
| 6269 | getTemplateInstantiationArgs(Record), |
| 6270 | TSK)) |
| 6271 | return true; |
| 6272 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6273 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6274 | if (!RecordDef) |
| 6275 | return true; |
| 6276 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6277 | } |
| 6278 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6279 | // Instantiate all of the members of the class. |
| 6280 | InstantiateClassMembers(NameLoc, RecordDef, |
| 6281 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6282 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6283 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 6284 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 6285 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6286 | // FIXME: We don't have any representation for explicit instantiations of |
| 6287 | // member classes. Such a representation is not needed for compilation, but it |
| 6288 | // should be available for clients that want to see all of the declarations in |
| 6289 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6290 | return TagD; |
| 6291 | } |
| 6292 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6293 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 6294 | SourceLocation ExternLoc, |
| 6295 | SourceLocation TemplateLoc, |
| 6296 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6297 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6298 | // TODO: check if/when DNInfo should replace Name. |
| 6299 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6300 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6301 | if (!Name) { |
| 6302 | if (!D.isInvalidType()) |
| 6303 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 6304 | diag::err_explicit_instantiation_requires_name) |
| 6305 | << D.getDeclSpec().getSourceRange() |
| 6306 | << D.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6307 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6308 | return true; |
| 6309 | } |
| 6310 | |
| 6311 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 6312 | // we find one that is. |
| 6313 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 6314 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 6315 | S = S->getParent(); |
| 6316 | |
| 6317 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6318 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 6319 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6320 | if (R.isNull()) |
| 6321 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6322 | |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6323 | // C++ [dcl.stc]p1: |
| 6324 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 6325 | // instantiation (14.7.2) directive. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6326 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6327 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 6328 | << Name; |
| 6329 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6330 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 6331 | != DeclSpec::SCS_unspecified) { |
| 6332 | // Complain about then remove the storage class specifier. |
| 6333 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 6334 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6335 | |
| 6336 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6337 | } |
| 6338 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6339 | // C++0x [temp.explicit]p1: |
| 6340 | // [...] An explicit instantiation of a function template shall not use the |
| 6341 | // inline or constexpr specifiers. |
| 6342 | // Presumably, this also applies to member functions of class templates as |
| 6343 | // well. |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6344 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6345 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6346 | getLangOptions().CPlusPlus0x ? |
| 6347 | diag::err_explicit_instantiation_inline : |
| 6348 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | fe6f648 | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 6349 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
| 6350 | if (D.getDeclSpec().isConstexprSpecified()) |
| 6351 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 6352 | // not already specified. |
| 6353 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 6354 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6355 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6356 | // C++0x [temp.explicit]p2: |
| 6357 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6358 | // definition and an explicit instantiation declaration. An explicit |
| 6359 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6360 | TemplateSpecializationKind TSK |
| 6361 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6362 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6363 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6364 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6365 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6366 | |
| 6367 | if (!R->isFunctionType()) { |
| 6368 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6369 | // A [...] static data member of a class template can be explicitly |
| 6370 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6371 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6372 | if (Previous.isAmbiguous()) |
| 6373 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6374 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6375 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6376 | if (!Prev || !Prev->isStaticDataMember()) { |
| 6377 | // We expect to see a data data member here. |
| 6378 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 6379 | << Name; |
| 6380 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6381 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6382 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6383 | return true; |
| 6384 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6385 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6386 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 6387 | // FIXME: Check for explicit specialization? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6388 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6389 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 6390 | << Prev; |
| 6391 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 6392 | // FIXME: Can we provide a note showing where this was declared? |
| 6393 | return true; |
| 6394 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6395 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6396 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6397 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6398 | // or a static data member of a class template specialization, the name of |
| 6399 | // the class template specialization in the qualified-id for the member |
| 6400 | // name shall be a simple-template-id. |
| 6401 | // |
| 6402 | // C++98 has the same restriction, just worded differently. |
| 6403 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6404 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6405 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6406 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6407 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6408 | // Check the scope of this explicit instantiation. |
| 6409 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6410 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6411 | // Verify that it is okay to explicitly instantiate here. |
| 6412 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 6413 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6414 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6415 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6416 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6417 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6418 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6419 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6420 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6421 | return (Decl*) 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6422 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6423 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6424 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6425 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6426 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6427 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6428 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6429 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6430 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6431 | |
| 6432 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6433 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6434 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6435 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6436 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 6437 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6438 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 6439 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6440 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 6441 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6442 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6443 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6444 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 6445 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6446 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6447 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6448 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6449 | // A [...] function [...] can be explicitly instantiated from its template. |
| 6450 | // A member function [...] of a class template can be explicitly |
| 6451 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6452 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6453 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6454 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6455 | P != PEnd; ++P) { |
| 6456 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6457 | if (!HasExplicitTemplateArgs) { |
| 6458 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 6459 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 6460 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6461 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6462 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6463 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 6464 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6465 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6466 | } |
| 6467 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6468 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6469 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 6470 | if (!FunTmpl) |
| 6471 | continue; |
| 6472 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6473 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6474 | FunctionDecl *Specialization = 0; |
| 6475 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6476 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6477 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6478 | R, Specialization, Info)) { |
| 6479 | // FIXME: Keep track of almost-matches? |
| 6480 | (void)TDK; |
| 6481 | continue; |
| 6482 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6483 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6484 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6485 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6486 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6487 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6488 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6489 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6490 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6491 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 6492 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 6493 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6494 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6495 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6496 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6497 | |
| 6498 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 6499 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6500 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6501 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6502 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6503 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 6504 | << Specialization |
| 6505 | << (Specialization->getTemplateSpecializationKind() == |
| 6506 | TSK_ExplicitSpecialization); |
| 6507 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 6508 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6509 | } |
| 6510 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 6511 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6512 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 6513 | PrevDecl = Specialization; |
| 6514 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6515 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6516 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6517 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6518 | PrevDecl, |
| 6519 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6520 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6521 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6522 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6523 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6524 | // FIXME: We may still want to build some representation of this |
| 6525 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6526 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6527 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6528 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 6529 | |
| 6530 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 256fc4d | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 6531 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 6532 | if (Attr) |
| 6533 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6534 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6535 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6536 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6537 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6538 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6539 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6540 | // or a static data member of a class template specialization, the name of |
| 6541 | // the class template specialization in the qualified-id for the member |
| 6542 | // name shall be a simple-template-id. |
| 6543 | // |
| 6544 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6545 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6546 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6547 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6548 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6549 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6550 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6551 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6552 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6553 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6554 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6555 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6556 | D.getIdentifierLoc(), |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6557 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6558 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6559 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6560 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6561 | } |
| 6562 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6563 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6564 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 6565 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 6566 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 6567 | // This has to hold, because SS is expected to be defined. |
| 6568 | assert(Name && "Expected a name in a dependent tag"); |
| 6569 | |
| 6570 | NestedNameSpecifier *NNS |
| 6571 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6572 | if (!NNS) |
| 6573 | return true; |
| 6574 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6575 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 6576 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6577 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 6578 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6579 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6580 | return true; |
| 6581 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6582 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6583 | // Create the resulting type. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6584 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6585 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 6586 | |
| 6587 | // Create type-source location information for this type. |
| 6588 | TypeLocBuilder TLB; |
| 6589 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame^] | 6590 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6591 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6592 | TL.setNameLoc(NameLoc); |
| 6593 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6594 | } |
| 6595 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6596 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6597 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6598 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6599 | SourceLocation IdLoc) { |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6600 | if (SS.isInvalid()) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6601 | return true; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6602 | |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6603 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6604 | Diag(TypenameLoc, |
| 6605 | getLangOptions().CPlusPlus0x ? |
| 6606 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6607 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6608 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6609 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6610 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6611 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 6612 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 6613 | if (T.isNull()) |
| 6614 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6615 | |
| 6616 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6617 | if (isa<DependentNameType>(T)) { |
| 6618 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame^] | 6619 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6620 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6621 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6622 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6623 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame^] | 6624 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6625 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6626 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6627 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6628 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6629 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6630 | } |
| 6631 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6632 | TypeResult |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6633 | Sema::ActOnTypenameType(Scope *S, |
| 6634 | SourceLocation TypenameLoc, |
| 6635 | const CXXScopeSpec &SS, |
| 6636 | SourceLocation TemplateKWLoc, |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6637 | TemplateTy TemplateIn, |
| 6638 | SourceLocation TemplateNameLoc, |
| 6639 | SourceLocation LAngleLoc, |
| 6640 | ASTTemplateArgsPtr TemplateArgsIn, |
| 6641 | SourceLocation RAngleLoc) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6642 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6643 | Diag(TypenameLoc, |
| 6644 | getLangOptions().CPlusPlus0x ? |
| 6645 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6646 | diag::ext_typename_outside_of_template) |
| 6647 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6648 | |
| 6649 | // Translate the parser's template argument list in our AST format. |
| 6650 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 6651 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 6652 | |
| 6653 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6654 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 6655 | // Construct a dependent template specialization type. |
| 6656 | assert(DTN && "dependent template has non-dependent name?"); |
| 6657 | assert(DTN->getQualifier() |
| 6658 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 6659 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 6660 | DTN->getQualifier(), |
| 6661 | DTN->getIdentifier(), |
| 6662 | TemplateArgs); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6663 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6664 | // Create source-location information for this type. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6665 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6666 | DependentTemplateSpecializationTypeLoc SpecTL |
| 6667 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6668 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 6669 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6670 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6671 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6672 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6673 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6674 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6675 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 6676 | } |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6677 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6678 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 6679 | if (T.isNull()) |
| 6680 | return true; |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6681 | |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6682 | // Provide source-location information for the template specialization type. |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6683 | TypeLocBuilder Builder; |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6684 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6685 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6686 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 6687 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6688 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6689 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6690 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6691 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 6692 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6693 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 6694 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame^] | 6695 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6696 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6697 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6698 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 6699 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 6700 | } |
| 6701 | |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6702 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6703 | /// \brief Build the type that describes a C++ typename specifier, |
| 6704 | /// e.g., "typename T::type". |
| 6705 | QualType |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6706 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 6707 | SourceLocation KeywordLoc, |
| 6708 | NestedNameSpecifierLoc QualifierLoc, |
| 6709 | const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6710 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6711 | CXXScopeSpec SS; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6712 | SS.Adopt(QualifierLoc); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6713 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6714 | DeclContext *Ctx = computeDeclContext(SS); |
| 6715 | if (!Ctx) { |
| 6716 | // If the nested-name-specifier is dependent and couldn't be |
| 6717 | // resolved to a type, build a typename type. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6718 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 6719 | return Context.getDependentNameType(Keyword, |
| 6720 | QualifierLoc.getNestedNameSpecifier(), |
| 6721 | &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 6722 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6723 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6724 | // If the nested-name-specifier refers to the current instantiation, |
| 6725 | // the "typename" keyword itself is superfluous. In C++03, the |
| 6726 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 6727 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 6728 | // 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] | 6729 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6730 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 6731 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6732 | |
| 6733 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6734 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6735 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6736 | unsigned DiagID = 0; |
| 6737 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6738 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6739 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6740 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6741 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6742 | |
| 6743 | case LookupResult::FoundUnresolvedValue: { |
| 6744 | // We found a using declaration that is a value. Most likely, the using |
| 6745 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6746 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6747 | IILoc); |
| 6748 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 6749 | << Name << Ctx << FullRange; |
| 6750 | if (UnresolvedUsingValueDecl *Using |
| 6751 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6752 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6753 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 6754 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 6755 | } |
| 6756 | } |
| 6757 | // Fall through to create a dependent typename type, from which we can recover |
| 6758 | // better. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6759 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 6760 | case LookupResult::NotFoundInCurrentInstantiation: |
| 6761 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6762 | return Context.getDependentNameType(Keyword, |
| 6763 | QualifierLoc.getNestedNameSpecifier(), |
| 6764 | &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6765 | |
| 6766 | case LookupResult::Found: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6767 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6768 | // We found a type. Build an ElaboratedType, since the |
| 6769 | // typename-specifier was just sugar. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6770 | return Context.getElaboratedType(ETK_Typename, |
| 6771 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6772 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6773 | } |
| 6774 | |
| 6775 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6776 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6777 | break; |
| 6778 | |
| 6779 | case LookupResult::FoundOverloaded: |
| 6780 | DiagID = diag::err_typename_nested_not_type; |
| 6781 | Referenced = *Result.begin(); |
| 6782 | break; |
| 6783 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 6784 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6785 | return QualType(); |
| 6786 | } |
| 6787 | |
| 6788 | // If we get here, it's because name lookup did not find a |
| 6789 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6790 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6791 | IILoc); |
| 6792 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6793 | if (Referenced) |
| 6794 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 6795 | << Name; |
| 6796 | return QualType(); |
| 6797 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6798 | |
| 6799 | namespace { |
| 6800 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 6801 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6802 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6803 | SourceLocation Loc; |
| 6804 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6805 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6806 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 6807 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6808 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6809 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6810 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6811 | DeclarationName Entity) |
| 6812 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6813 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6814 | |
| 6815 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6816 | /// transformed. |
| 6817 | /// |
| 6818 | /// For the purposes of type reconstruction, a type has already been |
| 6819 | /// transformed if it is NULL or if it is not dependent. |
| 6820 | bool AlreadyTransformed(QualType T) { |
| 6821 | return T.isNull() || !T->isDependentType(); |
| 6822 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6823 | |
| 6824 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6825 | /// rebuilt. |
| 6826 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6827 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6828 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 6829 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6830 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6831 | /// \brief Sets the "base" location and entity when that |
| 6832 | /// information is known based on another transformation. |
| 6833 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 6834 | this->Loc = Loc; |
| 6835 | this->Entity = Entity; |
| 6836 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6837 | }; |
| 6838 | } |
| 6839 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6840 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 6841 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6842 | /// 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] | 6843 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6844 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6845 | /// partial specialization thereof). This routine will rebuild that type now |
| 6846 | /// that we have entered the declarator's scope, which may produce different |
| 6847 | /// canonical types, e.g., |
| 6848 | /// |
| 6849 | /// \code |
| 6850 | /// template<typename T> |
| 6851 | /// struct X { |
| 6852 | /// typedef T* pointer; |
| 6853 | /// pointer data(); |
| 6854 | /// }; |
| 6855 | /// |
| 6856 | /// template<typename T> |
| 6857 | /// typename X<T>::pointer X<T>::data() { ... } |
| 6858 | /// \endcode |
| 6859 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 6860 | /// 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] | 6861 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 6862 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6863 | /// 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] | 6864 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 6865 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6866 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 6867 | SourceLocation Loc, |
| 6868 | DeclarationName Name) { |
| 6869 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6870 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6871 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6872 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 6873 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 6874 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6875 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6876 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6877 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 6878 | DeclarationName()); |
| 6879 | return Rebuilder.TransformExpr(E); |
| 6880 | } |
| 6881 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6882 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6883 | if (SS.isInvalid()) |
| 6884 | return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6885 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6886 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6887 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 6888 | DeclarationName()); |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6889 | NestedNameSpecifierLoc Rebuilt |
| 6890 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 6891 | if (!Rebuilt) |
| 6892 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6893 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6894 | SS.Adopt(Rebuilt); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6895 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6896 | } |
| 6897 | |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 6898 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 6899 | /// instantiation. |
| 6900 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 6901 | TemplateParameterList *Params) { |
| 6902 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 6903 | Decl *Param = Params->getParam(I); |
| 6904 | |
| 6905 | // There is nothing to rebuild in a type parameter. |
| 6906 | if (isa<TemplateTypeParmDecl>(Param)) |
| 6907 | continue; |
| 6908 | |
| 6909 | // Rebuild the template parameter list of a template template parameter. |
| 6910 | if (TemplateTemplateParmDecl *TTP |
| 6911 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 6912 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 6913 | TTP->getTemplateParameters())) |
| 6914 | return true; |
| 6915 | |
| 6916 | continue; |
| 6917 | } |
| 6918 | |
| 6919 | // Rebuild the type of a non-type template parameter. |
| 6920 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 6921 | TypeSourceInfo *NewTSI |
| 6922 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 6923 | NTTP->getLocation(), |
| 6924 | NTTP->getDeclName()); |
| 6925 | if (!NewTSI) |
| 6926 | return true; |
| 6927 | |
| 6928 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 6929 | NTTP->setTypeSourceInfo(NewTSI); |
| 6930 | NTTP->setType(NewTSI->getType()); |
| 6931 | } |
| 6932 | } |
| 6933 | |
| 6934 | return false; |
| 6935 | } |
| 6936 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6937 | /// \brief Produces a formatted string that describes the binding of |
| 6938 | /// template parameters to template arguments. |
| 6939 | std::string |
| 6940 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6941 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6942 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6943 | } |
| 6944 | |
| 6945 | std::string |
| 6946 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6947 | const TemplateArgument *Args, |
| 6948 | unsigned NumArgs) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6949 | SmallString<128> Str; |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6950 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6951 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6952 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6953 | return std::string(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6954 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6955 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6956 | if (I >= NumArgs) |
| 6957 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6958 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6959 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6960 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6961 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6962 | Out << ", "; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6963 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6964 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6965 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6966 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6967 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6968 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6969 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6970 | Out << " = "; |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6971 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6972 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6973 | |
| 6974 | Out << ']'; |
| 6975 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6976 | } |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 6977 | |
| 6978 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) { |
| 6979 | if (!FD) |
| 6980 | return; |
| 6981 | FD->setLateTemplateParsed(Flag); |
| 6982 | } |
| 6983 | |
| 6984 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 6985 | DeclContext *DC = CurContext; |
| 6986 | |
| 6987 | while (DC) { |
| 6988 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 6989 | const FunctionDecl *FD = RD->isLocalClass(); |
| 6990 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 6991 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 6992 | return false; |
| 6993 | |
| 6994 | DC = DC->getParent(); |
| 6995 | } |
| 6996 | return false; |
| 6997 | } |