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" |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 30 | using namespace clang; |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 31 | using namespace sema; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 32 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 33 | // Exported for use by Parser. |
| 34 | SourceRange |
| 35 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
| 36 | unsigned N) { |
| 37 | if (!N) return SourceRange(); |
| 38 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
| 39 | } |
| 40 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 41 | /// \brief Determine whether the declaration found is acceptable as the name |
| 42 | /// of a template and, if so, return that template declaration. Otherwise, |
| 43 | /// returns NULL. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 44 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, |
| 45 | NamedDecl *Orig) { |
| 46 | NamedDecl *D = Orig->getUnderlyingDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 48 | if (isa<TemplateDecl>(D)) |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 49 | return Orig; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 51 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 52 | // C++ [temp.local]p1: |
| 53 | // Like normal (non-template) classes, class templates have an |
| 54 | // injected-class-name (Clause 9). The injected-class-name |
| 55 | // can be used with or without a template-argument-list. When |
| 56 | // it is used without a template-argument-list, it is |
| 57 | // equivalent to the injected-class-name followed by the |
| 58 | // template-parameters of the class template enclosed in |
| 59 | // <>. When it is used with a template-argument-list, it |
| 60 | // refers to the specified class template specialization, |
| 61 | // which could be the current specialization or another |
| 62 | // specialization. |
| 63 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 542b548 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 64 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 65 | if (Record->getDescribedClassTemplate()) |
| 66 | return Record->getDescribedClassTemplate(); |
| 67 | |
| 68 | if (ClassTemplateSpecializationDecl *Spec |
| 69 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 70 | return Spec->getSpecializedTemplate(); |
| 71 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 73 | return 0; |
| 74 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 79 | void Sema::FilterAcceptableTemplateNames(LookupResult &R) { |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 80 | // The set of class templates we've already seen. |
| 81 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 82 | LookupResult::Filter filter = R.makeFilter(); |
| 83 | while (filter.hasNext()) { |
| 84 | NamedDecl *Orig = filter.next(); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 85 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 86 | if (!Repl) |
| 87 | filter.erase(); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 88 | else if (Repl != Orig) { |
| 89 | |
| 90 | // C++ [temp.local]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 91 | // 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] | 92 | // 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] | 93 | // one base class). If all of the injected-class-names that are found |
| 94 | // refer to specializations of the same class template, and if the name |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 95 | // is used as a template-name, the reference refers to the class |
| 96 | // template itself and not a specialization thereof, and is not |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 97 | // ambiguous. |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 98 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
| 99 | if (!ClassTemplates.insert(ClassTmpl)) { |
| 100 | filter.erase(); |
| 101 | continue; |
| 102 | } |
John McCall | 8ba6691 | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 103 | |
| 104 | // FIXME: we promote access to public here as a workaround to |
| 105 | // the fact that LookupResult doesn't let us remember that we |
| 106 | // found this template through a particular injected class name, |
| 107 | // which means we end up doing nasty things to the invariants. |
| 108 | // Pretending that access is public is *much* safer. |
| 109 | filter.replace(Repl, AS_public); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 110 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 111 | } |
| 112 | filter.done(); |
| 113 | } |
| 114 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 115 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) { |
| 116 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
| 117 | if (isAcceptableTemplateName(Context, *I)) |
| 118 | return true; |
| 119 | |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 120 | return false; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 123 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 124 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 125 | bool hasTemplateKeyword, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 126 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 127 | ParsedType ObjectTypePtr, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 128 | bool EnteringContext, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 129 | TemplateTy &TemplateResult, |
| 130 | bool &MemberOfUnknownSpecialization) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 131 | assert(getLangOptions().CPlusPlus && "No template names in C!"); |
| 132 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 133 | DeclarationName TName; |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 134 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 135 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 136 | switch (Name.getKind()) { |
| 137 | case UnqualifiedId::IK_Identifier: |
| 138 | TName = DeclarationName(Name.Identifier); |
| 139 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 140 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 141 | case UnqualifiedId::IK_OperatorFunctionId: |
| 142 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 143 | Name.OperatorFunctionId.Operator); |
| 144 | break; |
| 145 | |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 146 | case UnqualifiedId::IK_LiteralOperatorId: |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 147 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 148 | break; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 149 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 150 | default: |
| 151 | return TNK_Non_template; |
| 152 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 154 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 156 | LookupResult R(*this, TName, Name.getSourceRange().getBegin(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 157 | LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 158 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 159 | MemberOfUnknownSpecialization); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 160 | if (R.empty()) return TNK_Non_template; |
| 161 | if (R.isAmbiguous()) { |
| 162 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 163 | R.suppressDiagnostics(); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 164 | |
| 165 | // FIXME: we might have ambiguous templates, in which case we |
| 166 | // should at least parse them properly! |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 167 | return TNK_Non_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 168 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 169 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 170 | TemplateName Template; |
| 171 | TemplateNameKind TemplateKind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 173 | unsigned ResultCount = R.end() - R.begin(); |
| 174 | if (ResultCount > 1) { |
| 175 | // We assume that we'll preserve the qualifier from a function |
| 176 | // template name in other ways. |
| 177 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 178 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 179 | |
| 180 | // We'll do this lookup again later. |
| 181 | R.suppressDiagnostics(); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 182 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 183 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 184 | |
| 185 | if (SS.isSet() && !SS.isInvalid()) { |
| 186 | NestedNameSpecifier *Qualifier |
| 187 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 188 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 189 | hasTemplateKeyword, TD); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 190 | } else { |
| 191 | Template = TemplateName(TD); |
| 192 | } |
| 193 | |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 194 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 195 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 196 | |
| 197 | // We'll do this lookup again later. |
| 198 | R.suppressDiagnostics(); |
| 199 | } else { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 200 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
| 201 | isa<TypeAliasTemplateDecl>(TD)); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 202 | TemplateKind = TNK_Type_template; |
| 203 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 204 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 206 | TemplateResult = TemplateTy::make(Template); |
| 207 | return TemplateKind; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 208 | } |
| 209 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 210 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 211 | SourceLocation IILoc, |
| 212 | Scope *S, |
| 213 | const CXXScopeSpec *SS, |
| 214 | TemplateTy &SuggestedTemplate, |
| 215 | TemplateNameKind &SuggestedKind) { |
| 216 | // We can't recover unless there's a dependent scope specifier preceding the |
| 217 | // template name. |
Douglas Gregor | d5ab9b0 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 218 | // FIXME: Typo correction? |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 219 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 220 | computeDeclContext(*SS)) |
| 221 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 222 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 223 | // The code is missing a 'template' keyword prior to the dependent template |
| 224 | // name. |
| 225 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 226 | Diag(IILoc, diag::err_template_kw_missing) |
| 227 | << Qualifier << II.getName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 228 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 229 | SuggestedTemplate |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 230 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 231 | SuggestedKind = TNK_Dependent_template_name; |
| 232 | return true; |
| 233 | } |
| 234 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 235 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 236 | Scope *S, CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 237 | QualType ObjectType, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 238 | bool EnteringContext, |
| 239 | bool &MemberOfUnknownSpecialization) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 240 | // Determine where to perform name lookup |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 241 | MemberOfUnknownSpecialization = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 242 | DeclContext *LookupCtx = 0; |
| 243 | bool isDependent = false; |
| 244 | if (!ObjectType.isNull()) { |
| 245 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 246 | // x->B::f, and we are looking into the type of the object. |
| 247 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 248 | LookupCtx = computeDeclContext(ObjectType); |
| 249 | isDependent = ObjectType->isDependentType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 250 | assert((isDependent || !ObjectType->isIncompleteType()) && |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 251 | "Caller should have completed object type"); |
| 252 | } else if (SS.isSet()) { |
| 253 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 254 | // so long into the context associated with the prior nested-name-specifier. |
| 255 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 256 | isDependent = isDependentScopeSpecifier(SS); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 257 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 258 | // The declaration context must be complete. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 259 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 260 | return; |
| 261 | } |
| 262 | |
| 263 | bool ObjectTypeSearchedInScope = false; |
| 264 | if (LookupCtx) { |
| 265 | // Perform "qualified" name lookup into the declaration context we |
| 266 | // computed, which is either the type of the base of a member access |
| 267 | // expression or the declaration context associated with a prior |
| 268 | // nested-name-specifier. |
| 269 | LookupQualifiedName(Found, LookupCtx); |
| 270 | |
| 271 | if (!ObjectType.isNull() && Found.empty()) { |
| 272 | // C++ [basic.lookup.classref]p1: |
| 273 | // In a class member access expression (5.2.5), if the . or -> token is |
| 274 | // immediately followed by an identifier followed by a <, the |
| 275 | // identifier must be looked up to determine whether the < is the |
| 276 | // beginning of a template argument list (14.2) or a less-than operator. |
| 277 | // The identifier is first looked up in the class of the object |
| 278 | // expression. If the identifier is not found, it is then looked up in |
| 279 | // the context of the entire postfix-expression and shall name a class |
| 280 | // or function template. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 281 | if (S) LookupName(Found, S); |
| 282 | ObjectTypeSearchedInScope = true; |
| 283 | } |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 284 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 285 | // We cannot look into a dependent object type or nested nme |
| 286 | // specifier. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 287 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 288 | return; |
| 289 | } else { |
| 290 | // Perform unqualified name lookup in the current scope. |
| 291 | LookupName(Found, S); |
| 292 | } |
| 293 | |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 294 | if (Found.empty() && !isDependent) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 295 | // If we did not find any names, attempt to correct any typos. |
| 296 | DeclarationName Name = Found.getLookupName(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 297 | if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx, |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 298 | false, CTC_CXXCasts)) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 299 | FilterAcceptableTemplateNames(Found); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 300 | if (!Found.empty()) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 301 | if (LookupCtx) |
| 302 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) |
| 303 | << Name << LookupCtx << Found.getLookupName() << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 304 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 305 | Found.getLookupName().getAsString()); |
| 306 | else |
| 307 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) |
| 308 | << Name << Found.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 309 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 310 | Found.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 311 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) |
| 312 | Diag(Template->getLocation(), diag::note_previous_decl) |
| 313 | << Template->getDeclName(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 314 | } |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 315 | } else { |
| 316 | Found.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 317 | Found.setLookupName(Name); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 321 | FilterAcceptableTemplateNames(Found); |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 322 | if (Found.empty()) { |
| 323 | if (isDependent) |
| 324 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 325 | return; |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 326 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 327 | |
| 328 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
| 329 | // C++ [basic.lookup.classref]p1: |
| 330 | // [...] If the lookup in the class of the object expression finds a |
| 331 | // template, the name is also looked up in the context of the entire |
| 332 | // postfix-expression and [...] |
| 333 | // |
| 334 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 335 | LookupOrdinaryName); |
| 336 | LookupName(FoundOuter, S); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 337 | FilterAcceptableTemplateNames(FoundOuter); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 338 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 339 | if (FoundOuter.empty()) { |
| 340 | // - if the name is not found, the name found in the class of the |
| 341 | // object expression is used, otherwise |
| 342 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) { |
| 343 | // - if the name is found in the context of the entire |
| 344 | // postfix-expression and does not name a class template, the name |
| 345 | // found in the class of the object expression is used, otherwise |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 346 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 347 | // - if the name found is a class template, it must refer to the same |
| 348 | // entity as the one found in the class of the object expression, |
| 349 | // otherwise the program is ill-formed. |
| 350 | if (!Found.isSingleResult() || |
| 351 | Found.getFoundDecl()->getCanonicalDecl() |
| 352 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 353 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 21d07e4 | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 354 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 355 | << Found.getLookupName() |
| 356 | << ObjectType; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 357 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 358 | diag::note_ambig_member_ref_object_type) |
| 359 | << ObjectType; |
| 360 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 361 | diag::note_ambig_member_ref_scope); |
| 362 | |
| 363 | // Recover by taking the template that we found in the object |
| 364 | // expression's type. |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 370 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 371 | /// was just parsed. This is only possible with an explicit scope |
| 372 | /// specifier naming a dependent type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 373 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 374 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 375 | const DeclarationNameInfo &NameInfo, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 376 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 377 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 378 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 379 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 380 | if (!isAddressOfOperand && |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 381 | isa<CXXMethodDecl>(DC) && |
| 382 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 383 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 384 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 385 | // Since the 'this' expression is synthesized, we don't need to |
| 386 | // perform the double-lookup check. |
| 387 | NamedDecl *FirstQualifierInScope = 0; |
| 388 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 389 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 390 | /*This*/ 0, ThisType, |
| 391 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 392 | /*Op*/ SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 393 | SS.getWithLocInContext(Context), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 394 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 395 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 396 | TemplateArgs)); |
| 397 | } |
| 398 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 399 | return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 400 | } |
| 401 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 402 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 403 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 404 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 405 | const TemplateArgumentListInfo *TemplateArgs) { |
| 406 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 407 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 408 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 409 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 412 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 413 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 414 | /// declaration at location Loc. Returns true to indicate that this is |
| 415 | /// an error, and false otherwise. |
| 416 | bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 417 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 418 | |
| 419 | // Microsoft Visual C++ permits template parameters to be shadowed. |
| 420 | if (getLangOptions().Microsoft) |
| 421 | return false; |
| 422 | |
| 423 | // C++ [temp.local]p4: |
| 424 | // A template-parameter shall not be redeclared within its |
| 425 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 427 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 428 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
| 429 | return true; |
| 430 | } |
| 431 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 432 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 433 | /// the parameter D to reference the templated declaration and return a pointer |
| 434 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 435 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 436 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 437 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 438 | return Temp; |
| 439 | } |
| 440 | return 0; |
| 441 | } |
| 442 | |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 443 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 444 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 445 | assert(Kind == Template && |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 446 | "Only template template arguments can be pack expansions here"); |
| 447 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 448 | "Template template argument pack expansion without packs"); |
| 449 | ParsedTemplateArgument Result(*this); |
| 450 | Result.EllipsisLoc = EllipsisLoc; |
| 451 | return Result; |
| 452 | } |
| 453 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 454 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 455 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 456 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 457 | switch (Arg.getKind()) { |
| 458 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 459 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 460 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 461 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 462 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 463 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 464 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 465 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 466 | case ParsedTemplateArgument::NonType: { |
| 467 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 468 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 469 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 470 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 471 | case ParsedTemplateArgument::Template: { |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 472 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 473 | TemplateArgument TArg; |
| 474 | if (Arg.getEllipsisLoc().isValid()) |
| 475 | TArg = TemplateArgument(Template, llvm::Optional<unsigned int>()); |
| 476 | else |
| 477 | TArg = Template; |
| 478 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 479 | Arg.getScopeSpec().getWithLocInContext( |
| 480 | SemaRef.Context), |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 481 | Arg.getLocation(), |
| 482 | Arg.getEllipsisLoc()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 483 | } |
| 484 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 485 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 486 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 487 | return TemplateArgumentLoc(); |
| 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 | /// \brief Translates template arguments as provided by the parser |
| 491 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 492 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 493 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 494 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 495 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 496 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 497 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 498 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 499 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 500 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 501 | /// the keyword "typename" was used to declare the type parameter |
| 502 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 503 | /// "class" or "typename" keyword. ParamName is the name of the |
| 504 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 505 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 506 | /// If the type parameter has a default argument, it will be added |
| 507 | /// later via ActOnTypeParameterDefault. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 508 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
| 509 | SourceLocation EllipsisLoc, |
| 510 | SourceLocation KeyLoc, |
| 511 | IdentifierInfo *ParamName, |
| 512 | SourceLocation ParamNameLoc, |
| 513 | unsigned Depth, unsigned Position, |
| 514 | SourceLocation EqualLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 515 | ParsedType DefaultArg) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | assert(S->isTemplateParamScope() && |
| 517 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 518 | bool Invalid = false; |
| 519 | |
| 520 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 521 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 522 | LookupOrdinaryName, |
| 523 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 524 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 525 | Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 526 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 529 | SourceLocation Loc = ParamNameLoc; |
| 530 | if (!ParamName) |
| 531 | Loc = KeyLoc; |
| 532 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 533 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 534 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 535 | KeyLoc, Loc, Depth, Position, ParamName, |
| 536 | Typename, Ellipsis); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 537 | Param->setAccess(AS_public); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 538 | if (Invalid) |
| 539 | Param->setInvalidDecl(); |
| 540 | |
| 541 | if (ParamName) { |
| 542 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 543 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 544 | IdResolver.AddDecl(Param); |
| 545 | } |
| 546 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 547 | // C++0x [temp.param]p9: |
| 548 | // A default template-argument may be specified for any kind of |
| 549 | // template-parameter that is not a template parameter pack. |
| 550 | if (DefaultArg && Ellipsis) { |
| 551 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 552 | DefaultArg = ParsedType(); |
| 553 | } |
| 554 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 555 | // Handle the default argument, if provided. |
| 556 | if (DefaultArg) { |
| 557 | TypeSourceInfo *DefaultTInfo; |
| 558 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 559 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 560 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 561 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 562 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 563 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 564 | UPPC_DefaultArgument)) |
| 565 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 566 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 567 | // Check the template argument itself. |
| 568 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 569 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 570 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 571 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 572 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 573 | Param->setDefaultArgument(DefaultTInfo, false); |
| 574 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 575 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 576 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 579 | /// \brief Check that the type of a non-type template parameter is |
| 580 | /// well-formed. |
| 581 | /// |
| 582 | /// \returns the (possibly-promoted) parameter type if valid; |
| 583 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 585 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 586 | // We don't allow variably-modified types as the type of non-type template |
| 587 | // parameters. |
| 588 | if (T->isVariablyModifiedType()) { |
| 589 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 590 | << T; |
| 591 | return QualType(); |
| 592 | } |
| 593 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 594 | // C++ [temp.param]p4: |
| 595 | // |
| 596 | // A non-type template-parameter shall have one of the following |
| 597 | // (optionally cv-qualified) types: |
| 598 | // |
| 599 | // -- integral or enumeration type, |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 600 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | // -- pointer to object or pointer to function, |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 602 | T->isPointerType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 603 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 604 | T->isReferenceType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 605 | // -- pointer to member, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 606 | T->isMemberPointerType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 607 | // -- std::nullptr_t. |
| 608 | T->isNullPtrType() || |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 609 | // If T is a dependent type, we can't do the check now, so we |
| 610 | // assume that it is well-formed. |
| 611 | T->isDependentType()) |
| 612 | return T; |
| 613 | // C++ [temp.param]p8: |
| 614 | // |
| 615 | // A non-type template-parameter of type "array of T" or |
| 616 | // "function returning T" is adjusted to be of type "pointer to |
| 617 | // T" or "pointer to function returning T", respectively. |
| 618 | else if (T->isArrayType()) |
| 619 | // FIXME: Keep the type prior to promotion? |
| 620 | return Context.getArrayDecayedType(T); |
| 621 | else if (T->isFunctionType()) |
| 622 | // FIXME: Keep the type prior to promotion? |
| 623 | return Context.getPointerType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 624 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 625 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 626 | << T; |
| 627 | |
| 628 | return QualType(); |
| 629 | } |
| 630 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 631 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 632 | unsigned Depth, |
| 633 | unsigned Position, |
| 634 | SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 635 | Expr *Default) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 636 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 637 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 638 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 639 | assert(S->isTemplateParamScope() && |
| 640 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 641 | bool Invalid = false; |
| 642 | |
| 643 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 644 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 645 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 646 | LookupOrdinaryName, |
| 647 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 648 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 649 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 650 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 653 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 654 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 655 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 656 | Invalid = true; |
| 657 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 658 | |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 659 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 660 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 661 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 662 | D.getSourceRange().getBegin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 663 | D.getIdentifierLoc(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 664 | Depth, Position, ParamName, T, |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 665 | IsParameterPack, TInfo); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 666 | Param->setAccess(AS_public); |
| 667 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 668 | if (Invalid) |
| 669 | Param->setInvalidDecl(); |
| 670 | |
| 671 | if (D.getIdentifier()) { |
| 672 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 673 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 674 | IdResolver.AddDecl(Param); |
| 675 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 676 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 677 | // C++0x [temp.param]p9: |
| 678 | // A default template-argument may be specified for any kind of |
| 679 | // template-parameter that is not a template parameter pack. |
| 680 | if (Default && IsParameterPack) { |
| 681 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 682 | Default = 0; |
| 683 | } |
| 684 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 685 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 686 | if (Default) { |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 687 | // Check for unexpanded parameter packs. |
| 688 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 689 | return Param; |
| 690 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 691 | TemplateArgument Converted; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 692 | ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
| 693 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 694 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 695 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 696 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 697 | Default = DefaultRes.take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 698 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 699 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 700 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 701 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 702 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 703 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 704 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 705 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 706 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 707 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 708 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 709 | SourceLocation TmpLoc, |
| 710 | TemplateParamsTy *Params, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 711 | SourceLocation EllipsisLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 712 | IdentifierInfo *Name, |
| 713 | SourceLocation NameLoc, |
| 714 | unsigned Depth, |
| 715 | unsigned Position, |
| 716 | SourceLocation EqualLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 717 | ParsedTemplateArgument Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 718 | assert(S->isTemplateParamScope() && |
| 719 | "Template template parameter not in template parameter scope!"); |
| 720 | |
| 721 | // Construct the parameter object. |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 722 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 723 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 724 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 725 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 726 | Depth, Position, IsParameterPack, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 727 | Name, Params); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 728 | Param->setAccess(AS_public); |
| 729 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 730 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 731 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 732 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 733 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 734 | IdResolver.AddDecl(Param); |
| 735 | } |
| 736 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 737 | if (Params->size() == 0) { |
| 738 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 739 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 740 | Param->setInvalidDecl(); |
| 741 | } |
| 742 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 743 | // C++0x [temp.param]p9: |
| 744 | // A default template-argument may be specified for any kind of |
| 745 | // template-parameter that is not a template parameter pack. |
| 746 | if (IsParameterPack && !Default.isInvalid()) { |
| 747 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 748 | Default = ParsedTemplateArgument(); |
| 749 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 750 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 751 | if (!Default.isInvalid()) { |
| 752 | // Check only that we have a template template argument. We don't want to |
| 753 | // try to check well-formedness now, because our template template parameter |
| 754 | // might have dependent types in its template parameters, which we wouldn't |
| 755 | // be able to match now. |
| 756 | // |
| 757 | // If none of the template template parameter's template arguments mention |
| 758 | // other template parameters, we could actually perform more checking here. |
| 759 | // However, it isn't worth doing. |
| 760 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 761 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 762 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 763 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 764 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 765 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 766 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 767 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 768 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 769 | DefaultArg.getArgument().getAsTemplate(), |
| 770 | UPPC_DefaultArgument)) |
| 771 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 772 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 773 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 774 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 775 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 776 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 779 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 780 | /// contains the template parameters in Params/NumParams. |
| 781 | Sema::TemplateParamsTy * |
| 782 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 783 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 784 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 785 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 786 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 787 | SourceLocation RAngleLoc) { |
| 788 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 789 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 790 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 791 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 792 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 793 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 794 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 795 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 796 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 797 | if (SS.isSet()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 798 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 799 | } |
| 800 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 801 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 802 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 803 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 804 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 805 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 806 | TemplateParameterList *TemplateParams, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 807 | AccessSpecifier AS, |
| 808 | unsigned NumOuterTemplateParamLists, |
| 809 | TemplateParameterList** OuterTemplateParamLists) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 811 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 812 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 813 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 814 | |
| 815 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 816 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 817 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 818 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 819 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 820 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 821 | |
| 822 | // There is no such thing as an unnamed class template. |
| 823 | if (!Name) { |
| 824 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 825 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 829 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 830 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 831 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 832 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 833 | SemanticContext = computeDeclContext(SS, true); |
| 834 | if (!SemanticContext) { |
| 835 | // FIXME: Produce a reasonable diagnostic here |
| 836 | return true; |
| 837 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 839 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 840 | return true; |
| 841 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 842 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 843 | } else { |
| 844 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 845 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 846 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 848 | if (Previous.isAmbiguous()) |
| 849 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 850 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 851 | NamedDecl *PrevDecl = 0; |
| 852 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 853 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 854 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 855 | // If there is a previous declaration with the same name, check |
| 856 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 858 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 859 | |
| 860 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 861 | // class template partial specialization, or class template specialization. |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 862 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 863 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 864 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 865 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 866 | PrevClassTemplate |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 867 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 868 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 869 | PrevClassTemplate |
| 870 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 871 | ->getSpecializedTemplate(); |
| 872 | } |
| 873 | } |
| 874 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 875 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 876 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 877 | // [...] When looking for a prior declaration of a class or a function |
| 878 | // 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] | 879 | // function is neither a qualified name nor a template-id, scopes outside |
| 880 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 881 | if (!SS.isSet()) { |
| 882 | DeclContext *OutermostContext = CurContext; |
| 883 | while (!OutermostContext->isFileContext()) |
| 884 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 885 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 886 | if (PrevDecl && |
| 887 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 888 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 889 | SemanticContext = PrevDecl->getDeclContext(); |
| 890 | } else { |
| 891 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 892 | // context we computed is the semantic context for our new |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 893 | // declaration. |
| 894 | PrevDecl = PrevClassTemplate = 0; |
| 895 | SemanticContext = OutermostContext; |
| 896 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 897 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 898 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 899 | if (CurContext->isDependentContext()) { |
| 900 | // If this is a dependent context, we don't want to link the friend |
| 901 | // class template to the template in scope, because that would perform |
| 902 | // checking of the template parameter lists that can't be performed |
| 903 | // until the outer context is instantiated. |
| 904 | PrevDecl = PrevClassTemplate = 0; |
| 905 | } |
| 906 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 907 | PrevDecl = PrevClassTemplate = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 908 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 909 | if (PrevClassTemplate) { |
| 910 | // Ensure that the template parameter lists are compatible. |
| 911 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 912 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 913 | /*Complain=*/true, |
| 914 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 915 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 916 | |
| 917 | // C++ [temp.class]p4: |
| 918 | // In a redeclaration, partial specialization, explicit |
| 919 | // specialization or explicit instantiation of a class template, |
| 920 | // the class-key shall agree in kind with the original class |
| 921 | // template declaration (7.1.5.3). |
| 922 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 923 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
| 924 | TUK == TUK_Definition, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 925 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 926 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 927 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 928 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 929 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 932 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 933 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 934 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 935 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 936 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 937 | // FIXME: Would it make sense to try to "forget" the previous |
| 938 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 939 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 943 | // Maybe we will complain about the shadowed template parameter. |
| 944 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 945 | // Just pretend that we didn't see the previous declaration. |
| 946 | PrevDecl = 0; |
| 947 | } else if (PrevDecl) { |
| 948 | // C++ [temp]p5: |
| 949 | // A class template shall not have the same name as any other |
| 950 | // template, class, function, object, enumeration, enumerator, |
| 951 | // namespace, or type in the same scope (3.3), except as specified |
| 952 | // in (14.5.4). |
| 953 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 954 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 955 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 958 | // Check the template parameter list of this declaration, possibly |
| 959 | // merging in the template parameter list from the previous class |
| 960 | // template declaration. |
| 961 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 962 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 963 | (SS.isSet() && SemanticContext && |
Douglas Gregor | 461bf2e | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 964 | SemanticContext->isRecord() && |
| 965 | SemanticContext->isDependentContext()) |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 966 | ? TPC_ClassTemplateMember |
| 967 | : TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 968 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 970 | if (SS.isSet()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 971 | // 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] | 972 | // template out-of-line. |
| 973 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
| 974 | !(TUK == TUK_Friend && CurContext->isDependentContext())) |
| 975 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 976 | << Name << SemanticContext << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | CXXRecordDecl *NewClass = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 980 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 981 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 982 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 983 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 984 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 985 | if (NumOuterTemplateParamLists > 0) |
| 986 | NewClass->setTemplateParameterListsInfo(Context, |
| 987 | NumOuterTemplateParamLists, |
| 988 | OuterTemplateParamLists); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 989 | |
| 990 | ClassTemplateDecl *NewTemplate |
| 991 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 992 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 993 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 994 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 995 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 996 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 997 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 998 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 999 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1000 | (void)T; |
| 1001 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1002 | // 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] | 1003 | // class template, make a note of that. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1004 | if (PrevClassTemplate && |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1005 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1006 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1007 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1008 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1009 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1010 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1011 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1012 | // Set the lexical context of these templates |
| 1013 | NewClass->setLexicalDeclContext(CurContext); |
| 1014 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1015 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1016 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1017 | NewClass->startDefinition(); |
| 1018 | |
| 1019 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1020 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1021 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1022 | if (TUK != TUK_Friend) |
| 1023 | PushOnScopeChains(NewTemplate, S); |
| 1024 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1025 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1026 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1027 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1028 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1029 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1030 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1031 | PrevClassTemplate != NULL); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1032 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1033 | // Friend templates are visible in fairly strange ways. |
| 1034 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1035 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1036 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 1037 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1038 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1039 | /* AddToContext = */ false); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1040 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1041 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1042 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1043 | NewClass->getLocation(), |
| 1044 | NewTemplate, |
| 1045 | /*FIXME:*/NewClass->getLocation()); |
| 1046 | Friend->setAccess(AS_public); |
| 1047 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1048 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1049 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1050 | if (Invalid) { |
| 1051 | NewTemplate->setInvalidDecl(); |
| 1052 | NewClass->setInvalidDecl(); |
| 1053 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1054 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1057 | /// \brief Diagnose the presence of a default template argument on a |
| 1058 | /// template parameter, which is ill-formed in certain contexts. |
| 1059 | /// |
| 1060 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1061 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1062 | Sema::TemplateParamListContext TPC, |
| 1063 | SourceLocation ParamLoc, |
| 1064 | SourceRange DefArgRange) { |
| 1065 | switch (TPC) { |
| 1066 | case Sema::TPC_ClassTemplate: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1067 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1068 | return false; |
| 1069 | |
| 1070 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1071 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1072 | // C++ [temp.param]p9: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1073 | // A default template-argument shall not be specified in a |
| 1074 | // function template declaration or a function template |
| 1075 | // definition [...] |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1076 | // If a friend function template declaration specifies a default |
| 1077 | // template-argument, that declaration shall be a definition and shall be |
| 1078 | // the only declaration of the function template in the translation unit. |
| 1079 | // (C++98/03 doesn't have this wording; see DR226). |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1080 | if (!S.getLangOptions().CPlusPlus0x) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1081 | S.Diag(ParamLoc, |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1082 | diag::ext_template_parameter_default_in_function_template) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1083 | << DefArgRange; |
| 1084 | return false; |
| 1085 | |
| 1086 | case Sema::TPC_ClassTemplateMember: |
| 1087 | // C++0x [temp.param]p9: |
| 1088 | // A default template-argument shall not be specified in the |
| 1089 | // template-parameter-lists of the definition of a member of a |
| 1090 | // class template that appears outside of the member's class. |
| 1091 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1092 | << DefArgRange; |
| 1093 | return true; |
| 1094 | |
| 1095 | case Sema::TPC_FriendFunctionTemplate: |
| 1096 | // C++ [temp.param]p9: |
| 1097 | // A default template-argument shall not be specified in a |
| 1098 | // friend template declaration. |
| 1099 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1100 | << DefArgRange; |
| 1101 | return true; |
| 1102 | |
| 1103 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1104 | // for friend function templates if there is only a single |
| 1105 | // declaration (and it is a definition). Strange! |
| 1106 | } |
| 1107 | |
| 1108 | return false; |
| 1109 | } |
| 1110 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1111 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1112 | /// of a template template parameter, recursively. |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1113 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1114 | TemplateTemplateParmDecl *TTP) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1115 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1116 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1117 | NamedDecl *P = Params->getParam(I); |
| 1118 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1119 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1120 | NTTP->getTypeSourceInfo(), |
| 1121 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1122 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1123 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1124 | continue; |
| 1125 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1126 | |
| 1127 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1128 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1129 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1130 | return true; |
| 1131 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1132 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1133 | return false; |
| 1134 | } |
| 1135 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1136 | /// \brief Checks the validity of a template parameter list, possibly |
| 1137 | /// considering the template parameter list from a previous |
| 1138 | /// declaration. |
| 1139 | /// |
| 1140 | /// If an "old" template parameter list is provided, it must be |
| 1141 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1142 | /// template parameter list. |
| 1143 | /// |
| 1144 | /// \param NewParams Template parameter list for a new template |
| 1145 | /// declaration. This template parameter list will be updated with any |
| 1146 | /// default arguments that are carried through from the previous |
| 1147 | /// template parameter list. |
| 1148 | /// |
| 1149 | /// \param OldParams If provided, template parameter list from a |
| 1150 | /// previous declaration of the same template. Default template |
| 1151 | /// arguments will be merged from the old template parameter list to |
| 1152 | /// the new template parameter list. |
| 1153 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1154 | /// \param TPC Describes the context in which we are checking the given |
| 1155 | /// template parameter list. |
| 1156 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1157 | /// \returns true if an error occurred, false otherwise. |
| 1158 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1159 | TemplateParameterList *OldParams, |
| 1160 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1161 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1163 | // C++ [temp.param]p10: |
| 1164 | // The set of default template-arguments available for use with a |
| 1165 | // template declaration or definition is obtained by merging the |
| 1166 | // default arguments from the definition (if in scope) and all |
| 1167 | // declarations in scope in the same way default function |
| 1168 | // arguments are (8.3.6). |
| 1169 | bool SawDefaultArgument = false; |
| 1170 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1171 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1172 | bool SawParameterPack = false; |
| 1173 | SourceLocation ParameterPackLoc; |
| 1174 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1175 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1176 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1177 | if (OldParams) |
| 1178 | OldParam = OldParams->begin(); |
| 1179 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1180 | bool RemoveDefaultArguments = false; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1181 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1182 | NewParamEnd = NewParams->end(); |
| 1183 | NewParam != NewParamEnd; ++NewParam) { |
| 1184 | // Variables used to diagnose redundant default arguments |
| 1185 | bool RedundantDefaultArg = false; |
| 1186 | SourceLocation OldDefaultLoc; |
| 1187 | SourceLocation NewDefaultLoc; |
| 1188 | |
| 1189 | // Variables used to diagnose missing default arguments |
| 1190 | bool MissingDefaultArg = false; |
| 1191 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1192 | // C++0x [temp.param]p11: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1193 | // If a template parameter of a primary class template or alias template |
| 1194 | // is a template parameter pack, it shall be the last template parameter. |
| 1195 | if (SawParameterPack && |
| 1196 | (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1197 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1198 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1199 | Invalid = true; |
| 1200 | } |
| 1201 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1202 | if (TemplateTypeParmDecl *NewTypeParm |
| 1203 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1204 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1205 | if (NewTypeParm->hasDefaultArgument() && |
| 1206 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1207 | NewTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1208 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1209 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1210 | NewTypeParm->removeDefaultArgument(); |
| 1211 | |
| 1212 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1214 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1215 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1216 | if (NewTypeParm->isParameterPack()) { |
| 1217 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1218 | "Parameter packs can't have a default argument!"); |
| 1219 | SawParameterPack = true; |
| 1220 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1222 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1223 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1224 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1225 | SawDefaultArgument = true; |
| 1226 | RedundantDefaultArg = true; |
| 1227 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1228 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1229 | // Merge the default argument from the old declaration to the |
| 1230 | // new declaration. |
| 1231 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1232 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1233 | true); |
| 1234 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1235 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1236 | SawDefaultArgument = true; |
| 1237 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1238 | } else if (SawDefaultArgument) |
| 1239 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1240 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1241 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1242 | // Check for unexpanded parameter packs. |
| 1243 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1244 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1245 | UPPC_NonTypeTemplateParameterType)) { |
| 1246 | Invalid = true; |
| 1247 | continue; |
| 1248 | } |
| 1249 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1250 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1251 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1252 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1253 | NewNonTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1254 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1255 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1258 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1259 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1260 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1261 | if (NewNonTypeParm->isParameterPack()) { |
| 1262 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1263 | "Parameter packs can't have a default argument!"); |
| 1264 | SawParameterPack = true; |
| 1265 | ParameterPackLoc = NewNonTypeParm->getLocation(); |
| 1266 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1267 | NewNonTypeParm->hasDefaultArgument()) { |
| 1268 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1269 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1270 | SawDefaultArgument = true; |
| 1271 | RedundantDefaultArg = true; |
| 1272 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1273 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1274 | // Merge the default argument from the old declaration to the |
| 1275 | // new declaration. |
| 1276 | SawDefaultArgument = true; |
| 1277 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1278 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1279 | // parameter. |
| 1280 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1281 | OldNonTypeParm->getDefaultArgument(), |
| 1282 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1283 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1284 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1285 | SawDefaultArgument = true; |
| 1286 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1287 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1289 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1290 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1291 | TemplateTemplateParmDecl *NewTemplateParm |
| 1292 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1293 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1294 | // Check for unexpanded parameter packs, recursively. |
| 1295 | if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
| 1296 | Invalid = true; |
| 1297 | continue; |
| 1298 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1299 | |
| 1300 | if (NewTemplateParm->hasDefaultArgument() && |
| 1301 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1302 | NewTemplateParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1303 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1304 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1305 | |
| 1306 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1307 | TemplateTemplateParmDecl *OldTemplateParm |
| 1308 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1309 | if (NewTemplateParm->isParameterPack()) { |
| 1310 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1311 | "Parameter packs can't have a default argument!"); |
| 1312 | SawParameterPack = true; |
| 1313 | ParameterPackLoc = NewTemplateParm->getLocation(); |
| 1314 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1315 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1316 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1317 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1318 | SawDefaultArgument = true; |
| 1319 | RedundantDefaultArg = true; |
| 1320 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1321 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1322 | // Merge the default argument from the old declaration to the |
| 1323 | // new declaration. |
| 1324 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1325 | // FIXME: We need to create a new kind of "default argument" expression |
| 1326 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1327 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1328 | OldTemplateParm->getDefaultArgument(), |
| 1329 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1330 | PreviousDefaultArgLoc |
| 1331 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1332 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1333 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1334 | PreviousDefaultArgLoc |
| 1335 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1336 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1337 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | if (RedundantDefaultArg) { |
| 1341 | // C++ [temp.param]p12: |
| 1342 | // A template-parameter shall not be given default arguments |
| 1343 | // by two different declarations in the same scope. |
| 1344 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1345 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1346 | Invalid = true; |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1347 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1348 | // C++ [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1349 | // If a template-parameter of a class template has a default |
| 1350 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1351 | // have a default template-argument supplied or be a template parameter |
| 1352 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1353 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1354 | diag::err_template_param_default_arg_missing); |
| 1355 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1356 | Invalid = true; |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1357 | RemoveDefaultArguments = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | // If we have an old template parameter list that we're merging |
| 1361 | // in, move on to the next parameter. |
| 1362 | if (OldParams) |
| 1363 | ++OldParam; |
| 1364 | } |
| 1365 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1366 | // We were missing some default arguments at the end of the list, so remove |
| 1367 | // all of the default arguments. |
| 1368 | if (RemoveDefaultArguments) { |
| 1369 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1370 | NewParamEnd = NewParams->end(); |
| 1371 | NewParam != NewParamEnd; ++NewParam) { |
| 1372 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1373 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1374 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1375 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1376 | NTTP->removeDefaultArgument(); |
| 1377 | else |
| 1378 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1379 | } |
| 1380 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1381 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1382 | return Invalid; |
| 1383 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1384 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1385 | namespace { |
| 1386 | |
| 1387 | /// A class which looks for a use of a certain level of template |
| 1388 | /// parameter. |
| 1389 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1390 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1391 | |
| 1392 | unsigned Depth; |
| 1393 | bool Match; |
| 1394 | |
| 1395 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1396 | NamedDecl *ND = Params->getParam(0); |
| 1397 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1398 | Depth = PD->getDepth(); |
| 1399 | } else if (NonTypeTemplateParmDecl *PD = |
| 1400 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1401 | Depth = PD->getDepth(); |
| 1402 | } else { |
| 1403 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | bool Matches(unsigned ParmDepth) { |
| 1408 | if (ParmDepth >= Depth) { |
| 1409 | Match = true; |
| 1410 | return true; |
| 1411 | } |
| 1412 | return false; |
| 1413 | } |
| 1414 | |
| 1415 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1416 | return !Matches(T->getDepth()); |
| 1417 | } |
| 1418 | |
| 1419 | bool TraverseTemplateName(TemplateName N) { |
| 1420 | if (TemplateTemplateParmDecl *PD = |
| 1421 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1422 | if (Matches(PD->getDepth())) return false; |
| 1423 | return super::TraverseTemplateName(N); |
| 1424 | } |
| 1425 | |
| 1426 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1427 | if (NonTypeTemplateParmDecl *PD = |
| 1428 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1429 | if (PD->getDepth() == Depth) { |
| 1430 | Match = true; |
| 1431 | return false; |
| 1432 | } |
| 1433 | } |
| 1434 | return super::VisitDeclRefExpr(E); |
| 1435 | } |
Douglas Gregor | 18c8339 | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1436 | |
| 1437 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1438 | return TraverseType(T->getInjectedSpecializationType()); |
| 1439 | } |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1440 | }; |
| 1441 | } |
| 1442 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1443 | /// Determines whether a given type depends on the given parameter |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1444 | /// list. |
| 1445 | static bool |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1446 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1447 | DependencyChecker Checker(Params); |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1448 | Checker.TraverseType(T); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1449 | return Checker.Match; |
| 1450 | } |
| 1451 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1452 | // Find the source range corresponding to the named type in the given |
| 1453 | // nested-name-specifier, if any. |
| 1454 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1455 | QualType T, |
| 1456 | const CXXScopeSpec &SS) { |
| 1457 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1458 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1459 | if (const Type *CurType = NNS->getAsType()) { |
| 1460 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1461 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1462 | } else |
| 1463 | break; |
| 1464 | |
| 1465 | NNSLoc = NNSLoc.getPrefix(); |
| 1466 | } |
| 1467 | |
| 1468 | return SourceRange(); |
| 1469 | } |
| 1470 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1472 | /// specifier, returning the template parameter list that applies to the |
| 1473 | /// name. |
| 1474 | /// |
| 1475 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1476 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1477 | /// |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1478 | /// \param DeclLoc The location of the declaration itself. |
| 1479 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1480 | /// \param SS the scope specifier that will be matched to the given template |
| 1481 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1482 | /// being declared. |
| 1483 | /// |
| 1484 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1485 | /// innermost template parameter lists. |
| 1486 | /// |
| 1487 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1488 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1489 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1490 | /// matching template parameters to scope specifiers in friend |
| 1491 | /// declarations. |
| 1492 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1493 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1494 | /// declared is an explicit specialization, false otherwise. |
| 1495 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1496 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1497 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1498 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1499 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1500 | /// 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] | 1501 | /// itself a template). |
| 1502 | TemplateParameterList * |
| 1503 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1504 | SourceLocation DeclLoc, |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1505 | const CXXScopeSpec &SS, |
| 1506 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1507 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1508 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1509 | bool &IsExplicitSpecialization, |
| 1510 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1511 | IsExplicitSpecialization = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1512 | Invalid = false; |
| 1513 | |
| 1514 | // The sequence of nested types to which we will match up the template |
| 1515 | // parameter lists. We first build this list by starting with the type named |
| 1516 | // by the nested-name-specifier and walking out until we run out of types. |
| 1517 | llvm::SmallVector<QualType, 4> NestedTypes; |
| 1518 | QualType T; |
Douglas Gregor | 714c992 | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1519 | if (SS.getScopeRep()) { |
| 1520 | if (CXXRecordDecl *Record |
| 1521 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1522 | T = Context.getTypeDeclType(Record); |
| 1523 | else |
| 1524 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1525 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1526 | |
| 1527 | // If we found an explicit specialization that prevents us from needing |
| 1528 | // 'template<>' headers, this will be set to the location of that |
| 1529 | // explicit specialization. |
| 1530 | SourceLocation ExplicitSpecLoc; |
| 1531 | |
| 1532 | while (!T.isNull()) { |
| 1533 | NestedTypes.push_back(T); |
| 1534 | |
| 1535 | // Retrieve the parent of a record type. |
| 1536 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1537 | // If this type is an explicit specialization, we're done. |
| 1538 | if (ClassTemplateSpecializationDecl *Spec |
| 1539 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1540 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1541 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1542 | ExplicitSpecLoc = Spec->getLocation(); |
| 1543 | break; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1544 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1545 | } else if (Record->getTemplateSpecializationKind() |
| 1546 | == TSK_ExplicitSpecialization) { |
| 1547 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1548 | break; |
| 1549 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1550 | |
| 1551 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1552 | T = Context.getTypeDeclType(Parent); |
| 1553 | else |
| 1554 | T = QualType(); |
| 1555 | continue; |
| 1556 | } |
| 1557 | |
| 1558 | if (const TemplateSpecializationType *TST |
| 1559 | = T->getAs<TemplateSpecializationType>()) { |
| 1560 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1561 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1562 | T = Context.getTypeDeclType(Parent); |
| 1563 | else |
| 1564 | T = QualType(); |
| 1565 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1566 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | // Look one step prior in a dependent template specialization type. |
| 1570 | if (const DependentTemplateSpecializationType *DependentTST |
| 1571 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1572 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1573 | T = QualType(NNS->getAsType(), 0); |
| 1574 | else |
| 1575 | T = QualType(); |
| 1576 | continue; |
| 1577 | } |
| 1578 | |
| 1579 | // Look one step prior in a dependent name type. |
| 1580 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1581 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1582 | T = QualType(NNS->getAsType(), 0); |
| 1583 | else |
| 1584 | T = QualType(); |
| 1585 | continue; |
| 1586 | } |
| 1587 | |
| 1588 | // Retrieve the parent of an enumeration type. |
| 1589 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1590 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1591 | // check here. |
| 1592 | EnumDecl *Enum = EnumT->getDecl(); |
| 1593 | |
| 1594 | // Get to the parent type. |
| 1595 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1596 | T = Context.getTypeDeclType(Parent); |
| 1597 | else |
| 1598 | T = QualType(); |
| 1599 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1600 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1601 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1602 | T = QualType(); |
| 1603 | } |
| 1604 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1605 | // to the innermost while checking template-parameter-lists. |
| 1606 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1607 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1608 | // C++0x [temp.expl.spec]p17: |
| 1609 | // A member or a member template may be nested within many |
| 1610 | // enclosing class templates. In an explicit specialization for |
| 1611 | // such a member, the member declaration shall be preceded by a |
| 1612 | // template<> for each enclosing class template that is |
| 1613 | // explicitly specialized. |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1614 | bool SawNonEmptyTemplateParameterList = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1615 | unsigned ParamIdx = 0; |
| 1616 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1617 | ++TypeIdx) { |
| 1618 | T = NestedTypes[TypeIdx]; |
| 1619 | |
| 1620 | // Whether we expect a 'template<>' header. |
| 1621 | bool NeedEmptyTemplateHeader = false; |
| 1622 | |
| 1623 | // Whether we expect a template header with parameters. |
| 1624 | bool NeedNonemptyTemplateHeader = false; |
| 1625 | |
| 1626 | // For a dependent type, the set of template parameters that we |
| 1627 | // expect to see. |
| 1628 | TemplateParameterList *ExpectedTemplateParams = 0; |
| 1629 | |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1630 | // C++0x [temp.expl.spec]p15: |
| 1631 | // A member or a member template may be nested within many enclosing |
| 1632 | // class templates. In an explicit specialization for such a member, the |
| 1633 | // member declaration shall be preceded by a template<> for each |
| 1634 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1635 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1636 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1637 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1638 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1639 | NeedNonemptyTemplateHeader = true; |
| 1640 | } else if (Record->isDependentType()) { |
| 1641 | if (Record->getDescribedClassTemplate()) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1642 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1643 | ->getTemplateParameters(); |
| 1644 | NeedNonemptyTemplateHeader = true; |
| 1645 | } |
| 1646 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1647 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1648 | // C++0x [temp.expl.spec]p4: |
| 1649 | // Members of an explicitly specialized class template are defined |
| 1650 | // in the same manner as members of normal classes, and not using |
| 1651 | // the template<> syntax. |
| 1652 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1653 | NeedEmptyTemplateHeader = true; |
| 1654 | else |
Douglas Gregor | 95ea450 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1655 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1656 | } else if (Record->getTemplateSpecializationKind()) { |
| 1657 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1658 | != TSK_ExplicitSpecialization && |
| 1659 | TypeIdx == NumTypes - 1) |
| 1660 | IsExplicitSpecialization = true; |
| 1661 | |
| 1662 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1663 | } |
| 1664 | } else if (const TemplateSpecializationType *TST |
| 1665 | = T->getAs<TemplateSpecializationType>()) { |
| 1666 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1667 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1668 | NeedNonemptyTemplateHeader = true; |
| 1669 | } |
| 1670 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1671 | // FIXME: We actually could/should check the template arguments here |
| 1672 | // against the corresponding template parameter list. |
| 1673 | NeedNonemptyTemplateHeader = false; |
| 1674 | } |
| 1675 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1676 | // C++ [temp.expl.spec]p16: |
| 1677 | // In an explicit specialization declaration for a member of a class |
| 1678 | // template or a member template that ap- pears in namespace scope, the |
| 1679 | // member template and some of its enclosing class templates may remain |
| 1680 | // unspecialized, except that the declaration shall not explicitly |
| 1681 | // specialize a class member template if its en- closing class templates |
| 1682 | // are not explicitly specialized as well. |
| 1683 | if (ParamIdx < NumParamLists) { |
| 1684 | if (ParamLists[ParamIdx]->size() == 0) { |
| 1685 | if (SawNonEmptyTemplateParameterList) { |
| 1686 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1687 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1688 | Invalid = true; |
| 1689 | IsExplicitSpecialization = false; |
| 1690 | return 0; |
| 1691 | } |
| 1692 | } else |
| 1693 | SawNonEmptyTemplateParameterList = true; |
| 1694 | } |
| 1695 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1696 | if (NeedEmptyTemplateHeader) { |
| 1697 | // If we're on the last of the types, and we need a 'template<>' header |
| 1698 | // here, then it's an explicit specialization. |
| 1699 | if (TypeIdx == NumTypes - 1) |
| 1700 | IsExplicitSpecialization = true; |
| 1701 | |
| 1702 | if (ParamIdx < NumParamLists) { |
| 1703 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1704 | // The header has template parameters when it shouldn't. Complain. |
| 1705 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1706 | diag::err_template_param_list_matches_nontemplate) |
| 1707 | << T |
| 1708 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1709 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1710 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1711 | Invalid = true; |
| 1712 | return 0; |
| 1713 | } |
| 1714 | |
| 1715 | // Consume this template header. |
| 1716 | ++ParamIdx; |
| 1717 | continue; |
| 1718 | } |
| 1719 | |
| 1720 | if (!IsFriend) { |
| 1721 | // We don't have a template header, but we should. |
| 1722 | SourceLocation ExpectedTemplateLoc; |
| 1723 | if (NumParamLists > 0) |
| 1724 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1725 | else |
| 1726 | ExpectedTemplateLoc = DeclStartLoc; |
| 1727 | |
| 1728 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1729 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS) |
| 1730 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1731 | } |
| 1732 | |
| 1733 | continue; |
| 1734 | } |
| 1735 | |
| 1736 | if (NeedNonemptyTemplateHeader) { |
| 1737 | // In friend declarations we can have template-ids which don't |
| 1738 | // depend on the corresponding template parameter lists. But |
| 1739 | // assume that empty parameter lists are supposed to match this |
| 1740 | // template-id. |
| 1741 | if (IsFriend && T->isDependentType()) { |
| 1742 | if (ParamIdx < NumParamLists && |
| 1743 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
| 1744 | ExpectedTemplateParams = 0; |
| 1745 | else |
| 1746 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1747 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1748 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1749 | if (ParamIdx < NumParamLists) { |
| 1750 | // Check the template parameter list, if we can. |
| 1751 | if (ExpectedTemplateParams && |
| 1752 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1753 | ExpectedTemplateParams, |
| 1754 | true, TPL_TemplateMatch)) |
| 1755 | Invalid = true; |
| 1756 | |
| 1757 | if (!Invalid && |
| 1758 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1759 | TPC_ClassTemplateMember)) |
| 1760 | Invalid = true; |
| 1761 | |
| 1762 | ++ParamIdx; |
| 1763 | continue; |
| 1764 | } |
| 1765 | |
| 1766 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1767 | << T |
| 1768 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1769 | Invalid = true; |
| 1770 | continue; |
| 1771 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1772 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1773 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1774 | // If there were at least as many template-ids as there were template |
| 1775 | // parameter lists, then there are no template parameter lists remaining for |
| 1776 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1777 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1778 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1779 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1780 | // If there were too many template parameter lists, complain about that now. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1781 | if (ParamIdx < NumParamLists - 1) { |
| 1782 | bool HasAnyExplicitSpecHeader = false; |
| 1783 | bool AllExplicitSpecHeaders = true; |
| 1784 | for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) { |
| 1785 | if (ParamLists[I]->size() == 0) |
| 1786 | HasAnyExplicitSpecHeader = true; |
| 1787 | else |
| 1788 | AllExplicitSpecHeaders = false; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1789 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1790 | |
| 1791 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1792 | AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers |
| 1793 | : diag::err_template_spec_extra_headers) |
| 1794 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1795 | ParamLists[NumParamLists - 2]->getRAngleLoc()); |
| 1796 | |
| 1797 | // If there was a specialization somewhere, such that 'template<>' is |
| 1798 | // not required, and there were any 'template<>' headers, note where the |
| 1799 | // specialization occurred. |
| 1800 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1801 | Diag(ExplicitSpecLoc, |
| 1802 | diag::note_explicit_template_spec_does_not_need_header) |
| 1803 | << NestedTypes.back(); |
| 1804 | |
| 1805 | // We have a template parameter list with no corresponding scope, which |
| 1806 | // means that the resulting template declaration can't be instantiated |
| 1807 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1808 | if (!AllExplicitSpecHeaders) |
| 1809 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1810 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1812 | // C++ [temp.expl.spec]p16: |
| 1813 | // In an explicit specialization declaration for a member of a class |
| 1814 | // template or a member template that ap- pears in namespace scope, the |
| 1815 | // member template and some of its enclosing class templates may remain |
| 1816 | // unspecialized, except that the declaration shall not explicitly |
| 1817 | // specialize a class member template if its en- closing class templates |
| 1818 | // are not explicitly specialized as well. |
| 1819 | if (ParamLists[NumParamLists - 1]->size() == 0 && |
| 1820 | SawNonEmptyTemplateParameterList) { |
| 1821 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1822 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1823 | Invalid = true; |
| 1824 | IsExplicitSpecialization = false; |
| 1825 | return 0; |
| 1826 | } |
| 1827 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1828 | // Return the last template parameter list, which corresponds to the |
| 1829 | // entity being declared. |
| 1830 | return ParamLists[NumParamLists - 1]; |
| 1831 | } |
| 1832 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1833 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1834 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1835 | Diag(Template->getLocation(), diag::note_template_declared_here) |
| 1836 | << (isa<FunctionTemplateDecl>(Template)? 0 |
| 1837 | : isa<ClassTemplateDecl>(Template)? 1 |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1838 | : isa<TypeAliasTemplateDecl>(Template)? 2 |
| 1839 | : 3) |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1840 | << Template->getDeclName(); |
| 1841 | return; |
| 1842 | } |
| 1843 | |
| 1844 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1845 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1846 | IEnd = OST->end(); |
| 1847 | I != IEnd; ++I) |
| 1848 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1849 | << 0 << (*I)->getDeclName(); |
| 1850 | |
| 1851 | return; |
| 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1856 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1857 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1858 | TemplateArgumentListInfo &TemplateArgs) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1859 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 1860 | if (DTN && DTN->isIdentifier()) |
| 1861 | // When building a template-id where the template-name is dependent, |
| 1862 | // assume the template is a type template. Either our assumption is |
| 1863 | // correct, or the code is ill-formed and will be diagnosed when the |
| 1864 | // dependent name is substituted. |
| 1865 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 1866 | DTN->getQualifier(), |
| 1867 | DTN->getIdentifier(), |
| 1868 | TemplateArgs); |
| 1869 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1870 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1871 | if (!Template || isa<FunctionTemplateDecl>(Template)) { |
| 1872 | // We might have a substituted template template parameter pack. If so, |
| 1873 | // build a template specialization type for it. |
| 1874 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 1875 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1876 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1877 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 1878 | << Name; |
| 1879 | NoteAllFoundTemplates(Name); |
| 1880 | return QualType(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1881 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1882 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1883 | // Check that the template argument list is well-formed for this |
| 1884 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1885 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1886 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1887 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1888 | return QualType(); |
| 1889 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1890 | assert((Converted.size() == Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1891 | "Converted template argument list is too short!"); |
| 1892 | |
| 1893 | QualType CanonType; |
| 1894 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1895 | if (TypeAliasTemplateDecl *AliasTemplate |
| 1896 | = dyn_cast<TypeAliasTemplateDecl>(Template)) { |
| 1897 | // Find the canonical type for this type alias template specialization. |
| 1898 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 1899 | if (Pattern->isInvalidDecl()) |
| 1900 | return QualType(); |
| 1901 | |
| 1902 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1903 | Converted.data(), Converted.size()); |
| 1904 | |
| 1905 | // Only substitute for the innermost template argument list. |
| 1906 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 1804174 | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 1907 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | aff37b4 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 1908 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 1909 | for (unsigned I = 0; I < Depth; ++I) |
| 1910 | TemplateArgLists.addOuterTemplateArguments(0, 0); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1911 | |
| 1912 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
| 1913 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 1914 | TemplateArgLists, AliasTemplate->getLocation(), |
| 1915 | AliasTemplate->getDeclName()); |
| 1916 | if (CanonType.isNull()) |
| 1917 | return QualType(); |
| 1918 | } else if (Name.isDependent() || |
| 1919 | TemplateSpecializationType::anyDependentTemplateArguments( |
| 1920 | TemplateArgs)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1921 | // This class template specialization is a dependent |
| 1922 | // type. Therefore, its canonical type is another class template |
| 1923 | // specialization type that contains all of the converted |
| 1924 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1925 | // A<T, T> have identical types when A is declared as: |
| 1926 | // |
| 1927 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1928 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1930 | Converted.data(), |
| 1931 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1932 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1933 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1934 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1935 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1936 | // build the canonical type and return that to us. |
| 1937 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1938 | |
| 1939 | // This might work out to be a current instantiation, in which |
| 1940 | // case the canonical type needs to be the InjectedClassNameType. |
| 1941 | // |
| 1942 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1943 | // changes to CurContext don't change the set of current |
| 1944 | // instantiations. |
| 1945 | if (isa<ClassTemplateDecl>(Template)) { |
| 1946 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1947 | // If we get out to a namespace, we're done. |
| 1948 | if (Ctx->isFileContext()) break; |
| 1949 | |
| 1950 | // If this isn't a record, keep looking. |
| 1951 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1952 | if (!Record) continue; |
| 1953 | |
| 1954 | // Look for one of the two cases with InjectedClassNameTypes |
| 1955 | // and check whether it's the same template. |
| 1956 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1957 | !Record->getDescribedClassTemplate()) |
| 1958 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1959 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1960 | // Fetch the injected class name type and check whether its |
| 1961 | // injected type is equal to the type we just built. |
| 1962 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1963 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 1964 | ->getInjectedSpecializationType(); |
| 1965 | |
| 1966 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 1967 | continue; |
| 1968 | |
| 1969 | // If so, the canonical type of this TST is the injected |
| 1970 | // class name type of the record we just found. |
| 1971 | assert(ICNT.isCanonical()); |
| 1972 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1973 | break; |
| 1974 | } |
| 1975 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1976 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1977 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1978 | // Find the class template specialization declaration that |
| 1979 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1980 | void *InsertPos = 0; |
| 1981 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1982 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1983 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1984 | if (!Decl) { |
| 1985 | // This is the first time we have referenced this class template |
| 1986 | // specialization. Create the canonical declaration and add it to |
| 1987 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1989 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 1990 | ClassTemplate->getDeclContext(), |
| 1991 | ClassTemplate->getLocation(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1992 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1993 | ClassTemplate, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1994 | Converted.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1995 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 1996 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1997 | Decl->setLexicalDeclContext(CurContext); |
| 1998 | } |
| 1999 | |
| 2000 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2001 | assert(isa<RecordType>(CanonType) && |
| 2002 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2003 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2004 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2005 | // Build the fully-sugared type for this class template |
| 2006 | // specialization, which refers back to the class template |
| 2007 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2008 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2011 | TypeResult |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2012 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, |
| 2013 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2014 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2015 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2016 | SourceLocation RAngleLoc) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2017 | if (SS.isInvalid()) |
| 2018 | return true; |
| 2019 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2020 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2021 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2022 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2023 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2024 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2025 | |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2026 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2027 | QualType T = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2028 | DTN->getQualifier(), |
| 2029 | DTN->getIdentifier(), |
| 2030 | TemplateArgs); |
| 2031 | |
| 2032 | // Build type-source information. |
| 2033 | TypeLocBuilder TLB; |
| 2034 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2035 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2036 | SpecTL.setKeywordLoc(SourceLocation()); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2037 | SpecTL.setNameLoc(TemplateLoc); |
| 2038 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2039 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 2040 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2041 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2042 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2043 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2044 | } |
| 2045 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2046 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2047 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2048 | |
| 2049 | if (Result.isNull()) |
| 2050 | return true; |
| 2051 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2052 | // Build type-source information. |
| 2053 | TypeLocBuilder TLB; |
| 2054 | TemplateSpecializationTypeLoc SpecTL |
| 2055 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 2056 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2057 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2058 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2059 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2060 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2061 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2062 | if (SS.isNotEmpty()) { |
| 2063 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2064 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2065 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 2066 | ElabTL.setKeywordLoc(SourceLocation()); |
| 2067 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2068 | } |
| 2069 | |
| 2070 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2071 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2072 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2073 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2074 | TypeSpecifierType TagSpec, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2075 | SourceLocation TagLoc, |
| 2076 | CXXScopeSpec &SS, |
| 2077 | TemplateTy TemplateD, |
| 2078 | SourceLocation TemplateLoc, |
| 2079 | SourceLocation LAngleLoc, |
| 2080 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2081 | SourceLocation RAngleLoc) { |
| 2082 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
| 2083 | |
| 2084 | // Translate the parser's template argument list in our AST format. |
| 2085 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2086 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2087 | |
| 2088 | // Determine the tag kind |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2089 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2090 | ElaboratedTypeKeyword Keyword |
| 2091 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2093 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2094 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2095 | DTN->getQualifier(), |
| 2096 | DTN->getIdentifier(), |
| 2097 | TemplateArgs); |
| 2098 | |
| 2099 | // Build type-source information. |
| 2100 | TypeLocBuilder TLB; |
| 2101 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2102 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2103 | SpecTL.setKeywordLoc(TagLoc); |
| 2104 | SpecTL.setNameLoc(TemplateLoc); |
| 2105 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2106 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2107 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2108 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2109 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2110 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2111 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2112 | |
| 2113 | if (TypeAliasTemplateDecl *TAT = |
| 2114 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2115 | // C++0x [dcl.type.elab]p2: |
| 2116 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2117 | // resolves to an alias template specialization, the |
| 2118 | // elaborated-type-specifier is ill-formed. |
| 2119 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2120 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2121 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2122 | |
| 2123 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2124 | if (Result.isNull()) |
| 2125 | return TypeResult(); |
| 2126 | |
| 2127 | // Check the tag kind |
| 2128 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2129 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2130 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2131 | IdentifierInfo *Id = D->getIdentifier(); |
| 2132 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2133 | |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2134 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 2135 | TagLoc, *Id)) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2136 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2137 | << Result |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2138 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2139 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2140 | } |
| 2141 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2142 | |
| 2143 | // Provide source-location information for the template specialization. |
| 2144 | TypeLocBuilder TLB; |
| 2145 | TemplateSpecializationTypeLoc SpecTL |
| 2146 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 2147 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2148 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2149 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2150 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2151 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2152 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2153 | // Construct an elaborated type containing the nested-name-specifier (if any) |
| 2154 | // and keyword. |
| 2155 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2156 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 2157 | ElabTL.setKeywordLoc(TagLoc); |
| 2158 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2159 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2162 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2163 | LookupResult &R, |
| 2164 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2165 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2166 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2167 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | // 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] | 2169 | // though. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2170 | // foo<int> could identify a single function unambiguously |
| 2171 | // This approach does NOT work, since f<int>(1); |
| 2172 | // gets resolved prior to resorting to overload resolution |
| 2173 | // i.e., template<class T> void f(double); |
| 2174 | // vs template<class T, class U> void f(U); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2175 | |
| 2176 | // These should be filtered out by our callers. |
| 2177 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2178 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2179 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2180 | // We don't want lookup warnings at this point. |
| 2181 | R.suppressDiagnostics(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2182 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2183 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2184 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2185 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2186 | R.getLookupNameInfo(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2187 | RequiresADL, TemplateArgs, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2188 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2189 | |
| 2190 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2193 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2194 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2195 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2196 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2197 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2198 | DeclContext *DC; |
| 2199 | if (!(DC = computeDeclContext(SS, false)) || |
| 2200 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2201 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2202 | return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2203 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2204 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2205 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2206 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 2207 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2208 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2209 | if (R.isAmbiguous()) |
| 2210 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2211 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2212 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2213 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2214 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2215 | return ExprError(); |
| 2216 | } |
| 2217 | |
| 2218 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2219 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 2220 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 2221 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2222 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2223 | return ExprError(); |
| 2224 | } |
| 2225 | |
| 2226 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2229 | /// \brief Form a dependent template name. |
| 2230 | /// |
| 2231 | /// This action forms a dependent template name given the template |
| 2232 | /// name and its (presumably dependent) scope specifier. For |
| 2233 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2234 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2235 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2236 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2237 | SourceLocation TemplateKWLoc, |
| 2238 | CXXScopeSpec &SS, |
| 2239 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2240 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2241 | bool EnteringContext, |
| 2242 | TemplateTy &Result) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 2243 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() && |
| 2244 | !getLangOptions().CPlusPlus0x) |
| 2245 | Diag(TemplateKWLoc, diag::ext_template_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2246 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2247 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2248 | DeclContext *LookupCtx = 0; |
| 2249 | if (SS.isSet()) |
| 2250 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2251 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2252 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2253 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2254 | // C++0x [temp.names]p5: |
| 2255 | // If a name prefixed by the keyword template is not the name of |
| 2256 | // a template, the program is ill-formed. [Note: the keyword |
| 2257 | // template may not be applied to non-template members of class |
| 2258 | // templates. -end note ] [ Note: as is the case with the |
| 2259 | // typename prefix, the template prefix is allowed in cases |
| 2260 | // where it is not strictly necessary; i.e., when the |
| 2261 | // nested-name-specifier or the expression on the left of the -> |
| 2262 | // or . is not dependent on a template-parameter, or the use |
| 2263 | // does not appear in the scope of a template. -end note] |
| 2264 | // |
| 2265 | // Note: C++03 was more strict here, because it banned the use of |
| 2266 | // the "template" keyword prior to a template-name that was not a |
| 2267 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2268 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2269 | // 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] | 2270 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2271 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 2272 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2273 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2274 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2275 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | d078bd2 | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2276 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2277 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2278 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2279 | } else if (TNK == TNK_Non_template) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2280 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2281 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2282 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2283 | << Name.getSourceRange() |
| 2284 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2285 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2286 | } else { |
| 2287 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2288 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2289 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2292 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2293 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2294 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2295 | switch (Name.getKind()) { |
| 2296 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2297 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2298 | Name.Identifier)); |
| 2299 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2300 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2301 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2302 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2303 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2304 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2305 | |
| 2306 | case UnqualifiedId::IK_LiteralOperatorId: |
| 2307 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 2308 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2309 | default: |
| 2310 | break; |
| 2311 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2312 | |
| 2313 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2314 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2315 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2316 | << Name.getSourceRange() |
| 2317 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2318 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2321 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2322 | const TemplateArgumentLoc &AL, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2323 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2324 | const TemplateArgument &Arg = AL.getArgument(); |
| 2325 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2326 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2327 | switch(Arg.getKind()) { |
| 2328 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2329 | // C++ [temp.arg.type]p1: |
| 2330 | // A template-argument for a template-parameter which is a |
| 2331 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2332 | break; |
| 2333 | case TemplateArgument::Template: { |
| 2334 | // We have a template type parameter but the template argument |
| 2335 | // is a template without any arguments. |
| 2336 | SourceRange SR = AL.getSourceRange(); |
| 2337 | TemplateName Name = Arg.getAsTemplate(); |
| 2338 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 2339 | << Name << SR; |
| 2340 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 2341 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2342 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2343 | return true; |
| 2344 | } |
| 2345 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2346 | // We have a template type parameter but the template argument |
| 2347 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 2348 | SourceRange SR = AL.getSourceRange(); |
| 2349 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2350 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2351 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2352 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2353 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2354 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2355 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2356 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2357 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2358 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2359 | // Add the converted template type argument. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2360 | Converted.push_back( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2361 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2362 | return false; |
| 2363 | } |
| 2364 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2365 | /// \brief Substitute template arguments into the default template argument for |
| 2366 | /// the given template type parameter. |
| 2367 | /// |
| 2368 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2369 | /// the substitution. |
| 2370 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2371 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2372 | /// for. |
| 2373 | /// |
| 2374 | /// \param TemplateLoc the location of the template name that started the |
| 2375 | /// template-id we are checking. |
| 2376 | /// |
| 2377 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2378 | /// terminates the template-id. |
| 2379 | /// |
| 2380 | /// \param Param the template template parameter whose default we are |
| 2381 | /// substituting into. |
| 2382 | /// |
| 2383 | /// \param Converted the list of template arguments provided for template |
| 2384 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2385 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2386 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2387 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2388 | TemplateDecl *Template, |
| 2389 | SourceLocation TemplateLoc, |
| 2390 | SourceLocation RAngleLoc, |
| 2391 | TemplateTypeParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2392 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2393 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2394 | |
| 2395 | // If the argument type is dependent, instantiate it now based |
| 2396 | // on the previously-computed template arguments. |
| 2397 | if (ArgType->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2398 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2399 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2400 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2401 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2402 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2403 | |
| 2404 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2405 | Template, Converted.data(), |
| 2406 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2407 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2408 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2409 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2410 | Param->getDefaultArgumentLoc(), |
| 2411 | Param->getDeclName()); |
| 2412 | } |
| 2413 | |
| 2414 | return ArgType; |
| 2415 | } |
| 2416 | |
| 2417 | /// \brief Substitute template arguments into the default template argument for |
| 2418 | /// the given non-type template parameter. |
| 2419 | /// |
| 2420 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2421 | /// the substitution. |
| 2422 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2423 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2424 | /// for. |
| 2425 | /// |
| 2426 | /// \param TemplateLoc the location of the template name that started the |
| 2427 | /// template-id we are checking. |
| 2428 | /// |
| 2429 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2430 | /// terminates the template-id. |
| 2431 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2432 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2433 | /// substituting into. |
| 2434 | /// |
| 2435 | /// \param Converted the list of template arguments provided for template |
| 2436 | /// parameters that precede \p Param in the template parameter list. |
| 2437 | /// |
| 2438 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2439 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2440 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2441 | TemplateDecl *Template, |
| 2442 | SourceLocation TemplateLoc, |
| 2443 | SourceLocation RAngleLoc, |
| 2444 | NonTypeTemplateParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2445 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2446 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2447 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2448 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2449 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2450 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2451 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2452 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2453 | Template, Converted.data(), |
| 2454 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2455 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2456 | |
| 2457 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2458 | } |
| 2459 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2460 | /// \brief Substitute template arguments into the default template argument for |
| 2461 | /// the given template template parameter. |
| 2462 | /// |
| 2463 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2464 | /// the substitution. |
| 2465 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2466 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2467 | /// for. |
| 2468 | /// |
| 2469 | /// \param TemplateLoc the location of the template name that started the |
| 2470 | /// template-id we are checking. |
| 2471 | /// |
| 2472 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2473 | /// terminates the template-id. |
| 2474 | /// |
| 2475 | /// \param Param the template template parameter whose default we are |
| 2476 | /// substituting into. |
| 2477 | /// |
| 2478 | /// \param Converted the list of template arguments provided for template |
| 2479 | /// parameters that precede \p Param in the template parameter list. |
| 2480 | /// |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2481 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 2482 | /// source-location information) that precedes the template name. |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2483 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2484 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2485 | static TemplateName |
| 2486 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2487 | TemplateDecl *Template, |
| 2488 | SourceLocation TemplateLoc, |
| 2489 | SourceLocation RAngleLoc, |
| 2490 | TemplateTemplateParmDecl *Param, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2491 | llvm::SmallVectorImpl<TemplateArgument> &Converted, |
| 2492 | NestedNameSpecifierLoc &QualifierLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2493 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2494 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2495 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2496 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2497 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2498 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2499 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2500 | Template, Converted.data(), |
| 2501 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2502 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2503 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2504 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2505 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2506 | if (QualifierLoc) { |
| 2507 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2508 | AllTemplateArgs); |
| 2509 | if (!QualifierLoc) |
| 2510 | return TemplateName(); |
| 2511 | } |
| 2512 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2513 | return SemaRef.SubstTemplateName(QualifierLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2514 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2515 | Param->getDefaultArgument().getTemplateNameLoc(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2516 | AllTemplateArgs); |
| 2517 | } |
| 2518 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2519 | /// \brief If the given template parameter has a default template |
| 2520 | /// argument, substitute into that default template argument and |
| 2521 | /// return the corresponding template argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2522 | TemplateArgumentLoc |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2523 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2524 | SourceLocation TemplateLoc, |
| 2525 | SourceLocation RAngleLoc, |
| 2526 | Decl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2527 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 2528 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2529 | if (!TypeParm->hasDefaultArgument()) |
| 2530 | return TemplateArgumentLoc(); |
| 2531 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2532 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2533 | TemplateLoc, |
| 2534 | RAngleLoc, |
| 2535 | TypeParm, |
| 2536 | Converted); |
| 2537 | if (DI) |
| 2538 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2539 | |
| 2540 | return TemplateArgumentLoc(); |
| 2541 | } |
| 2542 | |
| 2543 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2544 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2545 | if (!NonTypeParm->hasDefaultArgument()) |
| 2546 | return TemplateArgumentLoc(); |
| 2547 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2548 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2549 | TemplateLoc, |
| 2550 | RAngleLoc, |
| 2551 | NonTypeParm, |
| 2552 | Converted); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2553 | if (Arg.isInvalid()) |
| 2554 | return TemplateArgumentLoc(); |
| 2555 | |
| 2556 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2557 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2558 | } |
| 2559 | |
| 2560 | TemplateTemplateParmDecl *TempTempParm |
| 2561 | = cast<TemplateTemplateParmDecl>(Param); |
| 2562 | if (!TempTempParm->hasDefaultArgument()) |
| 2563 | return TemplateArgumentLoc(); |
| 2564 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2565 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2566 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2567 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2568 | TemplateLoc, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2569 | RAngleLoc, |
| 2570 | TempTempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2571 | Converted, |
| 2572 | QualifierLoc); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2573 | if (TName.isNull()) |
| 2574 | return TemplateArgumentLoc(); |
| 2575 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2576 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2577 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2578 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2579 | } |
| 2580 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2581 | /// \brief Check that the given template argument corresponds to the given |
| 2582 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2583 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2584 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2585 | /// checked. |
| 2586 | /// |
| 2587 | /// \param Arg The template argument. |
| 2588 | /// |
| 2589 | /// \param Template The template in which the template argument resides. |
| 2590 | /// |
| 2591 | /// \param TemplateLoc The location of the template name for the template |
| 2592 | /// whose argument list we're matching. |
| 2593 | /// |
| 2594 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2595 | /// the template argument list. |
| 2596 | /// |
| 2597 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2598 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2599 | /// |
| 2600 | /// \param Converted The checked, converted argument will be added to the |
| 2601 | /// end of this small vector. |
| 2602 | /// |
| 2603 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2604 | /// explicitly written, deduced, etc. |
| 2605 | /// |
| 2606 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2607 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2608 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2609 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2610 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2611 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2612 | unsigned ArgumentPackIndex, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2613 | llvm::SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2614 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2615 | // Check template type parameters. |
| 2616 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2617 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2618 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2619 | // Check non-type template parameters. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2620 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2621 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2622 | // with the template arguments we've seen thus far. But if the |
| 2623 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2624 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2625 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2626 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2627 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2628 | if (NTTPType->isDependentType() && |
| 2629 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2630 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2631 | // Do substitution on the type of the non-type template parameter. |
| 2632 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2633 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2634 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2635 | |
| 2636 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2637 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2638 | NTTPType = SubstType(NTTPType, |
| 2639 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2640 | NTTP->getLocation(), |
| 2641 | NTTP->getDeclName()); |
| 2642 | // If that worked, check the non-type template parameter type |
| 2643 | // for validity. |
| 2644 | if (!NTTPType.isNull()) |
| 2645 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2646 | NTTP->getLocation()); |
| 2647 | if (NTTPType.isNull()) |
| 2648 | return true; |
| 2649 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2650 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2651 | switch (Arg.getArgument().getKind()) { |
| 2652 | case TemplateArgument::Null: |
| 2653 | assert(false && "Should never see a NULL template argument here"); |
| 2654 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2655 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2656 | case TemplateArgument::Expression: { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2657 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2658 | ExprResult Res = |
| 2659 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 2660 | Result, CTAK); |
| 2661 | if (Res.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2662 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2663 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2664 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2665 | break; |
| 2666 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2667 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2668 | case TemplateArgument::Declaration: |
| 2669 | case TemplateArgument::Integral: |
| 2670 | // We've already checked this template argument, so just copy |
| 2671 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2672 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2673 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2674 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2675 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2676 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2677 | // We were given a template template argument. It may not be ill-formed; |
| 2678 | // see below. |
| 2679 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2680 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2681 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2682 | // We have a template argument such as \c T::template X, which we |
| 2683 | // parsed as a template template argument. However, since we now |
| 2684 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2685 | // template name into an expression. |
| 2686 | |
| 2687 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2688 | Arg.getTemplateNameLoc()); |
| 2689 | |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2690 | CXXScopeSpec SS; |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2691 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2692 | ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2693 | SS.getWithLocInContext(Context), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2694 | NameInfo)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2695 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2696 | // If we parsed the template argument as a pack expansion, create a |
| 2697 | // pack expansion expression. |
| 2698 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2699 | E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc()); |
| 2700 | if (E.isInvalid()) |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2701 | return true; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2702 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2703 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2704 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2705 | E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result); |
| 2706 | if (E.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2707 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2708 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2709 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2710 | break; |
| 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 | // We have a template argument that actually does refer to a class |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2714 | // template, alias template, or template template parameter, and |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2715 | // therefore cannot be a non-type template argument. |
| 2716 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2717 | << Arg.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2718 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2719 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2720 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2721 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2722 | case TemplateArgument::Type: { |
| 2723 | // We have a non-type template parameter but the template |
| 2724 | // argument is a type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2725 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2726 | // C++ [temp.arg]p2: |
| 2727 | // In a template-argument, an ambiguity between a type-id and |
| 2728 | // an expression is resolved to a type-id, regardless of the |
| 2729 | // form of the corresponding template-parameter. |
| 2730 | // |
| 2731 | // We warn specifically about this case, since it can be rather |
| 2732 | // confusing for users. |
| 2733 | QualType T = Arg.getArgument().getAsType(); |
| 2734 | SourceRange SR = Arg.getSourceRange(); |
| 2735 | if (T->isFunctionType()) |
| 2736 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2737 | else |
| 2738 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2739 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2740 | return true; |
| 2741 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2742 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2743 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2744 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2745 | break; |
| 2746 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2747 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2748 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2752 | // Check template template parameters. |
| 2753 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2754 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2755 | // Substitute into the template parameter list of the template |
| 2756 | // template parameter, since previously-supplied template arguments |
| 2757 | // may appear within the template template parameter. |
| 2758 | { |
| 2759 | // Set up a template instantiation context. |
| 2760 | LocalInstantiationScope Scope(*this); |
| 2761 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2762 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2763 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2764 | |
| 2765 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2766 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2767 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2768 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2769 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2770 | if (!TempParm) |
| 2771 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2772 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2773 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2774 | switch (Arg.getArgument().getKind()) { |
| 2775 | case TemplateArgument::Null: |
| 2776 | assert(false && "Should never see a NULL template argument here"); |
| 2777 | return true; |
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 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2780 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2781 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2782 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2783 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2784 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2785 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2786 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2787 | case TemplateArgument::Expression: |
| 2788 | case TemplateArgument::Type: |
| 2789 | // We have a template template parameter but the template |
| 2790 | // argument does not refer to a template. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2791 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
| 2792 | << getLangOptions().CPlusPlus0x; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2793 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2794 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2795 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2796 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2797 | "Declaration argument with template template parameter"); |
| 2798 | break; |
| 2799 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2800 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2801 | "Integral argument with template template parameter"); |
| 2802 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2803 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2804 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2805 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2806 | break; |
| 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 | return false; |
| 2810 | } |
| 2811 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2812 | /// \brief Check that the given template argument list is well-formed |
| 2813 | /// for specializing the given template. |
| 2814 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2815 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2816 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2817 | bool PartialTemplateArgs, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2818 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2819 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2820 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2821 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2822 | bool Invalid = false; |
| 2823 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2824 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2825 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2826 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2827 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2828 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2829 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2830 | (NumArgs < Params->getMinRequiredArguments() && |
| 2831 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2832 | // FIXME: point at either the first arg beyond what we can handle, |
| 2833 | // or the '>', depending on whether we have too many or too few |
| 2834 | // arguments. |
| 2835 | SourceRange Range; |
| 2836 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2837 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2838 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2839 | << (NumArgs > NumParams) |
| 2840 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2841 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2842 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2843 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2844 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2845 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2846 | Invalid = true; |
| 2847 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2848 | |
| 2849 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2850 | // [...] The type and form of each template-argument specified in |
| 2851 | // a template-id shall match the type and form specified for the |
| 2852 | // corresponding parameter declared by the template in its |
| 2853 | // template-parameter-list. |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2854 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2855 | llvm::SmallVector<TemplateArgument, 2> ArgumentPack; |
| 2856 | TemplateParameterList::iterator Param = Params->begin(), |
| 2857 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2858 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 2859 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2860 | while (Param != ParamEnd) { |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2861 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2862 | // If we have an expanded parameter pack, make sure we don't have too |
| 2863 | // many arguments. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2864 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2865 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2866 | if (NTTP->isExpandedParameterPack() && |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2867 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 2868 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2869 | << true |
| 2870 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2871 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2872 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2873 | << Template; |
| 2874 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2875 | << Params->getSourceRange(); |
| 2876 | return true; |
| 2877 | } |
| 2878 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2879 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2880 | // Check the template argument we were given. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2881 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2882 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2883 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2884 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2885 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2886 | if ((*Param)->isTemplateParameterPack()) { |
| 2887 | // The template parameter was a template parameter pack, so take the |
| 2888 | // deduced argument and place it on the argument pack. Note that we |
| 2889 | // stay on the same template parameter so that we can deduce more |
| 2890 | // arguments. |
| 2891 | ArgumentPack.push_back(Converted.back()); |
| 2892 | Converted.pop_back(); |
| 2893 | } else { |
| 2894 | // Move to the next template parameter. |
| 2895 | ++Param; |
| 2896 | } |
| 2897 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2898 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2899 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2900 | |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2901 | // If we're checking a partial template argument list, we're done. |
| 2902 | if (PartialTemplateArgs) { |
| 2903 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 2904 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 2905 | ArgumentPack.data(), |
| 2906 | ArgumentPack.size())); |
| 2907 | |
| 2908 | return Invalid; |
| 2909 | } |
| 2910 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2911 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2912 | // arguments, just break out now and we'll fill in the argument pack below. |
| 2913 | if ((*Param)->isTemplateParameterPack()) |
| 2914 | break; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2915 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2916 | // We have a default template argument that we will use. |
| 2917 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2918 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2919 | // Retrieve the default template argument from the template |
| 2920 | // parameter. For each kind of template parameter, we substitute the |
| 2921 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2922 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2923 | // the default argument. |
| 2924 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2925 | if (!TTP->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2926 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2927 | break; |
| 2928 | } |
| 2929 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2930 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2931 | Template, |
| 2932 | TemplateLoc, |
| 2933 | RAngleLoc, |
| 2934 | TTP, |
| 2935 | Converted); |
| 2936 | if (!ArgType) |
| 2937 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2938 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2939 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2940 | ArgType); |
| 2941 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2942 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2943 | if (!NTTP->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2944 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2945 | break; |
| 2946 | } |
| 2947 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2948 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2949 | TemplateLoc, |
| 2950 | RAngleLoc, |
| 2951 | NTTP, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2952 | Converted); |
| 2953 | if (E.isInvalid()) |
| 2954 | return true; |
| 2955 | |
| 2956 | Expr *Ex = E.takeAs<Expr>(); |
| 2957 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2958 | } else { |
| 2959 | TemplateTemplateParmDecl *TempParm |
| 2960 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2961 | |
| 2962 | if (!TempParm->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2963 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2964 | break; |
| 2965 | } |
| 2966 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2967 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2968 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2969 | TemplateLoc, |
| 2970 | RAngleLoc, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2971 | TempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2972 | Converted, |
| 2973 | QualifierLoc); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2974 | if (Name.isNull()) |
| 2975 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2976 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2977 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 2978 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2979 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2980 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2981 | // Introduce an instantiation record that describes where we are using |
| 2982 | // the default template argument. |
| 2983 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2984 | Converted.data(), Converted.size(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2985 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2986 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2987 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2988 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2989 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2990 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2991 | |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2992 | // Core issue 150 (assumed resolution): if this is a template template |
| 2993 | // parameter, keep track of the default template arguments from the |
| 2994 | // template definition. |
| 2995 | if (isTemplateTemplateParameter) |
| 2996 | TemplateArgs.addArgument(Arg); |
| 2997 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2998 | // Move to the next template parameter and argument. |
| 2999 | ++Param; |
| 3000 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3001 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3002 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3003 | // Form argument packs for each of the parameter packs remaining. |
| 3004 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3005 | // If we're checking a partial list of template arguments, don't fill |
| 3006 | // in arguments for non-template parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3007 | |
| 3008 | if ((*Param)->isTemplateParameterPack()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3009 | if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3010 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3011 | else { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3012 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3013 | ArgumentPack.data(), |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3014 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3015 | ArgumentPack.clear(); |
| 3016 | } |
| 3017 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3018 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3019 | ++Param; |
| 3020 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3021 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3022 | return Invalid; |
| 3023 | } |
| 3024 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3025 | namespace { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3026 | class UnnamedLocalNoLinkageFinder |
| 3027 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3028 | { |
| 3029 | Sema &S; |
| 3030 | SourceRange SR; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3031 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3032 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3033 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3034 | public: |
| 3035 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3036 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3037 | bool Visit(QualType T) { |
| 3038 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3039 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3040 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3041 | #define TYPE(Class, Parent) \ |
| 3042 | bool Visit##Class##Type(const Class##Type *); |
| 3043 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 3044 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3045 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 3046 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3047 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3048 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3049 | bool VisitTagDecl(const TagDecl *Tag); |
| 3050 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3051 | }; |
| 3052 | } |
| 3053 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3054 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3055 | return false; |
| 3056 | } |
| 3057 | |
| 3058 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3059 | return Visit(T->getElementType()); |
| 3060 | } |
| 3061 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3062 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3063 | return Visit(T->getPointeeType()); |
| 3064 | } |
| 3065 | |
| 3066 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3067 | const BlockPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3068 | return Visit(T->getPointeeType()); |
| 3069 | } |
| 3070 | |
| 3071 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3072 | const LValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3073 | return Visit(T->getPointeeType()); |
| 3074 | } |
| 3075 | |
| 3076 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3077 | const RValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3078 | return Visit(T->getPointeeType()); |
| 3079 | } |
| 3080 | |
| 3081 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3082 | const MemberPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3083 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3084 | } |
| 3085 | |
| 3086 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3087 | const ConstantArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3088 | return Visit(T->getElementType()); |
| 3089 | } |
| 3090 | |
| 3091 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3092 | const IncompleteArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3093 | return Visit(T->getElementType()); |
| 3094 | } |
| 3095 | |
| 3096 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3097 | const VariableArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3098 | return Visit(T->getElementType()); |
| 3099 | } |
| 3100 | |
| 3101 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3102 | const DependentSizedArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3103 | return Visit(T->getElementType()); |
| 3104 | } |
| 3105 | |
| 3106 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3107 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3108 | return Visit(T->getElementType()); |
| 3109 | } |
| 3110 | |
| 3111 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3112 | return Visit(T->getElementType()); |
| 3113 | } |
| 3114 | |
| 3115 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 3116 | return Visit(T->getElementType()); |
| 3117 | } |
| 3118 | |
| 3119 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 3120 | const FunctionProtoType* T) { |
| 3121 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3122 | AEnd = T->arg_type_end(); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3123 | A != AEnd; ++A) { |
| 3124 | if (Visit(*A)) |
| 3125 | return true; |
| 3126 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3127 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3128 | return Visit(T->getResultType()); |
| 3129 | } |
| 3130 | |
| 3131 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 3132 | const FunctionNoProtoType* T) { |
| 3133 | return Visit(T->getResultType()); |
| 3134 | } |
| 3135 | |
| 3136 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 3137 | const UnresolvedUsingType*) { |
| 3138 | return false; |
| 3139 | } |
| 3140 | |
| 3141 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 3142 | return false; |
| 3143 | } |
| 3144 | |
| 3145 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 3146 | return Visit(T->getUnderlyingType()); |
| 3147 | } |
| 3148 | |
| 3149 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 3150 | return false; |
| 3151 | } |
| 3152 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3153 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 3154 | const UnaryTransformType*) { |
| 3155 | return false; |
| 3156 | } |
| 3157 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3158 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 3159 | return Visit(T->getDeducedType()); |
| 3160 | } |
| 3161 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3162 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 3163 | return VisitTagDecl(T->getDecl()); |
| 3164 | } |
| 3165 | |
| 3166 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 3167 | return VisitTagDecl(T->getDecl()); |
| 3168 | } |
| 3169 | |
| 3170 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 3171 | const TemplateTypeParmType*) { |
| 3172 | return false; |
| 3173 | } |
| 3174 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3175 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 3176 | const SubstTemplateTypeParmPackType *) { |
| 3177 | return false; |
| 3178 | } |
| 3179 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3180 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 3181 | const TemplateSpecializationType*) { |
| 3182 | return false; |
| 3183 | } |
| 3184 | |
| 3185 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 3186 | const InjectedClassNameType* T) { |
| 3187 | return VisitTagDecl(T->getDecl()); |
| 3188 | } |
| 3189 | |
| 3190 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 3191 | const DependentNameType* T) { |
| 3192 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3193 | } |
| 3194 | |
| 3195 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 3196 | const DependentTemplateSpecializationType* T) { |
| 3197 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3198 | } |
| 3199 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3200 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 3201 | const PackExpansionType* T) { |
| 3202 | return Visit(T->getPattern()); |
| 3203 | } |
| 3204 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3205 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 3206 | return false; |
| 3207 | } |
| 3208 | |
| 3209 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 3210 | const ObjCInterfaceType *) { |
| 3211 | return false; |
| 3212 | } |
| 3213 | |
| 3214 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 3215 | const ObjCObjectPointerType *) { |
| 3216 | return false; |
| 3217 | } |
| 3218 | |
| 3219 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 3220 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
| 3221 | S.Diag(SR.getBegin(), diag::ext_template_arg_local_type) |
| 3222 | << S.Context.getTypeDeclType(Tag) << SR; |
| 3223 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3226 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3227 | S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR; |
| 3228 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 3229 | return true; |
| 3230 | } |
| 3231 | |
| 3232 | return false; |
| 3233 | } |
| 3234 | |
| 3235 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 3236 | NestedNameSpecifier *NNS) { |
| 3237 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 3238 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3239 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3240 | switch (NNS->getKind()) { |
| 3241 | case NestedNameSpecifier::Identifier: |
| 3242 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3243 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3244 | case NestedNameSpecifier::Global: |
| 3245 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3246 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3247 | case NestedNameSpecifier::TypeSpec: |
| 3248 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3249 | return Visit(QualType(NNS->getAsType(), 0)); |
| 3250 | } |
Fariborz Jahanian | 7b1ec6c | 2010-10-13 16:19:16 +0000 | [diff] [blame] | 3251 | return false; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3252 | } |
| 3253 | |
| 3254 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3255 | /// \brief Check a template argument against its corresponding |
| 3256 | /// template type parameter. |
| 3257 | /// |
| 3258 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 3259 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3260 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3261 | TypeSourceInfo *ArgInfo) { |
| 3262 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3263 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 3264 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 3265 | |
| 3266 | if (Arg->isVariablyModifiedType()) { |
| 3267 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3268 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3269 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3272 | // C++03 [temp.arg.type]p2: |
| 3273 | // A local type, a type with no linkage, an unnamed type or a type |
| 3274 | // compounded from any of these types shall not be used as a |
| 3275 | // template-argument for a template type-parameter. |
| 3276 | // |
| 3277 | // C++0x allows these, and even in C++03 we allow them as an extension with |
| 3278 | // a warning. |
Douglas Gregor | db4d4bb | 2010-10-13 18:05:20 +0000 | [diff] [blame] | 3279 | if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3280 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 3281 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 3282 | } |
| 3283 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3284 | return false; |
| 3285 | } |
| 3286 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3287 | /// \brief Checks whether the given template argument is the address |
| 3288 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3289 | static bool |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3290 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 3291 | NonTypeTemplateParmDecl *Param, |
| 3292 | QualType ParamType, |
| 3293 | Expr *ArgIn, |
| 3294 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3295 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3296 | Expr *Arg = ArgIn; |
| 3297 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3298 | |
| 3299 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3300 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3301 | Arg = Cast->getSubExpr(); |
| 3302 | |
| 3303 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3304 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3305 | // A template-argument for a non-type, non-template |
| 3306 | // template-parameter shall be one of: [...] |
| 3307 | // |
| 3308 | // -- the address of an object or function with external |
| 3309 | // linkage, including function templates and function |
| 3310 | // template-ids but excluding non-static class members, |
| 3311 | // expressed as & id-expression where the & is optional if |
| 3312 | // the name refers to a function or array, or if the |
| 3313 | // corresponding template-parameter is a reference; or |
| 3314 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3315 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3316 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3317 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3318 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3319 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3320 | if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3321 | S.Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3322 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3323 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3324 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3325 | } |
| 3326 | |
| 3327 | Arg = Parens->getSubExpr(); |
| 3328 | } |
| 3329 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3330 | bool AddressTaken = false; |
| 3331 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3332 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3333 | if (UnOp->getOpcode() == UO_AddrOf) { |
Francois Pichet | 11f98d0 | 2011-04-28 05:12:34 +0000 | [diff] [blame] | 3334 | // Support &__uuidof(class_with_uuid) as a non-type template argument. |
| 3335 | // Very common in Microsoft COM headers. |
| 3336 | if (S.getLangOptions().Microsoft && |
| 3337 | isa<CXXUuidofExpr>(UnOp->getSubExpr())) { |
| 3338 | Converted = TemplateArgument(ArgIn); |
| 3339 | return false; |
| 3340 | } |
| 3341 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3342 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3343 | AddressTaken = true; |
| 3344 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 3345 | } |
Francois Pichet | a343a41 | 2011-04-29 09:08:14 +0000 | [diff] [blame] | 3346 | } else { |
| 3347 | if (S.getLangOptions().Microsoft && isa<CXXUuidofExpr>(Arg)) { |
| 3348 | Converted = TemplateArgument(ArgIn); |
| 3349 | return false; |
| 3350 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3351 | DRE = dyn_cast<DeclRefExpr>(Arg); |
Francois Pichet | a343a41 | 2011-04-29 09:08:14 +0000 | [diff] [blame] | 3352 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3353 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3354 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 3355 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3356 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3357 | return true; |
| 3358 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3359 | |
| 3360 | // Stop checking the precise nature of the argument if it is value dependent, |
| 3361 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3362 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3363 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3364 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3365 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3366 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3367 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 3368 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3369 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3370 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3371 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3372 | return true; |
| 3373 | } |
| 3374 | |
| 3375 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3376 | |
| 3377 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3378 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 3379 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3380 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3381 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3382 | return true; |
| 3383 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3384 | |
| 3385 | // Cannot refer to non-static member functions |
| 3386 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3387 | if (!Method->isStatic()) { |
| 3388 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3389 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3390 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3391 | return true; |
| 3392 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3393 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3394 | // Functions must have external linkage. |
| 3395 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3396 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3397 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3398 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3399 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3400 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3401 | << true; |
| 3402 | return true; |
| 3403 | } |
| 3404 | |
| 3405 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3406 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3407 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3408 | // If the template parameter has pointer type, the function decays. |
| 3409 | if (ParamType->isPointerType() && !AddressTaken) |
| 3410 | ArgType = S.Context.getPointerType(Func->getType()); |
| 3411 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 3412 | // If we originally had an address-of operator, but the |
| 3413 | // parameter has reference type, complain and (if things look |
| 3414 | // like they will work) drop the address-of operator. |
| 3415 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 3416 | ParamType.getNonReferenceType())) { |
| 3417 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3418 | << ParamType; |
| 3419 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3420 | return true; |
| 3421 | } |
| 3422 | |
| 3423 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3424 | << ParamType |
| 3425 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3426 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3427 | |
| 3428 | ArgType = Func->getType(); |
| 3429 | } |
| 3430 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3431 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3432 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3433 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3434 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3435 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3436 | << true; |
| 3437 | return true; |
| 3438 | } |
| 3439 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3440 | // A value of reference type is not an object. |
| 3441 | if (Var->getType()->isReferenceType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3442 | S.Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3443 | diag::err_template_arg_reference_var) |
| 3444 | << Var->getType() << Arg->getSourceRange(); |
| 3445 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3446 | return true; |
| 3447 | } |
| 3448 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3449 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3450 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3451 | |
| 3452 | // If the template parameter has pointer type, we must have taken |
| 3453 | // the address of this object. |
| 3454 | if (ParamType->isReferenceType()) { |
| 3455 | if (AddressTaken) { |
| 3456 | // If we originally had an address-of operator, but the |
| 3457 | // parameter has reference type, complain and (if things look |
| 3458 | // like they will work) drop the address-of operator. |
| 3459 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3460 | ParamType.getNonReferenceType())) { |
| 3461 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3462 | << ParamType; |
| 3463 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3464 | return true; |
| 3465 | } |
| 3466 | |
| 3467 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3468 | << ParamType |
| 3469 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3470 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3471 | |
| 3472 | ArgType = Var->getType(); |
| 3473 | } |
| 3474 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3475 | if (Var->getType()->isArrayType()) { |
| 3476 | // Array-to-pointer decay. |
| 3477 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3478 | } else { |
| 3479 | // If the template parameter has pointer type but the address of |
| 3480 | // this object was not taken, complain and (possibly) recover by |
| 3481 | // taking the address of the entity. |
| 3482 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3483 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3484 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3485 | << ParamType; |
| 3486 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3487 | return true; |
| 3488 | } |
| 3489 | |
| 3490 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3491 | << ParamType |
| 3492 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3493 | |
| 3494 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3495 | } |
| 3496 | } |
| 3497 | } else { |
| 3498 | // We found something else, but we don't know specifically what it is. |
| 3499 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3500 | diag::err_template_arg_not_object_or_func) |
| 3501 | << Arg->getSourceRange(); |
| 3502 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3503 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3504 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3505 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3506 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3507 | if (ParamType->isPointerType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3508 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3509 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 3510 | ObjCLifetimeConversion)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3511 | // For pointer-to-object types, qualification conversions are |
| 3512 | // permitted. |
| 3513 | } else { |
| 3514 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3515 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3516 | // C++ [temp.arg.nontype]p5b3: |
| 3517 | // For a non-type template-parameter of type reference to |
| 3518 | // object, no conversions apply. The type referred to by the |
| 3519 | // reference may be more cv-qualified than the (otherwise |
| 3520 | // identical) type of the template- argument. The |
| 3521 | // template-parameter is bound directly to the |
| 3522 | // template-argument, which shall be an lvalue. |
| 3523 | |
| 3524 | // FIXME: Other qualifiers? |
| 3525 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3526 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3527 | |
| 3528 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 3529 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3530 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3531 | << ParamType << Arg->getType() |
| 3532 | << Arg->getSourceRange(); |
| 3533 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3534 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3535 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3536 | } |
| 3537 | } |
| 3538 | |
| 3539 | // At this point, the template argument refers to an object or |
| 3540 | // function with external linkage. We now need to check whether the |
| 3541 | // argument and parameter types are compatible. |
| 3542 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3543 | ParamType.getNonReferenceType())) { |
| 3544 | // We can't perform this conversion or binding. |
| 3545 | if (ParamType->isReferenceType()) |
| 3546 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 3547 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 3548 | else |
| 3549 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 3550 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 3551 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3552 | return true; |
| 3553 | } |
| 3554 | } |
| 3555 | |
| 3556 | // Create the template argument. |
| 3557 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 3558 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3559 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3560 | } |
| 3561 | |
| 3562 | /// \brief Checks whether the given template argument is a pointer to |
| 3563 | /// member constant according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3564 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3565 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3566 | bool Invalid = false; |
| 3567 | |
| 3568 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3569 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3570 | Arg = Cast->getSubExpr(); |
| 3571 | |
| 3572 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3573 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3574 | // A template-argument for a non-type, non-template |
| 3575 | // template-parameter shall be one of: [...] |
| 3576 | // |
| 3577 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3578 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3579 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3580 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3581 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3582 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3583 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3584 | if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3585 | Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3586 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3587 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3588 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3589 | } |
| 3590 | |
| 3591 | Arg = Parens->getSubExpr(); |
| 3592 | } |
| 3593 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3594 | // A pointer-to-member constant written &Class::member. |
| 3595 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3596 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3597 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3598 | if (DRE && !DRE->getQualifier()) |
| 3599 | DRE = 0; |
| 3600 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3601 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3602 | // A constant of pointer-to-member type. |
| 3603 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3604 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3605 | if (VD->getType()->isMemberPointerType()) { |
| 3606 | if (isa<NonTypeTemplateParmDecl>(VD) || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3607 | (isa<VarDecl>(VD) && |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3608 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3609 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3610 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3611 | else |
| 3612 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3613 | return Invalid; |
| 3614 | } |
| 3615 | } |
| 3616 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3617 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3618 | DRE = 0; |
| 3619 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3620 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3621 | if (!DRE) |
| 3622 | return Diag(Arg->getSourceRange().getBegin(), |
| 3623 | diag::err_template_arg_not_pointer_to_member_form) |
| 3624 | << Arg->getSourceRange(); |
| 3625 | |
| 3626 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3627 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3628 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3629 | "Only non-static member pointers can make it here"); |
| 3630 | |
| 3631 | // Okay: this is the address of a non-static member, and therefore |
| 3632 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3633 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3634 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3635 | else |
| 3636 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3637 | return Invalid; |
| 3638 | } |
| 3639 | |
| 3640 | // 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] | 3641 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3642 | diag::err_template_arg_not_pointer_to_member_form) |
| 3643 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3644 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3645 | diag::note_template_arg_refers_here); |
| 3646 | return true; |
| 3647 | } |
| 3648 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3649 | /// \brief Check a template argument against its corresponding |
| 3650 | /// non-type template parameter. |
| 3651 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3652 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3653 | /// If an error occurred, it returns ExprError(); otherwise, it |
| 3654 | /// returns the converted template argument. \p |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3655 | /// InstantiatedParamType is the type of the non-type template |
| 3656 | /// parameter after it has been instantiated. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3657 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 3658 | QualType InstantiatedParamType, Expr *Arg, |
| 3659 | TemplateArgument &Converted, |
| 3660 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3661 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3662 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3663 | // If either the parameter has a dependent type or the argument is |
| 3664 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3665 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3666 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3667 | Converted = TemplateArgument(Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3668 | return Owned(Arg); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3669 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3670 | |
| 3671 | // C++ [temp.arg.nontype]p5: |
| 3672 | // The following conversions are performed on each expression used |
| 3673 | // as a non-type template-argument. If a non-type |
| 3674 | // template-argument cannot be converted to the type of the |
| 3675 | // corresponding template-parameter then the program is |
| 3676 | // ill-formed. |
| 3677 | // |
| 3678 | // -- for a non-type template-parameter of integral or |
| 3679 | // enumeration type, integral promotions (4.5) and integral |
| 3680 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3681 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3682 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3683 | if (ParamType->isIntegralOrEnumerationType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3684 | // C++ [temp.arg.nontype]p1: |
| 3685 | // A template-argument for a non-type, non-template |
| 3686 | // template-parameter shall be one of: |
| 3687 | // |
| 3688 | // -- an integral constant-expression of integral or enumeration |
| 3689 | // type; or |
| 3690 | // -- the name of a non-type template-parameter; or |
| 3691 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3692 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3693 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3694 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3695 | diag::err_template_arg_not_integral_or_enumeral) |
| 3696 | << ArgType << Arg->getSourceRange(); |
| 3697 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3698 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3699 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3700 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3701 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 3702 | << ArgType << Arg->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3703 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3704 | } |
| 3705 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3706 | // From here on out, all we care about are the unqualified forms |
| 3707 | // of the parameter and argument types. |
| 3708 | ParamType = ParamType.getUnqualifiedType(); |
| 3709 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3710 | |
| 3711 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3712 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3713 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3714 | } else if (CTAK == CTAK_Deduced) { |
| 3715 | // C++ [temp.deduct.type]p17: |
| 3716 | // If, in the declaration of a function template with a non-type |
| 3717 | // template-parameter, the non-type template- parameter is used |
| 3718 | // in an expression in the function parameter-list and, if the |
| 3719 | // corresponding template-argument is deduced, the |
| 3720 | // template-argument type shall match the type of the |
| 3721 | // template-parameter exactly, except that a template-argument |
| 3722 | // deduced from an array bound may be of any integral type. |
| 3723 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3724 | << ArgType << ParamType; |
| 3725 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3726 | return ExprError(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3727 | } else if (ParamType->isBooleanType()) { |
| 3728 | // This is an integral-to-boolean conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3729 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3730 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3731 | !ParamType->isEnumeralType()) { |
| 3732 | // This is an integral promotion or conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3733 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3734 | } else { |
| 3735 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3736 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3737 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3738 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3739 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3740 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3741 | } |
| 3742 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3743 | // Add the value of this argument to the list of converted |
| 3744 | // arguments. We use the bitwidth and signedness of the template |
| 3745 | // parameter. |
| 3746 | if (Arg->isValueDependent()) { |
| 3747 | // The argument is value-dependent. Create a new |
| 3748 | // TemplateArgument with the converted expression. |
| 3749 | Converted = TemplateArgument(Arg); |
| 3750 | return Owned(Arg); |
| 3751 | } |
| 3752 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3753 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3754 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3755 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3756 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3757 | if (ParamType->isBooleanType()) { |
| 3758 | // Value must be zero or one. |
| 3759 | Value = Value != 0; |
| 3760 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 3761 | if (Value.getBitWidth() != AllowedBits) |
| 3762 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3763 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3764 | } else { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3765 | llvm::APSInt OldValue = Value; |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3766 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3767 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3768 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3769 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3770 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3771 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3772 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3773 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3774 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3775 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3776 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3777 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3778 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3779 | << Arg->getSourceRange(); |
| 3780 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3781 | } |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3782 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3783 | // Complain if we overflowed the template parameter's type. |
| 3784 | unsigned RequiredBits; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3785 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3786 | RequiredBits = OldValue.getActiveBits(); |
| 3787 | else if (OldValue.isUnsigned()) |
| 3788 | RequiredBits = OldValue.getActiveBits() + 1; |
| 3789 | else |
| 3790 | RequiredBits = OldValue.getMinSignedBits(); |
| 3791 | if (RequiredBits > AllowedBits) { |
| 3792 | Diag(Arg->getSourceRange().getBegin(), |
| 3793 | diag::warn_template_arg_too_large) |
| 3794 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3795 | << Arg->getSourceRange(); |
| 3796 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3797 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3798 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3799 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3800 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3801 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3802 | : IntegerType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3803 | return Owned(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3804 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3805 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3806 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 3807 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3808 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 3809 | // from a template argument of type std::nullptr_t to a non-type |
| 3810 | // template parameter of type pointer to object, pointer to |
| 3811 | // function, or pointer-to-member, respectively. |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 3812 | if (ArgType->isNullPtrType()) { |
| 3813 | if (ParamType->isPointerType() || ParamType->isMemberPointerType()) { |
| 3814 | Converted = TemplateArgument((NamedDecl *)0); |
| 3815 | return Owned(Arg); |
| 3816 | } |
| 3817 | |
| 3818 | if (ParamType->isNullPtrType()) { |
| 3819 | llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true); |
| 3820 | Converted = TemplateArgument(Zero, Context.NullPtrTy); |
| 3821 | return Owned(Arg); |
| 3822 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3823 | } |
| 3824 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3825 | // Handle pointer-to-function, reference-to-function, and |
| 3826 | // pointer-to-member-function all in (roughly) the same way. |
| 3827 | if (// -- For a non-type template-parameter of type pointer to |
| 3828 | // function, only the function-to-pointer conversion (4.3) is |
| 3829 | // applied. If the template-argument represents a set of |
| 3830 | // overloaded functions (or a pointer to such), the matching |
| 3831 | // function is selected from the set (13.4). |
| 3832 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3833 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3834 | // -- For a non-type template-parameter of type reference to |
| 3835 | // function, no conversions apply. If the template-argument |
| 3836 | // represents a set of overloaded functions, the matching |
| 3837 | // function is selected from the set (13.4). |
| 3838 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3839 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3840 | // -- For a non-type template-parameter of type pointer to |
| 3841 | // member function, no conversions apply. If the |
| 3842 | // template-argument represents a set of overloaded member |
| 3843 | // functions, the matching member function is selected from |
| 3844 | // the set (13.4). |
| 3845 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3846 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3847 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3848 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3849 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3850 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3851 | true, |
| 3852 | FoundResult)) { |
| 3853 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3854 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3855 | |
| 3856 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3857 | ArgType = Arg->getType(); |
| 3858 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3859 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3860 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3861 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3862 | if (!ParamType->isMemberPointerType()) { |
| 3863 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3864 | ParamType, |
| 3865 | Arg, Converted)) |
| 3866 | return ExprError(); |
| 3867 | return Owned(Arg); |
| 3868 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3869 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3870 | bool ObjCLifetimeConversion; |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3871 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3872 | false, ObjCLifetimeConversion)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3873 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3874 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 3875 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3876 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3877 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3878 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3879 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3880 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3881 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3882 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3883 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3884 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 3885 | return ExprError(); |
| 3886 | return Owned(Arg); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3887 | } |
| 3888 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 3889 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3890 | // -- for a non-type template-parameter of type pointer to |
| 3891 | // object, qualification conversions (4.4) and the |
| 3892 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3893 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3894 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3895 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3896 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3897 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3898 | ParamType, |
| 3899 | Arg, Converted)) |
| 3900 | return ExprError(); |
| 3901 | return Owned(Arg); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3902 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3903 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3904 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3905 | // -- For a non-type template-parameter of type reference to |
| 3906 | // object, no conversions apply. The type referred to by the |
| 3907 | // reference may be more cv-qualified than the (otherwise |
| 3908 | // identical) type of the template-argument. The |
| 3909 | // template-parameter is bound directly to the |
| 3910 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3911 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3912 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3913 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3914 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3915 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 3916 | ParamRefType->getPointeeType(), |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3917 | true, |
| 3918 | FoundResult)) { |
| 3919 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3920 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3921 | |
| 3922 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3923 | ArgType = Arg->getType(); |
| 3924 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3925 | return ExprError(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3926 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3927 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3928 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3929 | ParamType, |
| 3930 | Arg, Converted)) |
| 3931 | return ExprError(); |
| 3932 | return Owned(Arg); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3933 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3934 | |
| 3935 | // -- For a non-type template-parameter of type pointer to data |
| 3936 | // member, qualification conversions (4.4) are applied. |
| 3937 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 3938 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3939 | bool ObjCLifetimeConversion; |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 3940 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3941 | // Types match exactly: nothing more to do here. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3942 | } else if (IsQualificationConversion(ArgType, ParamType, false, |
| 3943 | ObjCLifetimeConversion)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3944 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3945 | } else { |
| 3946 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3947 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3948 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3949 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3950 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3951 | return ExprError(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3952 | } |
| 3953 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3954 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 3955 | return ExprError(); |
| 3956 | return Owned(Arg); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3957 | } |
| 3958 | |
| 3959 | /// \brief Check a template argument against its corresponding |
| 3960 | /// template template parameter. |
| 3961 | /// |
| 3962 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 3963 | /// It returns true if an error occurred, and false otherwise. |
| 3964 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3965 | const TemplateArgumentLoc &Arg) { |
| 3966 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 3967 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 3968 | if (!Template) { |
| 3969 | // Any dependent template name is fine. |
| 3970 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 3971 | return false; |
| 3972 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3973 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3974 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3975 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3976 | // the name of a class template or an alias template, expressed as an |
| 3977 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3978 | // primary class templates are considered when matching the |
| 3979 | // template template argument with the corresponding parameter; |
| 3980 | // partial specializations are not considered even if their |
| 3981 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3982 | // |
| 3983 | // Note that we also allow template template parameters here, which |
| 3984 | // will happen when we are dealing with, e.g., class template |
| 3985 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3986 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3987 | !isa<TemplateTemplateParmDecl>(Template) && |
| 3988 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3989 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3990 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3991 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3992 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3993 | << Template; |
| 3994 | } |
| 3995 | |
| 3996 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 3997 | Param->getTemplateParameters(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3998 | true, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3999 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4000 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4003 | /// \brief Given a non-type template argument that refers to a |
| 4004 | /// declaration and the type of its corresponding non-type template |
| 4005 | /// parameter, produce an expression that properly refers to that |
| 4006 | /// declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4007 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4008 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 4009 | QualType ParamType, |
| 4010 | SourceLocation Loc) { |
| 4011 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 4012 | "Only declaration template arguments permitted here"); |
| 4013 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 4014 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4015 | if (VD->getDeclContext()->isRecord() && |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4016 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 4017 | // If the value is a class member, we might have a pointer-to-member. |
| 4018 | // Determine whether the non-type template template parameter is of |
| 4019 | // pointer-to-member type. If so, we need to build an appropriate |
| 4020 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 4021 | // would refer to the member itself. |
| 4022 | if (ParamType->isMemberPointerType()) { |
| 4023 | QualType ClassType |
| 4024 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 4025 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4026 | = NestedNameSpecifier::Create(Context, 0, false, |
| 4027 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4028 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 4029 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4030 | |
| 4031 | // The actual value-ness of this is unimportant, but for |
| 4032 | // internal consistency's sake, references to instance methods |
| 4033 | // are r-values. |
| 4034 | ExprValueKind VK = VK_LValue; |
| 4035 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 4036 | VK = VK_RValue; |
| 4037 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4038 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4039 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4040 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4041 | Loc, |
| 4042 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4043 | if (RefExpr.isInvalid()) |
| 4044 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4045 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4046 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4047 | |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4048 | // We might need to perform a trailing qualification conversion, since |
| 4049 | // the element type on the parameter could be more qualified than the |
| 4050 | // element type in the expression we constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4051 | bool ObjCLifetimeConversion; |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4052 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4053 | ParamType.getUnqualifiedType(), false, |
| 4054 | ObjCLifetimeConversion)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4055 | RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4056 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4057 | assert(!RefExpr.isInvalid() && |
| 4058 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4059 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4060 | return move(RefExpr); |
| 4061 | } |
| 4062 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4063 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4064 | QualType T = VD->getType().getNonReferenceType(); |
| 4065 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4066 | // When the non-type template parameter is a pointer, take the |
| 4067 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4068 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4069 | if (RefExpr.isInvalid()) |
| 4070 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4071 | |
| 4072 | if (T->isFunctionType() || T->isArrayType()) { |
| 4073 | // Decay functions and arrays. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4074 | RefExpr = DefaultFunctionArrayConversion(RefExpr.take()); |
| 4075 | if (RefExpr.isInvalid()) |
| 4076 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4077 | |
| 4078 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4079 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4080 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4081 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4082 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4085 | ExprValueKind VK = VK_RValue; |
| 4086 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4087 | // If the non-type template parameter has reference type, qualify the |
| 4088 | // resulting declaration reference with the extra qualifiers on the |
| 4089 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4090 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 4091 | VK = VK_LValue; |
| 4092 | T = Context.getQualifiedType(T, |
| 4093 | TargetRef->getPointeeType().getQualifiers()); |
| 4094 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4095 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4096 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
| 4099 | /// \brief Construct a new expression that refers to the given |
| 4100 | /// integral template argument with the given source-location |
| 4101 | /// information. |
| 4102 | /// |
| 4103 | /// This routine takes care of the mapping from an integral template |
| 4104 | /// argument (which may have any integral type) to the appropriate |
| 4105 | /// literal value. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4106 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4107 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 4108 | SourceLocation Loc) { |
| 4109 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4110 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4111 | QualType T = Arg.getIntegralType(); |
| 4112 | if (T->isCharType() || T->isWideCharType()) |
| 4113 | return Owned(new (Context) CharacterLiteral( |
| 4114 | Arg.getAsIntegral()->getZExtValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4115 | T->isWideCharType(), T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4116 | if (T->isBooleanType()) |
| 4117 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 4118 | Arg.getAsIntegral()->getBoolValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4119 | T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4120 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4121 | if (T->isNullPtrType()) |
| 4122 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 4123 | |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4124 | // If this is an enum type that we're instantiating, we need to use an integer |
| 4125 | // type the same size as the enumerator. We don't want to build an |
| 4126 | // IntegerLiteral with enum type. |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4127 | QualType BT; |
| 4128 | if (const EnumType *ET = T->getAs<EnumType>()) |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4129 | BT = ET->getDecl()->getIntegerType(); |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4130 | else |
| 4131 | BT = T; |
| 4132 | |
| 4133 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
Douglas Gregor | 8f5667d | 2011-02-18 02:12:44 +0000 | [diff] [blame] | 4134 | if (T->isEnumeralType()) { |
| 4135 | // FIXME: This is a hack. We need a better way to handle substituted |
| 4136 | // non-type template parameters. |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4137 | E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0, |
| 4138 | Context.getTrivialTypeSourceInfo(T, Loc), |
| 4139 | Loc, Loc); |
Douglas Gregor | 8f5667d | 2011-02-18 02:12:44 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4142 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4145 | /// \brief Match two template parameters within template parameter lists. |
| 4146 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 4147 | bool Complain, |
| 4148 | Sema::TemplateParameterListEqualKind Kind, |
| 4149 | SourceLocation TemplateArgLoc) { |
| 4150 | // Check the actual kind (type, non-type, template). |
| 4151 | if (Old->getKind() != New->getKind()) { |
| 4152 | if (Complain) { |
| 4153 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 4154 | if (TemplateArgLoc.isValid()) { |
| 4155 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4156 | NextDiag = diag::note_template_param_different_kind; |
| 4157 | } |
| 4158 | S.Diag(New->getLocation(), NextDiag) |
| 4159 | << (Kind != Sema::TPL_TemplateMatch); |
| 4160 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 4161 | << (Kind != Sema::TPL_TemplateMatch); |
| 4162 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4163 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4164 | return false; |
| 4165 | } |
| 4166 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4167 | // Check that both are parameter packs are neither are parameter packs. |
| 4168 | // However, if we are matching a template template argument to a |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4169 | // template template parameter, the template template parameter can have |
| 4170 | // a parameter pack where the template template argument does not. |
| 4171 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 4172 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4173 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4174 | if (Complain) { |
| 4175 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 4176 | if (TemplateArgLoc.isValid()) { |
| 4177 | S.Diag(TemplateArgLoc, |
| 4178 | diag::err_template_arg_template_params_mismatch); |
| 4179 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 4180 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4181 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4182 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 4183 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 4184 | : 2; |
| 4185 | S.Diag(New->getLocation(), NextDiag) |
| 4186 | << ParamKind << New->isParameterPack(); |
| 4187 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 4188 | << ParamKind << Old->isParameterPack(); |
| 4189 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4190 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4191 | return false; |
| 4192 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4193 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4194 | // For non-type template parameters, check the type of the parameter. |
| 4195 | if (NonTypeTemplateParmDecl *OldNTTP |
| 4196 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 4197 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4198 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4199 | // If we are matching a template template argument to a template |
| 4200 | // template parameter and one of the non-type template parameter types |
| 4201 | // is dependent, then we must wait until template instantiation time |
| 4202 | // to actually compare the arguments. |
| 4203 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4204 | (OldNTTP->getType()->isDependentType() || |
| 4205 | NewNTTP->getType()->isDependentType())) |
| 4206 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4207 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4208 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 4209 | if (Complain) { |
| 4210 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 4211 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4212 | S.Diag(TemplateArgLoc, |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4213 | diag::err_template_arg_template_params_mismatch); |
| 4214 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 4215 | } |
| 4216 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 4217 | << NewNTTP->getType() |
| 4218 | << (Kind != Sema::TPL_TemplateMatch); |
| 4219 | S.Diag(OldNTTP->getLocation(), |
| 4220 | diag::note_template_nontype_parm_prev_declaration) |
| 4221 | << OldNTTP->getType(); |
| 4222 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4223 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4224 | return false; |
| 4225 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4226 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4227 | return true; |
| 4228 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4229 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4230 | // For template template parameters, check the template parameter types. |
| 4231 | // The template parameter lists of template template |
| 4232 | // parameters must agree. |
| 4233 | if (TemplateTemplateParmDecl *OldTTP |
| 4234 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4235 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4236 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 4237 | OldTTP->getTemplateParameters(), |
| 4238 | Complain, |
| 4239 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4240 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4241 | : Kind), |
| 4242 | TemplateArgLoc); |
| 4243 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4244 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4245 | return true; |
| 4246 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4247 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4248 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 4249 | /// lists. |
| 4250 | static |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4251 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4252 | TemplateParameterList *New, |
| 4253 | TemplateParameterList *Old, |
| 4254 | Sema::TemplateParameterListEqualKind Kind, |
| 4255 | SourceLocation TemplateArgLoc) { |
| 4256 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 4257 | if (TemplateArgLoc.isValid()) { |
| 4258 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4259 | NextDiag = diag::note_template_param_list_different_arity; |
| 4260 | } |
| 4261 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 4262 | << (New->size() > Old->size()) |
| 4263 | << (Kind != Sema::TPL_TemplateMatch) |
| 4264 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 4265 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 4266 | << (Kind != Sema::TPL_TemplateMatch) |
| 4267 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 4268 | } |
| 4269 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4270 | /// \brief Determine whether the given template parameter lists are |
| 4271 | /// equivalent. |
| 4272 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4273 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4274 | /// source code as part of a new template declaration. |
| 4275 | /// |
| 4276 | /// \param Old The old template parameter list, typically found via |
| 4277 | /// name lookup of the template declared with this template parameter |
| 4278 | /// list. |
| 4279 | /// |
| 4280 | /// \param Complain If true, this routine will produce a diagnostic if |
| 4281 | /// the template parameter lists are not equivalent. |
| 4282 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4283 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4284 | /// |
| 4285 | /// \param TemplateArgLoc If this source location is valid, then we |
| 4286 | /// are actually checking the template parameter list of a template |
| 4287 | /// argument (New) against the template parameter list of its |
| 4288 | /// corresponding template template parameter (Old). We produce |
| 4289 | /// slightly different diagnostics in this scenario. |
| 4290 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4291 | /// \returns True if the template parameter lists are equal, false |
| 4292 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4293 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4294 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 4295 | TemplateParameterList *Old, |
| 4296 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4297 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4298 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4299 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 4300 | if (Complain) |
| 4301 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4302 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4303 | |
| 4304 | return false; |
| 4305 | } |
| 4306 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4307 | // C++0x [temp.arg.template]p3: |
| 4308 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4309 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4310 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4311 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4312 | // template-parameter-list of P. [...] |
| 4313 | TemplateParameterList::iterator NewParm = New->begin(); |
| 4314 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4315 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4316 | OldParmEnd = Old->end(); |
| 4317 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 4318 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 4319 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4320 | if (NewParm == NewParmEnd) { |
| 4321 | if (Complain) |
| 4322 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4323 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4324 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4325 | return false; |
| 4326 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4327 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4328 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4329 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4330 | return false; |
| 4331 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4332 | ++NewParm; |
| 4333 | continue; |
| 4334 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4335 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4336 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4337 | // [...] When P's template- parameter-list contains a template parameter |
| 4338 | // pack (14.5.3), the template parameter pack will match zero or more |
| 4339 | // template parameters or template parameter packs in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4340 | // template-parameter-list of A with the same type and form as the |
| 4341 | // template parameter pack in P (ignoring whether those template |
| 4342 | // parameters are template parameter packs). |
| 4343 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 4344 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4345 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4346 | return false; |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4347 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4348 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4349 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4350 | // Make sure we exhausted all of the arguments. |
| 4351 | if (NewParm != NewParmEnd) { |
| 4352 | if (Complain) |
| 4353 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4354 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4355 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4356 | return false; |
| 4357 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4358 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4359 | return true; |
| 4360 | } |
| 4361 | |
| 4362 | /// \brief Check whether a template can be declared within this scope. |
| 4363 | /// |
| 4364 | /// If the template declaration is valid in this scope, returns |
| 4365 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4366 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4367 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4368 | // Find the nearest enclosing declaration scope. |
| 4369 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4370 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4371 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4372 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4373 | // C++ [temp]p2: |
| 4374 | // A template-declaration can appear only as a namespace scope or |
| 4375 | // class scope declaration. |
| 4376 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4377 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 4378 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4379 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4380 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4381 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4382 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4383 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4384 | |
| 4385 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 4386 | return false; |
| 4387 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4388 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4389 | diag::err_template_outside_namespace_or_class_scope) |
| 4390 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4391 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4392 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4393 | /// \brief Determine what kind of template specialization the given declaration |
| 4394 | /// is. |
| 4395 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 4396 | if (!D) |
| 4397 | return TSK_Undeclared; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4398 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4399 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 4400 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4401 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 4402 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4403 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 4404 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4405 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4406 | return TSK_Undeclared; |
| 4407 | } |
| 4408 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4409 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4410 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4411 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4412 | /// This routine determines whether a template specialization can be declared |
| 4413 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4414 | /// |
| 4415 | /// \param S the semantic analysis object for which this check is being |
| 4416 | /// performed. |
| 4417 | /// |
| 4418 | /// \param Specialized the entity being specialized or instantiated, which |
| 4419 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4420 | /// a member of a class template (member function, static data member, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4421 | /// member class). |
| 4422 | /// |
| 4423 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 4424 | /// |
| 4425 | /// \param Loc the location of the explicit specialization or instantiation of |
| 4426 | /// this entity. |
| 4427 | /// |
| 4428 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 4429 | /// a class template. |
| 4430 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4431 | /// \returns true if there was an error that we cannot recover from, false |
| 4432 | /// otherwise. |
| 4433 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 4434 | NamedDecl *Specialized, |
| 4435 | NamedDecl *PrevDecl, |
| 4436 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4437 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4438 | // Keep these "kind" numbers in sync with the %select statements in the |
| 4439 | // various diagnostics emitted by this routine. |
| 4440 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4441 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4442 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4443 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4444 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4445 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4446 | EntityKind = 3; |
| 4447 | else if (isa<VarDecl>(Specialized)) |
| 4448 | EntityKind = 4; |
| 4449 | else if (isa<RecordDecl>(Specialized)) |
| 4450 | EntityKind = 5; |
| 4451 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4452 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 4453 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4454 | return true; |
| 4455 | } |
| 4456 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4457 | // C++ [temp.expl.spec]p2: |
| 4458 | // An explicit specialization shall be declared in the namespace |
| 4459 | // of which the template is a member, or, for member templates, in |
| 4460 | // the namespace of which the enclosing class or enclosing class |
| 4461 | // template is a member. An explicit specialization of a member |
| 4462 | // function, member class or static data member of a class |
| 4463 | // template shall be declared in the namespace of which the class |
| 4464 | // template is a member. Such a declaration may also be a |
| 4465 | // definition. If the declaration is not a definition, the |
| 4466 | // specialization may be defined later in the name- space in which |
| 4467 | // the explicit specialization was declared, or in a namespace |
| 4468 | // that encloses the one in which the explicit specialization was |
| 4469 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4470 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4471 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4472 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4473 | return true; |
| 4474 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4475 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4476 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
Francois Pichet | e1e96a6 | 2011-05-14 19:17:07 +0000 | [diff] [blame] | 4477 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4478 | << Specialized; |
| 4479 | return true; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4480 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4481 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4482 | // C++ [temp.class.spec]p6: |
| 4483 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4484 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4485 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4486 | bool ComplainedAboutScope = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4487 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4488 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4489 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4490 | if ((!PrevDecl || |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4491 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4492 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4493 | // C++ [temp.exp.spec]p2: |
| 4494 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4495 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4496 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4497 | // An explicit specialization of a member function, member class or |
| 4498 | // static data member of a class template shall be declared in the |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4499 | // namespace of which the class template is a member. |
| 4500 | // |
| 4501 | // C++0x [temp.expl.spec]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4502 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4503 | // the specialized template. |
| 4504 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) && |
| 4505 | !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) { |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4506 | bool IsCPlusPlus0xExtension |
| 4507 | = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4508 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4509 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4510 | ? diag::ext_template_spec_decl_out_of_scope_global |
| 4511 | : diag::err_template_spec_decl_out_of_scope_global) |
| 4512 | << EntityKind << Specialized; |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4513 | else if (isa<NamespaceDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4514 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4515 | ? diag::ext_template_spec_decl_out_of_scope |
| 4516 | : diag::err_template_spec_decl_out_of_scope) |
| 4517 | << EntityKind << Specialized |
| 4518 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4519 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4520 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 4521 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4522 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4523 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4524 | |
| 4525 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4526 | // namespace. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4527 | // Note that HandleDeclarator() performs this check for explicit |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4528 | // specializations of function templates, static data members, and member |
| 4529 | // functions, so we skip the check here for those kinds of entities. |
| 4530 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4531 | // Should we refactor that check, so that it occurs later? |
| 4532 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4533 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 4534 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4535 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 4536 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 4537 | << EntityKind << Specialized; |
| 4538 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 4539 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 4540 | << EntityKind << Specialized |
| 4541 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4542 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4543 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4544 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4545 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4546 | // FIXME: check for specialization-after-instantiation errors and such. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4547 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4548 | return false; |
| 4549 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4550 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4551 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 4552 | /// that checks non-type template partial specialization arguments. |
| 4553 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 4554 | NonTypeTemplateParmDecl *Param, |
| 4555 | const TemplateArgument *Args, |
| 4556 | unsigned NumArgs) { |
| 4557 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4558 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 4559 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4560 | Args[I].pack_begin(), |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4561 | Args[I].pack_size())) |
| 4562 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4563 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4564 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4565 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4566 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4567 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4568 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4569 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4570 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4571 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4572 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4573 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4574 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4575 | |
| 4576 | // Strip off any implicit casts we added as part of type checking. |
| 4577 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4578 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4579 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4580 | // C++ [temp.class.spec]p8: |
| 4581 | // A non-type argument is non-specialized if it is the name of a |
| 4582 | // non-type parameter. All other non-type arguments are |
| 4583 | // specialized. |
| 4584 | // |
| 4585 | // Below, we check the two conditions that only apply to |
| 4586 | // specialized non-type arguments, so skip any non-specialized |
| 4587 | // arguments. |
| 4588 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4589 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4590 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4591 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4592 | // C++ [temp.class.spec]p9: |
| 4593 | // Within the argument list of a class template partial |
| 4594 | // specialization, the following restrictions apply: |
| 4595 | // -- A partially specialized non-type argument expression |
| 4596 | // shall not involve a template parameter of the partial |
| 4597 | // specialization except when the argument expression is a |
| 4598 | // simple identifier. |
| 4599 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4600 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4601 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4602 | << ArgExpr->getSourceRange(); |
| 4603 | return true; |
| 4604 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4605 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4606 | // -- The type of a template parameter corresponding to a |
| 4607 | // specialized non-type argument shall not be dependent on a |
| 4608 | // parameter of the specialization. |
| 4609 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4610 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4611 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 4612 | << Param->getType() |
| 4613 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4614 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4615 | return true; |
| 4616 | } |
| 4617 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4618 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4619 | return false; |
| 4620 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4621 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4622 | /// \brief Check the non-type template arguments of a class template |
| 4623 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 4624 | /// |
| 4625 | /// \param TemplateParams the template parameters of the primary class |
| 4626 | /// template. |
| 4627 | /// |
| 4628 | /// \param TemplateArg the template arguments of the class template |
| 4629 | /// partial specialization. |
| 4630 | /// |
| 4631 | /// \returns true if there was an error, false otherwise. |
| 4632 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 4633 | TemplateParameterList *TemplateParams, |
| 4634 | llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
| 4635 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 4636 | |
| 4637 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4638 | NonTypeTemplateParmDecl *Param |
| 4639 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 4640 | if (!Param) |
| 4641 | continue; |
| 4642 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4643 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4644 | &ArgList[I], 1)) |
| 4645 | return true; |
| 4646 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4647 | |
| 4648 | return false; |
| 4649 | } |
| 4650 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4651 | /// \brief Retrieve the previous declaration of the given declaration. |
| 4652 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 4653 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4654 | return VD->getPreviousDeclaration(); |
| 4655 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 4656 | return FD->getPreviousDeclaration(); |
| 4657 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 4658 | return TD->getPreviousDeclaration(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4659 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4660 | return TD->getPreviousDeclaration(); |
| 4661 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 4662 | return FTD->getPreviousDeclaration(); |
| 4663 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 4664 | return CTD->getPreviousDeclaration(); |
| 4665 | return 0; |
| 4666 | } |
| 4667 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4668 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4669 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4670 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4671 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4672 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4673 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4674 | SourceLocation TemplateNameLoc, |
| 4675 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4676 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4677 | SourceLocation RAngleLoc, |
| 4678 | AttributeList *Attr, |
| 4679 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4680 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4681 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4682 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 4683 | // store the location of the outermost template keyword in the declaration. |
| 4684 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
| 4685 | ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation(); |
| 4686 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4687 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4688 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4689 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4690 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4691 | |
| 4692 | if (!ClassTemplate) { |
| 4693 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4694 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4695 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4696 | return true; |
| 4697 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4698 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4699 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4700 | bool isPartialSpecialization = false; |
| 4701 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4702 | // Check the validity of the template headers that introduce this |
| 4703 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4704 | // FIXME: We probably shouldn't complain about these headers for |
| 4705 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4706 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4707 | TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 4708 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, |
| 4709 | TemplateNameLoc, |
| 4710 | SS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4711 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4712 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4713 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4714 | isExplicitSpecialization, |
| 4715 | Invalid); |
| 4716 | if (Invalid) |
| 4717 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4718 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4719 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4720 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4721 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4722 | if (TUK == TUK_Friend) { |
| 4723 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4724 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4725 | return true; |
| 4726 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4727 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4728 | // C++ [temp.class.spec]p10: |
| 4729 | // The template parameter list of a specialization shall not |
| 4730 | // contain default template argument values. |
| 4731 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4732 | Decl *Param = TemplateParams->getParam(I); |
| 4733 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4734 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4735 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4736 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4737 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4738 | } |
| 4739 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4740 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4741 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4742 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4743 | diag::err_default_arg_in_partial_spec) |
| 4744 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4745 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4746 | } |
| 4747 | } else { |
| 4748 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4749 | if (TTP->hasDefaultArgument()) { |
| 4750 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4751 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4752 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4753 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4754 | } |
| 4755 | } |
| 4756 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4757 | } else if (TemplateParams) { |
| 4758 | if (TUK == TUK_Friend) |
| 4759 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4760 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4761 | SourceRange(TemplateParams->getTemplateLoc(), |
| 4762 | TemplateParams->getRAngleLoc())) |
| 4763 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4764 | else |
| 4765 | isExplicitSpecialization = true; |
| 4766 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4767 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4768 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4769 | isExplicitSpecialization = true; |
| 4770 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4771 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4772 | // Check that the specialization uses the same tag kind as the |
| 4773 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4774 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4775 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4776 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 4777 | Kind, TUK == TUK_Definition, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4778 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4779 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4780 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4781 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4782 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4783 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4784 | diag::note_previous_use); |
| 4785 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4786 | } |
| 4787 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4788 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4789 | TemplateArgumentListInfo TemplateArgs; |
| 4790 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 4791 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4792 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4793 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4794 | // Check for unexpanded parameter packs in any of the template arguments. |
| 4795 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4796 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4797 | UPPC_PartialSpecialization)) |
| 4798 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4799 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4800 | // Check that the template argument list is well-formed for this |
| 4801 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4802 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4803 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4804 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4805 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4806 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4807 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4808 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4809 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4810 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4811 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4812 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4813 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4814 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4815 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4816 | return true; |
| 4817 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4818 | if (!Name.isDependent() && |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4819 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4820 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4821 | TemplateArgs.size())) { |
| 4822 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 4823 | << ClassTemplate->getDeclName(); |
| 4824 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4825 | } |
| 4826 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4827 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4828 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4829 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 4830 | |
| 4831 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4832 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4833 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4834 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 4835 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4836 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4837 | else |
| 4838 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4839 | = ClassTemplate->findSpecialization(Converted.data(), |
| 4840 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4841 | |
| 4842 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4843 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4844 | // Check whether we can declare a class template specialization in |
| 4845 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4846 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4847 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 4848 | TemplateNameLoc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4849 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4850 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4851 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4852 | // The canonical type |
| 4853 | QualType CanonType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4854 | if (PrevDecl && |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4855 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4856 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4857 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4858 | // arguments was referenced but not declared, or we're only |
| 4859 | // referencing this specialization as a friend, reuse that |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4860 | // declaration node as our own, updating its source location and |
| 4861 | // the list of outer template parameters to reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4862 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4863 | Specialization->setLocation(TemplateNameLoc); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4864 | if (TemplateParameterLists.size() > 0) { |
| 4865 | Specialization->setTemplateParameterListsInfo(Context, |
| 4866 | TemplateParameterLists.size(), |
| 4867 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4868 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4869 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4870 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4871 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4872 | // Build the canonical type that describes the converted template |
| 4873 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4874 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4875 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4876 | Converted.data(), |
| 4877 | Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4878 | |
| 4879 | if (Context.hasSameType(CanonType, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4880 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 4881 | // C++ [temp.class.spec]p9b3: |
| 4882 | // |
| 4883 | // -- The argument list of the specialization shall not be identical |
| 4884 | // to the implicit argument list of the primary template. |
| 4885 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 4886 | << (TUK == TUK_Definition) |
| 4887 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 4888 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 4889 | ClassTemplate->getIdentifier(), |
| 4890 | TemplateNameLoc, |
| 4891 | Attr, |
| 4892 | TemplateParams, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 4893 | AS_none, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4894 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 4895 | (TemplateParameterList**) TemplateParameterLists.release()); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4896 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4897 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4898 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4899 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 4900 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4901 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4902 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4903 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4904 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4905 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4906 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 4907 | TemplateParams, |
| 4908 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4909 | Converted.data(), |
| 4910 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4911 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4912 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4913 | PrevPartial, |
| 4914 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4915 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4916 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4917 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4918 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4919 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4920 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4921 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4922 | if (!PrevPartial) |
| 4923 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4924 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4925 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4926 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4927 | // template specialization, make a note of that. |
| 4928 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 4929 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4930 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4931 | // Check that all of the template parameters of the class template |
| 4932 | // partial specialization are deducible from the template |
| 4933 | // arguments. If not, this class template partial specialization |
| 4934 | // will never be used. |
| 4935 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 4936 | DeducibleParams.resize(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4937 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4938 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4939 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4940 | unsigned NumNonDeducible = 0; |
| 4941 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 4942 | if (!DeducibleParams[I]) |
| 4943 | ++NumNonDeducible; |
| 4944 | |
| 4945 | if (NumNonDeducible) { |
| 4946 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 4947 | << (NumNonDeducible > 1) |
| 4948 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 4949 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 4950 | if (!DeducibleParams[I]) { |
| 4951 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 4952 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4953 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4954 | diag::note_partial_spec_unused_parameter) |
| 4955 | << Param->getDeclName(); |
| 4956 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4957 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4958 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 4959 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4960 | } |
| 4961 | } |
| 4962 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4963 | } else { |
| 4964 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4965 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4966 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4967 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4968 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4969 | KWLoc, TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4970 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4971 | Converted.data(), |
| 4972 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4973 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4974 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4975 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4976 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4977 | TemplateParameterLists.size(), |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4978 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4979 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4980 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4981 | if (!PrevDecl) |
| 4982 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4983 | |
| 4984 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4985 | } |
| 4986 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4987 | // C++ [temp.expl.spec]p6: |
| 4988 | // 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] | 4989 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4990 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4991 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4992 | // use occurs; no diagnostic is required. |
| 4993 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4994 | bool Okay = false; |
| 4995 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4996 | // Is there any previous explicit specialization declaration? |
| 4997 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 4998 | Okay = true; |
| 4999 | break; |
| 5000 | } |
| 5001 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5002 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5003 | if (!Okay) { |
| 5004 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 5005 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 5006 | << Context.getTypeDeclType(Specialization) << Range; |
| 5007 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5008 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5009 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5010 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5011 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5012 | return true; |
| 5013 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5014 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5015 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5016 | // If this is not a friend, note that this is an explicit specialization. |
| 5017 | if (TUK != TUK_Friend) |
| 5018 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5019 | |
| 5020 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5021 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5022 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5023 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5024 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5025 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5026 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 5027 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5028 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5029 | } |
| 5030 | } |
| 5031 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 5032 | if (Attr) |
| 5033 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 5034 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 5035 | // Build the fully-sugared type for this class template |
| 5036 | // specialization as the user wrote in the specialization |
| 5037 | // itself. This means that we'll pretty-print the type retrieved |
| 5038 | // from the specialization's declaration the way that the user |
| 5039 | // actually wrote the specialization, rather than formatting the |
| 5040 | // name based on the "canonical" representation used to store the |
| 5041 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5042 | TypeSourceInfo *WrittenTy |
| 5043 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5044 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5045 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5046 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5047 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5048 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5049 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5050 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5051 | // C++ [temp.expl.spec]p9: |
| 5052 | // A template explicit specialization is in the scope of the |
| 5053 | // namespace in which the template was defined. |
| 5054 | // |
| 5055 | // We actually implement this paragraph where we set the semantic |
| 5056 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 5057 | // but we also maintain the lexical context where the actual |
| 5058 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5059 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5060 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5061 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5062 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5063 | Specialization->startDefinition(); |
| 5064 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5065 | if (TUK == TUK_Friend) { |
| 5066 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 5067 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5068 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5069 | /*FIXME:*/KWLoc); |
| 5070 | Friend->setAccess(AS_public); |
| 5071 | CurContext->addDecl(Friend); |
| 5072 | } else { |
| 5073 | // Add the specialization into its lexical context, so that it can |
| 5074 | // be seen when iterating through the list of declarations in that |
| 5075 | // context. However, specializations are not found by name lookup. |
| 5076 | CurContext->addDecl(Specialization); |
| 5077 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5078 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5079 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5080 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5081 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5082 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5083 | Declarator &D) { |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5084 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 5085 | } |
| 5086 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5087 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5088 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5089 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5090 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5091 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5092 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5093 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5094 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5095 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5096 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5097 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5098 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5099 | Decl *DP = HandleDeclarator(ParentScope, D, |
| 5100 | move(TemplateParameterLists), |
| 5101 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5102 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5103 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5104 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5105 | FunctionTemplate->getTemplatedDecl()); |
| 5106 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 5107 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 5108 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5109 | } |
| 5110 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5111 | /// \brief Strips various properties off an implicit instantiation |
| 5112 | /// that has just been explicitly specialized. |
| 5113 | static void StripImplicitInstantiation(NamedDecl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5114 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5115 | |
| 5116 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5117 | FD->setInlineSpecified(false); |
| 5118 | } |
| 5119 | } |
| 5120 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5121 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5122 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5123 | /// for those cases where they are required and determining whether the |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5124 | /// new specialization/instantiation will have any effect. |
| 5125 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5126 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5127 | /// instantiation. |
| 5128 | /// |
| 5129 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 5130 | /// |
| 5131 | /// \param PrevDecl the previous declaration of the entity. |
| 5132 | /// |
| 5133 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 5134 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5135 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5136 | /// declaration was instantiated (either implicitly or explicitly). |
| 5137 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5138 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5139 | /// specialization or instantiation has no effect and should be ignored. |
| 5140 | /// |
| 5141 | /// \returns true if there was an error that should prevent the introduction of |
| 5142 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5143 | bool |
| 5144 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 5145 | TemplateSpecializationKind NewTSK, |
| 5146 | NamedDecl *PrevDecl, |
| 5147 | TemplateSpecializationKind PrevTSK, |
| 5148 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5149 | bool &HasNoEffect) { |
| 5150 | HasNoEffect = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5151 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5152 | switch (NewTSK) { |
| 5153 | case TSK_Undeclared: |
| 5154 | case TSK_ImplicitInstantiation: |
| 5155 | assert(false && "Don't check implicit instantiations here"); |
| 5156 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5157 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5158 | case TSK_ExplicitSpecialization: |
| 5159 | switch (PrevTSK) { |
| 5160 | case TSK_Undeclared: |
| 5161 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5162 | // Okay, we're just specializing something that is either already |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5163 | // explicitly specialized or has merely been mentioned without any |
| 5164 | // instantiation. |
| 5165 | return false; |
| 5166 | |
| 5167 | case TSK_ImplicitInstantiation: |
| 5168 | if (PrevPointOfInstantiation.isInvalid()) { |
| 5169 | // The declaration itself has not actually been instantiated, so it is |
| 5170 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5171 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5172 | return false; |
| 5173 | } |
| 5174 | // Fall through |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5175 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5176 | case TSK_ExplicitInstantiationDeclaration: |
| 5177 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5178 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 5179 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5180 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5181 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5182 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5183 | // 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] | 5184 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5185 | // before the first use of that specialization that would cause an |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5186 | // implicit instantiation to take place, in every translation unit in |
| 5187 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5188 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 5189 | // Is there any previous explicit specialization declaration? |
| 5190 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 5191 | return false; |
| 5192 | } |
| 5193 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5194 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5195 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5196 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5197 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5198 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5199 | return true; |
| 5200 | } |
| 5201 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5202 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5203 | case TSK_ExplicitInstantiationDeclaration: |
| 5204 | switch (PrevTSK) { |
| 5205 | case TSK_ExplicitInstantiationDeclaration: |
| 5206 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5207 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5208 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5209 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5210 | case TSK_Undeclared: |
| 5211 | case TSK_ImplicitInstantiation: |
| 5212 | // We're explicitly instantiating something that may have already been |
| 5213 | // implicitly instantiated; that's fine. |
| 5214 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5215 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5216 | case TSK_ExplicitSpecialization: |
| 5217 | // C++0x [temp.explicit]p4: |
| 5218 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5219 | // of a template appears after a declaration of an explicit |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5220 | // specialization for that template, the explicit instantiation has no |
| 5221 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5222 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5223 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5224 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5225 | case TSK_ExplicitInstantiationDefinition: |
| 5226 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5227 | // If an entity is the subject of both an explicit instantiation |
| 5228 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5229 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5230 | Diag(NewLoc, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5231 | diag::err_explicit_instantiation_declaration_after_definition); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5232 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5233 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5234 | assert(PrevPointOfInstantiation.isValid() && |
| 5235 | "Explicit instantiation without point of instantiation?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5236 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5237 | return false; |
| 5238 | } |
| 5239 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5240 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5241 | case TSK_ExplicitInstantiationDefinition: |
| 5242 | switch (PrevTSK) { |
| 5243 | case TSK_Undeclared: |
| 5244 | case TSK_ImplicitInstantiation: |
| 5245 | // We're explicitly instantiating something that may have already been |
| 5246 | // implicitly instantiated; that's fine. |
| 5247 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5248 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5249 | case TSK_ExplicitSpecialization: |
| 5250 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 5251 | // For a given set of template parameters, if an explicit |
| 5252 | // instantiation of a template appears after a declaration of |
| 5253 | // an explicit specialization for that template, the explicit |
| 5254 | // instantiation has no effect. |
| 5255 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5256 | // 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] | 5257 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5258 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5259 | if (!getLangOptions().CPlusPlus0x) { |
| 5260 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5261 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5262 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5263 | diag::note_previous_template_specialization); |
| 5264 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5265 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5266 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5267 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5268 | case TSK_ExplicitInstantiationDeclaration: |
| 5269 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5270 | // were previously asked to suppress instantiations. That's fine. |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5271 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5272 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5273 | case TSK_ExplicitInstantiationDefinition: |
| 5274 | // C++0x [temp.spec]p5: |
| 5275 | // For a given template and a given set of template-arguments, |
| 5276 | // - an explicit instantiation definition shall appear at most once |
| 5277 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5278 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5279 | << PrevDecl; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5280 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5281 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5282 | HasNoEffect = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5283 | return false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5284 | } |
| 5285 | break; |
| 5286 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5287 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5288 | assert(false && "Missing specialization/instantiation case?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5289 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5290 | return false; |
| 5291 | } |
| 5292 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5293 | /// \brief Perform semantic analysis for the given dependent function |
| 5294 | /// template specialization. The only possible way to get a dependent |
| 5295 | /// function template specialization is with a friend declaration, |
| 5296 | /// like so: |
| 5297 | /// |
| 5298 | /// template <class T> void foo(T); |
| 5299 | /// template <class T> class A { |
| 5300 | /// friend void foo<>(T); |
| 5301 | /// }; |
| 5302 | /// |
| 5303 | /// There really isn't any useful analysis we can do here, so we |
| 5304 | /// just store the information. |
| 5305 | bool |
| 5306 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 5307 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 5308 | LookupResult &Previous) { |
| 5309 | // Remove anything from Previous that isn't a function template in |
| 5310 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5311 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5312 | LookupResult::Filter F = Previous.makeFilter(); |
| 5313 | while (F.hasNext()) { |
| 5314 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 5315 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5316 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 5317 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5318 | F.erase(); |
| 5319 | } |
| 5320 | F.done(); |
| 5321 | |
| 5322 | // Should this be diagnosed here? |
| 5323 | if (Previous.empty()) return true; |
| 5324 | |
| 5325 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 5326 | ExplicitTemplateArgs); |
| 5327 | return false; |
| 5328 | } |
| 5329 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5330 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5331 | /// specialization. |
| 5332 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5333 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5334 | /// explicit function template specialization. On successful completion, |
| 5335 | /// the function declaration \p FD will become a function template |
| 5336 | /// specialization. |
| 5337 | /// |
| 5338 | /// \param FD the function declaration, which will be updated to become a |
| 5339 | /// function template specialization. |
| 5340 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5341 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 5342 | /// if any. Note that this may be valid info even when 0 arguments are |
| 5343 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 5344 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5345 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5346 | /// \param PrevDecl the set of declarations that may be specialized by |
| 5347 | /// this function specialization. |
| 5348 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5349 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5350 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5351 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5352 | // The set of function template specializations that could match this |
| 5353 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5354 | UnresolvedSet<8> Candidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5355 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5356 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5357 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5358 | I != E; ++I) { |
| 5359 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 5360 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5361 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5362 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5363 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 5364 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5365 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5366 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5367 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5368 | // A trailing template-argument can be left unspecified in the |
| 5369 | // template-id naming an explicit function template specialization |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5370 | // provided it can be deduced from the function argument type. |
| 5371 | // Perform template argument deduction to determine whether we may be |
| 5372 | // specializing this template. |
| 5373 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5374 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5375 | FunctionDecl *Specialization = 0; |
| 5376 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5377 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5378 | FD->getType(), |
| 5379 | Specialization, |
| 5380 | Info)) { |
| 5381 | // FIXME: Template argument deduction failed; record why it failed, so |
| 5382 | // that we can provide nifty diagnostics. |
| 5383 | (void)TDK; |
| 5384 | continue; |
| 5385 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5386 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5387 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5388 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5389 | } |
| 5390 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5391 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5392 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5393 | UnresolvedSetIterator Result |
| 5394 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5395 | TPOC_Other, 0, FD->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5396 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5397 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5398 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5399 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5400 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5401 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5402 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5403 | |
| 5404 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 5405 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | abfb405 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 5406 | |
| 5407 | FunctionTemplateSpecializationInfo *SpecInfo |
| 5408 | = Specialization->getTemplateSpecializationInfo(); |
| 5409 | assert(SpecInfo && "Function template specialization info missing?"); |
| 5410 | { |
| 5411 | // Note: do not overwrite location info if previous template |
| 5412 | // specialization kind was explicit. |
| 5413 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
| 5414 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) |
| 5415 | Specialization->setLocation(FD->getLocation()); |
| 5416 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5417 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5418 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5419 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5420 | |
| 5421 | // If this is a friend declaration, then we're not really declaring |
| 5422 | // an explicit specialization. |
| 5423 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5424 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5425 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5426 | if (!isFriend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5427 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5428 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5429 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5430 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5431 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5432 | |
| 5433 | // C++ [temp.expl.spec]p6: |
| 5434 | // 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] | 5435 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5436 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5437 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5438 | // use occurs; no diagnostic is required. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5439 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5440 | if (!isFriend && |
| 5441 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5442 | TSK_ExplicitSpecialization, |
| 5443 | Specialization, |
| 5444 | SpecInfo->getTemplateSpecializationKind(), |
| 5445 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5446 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5447 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5448 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5449 | // Mark the prior declaration as an explicit specialization, so that later |
| 5450 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5451 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5452 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5453 | MarkUnusedFileScopedDecl(Specialization); |
| 5454 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5455 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5456 | // Turn the given function declaration into a function template |
| 5457 | // specialization, with the template arguments from the previous |
| 5458 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5459 | // Take copies of (semantic and syntactic) template argument lists. |
| 5460 | const TemplateArgumentList* TemplArgs = new (Context) |
| 5461 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
| 5462 | const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs |
| 5463 | ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5464 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5465 | TemplArgs, /*InsertPos=*/0, |
| 5466 | SpecInfo->getTemplateSpecializationKind(), |
| 5467 | TemplArgsAsWritten); |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5468 | FD->setStorageClass(Specialization->getStorageClass()); |
| 5469 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5470 | // The "previous declaration" for this function template specialization is |
| 5471 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5472 | Previous.clear(); |
| 5473 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5474 | return false; |
| 5475 | } |
| 5476 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5477 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5478 | /// specialization. |
| 5479 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5480 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5481 | /// explicit member function specialization. On successful completion, |
| 5482 | /// the function declaration \p FD will become a member function |
| 5483 | /// specialization. |
| 5484 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5485 | /// \param Member the member declaration, which will be updated to become a |
| 5486 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5487 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5488 | /// \param Previous the set of declarations, one of which may be specialized |
| 5489 | /// by this function specialization; the set will be modified to contain the |
| 5490 | /// redeclared member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5491 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5492 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5493 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5494 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5495 | // Try to find the member we are instantiating. |
| 5496 | NamedDecl *Instantiation = 0; |
| 5497 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5498 | MemberSpecializationInfo *MSInfo = 0; |
| 5499 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5500 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5501 | // Nowhere to look anyway. |
| 5502 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5503 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5504 | I != E; ++I) { |
| 5505 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5506 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5507 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 5508 | Instantiation = Method; |
| 5509 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5510 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5511 | break; |
| 5512 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5513 | } |
| 5514 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5515 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5516 | VarDecl *PrevVar; |
| 5517 | if (Previous.isSingleResult() && |
| 5518 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5519 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5520 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5521 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5522 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5523 | } |
| 5524 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5525 | CXXRecordDecl *PrevRecord; |
| 5526 | if (Previous.isSingleResult() && |
| 5527 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 5528 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5529 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5530 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5531 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5532 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5533 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5534 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5535 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5536 | // specializations are always out-of-line, the caller will complain about |
| 5537 | // this mismatch later. |
| 5538 | return false; |
| 5539 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5540 | |
| 5541 | // If this is a friend, just bail out here before we start turning |
| 5542 | // things into explicit specializations. |
| 5543 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 5544 | // Preserve instantiation information. |
| 5545 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 5546 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 5547 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5548 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5549 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 5550 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 5551 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5552 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5553 | } |
| 5554 | |
| 5555 | Previous.clear(); |
| 5556 | Previous.addDecl(Instantiation); |
| 5557 | return false; |
| 5558 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5559 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5560 | // Make sure that this is a specialization of a member. |
| 5561 | if (!InstantiatedFrom) { |
| 5562 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 5563 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5564 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 5565 | return true; |
| 5566 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5567 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5568 | // C++ [temp.expl.spec]p6: |
| 5569 | // 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] | 5570 | // explicitly specialized then that spe- cialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5571 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5572 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5573 | // use occurs; no diagnostic is required. |
| 5574 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5575 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5576 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5577 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 5578 | TSK_ExplicitSpecialization, |
| 5579 | Instantiation, |
| 5580 | MSInfo->getTemplateSpecializationKind(), |
| 5581 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5582 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5583 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5584 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5585 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5586 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5587 | InstantiatedFrom, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5588 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5589 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5590 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 5591 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5592 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5593 | // the original declaration to note that it is an explicit specialization |
| 5594 | // (if it was previously an implicit instantiation). This latter step |
| 5595 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5596 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5597 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 5598 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 5599 | TSK_ImplicitInstantiation) { |
| 5600 | InstantiationFunction->setTemplateSpecializationKind( |
| 5601 | TSK_ExplicitSpecialization); |
| 5602 | InstantiationFunction->setLocation(Member->getLocation()); |
| 5603 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5604 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5605 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 5606 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5607 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5608 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5609 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5610 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 5611 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 5612 | TSK_ImplicitInstantiation) { |
| 5613 | InstantiationVar->setTemplateSpecializationKind( |
| 5614 | TSK_ExplicitSpecialization); |
| 5615 | InstantiationVar->setLocation(Member->getLocation()); |
| 5616 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5617 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5618 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 5619 | cast<VarDecl>(InstantiatedFrom), |
| 5620 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5621 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5622 | } else { |
| 5623 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5624 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 5625 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 5626 | TSK_ImplicitInstantiation) { |
| 5627 | InstantiationClass->setTemplateSpecializationKind( |
| 5628 | TSK_ExplicitSpecialization); |
| 5629 | InstantiationClass->setLocation(Member->getLocation()); |
| 5630 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5631 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5632 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5633 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5634 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5635 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5636 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5637 | // Save the caller the trouble of having to figure out which declaration |
| 5638 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5639 | Previous.clear(); |
| 5640 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5641 | return false; |
| 5642 | } |
| 5643 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5644 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5645 | /// |
| 5646 | /// \returns true if a serious error occurs, false otherwise. |
| 5647 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5648 | SourceLocation InstLoc, |
| 5649 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5650 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5651 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5652 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5653 | if (CurContext->isRecord()) { |
| 5654 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5655 | << D; |
| 5656 | return true; |
| 5657 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5658 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5659 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5660 | // An explicit instantiation shall appear in an enclosing namespace of its |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5661 | // template. |
| 5662 | // |
| 5663 | // This is DR275, which we do not retroactively apply to C++98/03. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5664 | if (S.getLangOptions().CPlusPlus0x && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5665 | !CurContext->Encloses(OrigContext)) { |
| 5666 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5667 | S.Diag(InstLoc, |
| 5668 | S.getLangOptions().CPlusPlus0x? |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5669 | diag::err_explicit_instantiation_out_of_scope |
| 5670 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5671 | << D << NS; |
| 5672 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5673 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5674 | S.getLangOptions().CPlusPlus0x? |
| 5675 | diag::err_explicit_instantiation_must_be_global |
| 5676 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5677 | << D; |
| 5678 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5679 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5680 | } |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5681 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5682 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5683 | // If the name declared in the explicit instantiation is an unqualified |
| 5684 | // name, the explicit instantiation shall appear in the namespace where |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5685 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 5686 | // namespace from its enclosing namespace set. |
| 5687 | if (WasQualifiedName) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5688 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5689 | |
| 5690 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5691 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5692 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5693 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5694 | S.getLangOptions().CPlusPlus0x? |
| 5695 | diag::err_explicit_instantiation_unqualified_wrong_namespace |
| 5696 | : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5697 | << D << OrigContext; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5698 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5699 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5700 | } |
| 5701 | |
| 5702 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5703 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5704 | if (!SS.isSet()) |
| 5705 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5706 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5707 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5708 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5709 | // or a static data member of a class template specialization, the name of |
| 5710 | // the class template specialization in the qualified-id for the member |
| 5711 | // name shall be a simple-template-id. |
| 5712 | // |
| 5713 | // C++98 has the same restriction, just worded differently. |
| 5714 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5715 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5716 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5717 | if (isa<TemplateSpecializationType>(T)) |
| 5718 | return true; |
| 5719 | |
| 5720 | return false; |
| 5721 | } |
| 5722 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5723 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5724 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5725 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5726 | SourceLocation ExternLoc, |
| 5727 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5728 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5729 | SourceLocation KWLoc, |
| 5730 | const CXXScopeSpec &SS, |
| 5731 | TemplateTy TemplateD, |
| 5732 | SourceLocation TemplateNameLoc, |
| 5733 | SourceLocation LAngleLoc, |
| 5734 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5735 | SourceLocation RAngleLoc, |
| 5736 | AttributeList *Attr) { |
| 5737 | // Find the class template we're specializing |
| 5738 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5739 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5740 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5741 | |
| 5742 | // Check that the specialization uses the same tag kind as the |
| 5743 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5744 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5745 | assert(Kind != TTK_Enum && |
| 5746 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5747 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 5748 | Kind, /*isDefinition*/false, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5749 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5750 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5751 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5752 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5753 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5754 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5755 | diag::note_previous_use); |
| 5756 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5757 | } |
| 5758 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5759 | // C++0x [temp.explicit]p2: |
| 5760 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5761 | // definition and an explicit instantiation declaration. An explicit |
| 5762 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5763 | TemplateSpecializationKind TSK |
| 5764 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5765 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5766 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5767 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5768 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5769 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5770 | |
| 5771 | // Check that the template argument list is well-formed for this |
| 5772 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5773 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5774 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5775 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5776 | return true; |
| 5777 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5778 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5779 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5780 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5781 | // Find the class template specialization declaration that |
| 5782 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5783 | void *InsertPos = 0; |
| 5784 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5785 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5786 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5787 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5788 | TemplateSpecializationKind PrevDecl_TSK |
| 5789 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 5790 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5791 | // C++0x [temp.explicit]p2: |
| 5792 | // [...] An explicit instantiation shall appear in an enclosing |
| 5793 | // namespace of its template. [...] |
| 5794 | // |
| 5795 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5796 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 5797 | SS.isSet())) |
| 5798 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5799 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5800 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5801 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5802 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5803 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5804 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5805 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5806 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5807 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5808 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5809 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5810 | // Even though HasNoEffect == true means that this explicit instantiation |
| 5811 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 5812 | |
| 5813 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 5814 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5815 | // Since the only prior class template specialization with these |
| 5816 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5817 | // declaration node as our own, updating the source location |
| 5818 | // for the template name to reflect our new declaration. |
| 5819 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5820 | Specialization = PrevDecl; |
| 5821 | Specialization->setLocation(TemplateNameLoc); |
| 5822 | PrevDecl = 0; |
| 5823 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5824 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5825 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5826 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5827 | // Create a new class template specialization declaration node for |
| 5828 | // this explicit specialization. |
| 5829 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5830 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5831 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5832 | KWLoc, TemplateNameLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5833 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5834 | Converted.data(), |
| 5835 | Converted.size(), |
| 5836 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5837 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5838 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5839 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5840 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5841 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5842 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5843 | } |
| 5844 | |
| 5845 | // Build the fully-sugared type for this explicit instantiation as |
| 5846 | // the user wrote in the explicit instantiation itself. This means |
| 5847 | // that we'll pretty-print the type retrieved from the |
| 5848 | // specialization's declaration the way that the user actually wrote |
| 5849 | // the explicit instantiation, rather than formatting the name based |
| 5850 | // on the "canonical" representation used to store the template |
| 5851 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5852 | TypeSourceInfo *WrittenTy |
| 5853 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5854 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5855 | Context.getTypeDeclType(Specialization)); |
| 5856 | Specialization->setTypeAsWritten(WrittenTy); |
| 5857 | TemplateArgsIn.release(); |
| 5858 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5859 | // Set source locations for keywords. |
| 5860 | Specialization->setExternLoc(ExternLoc); |
| 5861 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 5862 | |
| 5863 | // Add the explicit instantiation into its lexical context. However, |
| 5864 | // since explicit instantiations are never found by name lookup, we |
| 5865 | // just put it into the declaration context directly. |
| 5866 | Specialization->setLexicalDeclContext(CurContext); |
| 5867 | CurContext->addDecl(Specialization); |
| 5868 | |
| 5869 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 5870 | if (HasNoEffect) { |
| 5871 | // Set the template specialization kind. |
| 5872 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5873 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5874 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5875 | |
| 5876 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5877 | // A definition of a class template or class member template |
| 5878 | // shall be in scope at the point of the explicit instantiation of |
| 5879 | // the class template or class member template. |
| 5880 | // |
| 5881 | // This check comes when we actually try to perform the |
| 5882 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5883 | ClassTemplateSpecializationDecl *Def |
| 5884 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5885 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5886 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5887 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5888 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5889 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5890 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 5891 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5892 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5893 | // Instantiate the members of this class template specialization. |
| 5894 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5895 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5896 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 5897 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 5898 | |
| 5899 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 5900 | // TSK_ExplicitInstantiationDefinition |
| 5901 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 5902 | TSK == TSK_ExplicitInstantiationDefinition) |
| 5903 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5904 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5905 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5906 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5907 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5908 | // Set the template specialization kind. |
| 5909 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5910 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5911 | } |
| 5912 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5913 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5914 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5915 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5916 | SourceLocation ExternLoc, |
| 5917 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5918 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5919 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5920 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5921 | IdentifierInfo *Name, |
| 5922 | SourceLocation NameLoc, |
| 5923 | AttributeList *Attr) { |
| 5924 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 5925 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5926 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5927 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5928 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
| 5929 | MultiTemplateParamsArg(*this, 0, 0), |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 5930 | Owned, IsDependent, false, false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5931 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5932 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 5933 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5934 | if (!TagD) |
| 5935 | return true; |
| 5936 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5937 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5938 | if (Tag->isEnum()) { |
| 5939 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 5940 | << Context.getTypeDeclType(Tag); |
| 5941 | return true; |
| 5942 | } |
| 5943 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 5944 | if (Tag->isInvalidDecl()) |
| 5945 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5946 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5947 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 5948 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 5949 | if (!Pattern) { |
| 5950 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 5951 | << Context.getTypeDeclType(Record); |
| 5952 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 5953 | return true; |
| 5954 | } |
| 5955 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5956 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5957 | // If the explicit instantiation is for a class or member class, the |
| 5958 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5959 | // simple-template-id. |
| 5960 | // |
| 5961 | // C++98 has the same restriction, just worded differently. |
| 5962 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5963 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5964 | << Record << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5965 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5966 | // C++0x [temp.explicit]p2: |
| 5967 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5968 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5969 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 5970 | TemplateSpecializationKind TSK |
| 5971 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5972 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5973 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5974 | // C++0x [temp.explicit]p2: |
| 5975 | // [...] An explicit instantiation shall appear in an enclosing |
| 5976 | // namespace of its template. [...] |
| 5977 | // |
| 5978 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5979 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5980 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5981 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5982 | CXXRecordDecl *PrevDecl |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5983 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5984 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5985 | PrevDecl = Record; |
| 5986 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5987 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5988 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5989 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5990 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5991 | PrevDecl, |
| 5992 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5993 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5994 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5995 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5996 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5997 | return TagD; |
| 5998 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5999 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6000 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6001 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6002 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6003 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6004 | // 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] | 6005 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6006 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6007 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6008 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 6009 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 6010 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6011 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 6012 | << Pattern; |
| 6013 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6014 | } else { |
| 6015 | if (InstantiateClass(NameLoc, Record, Def, |
| 6016 | getTemplateInstantiationArgs(Record), |
| 6017 | TSK)) |
| 6018 | return true; |
| 6019 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6020 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6021 | if (!RecordDef) |
| 6022 | return true; |
| 6023 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6024 | } |
| 6025 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6026 | // Instantiate all of the members of the class. |
| 6027 | InstantiateClassMembers(NameLoc, RecordDef, |
| 6028 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6029 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6030 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 6031 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 6032 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6033 | // FIXME: We don't have any representation for explicit instantiations of |
| 6034 | // member classes. Such a representation is not needed for compilation, but it |
| 6035 | // should be available for clients that want to see all of the declarations in |
| 6036 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6037 | return TagD; |
| 6038 | } |
| 6039 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6040 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 6041 | SourceLocation ExternLoc, |
| 6042 | SourceLocation TemplateLoc, |
| 6043 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6044 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6045 | // TODO: check if/when DNInfo should replace Name. |
| 6046 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6047 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6048 | if (!Name) { |
| 6049 | if (!D.isInvalidType()) |
| 6050 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 6051 | diag::err_explicit_instantiation_requires_name) |
| 6052 | << D.getDeclSpec().getSourceRange() |
| 6053 | << D.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6054 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6055 | return true; |
| 6056 | } |
| 6057 | |
| 6058 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 6059 | // we find one that is. |
| 6060 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 6061 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 6062 | S = S->getParent(); |
| 6063 | |
| 6064 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6065 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 6066 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6067 | if (R.isNull()) |
| 6068 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6069 | |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6070 | // C++ [dcl.stc]p1: |
| 6071 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 6072 | // instantiation (14.7.2) directive. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6073 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6074 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 6075 | << Name; |
| 6076 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6077 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 6078 | != DeclSpec::SCS_unspecified) { |
| 6079 | // Complain about then remove the storage class specifier. |
| 6080 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 6081 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6082 | |
| 6083 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6084 | } |
| 6085 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6086 | // C++0x [temp.explicit]p1: |
| 6087 | // [...] An explicit instantiation of a function template shall not use the |
| 6088 | // inline or constexpr specifiers. |
| 6089 | // Presumably, this also applies to member functions of class templates as |
| 6090 | // well. |
| 6091 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6092 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6093 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6094 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6095 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6096 | // FIXME: check for constexpr specifier. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6097 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6098 | // C++0x [temp.explicit]p2: |
| 6099 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6100 | // definition and an explicit instantiation declaration. An explicit |
| 6101 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6102 | TemplateSpecializationKind TSK |
| 6103 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6104 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6105 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6106 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6107 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6108 | |
| 6109 | if (!R->isFunctionType()) { |
| 6110 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6111 | // A [...] static data member of a class template can be explicitly |
| 6112 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6113 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6114 | if (Previous.isAmbiguous()) |
| 6115 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6116 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6117 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6118 | if (!Prev || !Prev->isStaticDataMember()) { |
| 6119 | // We expect to see a data data member here. |
| 6120 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 6121 | << Name; |
| 6122 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6123 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6124 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6125 | return true; |
| 6126 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6127 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6128 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 6129 | // FIXME: Check for explicit specialization? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6130 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6131 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 6132 | << Prev; |
| 6133 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 6134 | // FIXME: Can we provide a note showing where this was declared? |
| 6135 | return true; |
| 6136 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6137 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6138 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6139 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6140 | // or a static data member of a class template specialization, the name of |
| 6141 | // the class template specialization in the qualified-id for the member |
| 6142 | // name shall be a simple-template-id. |
| 6143 | // |
| 6144 | // C++98 has the same restriction, just worded differently. |
| 6145 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6146 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6147 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6148 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6149 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6150 | // Check the scope of this explicit instantiation. |
| 6151 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6152 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6153 | // Verify that it is okay to explicitly instantiate here. |
| 6154 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 6155 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6156 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6157 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6158 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6159 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6160 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6161 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6162 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6163 | return (Decl*) 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6164 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6165 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6166 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6167 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6168 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6169 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6170 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6171 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6172 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6173 | |
| 6174 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6175 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6176 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6177 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6178 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 6179 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6180 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 6181 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6182 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 6183 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6184 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6185 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6186 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 6187 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6188 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6189 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6190 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6191 | // A [...] function [...] can be explicitly instantiated from its template. |
| 6192 | // A member function [...] of a class template can be explicitly |
| 6193 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6194 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6195 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6196 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6197 | P != PEnd; ++P) { |
| 6198 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6199 | if (!HasExplicitTemplateArgs) { |
| 6200 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 6201 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 6202 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6203 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6204 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6205 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 6206 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6207 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6208 | } |
| 6209 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6210 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6211 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 6212 | if (!FunTmpl) |
| 6213 | continue; |
| 6214 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6215 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6216 | FunctionDecl *Specialization = 0; |
| 6217 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6218 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6219 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6220 | R, Specialization, Info)) { |
| 6221 | // FIXME: Keep track of almost-matches? |
| 6222 | (void)TDK; |
| 6223 | continue; |
| 6224 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6225 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6226 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6227 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6228 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6229 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6230 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6231 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6232 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6233 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 6234 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 6235 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6236 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6237 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6238 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6239 | |
| 6240 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 6241 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6242 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6243 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6244 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6245 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 6246 | << Specialization |
| 6247 | << (Specialization->getTemplateSpecializationKind() == |
| 6248 | TSK_ExplicitSpecialization); |
| 6249 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 6250 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6251 | } |
| 6252 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6253 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6254 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 6255 | PrevDecl = Specialization; |
| 6256 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6257 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6258 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6259 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6260 | PrevDecl, |
| 6261 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6262 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6263 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6264 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6265 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6266 | // FIXME: We may still want to build some representation of this |
| 6267 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6268 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6269 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6270 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 6271 | |
| 6272 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6273 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6274 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6275 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6276 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6277 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6278 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6279 | // or a static data member of a class template specialization, the name of |
| 6280 | // the class template specialization in the qualified-id for the member |
| 6281 | // name shall be a simple-template-id. |
| 6282 | // |
| 6283 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6284 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6285 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6286 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6287 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6288 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6289 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6290 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6291 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6292 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6293 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6294 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6295 | D.getIdentifierLoc(), |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6296 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6297 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6298 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6299 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6300 | } |
| 6301 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6302 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6303 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 6304 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 6305 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 6306 | // This has to hold, because SS is expected to be defined. |
| 6307 | assert(Name && "Expected a name in a dependent tag"); |
| 6308 | |
| 6309 | NestedNameSpecifier *NNS |
| 6310 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6311 | if (!NNS) |
| 6312 | return true; |
| 6313 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6314 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 6315 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6316 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 6317 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6318 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6319 | return true; |
| 6320 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6321 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6322 | // Create the resulting type. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6323 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6324 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 6325 | |
| 6326 | // Create type-source location information for this type. |
| 6327 | TypeLocBuilder TLB; |
| 6328 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
| 6329 | TL.setKeywordLoc(TagLoc); |
| 6330 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6331 | TL.setNameLoc(NameLoc); |
| 6332 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6333 | } |
| 6334 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6335 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6336 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6337 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6338 | SourceLocation IdLoc) { |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6339 | if (SS.isInvalid()) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6340 | return true; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6341 | |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6342 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 6343 | !getLangOptions().CPlusPlus0x) |
| 6344 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6345 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6346 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6347 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6348 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 6349 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 6350 | if (T.isNull()) |
| 6351 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6352 | |
| 6353 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6354 | if (isa<DependentNameType>(T)) { |
| 6355 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6356 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6357 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6358 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6359 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6360 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6361 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6362 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6363 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6364 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6365 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6366 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6367 | } |
| 6368 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6369 | TypeResult |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6370 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6371 | const CXXScopeSpec &SS, |
| 6372 | SourceLocation TemplateLoc, |
| 6373 | TemplateTy TemplateIn, |
| 6374 | SourceLocation TemplateNameLoc, |
| 6375 | SourceLocation LAngleLoc, |
| 6376 | ASTTemplateArgsPtr TemplateArgsIn, |
| 6377 | SourceLocation RAngleLoc) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6378 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 6379 | !getLangOptions().CPlusPlus0x) |
| 6380 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6381 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6382 | |
| 6383 | // Translate the parser's template argument list in our AST format. |
| 6384 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 6385 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 6386 | |
| 6387 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6388 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 6389 | // Construct a dependent template specialization type. |
| 6390 | assert(DTN && "dependent template has non-dependent name?"); |
| 6391 | assert(DTN->getQualifier() |
| 6392 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 6393 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 6394 | DTN->getQualifier(), |
| 6395 | DTN->getIdentifier(), |
| 6396 | TemplateArgs); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6397 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6398 | // Create source-location information for this type. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6399 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6400 | DependentTemplateSpecializationTypeLoc SpecTL |
| 6401 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6402 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6403 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6404 | SpecTL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 6405 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6406 | SpecTL.setNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6407 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6408 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6409 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 6410 | } |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6411 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6412 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 6413 | if (T.isNull()) |
| 6414 | return true; |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6415 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6416 | // Provide source-location information for the template specialization |
| 6417 | // type. |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6418 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6419 | TemplateSpecializationTypeLoc SpecTL |
| 6420 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
| 6421 | |
| 6422 | // FIXME: No place to set the location of the 'template' keyword! |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6423 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6424 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6425 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6426 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6427 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 6428 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6429 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 6430 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 6431 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6432 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6433 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6434 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 6435 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 6436 | } |
| 6437 | |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6438 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6439 | /// \brief Build the type that describes a C++ typename specifier, |
| 6440 | /// e.g., "typename T::type". |
| 6441 | QualType |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6442 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 6443 | SourceLocation KeywordLoc, |
| 6444 | NestedNameSpecifierLoc QualifierLoc, |
| 6445 | const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6446 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6447 | CXXScopeSpec SS; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6448 | SS.Adopt(QualifierLoc); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6449 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6450 | DeclContext *Ctx = computeDeclContext(SS); |
| 6451 | if (!Ctx) { |
| 6452 | // If the nested-name-specifier is dependent and couldn't be |
| 6453 | // resolved to a type, build a typename type. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6454 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 6455 | return Context.getDependentNameType(Keyword, |
| 6456 | QualifierLoc.getNestedNameSpecifier(), |
| 6457 | &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 6458 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6459 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6460 | // If the nested-name-specifier refers to the current instantiation, |
| 6461 | // the "typename" keyword itself is superfluous. In C++03, the |
| 6462 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 6463 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 6464 | // 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] | 6465 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6466 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 6467 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6468 | |
| 6469 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6470 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6471 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6472 | unsigned DiagID = 0; |
| 6473 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6474 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6475 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6476 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6477 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6478 | |
| 6479 | case LookupResult::FoundUnresolvedValue: { |
| 6480 | // We found a using declaration that is a value. Most likely, the using |
| 6481 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6482 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6483 | IILoc); |
| 6484 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 6485 | << Name << Ctx << FullRange; |
| 6486 | if (UnresolvedUsingValueDecl *Using |
| 6487 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6488 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6489 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 6490 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 6491 | } |
| 6492 | } |
| 6493 | // Fall through to create a dependent typename type, from which we can recover |
| 6494 | // better. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6495 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 6496 | case LookupResult::NotFoundInCurrentInstantiation: |
| 6497 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6498 | return Context.getDependentNameType(Keyword, |
| 6499 | QualifierLoc.getNestedNameSpecifier(), |
| 6500 | &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6501 | |
| 6502 | case LookupResult::Found: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6503 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6504 | // We found a type. Build an ElaboratedType, since the |
| 6505 | // typename-specifier was just sugar. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6506 | return Context.getElaboratedType(ETK_Typename, |
| 6507 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6508 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6509 | } |
| 6510 | |
| 6511 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6512 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6513 | break; |
| 6514 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6515 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6516 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6517 | return QualType(); |
| 6518 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6519 | case LookupResult::FoundOverloaded: |
| 6520 | DiagID = diag::err_typename_nested_not_type; |
| 6521 | Referenced = *Result.begin(); |
| 6522 | break; |
| 6523 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 6524 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6525 | return QualType(); |
| 6526 | } |
| 6527 | |
| 6528 | // If we get here, it's because name lookup did not find a |
| 6529 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6530 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6531 | IILoc); |
| 6532 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6533 | if (Referenced) |
| 6534 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 6535 | << Name; |
| 6536 | return QualType(); |
| 6537 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6538 | |
| 6539 | namespace { |
| 6540 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 6541 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6542 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6543 | SourceLocation Loc; |
| 6544 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6545 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6546 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 6547 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6548 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6549 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6550 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6551 | DeclarationName Entity) |
| 6552 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6553 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6554 | |
| 6555 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6556 | /// transformed. |
| 6557 | /// |
| 6558 | /// For the purposes of type reconstruction, a type has already been |
| 6559 | /// transformed if it is NULL or if it is not dependent. |
| 6560 | bool AlreadyTransformed(QualType T) { |
| 6561 | return T.isNull() || !T->isDependentType(); |
| 6562 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6563 | |
| 6564 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6565 | /// rebuilt. |
| 6566 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6567 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6568 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 6569 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6570 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6571 | /// \brief Sets the "base" location and entity when that |
| 6572 | /// information is known based on another transformation. |
| 6573 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 6574 | this->Loc = Loc; |
| 6575 | this->Entity = Entity; |
| 6576 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6577 | }; |
| 6578 | } |
| 6579 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6580 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 6581 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6582 | /// 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] | 6583 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6584 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6585 | /// partial specialization thereof). This routine will rebuild that type now |
| 6586 | /// that we have entered the declarator's scope, which may produce different |
| 6587 | /// canonical types, e.g., |
| 6588 | /// |
| 6589 | /// \code |
| 6590 | /// template<typename T> |
| 6591 | /// struct X { |
| 6592 | /// typedef T* pointer; |
| 6593 | /// pointer data(); |
| 6594 | /// }; |
| 6595 | /// |
| 6596 | /// template<typename T> |
| 6597 | /// typename X<T>::pointer X<T>::data() { ... } |
| 6598 | /// \endcode |
| 6599 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 6600 | /// 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] | 6601 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 6602 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6603 | /// 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] | 6604 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 6605 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6606 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 6607 | SourceLocation Loc, |
| 6608 | DeclarationName Name) { |
| 6609 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6610 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6611 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6612 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 6613 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 6614 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6615 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6616 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6617 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 6618 | DeclarationName()); |
| 6619 | return Rebuilder.TransformExpr(E); |
| 6620 | } |
| 6621 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6622 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6623 | if (SS.isInvalid()) |
| 6624 | return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6625 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6626 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6627 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 6628 | DeclarationName()); |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6629 | NestedNameSpecifierLoc Rebuilt |
| 6630 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 6631 | if (!Rebuilt) |
| 6632 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6633 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6634 | SS.Adopt(Rebuilt); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6635 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6636 | } |
| 6637 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6638 | /// \brief Produces a formatted string that describes the binding of |
| 6639 | /// template parameters to template arguments. |
| 6640 | std::string |
| 6641 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6642 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6643 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6644 | } |
| 6645 | |
| 6646 | std::string |
| 6647 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6648 | const TemplateArgument *Args, |
| 6649 | unsigned NumArgs) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6650 | llvm::SmallString<128> Str; |
| 6651 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6652 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6653 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6654 | return std::string(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6655 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6656 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6657 | if (I >= NumArgs) |
| 6658 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6659 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6660 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6661 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6662 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6663 | Out << ", "; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6664 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6665 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6666 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6667 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6668 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6669 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6670 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6671 | Out << " = "; |
| 6672 | Args[I].print(Context.PrintingPolicy, Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6673 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6674 | |
| 6675 | Out << ']'; |
| 6676 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6677 | } |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 6678 | |
| 6679 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) { |
| 6680 | if (!FD) |
| 6681 | return; |
| 6682 | FD->setLateTemplateParsed(Flag); |
| 6683 | } |
| 6684 | |
| 6685 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 6686 | DeclContext *DC = CurContext; |
| 6687 | |
| 6688 | while (DC) { |
| 6689 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 6690 | const FunctionDecl *FD = RD->isLocalClass(); |
| 6691 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 6692 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 6693 | return false; |
| 6694 | |
| 6695 | DC = DC->getParent(); |
| 6696 | } |
| 6697 | return false; |
| 6698 | } |