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() || |
| 605 | // -- pointer to member. |
| 606 | T->isMemberPointerType() || |
| 607 | // If T is a dependent type, we can't do the check now, so we |
| 608 | // assume that it is well-formed. |
| 609 | T->isDependentType()) |
| 610 | return T; |
| 611 | // C++ [temp.param]p8: |
| 612 | // |
| 613 | // A non-type template-parameter of type "array of T" or |
| 614 | // "function returning T" is adjusted to be of type "pointer to |
| 615 | // T" or "pointer to function returning T", respectively. |
| 616 | else if (T->isArrayType()) |
| 617 | // FIXME: Keep the type prior to promotion? |
| 618 | return Context.getArrayDecayedType(T); |
| 619 | else if (T->isFunctionType()) |
| 620 | // FIXME: Keep the type prior to promotion? |
| 621 | return Context.getPointerType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 622 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 623 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 624 | << T; |
| 625 | |
| 626 | return QualType(); |
| 627 | } |
| 628 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 629 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 630 | unsigned Depth, |
| 631 | unsigned Position, |
| 632 | SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 633 | Expr *Default) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 634 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 635 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 636 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 637 | assert(S->isTemplateParamScope() && |
| 638 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 639 | bool Invalid = false; |
| 640 | |
| 641 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 642 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 643 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 644 | LookupOrdinaryName, |
| 645 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 646 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 647 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 648 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 651 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 652 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 653 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 654 | Invalid = true; |
| 655 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 656 | |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 657 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 658 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 659 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 660 | D.getSourceRange().getBegin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 661 | D.getIdentifierLoc(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 662 | Depth, Position, ParamName, T, |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 663 | IsParameterPack, TInfo); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 664 | Param->setAccess(AS_public); |
| 665 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 666 | if (Invalid) |
| 667 | Param->setInvalidDecl(); |
| 668 | |
| 669 | if (D.getIdentifier()) { |
| 670 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 671 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 672 | IdResolver.AddDecl(Param); |
| 673 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 674 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 675 | // C++0x [temp.param]p9: |
| 676 | // A default template-argument may be specified for any kind of |
| 677 | // template-parameter that is not a template parameter pack. |
| 678 | if (Default && IsParameterPack) { |
| 679 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 680 | Default = 0; |
| 681 | } |
| 682 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 683 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 684 | if (Default) { |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 685 | // Check for unexpanded parameter packs. |
| 686 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 687 | return Param; |
| 688 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 689 | TemplateArgument Converted; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 690 | ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
| 691 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 692 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 693 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 694 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 695 | Default = DefaultRes.take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 696 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 697 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 698 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 699 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 700 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 701 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 702 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 703 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 704 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 705 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 706 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 707 | SourceLocation TmpLoc, |
| 708 | TemplateParamsTy *Params, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 709 | SourceLocation EllipsisLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 710 | IdentifierInfo *Name, |
| 711 | SourceLocation NameLoc, |
| 712 | unsigned Depth, |
| 713 | unsigned Position, |
| 714 | SourceLocation EqualLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 715 | ParsedTemplateArgument Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 716 | assert(S->isTemplateParamScope() && |
| 717 | "Template template parameter not in template parameter scope!"); |
| 718 | |
| 719 | // Construct the parameter object. |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 720 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 721 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 722 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 723 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 724 | Depth, Position, IsParameterPack, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 725 | Name, Params); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 726 | Param->setAccess(AS_public); |
| 727 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 728 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 729 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 730 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 731 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 732 | IdResolver.AddDecl(Param); |
| 733 | } |
| 734 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 735 | if (Params->size() == 0) { |
| 736 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 737 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 738 | Param->setInvalidDecl(); |
| 739 | } |
| 740 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 741 | // C++0x [temp.param]p9: |
| 742 | // A default template-argument may be specified for any kind of |
| 743 | // template-parameter that is not a template parameter pack. |
| 744 | if (IsParameterPack && !Default.isInvalid()) { |
| 745 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 746 | Default = ParsedTemplateArgument(); |
| 747 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 748 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 749 | if (!Default.isInvalid()) { |
| 750 | // Check only that we have a template template argument. We don't want to |
| 751 | // try to check well-formedness now, because our template template parameter |
| 752 | // might have dependent types in its template parameters, which we wouldn't |
| 753 | // be able to match now. |
| 754 | // |
| 755 | // If none of the template template parameter's template arguments mention |
| 756 | // other template parameters, we could actually perform more checking here. |
| 757 | // However, it isn't worth doing. |
| 758 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 759 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 760 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 761 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 762 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 763 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 764 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 765 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 766 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 767 | DefaultArg.getArgument().getAsTemplate(), |
| 768 | UPPC_DefaultArgument)) |
| 769 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 770 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 771 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 772 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 773 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 774 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 777 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 778 | /// contains the template parameters in Params/NumParams. |
| 779 | Sema::TemplateParamsTy * |
| 780 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 781 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 782 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 783 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 784 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 785 | SourceLocation RAngleLoc) { |
| 786 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 787 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 788 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 789 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 790 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 791 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 792 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 793 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 794 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 795 | if (SS.isSet()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 796 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 797 | } |
| 798 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 799 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 800 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 801 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 802 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 803 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 804 | TemplateParameterList *TemplateParams, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 805 | AccessSpecifier AS, |
| 806 | unsigned NumOuterTemplateParamLists, |
| 807 | TemplateParameterList** OuterTemplateParamLists) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 808 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 809 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 810 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 811 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 812 | |
| 813 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 814 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 815 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 816 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 817 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 818 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 819 | |
| 820 | // There is no such thing as an unnamed class template. |
| 821 | if (!Name) { |
| 822 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 823 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 827 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 828 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 829 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 830 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 831 | SemanticContext = computeDeclContext(SS, true); |
| 832 | if (!SemanticContext) { |
| 833 | // FIXME: Produce a reasonable diagnostic here |
| 834 | return true; |
| 835 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 836 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 837 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 838 | return true; |
| 839 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 840 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 841 | } else { |
| 842 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 843 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 844 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 846 | if (Previous.isAmbiguous()) |
| 847 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 848 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 849 | NamedDecl *PrevDecl = 0; |
| 850 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 851 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 852 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 853 | // If there is a previous declaration with the same name, check |
| 854 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 855 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 856 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 857 | |
| 858 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 859 | // class template partial specialization, or class template specialization. |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 860 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 861 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 862 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 863 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 864 | PrevClassTemplate |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 865 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 866 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 867 | PrevClassTemplate |
| 868 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 869 | ->getSpecializedTemplate(); |
| 870 | } |
| 871 | } |
| 872 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 873 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 874 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 875 | // [...] When looking for a prior declaration of a class or a function |
| 876 | // 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] | 877 | // function is neither a qualified name nor a template-id, scopes outside |
| 878 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 879 | if (!SS.isSet()) { |
| 880 | DeclContext *OutermostContext = CurContext; |
| 881 | while (!OutermostContext->isFileContext()) |
| 882 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 883 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 884 | if (PrevDecl && |
| 885 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 886 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 887 | SemanticContext = PrevDecl->getDeclContext(); |
| 888 | } else { |
| 889 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 890 | // context we computed is the semantic context for our new |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 891 | // declaration. |
| 892 | PrevDecl = PrevClassTemplate = 0; |
| 893 | SemanticContext = OutermostContext; |
| 894 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 895 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 896 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 897 | if (CurContext->isDependentContext()) { |
| 898 | // If this is a dependent context, we don't want to link the friend |
| 899 | // class template to the template in scope, because that would perform |
| 900 | // checking of the template parameter lists that can't be performed |
| 901 | // until the outer context is instantiated. |
| 902 | PrevDecl = PrevClassTemplate = 0; |
| 903 | } |
| 904 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 905 | PrevDecl = PrevClassTemplate = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 906 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 907 | if (PrevClassTemplate) { |
| 908 | // Ensure that the template parameter lists are compatible. |
| 909 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 910 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 911 | /*Complain=*/true, |
| 912 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 913 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 914 | |
| 915 | // C++ [temp.class]p4: |
| 916 | // In a redeclaration, partial specialization, explicit |
| 917 | // specialization or explicit instantiation of a class template, |
| 918 | // the class-key shall agree in kind with the original class |
| 919 | // template declaration (7.1.5.3). |
| 920 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 921 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 922 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 923 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 924 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 925 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 926 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 929 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 930 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 931 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 932 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 933 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 934 | // FIXME: Would it make sense to try to "forget" the previous |
| 935 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 936 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 940 | // Maybe we will complain about the shadowed template parameter. |
| 941 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 942 | // Just pretend that we didn't see the previous declaration. |
| 943 | PrevDecl = 0; |
| 944 | } else if (PrevDecl) { |
| 945 | // C++ [temp]p5: |
| 946 | // A class template shall not have the same name as any other |
| 947 | // template, class, function, object, enumeration, enumerator, |
| 948 | // namespace, or type in the same scope (3.3), except as specified |
| 949 | // in (14.5.4). |
| 950 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 951 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 952 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 955 | // Check the template parameter list of this declaration, possibly |
| 956 | // merging in the template parameter list from the previous class |
| 957 | // template declaration. |
| 958 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 959 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 960 | (SS.isSet() && SemanticContext && |
Douglas Gregor | 461bf2e | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 961 | SemanticContext->isRecord() && |
| 962 | SemanticContext->isDependentContext()) |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 963 | ? TPC_ClassTemplateMember |
| 964 | : TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 965 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 966 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 967 | if (SS.isSet()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 968 | // 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] | 969 | // template out-of-line. |
| 970 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
| 971 | !(TUK == TUK_Friend && CurContext->isDependentContext())) |
| 972 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 973 | << Name << SemanticContext << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | CXXRecordDecl *NewClass = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 977 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 978 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 979 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 980 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 981 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 982 | if (NumOuterTemplateParamLists > 0) |
| 983 | NewClass->setTemplateParameterListsInfo(Context, |
| 984 | NumOuterTemplateParamLists, |
| 985 | OuterTemplateParamLists); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 986 | |
| 987 | ClassTemplateDecl *NewTemplate |
| 988 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 989 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 990 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 991 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 992 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 993 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 994 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 995 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 996 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 997 | (void)T; |
| 998 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 999 | // 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] | 1000 | // class template, make a note of that. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1001 | if (PrevClassTemplate && |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1002 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1003 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1004 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1005 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1006 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1007 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1008 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1009 | // Set the lexical context of these templates |
| 1010 | NewClass->setLexicalDeclContext(CurContext); |
| 1011 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1012 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1013 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1014 | NewClass->startDefinition(); |
| 1015 | |
| 1016 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1017 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1018 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1019 | if (TUK != TUK_Friend) |
| 1020 | PushOnScopeChains(NewTemplate, S); |
| 1021 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1022 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1023 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1024 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1025 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1026 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1027 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1028 | PrevClassTemplate != NULL); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1029 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1030 | // Friend templates are visible in fairly strange ways. |
| 1031 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1032 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1033 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 1034 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1035 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1036 | /* AddToContext = */ false); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1037 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1038 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1039 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1040 | NewClass->getLocation(), |
| 1041 | NewTemplate, |
| 1042 | /*FIXME:*/NewClass->getLocation()); |
| 1043 | Friend->setAccess(AS_public); |
| 1044 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1045 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1046 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1047 | if (Invalid) { |
| 1048 | NewTemplate->setInvalidDecl(); |
| 1049 | NewClass->setInvalidDecl(); |
| 1050 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1051 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1054 | /// \brief Diagnose the presence of a default template argument on a |
| 1055 | /// template parameter, which is ill-formed in certain contexts. |
| 1056 | /// |
| 1057 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1058 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1059 | Sema::TemplateParamListContext TPC, |
| 1060 | SourceLocation ParamLoc, |
| 1061 | SourceRange DefArgRange) { |
| 1062 | switch (TPC) { |
| 1063 | case Sema::TPC_ClassTemplate: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1064 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1065 | return false; |
| 1066 | |
| 1067 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1068 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1069 | // C++ [temp.param]p9: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1070 | // A default template-argument shall not be specified in a |
| 1071 | // function template declaration or a function template |
| 1072 | // definition [...] |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1073 | // If a friend function template declaration specifies a default |
| 1074 | // template-argument, that declaration shall be a definition and shall be |
| 1075 | // the only declaration of the function template in the translation unit. |
| 1076 | // (C++98/03 doesn't have this wording; see DR226). |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1077 | if (!S.getLangOptions().CPlusPlus0x) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1078 | S.Diag(ParamLoc, |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1079 | diag::ext_template_parameter_default_in_function_template) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1080 | << DefArgRange; |
| 1081 | return false; |
| 1082 | |
| 1083 | case Sema::TPC_ClassTemplateMember: |
| 1084 | // C++0x [temp.param]p9: |
| 1085 | // A default template-argument shall not be specified in the |
| 1086 | // template-parameter-lists of the definition of a member of a |
| 1087 | // class template that appears outside of the member's class. |
| 1088 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1089 | << DefArgRange; |
| 1090 | return true; |
| 1091 | |
| 1092 | case Sema::TPC_FriendFunctionTemplate: |
| 1093 | // C++ [temp.param]p9: |
| 1094 | // A default template-argument shall not be specified in a |
| 1095 | // friend template declaration. |
| 1096 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1097 | << DefArgRange; |
| 1098 | return true; |
| 1099 | |
| 1100 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1101 | // for friend function templates if there is only a single |
| 1102 | // declaration (and it is a definition). Strange! |
| 1103 | } |
| 1104 | |
| 1105 | return false; |
| 1106 | } |
| 1107 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1108 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1109 | /// of a template template parameter, recursively. |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1110 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1111 | TemplateTemplateParmDecl *TTP) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1112 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1113 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1114 | NamedDecl *P = Params->getParam(I); |
| 1115 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1116 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1117 | NTTP->getTypeSourceInfo(), |
| 1118 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1119 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1120 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1121 | continue; |
| 1122 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1123 | |
| 1124 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1125 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1126 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1127 | return true; |
| 1128 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1129 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1130 | return false; |
| 1131 | } |
| 1132 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1133 | /// \brief Checks the validity of a template parameter list, possibly |
| 1134 | /// considering the template parameter list from a previous |
| 1135 | /// declaration. |
| 1136 | /// |
| 1137 | /// If an "old" template parameter list is provided, it must be |
| 1138 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1139 | /// template parameter list. |
| 1140 | /// |
| 1141 | /// \param NewParams Template parameter list for a new template |
| 1142 | /// declaration. This template parameter list will be updated with any |
| 1143 | /// default arguments that are carried through from the previous |
| 1144 | /// template parameter list. |
| 1145 | /// |
| 1146 | /// \param OldParams If provided, template parameter list from a |
| 1147 | /// previous declaration of the same template. Default template |
| 1148 | /// arguments will be merged from the old template parameter list to |
| 1149 | /// the new template parameter list. |
| 1150 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1151 | /// \param TPC Describes the context in which we are checking the given |
| 1152 | /// template parameter list. |
| 1153 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1154 | /// \returns true if an error occurred, false otherwise. |
| 1155 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1156 | TemplateParameterList *OldParams, |
| 1157 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1158 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1160 | // C++ [temp.param]p10: |
| 1161 | // The set of default template-arguments available for use with a |
| 1162 | // template declaration or definition is obtained by merging the |
| 1163 | // default arguments from the definition (if in scope) and all |
| 1164 | // declarations in scope in the same way default function |
| 1165 | // arguments are (8.3.6). |
| 1166 | bool SawDefaultArgument = false; |
| 1167 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1168 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1169 | bool SawParameterPack = false; |
| 1170 | SourceLocation ParameterPackLoc; |
| 1171 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1172 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1173 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1174 | if (OldParams) |
| 1175 | OldParam = OldParams->begin(); |
| 1176 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1177 | bool RemoveDefaultArguments = false; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1178 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1179 | NewParamEnd = NewParams->end(); |
| 1180 | NewParam != NewParamEnd; ++NewParam) { |
| 1181 | // Variables used to diagnose redundant default arguments |
| 1182 | bool RedundantDefaultArg = false; |
| 1183 | SourceLocation OldDefaultLoc; |
| 1184 | SourceLocation NewDefaultLoc; |
| 1185 | |
| 1186 | // Variables used to diagnose missing default arguments |
| 1187 | bool MissingDefaultArg = false; |
| 1188 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1189 | // C++0x [temp.param]p11: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1190 | // If a template parameter of a primary class template or alias template |
| 1191 | // is a template parameter pack, it shall be the last template parameter. |
| 1192 | if (SawParameterPack && |
| 1193 | (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1194 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1195 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1196 | Invalid = true; |
| 1197 | } |
| 1198 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1199 | if (TemplateTypeParmDecl *NewTypeParm |
| 1200 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1201 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1202 | if (NewTypeParm->hasDefaultArgument() && |
| 1203 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1204 | NewTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1205 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1206 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1207 | NewTypeParm->removeDefaultArgument(); |
| 1208 | |
| 1209 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1210 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1211 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1212 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1213 | if (NewTypeParm->isParameterPack()) { |
| 1214 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1215 | "Parameter packs can't have a default argument!"); |
| 1216 | SawParameterPack = true; |
| 1217 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1219 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1220 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1221 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1222 | SawDefaultArgument = true; |
| 1223 | RedundantDefaultArg = true; |
| 1224 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1225 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1226 | // Merge the default argument from the old declaration to the |
| 1227 | // new declaration. |
| 1228 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1229 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1230 | true); |
| 1231 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1232 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1233 | SawDefaultArgument = true; |
| 1234 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1235 | } else if (SawDefaultArgument) |
| 1236 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1237 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1238 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1239 | // Check for unexpanded parameter packs. |
| 1240 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1241 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1242 | UPPC_NonTypeTemplateParameterType)) { |
| 1243 | Invalid = true; |
| 1244 | continue; |
| 1245 | } |
| 1246 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1247 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1248 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1249 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1250 | NewNonTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1251 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1252 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1255 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1256 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1257 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1258 | if (NewNonTypeParm->isParameterPack()) { |
| 1259 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1260 | "Parameter packs can't have a default argument!"); |
| 1261 | SawParameterPack = true; |
| 1262 | ParameterPackLoc = NewNonTypeParm->getLocation(); |
| 1263 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1264 | NewNonTypeParm->hasDefaultArgument()) { |
| 1265 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1266 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1267 | SawDefaultArgument = true; |
| 1268 | RedundantDefaultArg = true; |
| 1269 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1270 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1271 | // Merge the default argument from the old declaration to the |
| 1272 | // new declaration. |
| 1273 | SawDefaultArgument = true; |
| 1274 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1275 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1276 | // parameter. |
| 1277 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1278 | OldNonTypeParm->getDefaultArgument(), |
| 1279 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1280 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1281 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1282 | SawDefaultArgument = true; |
| 1283 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1284 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1285 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1286 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1287 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1288 | TemplateTemplateParmDecl *NewTemplateParm |
| 1289 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1290 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1291 | // Check for unexpanded parameter packs, recursively. |
| 1292 | if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
| 1293 | Invalid = true; |
| 1294 | continue; |
| 1295 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1296 | |
| 1297 | if (NewTemplateParm->hasDefaultArgument() && |
| 1298 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1299 | NewTemplateParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1300 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1301 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1302 | |
| 1303 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1304 | TemplateTemplateParmDecl *OldTemplateParm |
| 1305 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1306 | if (NewTemplateParm->isParameterPack()) { |
| 1307 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1308 | "Parameter packs can't have a default argument!"); |
| 1309 | SawParameterPack = true; |
| 1310 | ParameterPackLoc = NewTemplateParm->getLocation(); |
| 1311 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1312 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1313 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1314 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1315 | SawDefaultArgument = true; |
| 1316 | RedundantDefaultArg = true; |
| 1317 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1318 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1319 | // Merge the default argument from the old declaration to the |
| 1320 | // new declaration. |
| 1321 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1322 | // FIXME: We need to create a new kind of "default argument" expression |
| 1323 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1324 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1325 | OldTemplateParm->getDefaultArgument(), |
| 1326 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1327 | PreviousDefaultArgLoc |
| 1328 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1329 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1330 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1331 | PreviousDefaultArgLoc |
| 1332 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1333 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1334 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | if (RedundantDefaultArg) { |
| 1338 | // C++ [temp.param]p12: |
| 1339 | // A template-parameter shall not be given default arguments |
| 1340 | // by two different declarations in the same scope. |
| 1341 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1342 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1343 | Invalid = true; |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1344 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1345 | // C++ [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1346 | // If a template-parameter of a class template has a default |
| 1347 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1348 | // have a default template-argument supplied or be a template parameter |
| 1349 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1351 | diag::err_template_param_default_arg_missing); |
| 1352 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1353 | Invalid = true; |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1354 | RemoveDefaultArguments = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | // If we have an old template parameter list that we're merging |
| 1358 | // in, move on to the next parameter. |
| 1359 | if (OldParams) |
| 1360 | ++OldParam; |
| 1361 | } |
| 1362 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1363 | // We were missing some default arguments at the end of the list, so remove |
| 1364 | // all of the default arguments. |
| 1365 | if (RemoveDefaultArguments) { |
| 1366 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1367 | NewParamEnd = NewParams->end(); |
| 1368 | NewParam != NewParamEnd; ++NewParam) { |
| 1369 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1370 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1371 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1372 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1373 | NTTP->removeDefaultArgument(); |
| 1374 | else |
| 1375 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1376 | } |
| 1377 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1378 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1379 | return Invalid; |
| 1380 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1381 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1382 | namespace { |
| 1383 | |
| 1384 | /// A class which looks for a use of a certain level of template |
| 1385 | /// parameter. |
| 1386 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1387 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1388 | |
| 1389 | unsigned Depth; |
| 1390 | bool Match; |
| 1391 | |
| 1392 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1393 | NamedDecl *ND = Params->getParam(0); |
| 1394 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1395 | Depth = PD->getDepth(); |
| 1396 | } else if (NonTypeTemplateParmDecl *PD = |
| 1397 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1398 | Depth = PD->getDepth(); |
| 1399 | } else { |
| 1400 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | bool Matches(unsigned ParmDepth) { |
| 1405 | if (ParmDepth >= Depth) { |
| 1406 | Match = true; |
| 1407 | return true; |
| 1408 | } |
| 1409 | return false; |
| 1410 | } |
| 1411 | |
| 1412 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1413 | return !Matches(T->getDepth()); |
| 1414 | } |
| 1415 | |
| 1416 | bool TraverseTemplateName(TemplateName N) { |
| 1417 | if (TemplateTemplateParmDecl *PD = |
| 1418 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1419 | if (Matches(PD->getDepth())) return false; |
| 1420 | return super::TraverseTemplateName(N); |
| 1421 | } |
| 1422 | |
| 1423 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1424 | if (NonTypeTemplateParmDecl *PD = |
| 1425 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1426 | if (PD->getDepth() == Depth) { |
| 1427 | Match = true; |
| 1428 | return false; |
| 1429 | } |
| 1430 | } |
| 1431 | return super::VisitDeclRefExpr(E); |
| 1432 | } |
Douglas Gregor | 18c8339 | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1433 | |
| 1434 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1435 | return TraverseType(T->getInjectedSpecializationType()); |
| 1436 | } |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1437 | }; |
| 1438 | } |
| 1439 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1440 | /// Determines whether a given type depends on the given parameter |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1441 | /// list. |
| 1442 | static bool |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1443 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1444 | DependencyChecker Checker(Params); |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1445 | Checker.TraverseType(T); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1446 | return Checker.Match; |
| 1447 | } |
| 1448 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1449 | // Find the source range corresponding to the named type in the given |
| 1450 | // nested-name-specifier, if any. |
| 1451 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1452 | QualType T, |
| 1453 | const CXXScopeSpec &SS) { |
| 1454 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1455 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1456 | if (const Type *CurType = NNS->getAsType()) { |
| 1457 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1458 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1459 | } else |
| 1460 | break; |
| 1461 | |
| 1462 | NNSLoc = NNSLoc.getPrefix(); |
| 1463 | } |
| 1464 | |
| 1465 | return SourceRange(); |
| 1466 | } |
| 1467 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1469 | /// specifier, returning the template parameter list that applies to the |
| 1470 | /// name. |
| 1471 | /// |
| 1472 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1473 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1474 | /// |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1475 | /// \param DeclLoc The location of the declaration itself. |
| 1476 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1477 | /// \param SS the scope specifier that will be matched to the given template |
| 1478 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1479 | /// being declared. |
| 1480 | /// |
| 1481 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1482 | /// innermost template parameter lists. |
| 1483 | /// |
| 1484 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1485 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1486 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1487 | /// matching template parameters to scope specifiers in friend |
| 1488 | /// declarations. |
| 1489 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1490 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1491 | /// declared is an explicit specialization, false otherwise. |
| 1492 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1493 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1494 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1495 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1496 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1497 | /// 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] | 1498 | /// itself a template). |
| 1499 | TemplateParameterList * |
| 1500 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1501 | SourceLocation DeclLoc, |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1502 | const CXXScopeSpec &SS, |
| 1503 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1504 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1505 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1506 | bool &IsExplicitSpecialization, |
| 1507 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1508 | IsExplicitSpecialization = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1509 | Invalid = false; |
| 1510 | |
| 1511 | // The sequence of nested types to which we will match up the template |
| 1512 | // parameter lists. We first build this list by starting with the type named |
| 1513 | // by the nested-name-specifier and walking out until we run out of types. |
| 1514 | llvm::SmallVector<QualType, 4> NestedTypes; |
| 1515 | QualType T; |
| 1516 | if (SS.getScopeRep()) |
| 1517 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1518 | |
| 1519 | // If we found an explicit specialization that prevents us from needing |
| 1520 | // 'template<>' headers, this will be set to the location of that |
| 1521 | // explicit specialization. |
| 1522 | SourceLocation ExplicitSpecLoc; |
| 1523 | |
| 1524 | while (!T.isNull()) { |
| 1525 | NestedTypes.push_back(T); |
| 1526 | |
| 1527 | // Retrieve the parent of a record type. |
| 1528 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1529 | // If this type is an explicit specialization, we're done. |
| 1530 | if (ClassTemplateSpecializationDecl *Spec |
| 1531 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1532 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1533 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1534 | ExplicitSpecLoc = Spec->getLocation(); |
| 1535 | break; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1536 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1537 | } else if (Record->getTemplateSpecializationKind() |
| 1538 | == TSK_ExplicitSpecialization) { |
| 1539 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1540 | break; |
| 1541 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1542 | |
| 1543 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1544 | T = Context.getTypeDeclType(Parent); |
| 1545 | else |
| 1546 | T = QualType(); |
| 1547 | continue; |
| 1548 | } |
| 1549 | |
| 1550 | if (const TemplateSpecializationType *TST |
| 1551 | = T->getAs<TemplateSpecializationType>()) { |
| 1552 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1553 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1554 | T = Context.getTypeDeclType(Parent); |
| 1555 | else |
| 1556 | T = QualType(); |
| 1557 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1558 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | // Look one step prior in a dependent template specialization type. |
| 1562 | if (const DependentTemplateSpecializationType *DependentTST |
| 1563 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1564 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1565 | T = QualType(NNS->getAsType(), 0); |
| 1566 | else |
| 1567 | T = QualType(); |
| 1568 | continue; |
| 1569 | } |
| 1570 | |
| 1571 | // Look one step prior in a dependent name type. |
| 1572 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1573 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1574 | T = QualType(NNS->getAsType(), 0); |
| 1575 | else |
| 1576 | T = QualType(); |
| 1577 | continue; |
| 1578 | } |
| 1579 | |
| 1580 | // Retrieve the parent of an enumeration type. |
| 1581 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1582 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1583 | // check here. |
| 1584 | EnumDecl *Enum = EnumT->getDecl(); |
| 1585 | |
| 1586 | // Get to the parent type. |
| 1587 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1588 | T = Context.getTypeDeclType(Parent); |
| 1589 | else |
| 1590 | T = QualType(); |
| 1591 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1592 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1593 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1594 | T = QualType(); |
| 1595 | } |
| 1596 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1597 | // to the innermost while checking template-parameter-lists. |
| 1598 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1599 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1600 | // C++0x [temp.expl.spec]p17: |
| 1601 | // A member or a member template may be nested within many |
| 1602 | // enclosing class templates. In an explicit specialization for |
| 1603 | // such a member, the member declaration shall be preceded by a |
| 1604 | // template<> for each enclosing class template that is |
| 1605 | // explicitly specialized. |
| 1606 | unsigned ParamIdx = 0; |
| 1607 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1608 | ++TypeIdx) { |
| 1609 | T = NestedTypes[TypeIdx]; |
| 1610 | |
| 1611 | // Whether we expect a 'template<>' header. |
| 1612 | bool NeedEmptyTemplateHeader = false; |
| 1613 | |
| 1614 | // Whether we expect a template header with parameters. |
| 1615 | bool NeedNonemptyTemplateHeader = false; |
| 1616 | |
| 1617 | // For a dependent type, the set of template parameters that we |
| 1618 | // expect to see. |
| 1619 | TemplateParameterList *ExpectedTemplateParams = 0; |
| 1620 | |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1621 | // C++0x [temp.expl.spec]p15: |
| 1622 | // A member or a member template may be nested within many enclosing |
| 1623 | // class templates. In an explicit specialization for such a member, the |
| 1624 | // member declaration shall be preceded by a template<> for each |
| 1625 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1626 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1627 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1628 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1629 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1630 | NeedNonemptyTemplateHeader = true; |
| 1631 | } else if (Record->isDependentType()) { |
| 1632 | if (Record->getDescribedClassTemplate()) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1633 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1634 | ->getTemplateParameters(); |
| 1635 | NeedNonemptyTemplateHeader = true; |
| 1636 | } |
| 1637 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1638 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1639 | // C++0x [temp.expl.spec]p4: |
| 1640 | // Members of an explicitly specialized class template are defined |
| 1641 | // in the same manner as members of normal classes, and not using |
| 1642 | // the template<> syntax. |
| 1643 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1644 | NeedEmptyTemplateHeader = true; |
| 1645 | else |
| 1646 | break; |
| 1647 | } else if (Record->getTemplateSpecializationKind()) { |
| 1648 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1649 | != TSK_ExplicitSpecialization && |
| 1650 | TypeIdx == NumTypes - 1) |
| 1651 | IsExplicitSpecialization = true; |
| 1652 | |
| 1653 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1654 | } |
| 1655 | } else if (const TemplateSpecializationType *TST |
| 1656 | = T->getAs<TemplateSpecializationType>()) { |
| 1657 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1658 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1659 | NeedNonemptyTemplateHeader = true; |
| 1660 | } |
| 1661 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1662 | // FIXME: We actually could/should check the template arguments here |
| 1663 | // against the corresponding template parameter list. |
| 1664 | NeedNonemptyTemplateHeader = false; |
| 1665 | } |
| 1666 | |
| 1667 | if (NeedEmptyTemplateHeader) { |
| 1668 | // If we're on the last of the types, and we need a 'template<>' header |
| 1669 | // here, then it's an explicit specialization. |
| 1670 | if (TypeIdx == NumTypes - 1) |
| 1671 | IsExplicitSpecialization = true; |
| 1672 | |
| 1673 | if (ParamIdx < NumParamLists) { |
| 1674 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1675 | // The header has template parameters when it shouldn't. Complain. |
| 1676 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1677 | diag::err_template_param_list_matches_nontemplate) |
| 1678 | << T |
| 1679 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1680 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1681 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1682 | Invalid = true; |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
| 1686 | // Consume this template header. |
| 1687 | ++ParamIdx; |
| 1688 | continue; |
| 1689 | } |
| 1690 | |
| 1691 | if (!IsFriend) { |
| 1692 | // We don't have a template header, but we should. |
| 1693 | SourceLocation ExpectedTemplateLoc; |
| 1694 | if (NumParamLists > 0) |
| 1695 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1696 | else |
| 1697 | ExpectedTemplateLoc = DeclStartLoc; |
| 1698 | |
| 1699 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1700 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS) |
| 1701 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1702 | } |
| 1703 | |
| 1704 | continue; |
| 1705 | } |
| 1706 | |
| 1707 | if (NeedNonemptyTemplateHeader) { |
| 1708 | // In friend declarations we can have template-ids which don't |
| 1709 | // depend on the corresponding template parameter lists. But |
| 1710 | // assume that empty parameter lists are supposed to match this |
| 1711 | // template-id. |
| 1712 | if (IsFriend && T->isDependentType()) { |
| 1713 | if (ParamIdx < NumParamLists && |
| 1714 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
| 1715 | ExpectedTemplateParams = 0; |
| 1716 | else |
| 1717 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1719 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1720 | if (ParamIdx < NumParamLists) { |
| 1721 | // Check the template parameter list, if we can. |
| 1722 | if (ExpectedTemplateParams && |
| 1723 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1724 | ExpectedTemplateParams, |
| 1725 | true, TPL_TemplateMatch)) |
| 1726 | Invalid = true; |
| 1727 | |
| 1728 | if (!Invalid && |
| 1729 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1730 | TPC_ClassTemplateMember)) |
| 1731 | Invalid = true; |
| 1732 | |
| 1733 | ++ParamIdx; |
| 1734 | continue; |
| 1735 | } |
| 1736 | |
| 1737 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1738 | << T |
| 1739 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1740 | Invalid = true; |
| 1741 | continue; |
| 1742 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1743 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1744 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1745 | // If there were at least as many template-ids as there were template |
| 1746 | // parameter lists, then there are no template parameter lists remaining for |
| 1747 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1748 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1749 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1751 | // If there were too many template parameter lists, complain about that now. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1752 | if (ParamIdx < NumParamLists - 1) { |
| 1753 | bool HasAnyExplicitSpecHeader = false; |
| 1754 | bool AllExplicitSpecHeaders = true; |
| 1755 | for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) { |
| 1756 | if (ParamLists[I]->size() == 0) |
| 1757 | HasAnyExplicitSpecHeader = true; |
| 1758 | else |
| 1759 | AllExplicitSpecHeaders = false; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1760 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1761 | |
| 1762 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1763 | AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers |
| 1764 | : diag::err_template_spec_extra_headers) |
| 1765 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1766 | ParamLists[NumParamLists - 2]->getRAngleLoc()); |
| 1767 | |
| 1768 | // If there was a specialization somewhere, such that 'template<>' is |
| 1769 | // not required, and there were any 'template<>' headers, note where the |
| 1770 | // specialization occurred. |
| 1771 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1772 | Diag(ExplicitSpecLoc, |
| 1773 | diag::note_explicit_template_spec_does_not_need_header) |
| 1774 | << NestedTypes.back(); |
| 1775 | |
| 1776 | // We have a template parameter list with no corresponding scope, which |
| 1777 | // means that the resulting template declaration can't be instantiated |
| 1778 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1779 | if (!AllExplicitSpecHeaders) |
| 1780 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1781 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1782 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1783 | // Return the last template parameter list, which corresponds to the |
| 1784 | // entity being declared. |
| 1785 | return ParamLists[NumParamLists - 1]; |
| 1786 | } |
| 1787 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1788 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1789 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1790 | Diag(Template->getLocation(), diag::note_template_declared_here) |
| 1791 | << (isa<FunctionTemplateDecl>(Template)? 0 |
| 1792 | : isa<ClassTemplateDecl>(Template)? 1 |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1793 | : isa<TypeAliasTemplateDecl>(Template)? 2 |
| 1794 | : 3) |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1795 | << Template->getDeclName(); |
| 1796 | return; |
| 1797 | } |
| 1798 | |
| 1799 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1800 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1801 | IEnd = OST->end(); |
| 1802 | I != IEnd; ++I) |
| 1803 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1804 | << 0 << (*I)->getDeclName(); |
| 1805 | |
| 1806 | return; |
| 1807 | } |
| 1808 | } |
| 1809 | |
| 1810 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1811 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1812 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1813 | TemplateArgumentListInfo &TemplateArgs) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1814 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 1815 | if (DTN && DTN->isIdentifier()) |
| 1816 | // When building a template-id where the template-name is dependent, |
| 1817 | // assume the template is a type template. Either our assumption is |
| 1818 | // correct, or the code is ill-formed and will be diagnosed when the |
| 1819 | // dependent name is substituted. |
| 1820 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 1821 | DTN->getQualifier(), |
| 1822 | DTN->getIdentifier(), |
| 1823 | TemplateArgs); |
| 1824 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1825 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1826 | if (!Template || isa<FunctionTemplateDecl>(Template)) { |
| 1827 | // We might have a substituted template template parameter pack. If so, |
| 1828 | // build a template specialization type for it. |
| 1829 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 1830 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1831 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1832 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 1833 | << Name; |
| 1834 | NoteAllFoundTemplates(Name); |
| 1835 | return QualType(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1836 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1837 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1838 | // Check that the template argument list is well-formed for this |
| 1839 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1840 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1841 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1842 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1843 | return QualType(); |
| 1844 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1845 | assert((Converted.size() == Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1846 | "Converted template argument list is too short!"); |
| 1847 | |
| 1848 | QualType CanonType; |
| 1849 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1850 | if (TypeAliasTemplateDecl *AliasTemplate |
| 1851 | = dyn_cast<TypeAliasTemplateDecl>(Template)) { |
| 1852 | // Find the canonical type for this type alias template specialization. |
| 1853 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 1854 | if (Pattern->isInvalidDecl()) |
| 1855 | return QualType(); |
| 1856 | |
| 1857 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1858 | Converted.data(), Converted.size()); |
| 1859 | |
| 1860 | // Only substitute for the innermost template argument list. |
| 1861 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 1804174 | 2011-05-14 15:04:18 +0000 | [diff] [blame^] | 1862 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | aff37b4 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 1863 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 1864 | for (unsigned I = 0; I < Depth; ++I) |
| 1865 | TemplateArgLists.addOuterTemplateArguments(0, 0); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1866 | |
| 1867 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
| 1868 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 1869 | TemplateArgLists, AliasTemplate->getLocation(), |
| 1870 | AliasTemplate->getDeclName()); |
| 1871 | if (CanonType.isNull()) |
| 1872 | return QualType(); |
| 1873 | } else if (Name.isDependent() || |
| 1874 | TemplateSpecializationType::anyDependentTemplateArguments( |
| 1875 | TemplateArgs)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1876 | // This class template specialization is a dependent |
| 1877 | // type. Therefore, its canonical type is another class template |
| 1878 | // specialization type that contains all of the converted |
| 1879 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1880 | // A<T, T> have identical types when A is declared as: |
| 1881 | // |
| 1882 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1883 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1884 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1885 | Converted.data(), |
| 1886 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1887 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1888 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1889 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1890 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1891 | // build the canonical type and return that to us. |
| 1892 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1893 | |
| 1894 | // This might work out to be a current instantiation, in which |
| 1895 | // case the canonical type needs to be the InjectedClassNameType. |
| 1896 | // |
| 1897 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1898 | // changes to CurContext don't change the set of current |
| 1899 | // instantiations. |
| 1900 | if (isa<ClassTemplateDecl>(Template)) { |
| 1901 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1902 | // If we get out to a namespace, we're done. |
| 1903 | if (Ctx->isFileContext()) break; |
| 1904 | |
| 1905 | // If this isn't a record, keep looking. |
| 1906 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1907 | if (!Record) continue; |
| 1908 | |
| 1909 | // Look for one of the two cases with InjectedClassNameTypes |
| 1910 | // and check whether it's the same template. |
| 1911 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1912 | !Record->getDescribedClassTemplate()) |
| 1913 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1914 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1915 | // Fetch the injected class name type and check whether its |
| 1916 | // injected type is equal to the type we just built. |
| 1917 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1918 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 1919 | ->getInjectedSpecializationType(); |
| 1920 | |
| 1921 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 1922 | continue; |
| 1923 | |
| 1924 | // If so, the canonical type of this TST is the injected |
| 1925 | // class name type of the record we just found. |
| 1926 | assert(ICNT.isCanonical()); |
| 1927 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1928 | break; |
| 1929 | } |
| 1930 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1931 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1932 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1933 | // Find the class template specialization declaration that |
| 1934 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1935 | void *InsertPos = 0; |
| 1936 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1937 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1938 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1939 | if (!Decl) { |
| 1940 | // This is the first time we have referenced this class template |
| 1941 | // specialization. Create the canonical declaration and add it to |
| 1942 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1944 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 1945 | ClassTemplate->getDeclContext(), |
| 1946 | ClassTemplate->getLocation(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1947 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1948 | ClassTemplate, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1949 | Converted.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1950 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 1951 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1952 | Decl->setLexicalDeclContext(CurContext); |
| 1953 | } |
| 1954 | |
| 1955 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1956 | assert(isa<RecordType>(CanonType) && |
| 1957 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1958 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1959 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1960 | // Build the fully-sugared type for this class template |
| 1961 | // specialization, which refers back to the class template |
| 1962 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1963 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1966 | TypeResult |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1967 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, |
| 1968 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1969 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1970 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1971 | SourceLocation RAngleLoc) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1972 | if (SS.isInvalid()) |
| 1973 | return true; |
| 1974 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1975 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1976 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1977 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1978 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1979 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1980 | |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 1981 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 1982 | QualType T = Context.getDependentTemplateSpecializationType(ETK_None, |
| 1983 | DTN->getQualifier(), |
| 1984 | DTN->getIdentifier(), |
| 1985 | TemplateArgs); |
| 1986 | |
| 1987 | // Build type-source information. |
| 1988 | TypeLocBuilder TLB; |
| 1989 | DependentTemplateSpecializationTypeLoc SpecTL |
| 1990 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1991 | SpecTL.setKeywordLoc(SourceLocation()); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 1992 | SpecTL.setNameLoc(TemplateLoc); |
| 1993 | SpecTL.setLAngleLoc(LAngleLoc); |
| 1994 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 1995 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 1996 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 1997 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 1998 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 1999 | } |
| 2000 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2001 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2002 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2003 | |
| 2004 | if (Result.isNull()) |
| 2005 | return true; |
| 2006 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2007 | // Build type-source information. |
| 2008 | TypeLocBuilder TLB; |
| 2009 | TemplateSpecializationTypeLoc SpecTL |
| 2010 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 2011 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2012 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2013 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2014 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2015 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2016 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2017 | if (SS.isNotEmpty()) { |
| 2018 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2019 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2020 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 2021 | ElabTL.setKeywordLoc(SourceLocation()); |
| 2022 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2023 | } |
| 2024 | |
| 2025 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2026 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2027 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2028 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2029 | TypeSpecifierType TagSpec, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2030 | SourceLocation TagLoc, |
| 2031 | CXXScopeSpec &SS, |
| 2032 | TemplateTy TemplateD, |
| 2033 | SourceLocation TemplateLoc, |
| 2034 | SourceLocation LAngleLoc, |
| 2035 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2036 | SourceLocation RAngleLoc) { |
| 2037 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
| 2038 | |
| 2039 | // Translate the parser's template argument list in our AST format. |
| 2040 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2041 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2042 | |
| 2043 | // Determine the tag kind |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2044 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2045 | ElaboratedTypeKeyword Keyword |
| 2046 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2047 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2048 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2049 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2050 | DTN->getQualifier(), |
| 2051 | DTN->getIdentifier(), |
| 2052 | TemplateArgs); |
| 2053 | |
| 2054 | // Build type-source information. |
| 2055 | TypeLocBuilder TLB; |
| 2056 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2057 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2058 | SpecTL.setKeywordLoc(TagLoc); |
| 2059 | SpecTL.setNameLoc(TemplateLoc); |
| 2060 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2061 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2062 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2063 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2064 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2065 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2066 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2067 | |
| 2068 | if (TypeAliasTemplateDecl *TAT = |
| 2069 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2070 | // C++0x [dcl.type.elab]p2: |
| 2071 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2072 | // resolves to an alias template specialization, the |
| 2073 | // elaborated-type-specifier is ill-formed. |
| 2074 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2075 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2076 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2077 | |
| 2078 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2079 | if (Result.isNull()) |
| 2080 | return TypeResult(); |
| 2081 | |
| 2082 | // Check the tag kind |
| 2083 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2084 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2085 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2086 | IdentifierInfo *Id = D->getIdentifier(); |
| 2087 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2088 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2089 | if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { |
| 2090 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2091 | << Result |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2092 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2093 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2094 | } |
| 2095 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2096 | |
| 2097 | // Provide source-location information for the template specialization. |
| 2098 | TypeLocBuilder TLB; |
| 2099 | TemplateSpecializationTypeLoc SpecTL |
| 2100 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 2101 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2102 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2103 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2104 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2105 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2106 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2107 | // Construct an elaborated type containing the nested-name-specifier (if any) |
| 2108 | // and keyword. |
| 2109 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2110 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 2111 | ElabTL.setKeywordLoc(TagLoc); |
| 2112 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2113 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2116 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2117 | LookupResult &R, |
| 2118 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2119 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2120 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2121 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2122 | // 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] | 2123 | // though. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2124 | // foo<int> could identify a single function unambiguously |
| 2125 | // This approach does NOT work, since f<int>(1); |
| 2126 | // gets resolved prior to resorting to overload resolution |
| 2127 | // i.e., template<class T> void f(double); |
| 2128 | // vs template<class T, class U> void f(U); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2129 | |
| 2130 | // These should be filtered out by our callers. |
| 2131 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2132 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2133 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2134 | // We don't want lookup warnings at this point. |
| 2135 | R.suppressDiagnostics(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2136 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2137 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2138 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2139 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2140 | R.getLookupNameInfo(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2141 | RequiresADL, TemplateArgs, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2142 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2143 | |
| 2144 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2147 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2148 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2149 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2150 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2151 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2152 | DeclContext *DC; |
| 2153 | if (!(DC = computeDeclContext(SS, false)) || |
| 2154 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2155 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2156 | return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2157 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2158 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2159 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2160 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 2161 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2162 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2163 | if (R.isAmbiguous()) |
| 2164 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2165 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2166 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2167 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2168 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2169 | return ExprError(); |
| 2170 | } |
| 2171 | |
| 2172 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2173 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 2174 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 2175 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2176 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2177 | return ExprError(); |
| 2178 | } |
| 2179 | |
| 2180 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2183 | /// \brief Form a dependent template name. |
| 2184 | /// |
| 2185 | /// This action forms a dependent template name given the template |
| 2186 | /// name and its (presumably dependent) scope specifier. For |
| 2187 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2188 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2189 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2190 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2191 | SourceLocation TemplateKWLoc, |
| 2192 | CXXScopeSpec &SS, |
| 2193 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2194 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2195 | bool EnteringContext, |
| 2196 | TemplateTy &Result) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 2197 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() && |
| 2198 | !getLangOptions().CPlusPlus0x) |
| 2199 | Diag(TemplateKWLoc, diag::ext_template_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2200 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2201 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2202 | DeclContext *LookupCtx = 0; |
| 2203 | if (SS.isSet()) |
| 2204 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2205 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2206 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2207 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2208 | // C++0x [temp.names]p5: |
| 2209 | // If a name prefixed by the keyword template is not the name of |
| 2210 | // a template, the program is ill-formed. [Note: the keyword |
| 2211 | // template may not be applied to non-template members of class |
| 2212 | // templates. -end note ] [ Note: as is the case with the |
| 2213 | // typename prefix, the template prefix is allowed in cases |
| 2214 | // where it is not strictly necessary; i.e., when the |
| 2215 | // nested-name-specifier or the expression on the left of the -> |
| 2216 | // or . is not dependent on a template-parameter, or the use |
| 2217 | // does not appear in the scope of a template. -end note] |
| 2218 | // |
| 2219 | // Note: C++03 was more strict here, because it banned the use of |
| 2220 | // the "template" keyword prior to a template-name that was not a |
| 2221 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2222 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2223 | // 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] | 2224 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2225 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 2226 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2227 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2228 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2229 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | d078bd2 | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2230 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2231 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2232 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2233 | } else if (TNK == TNK_Non_template) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2234 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2235 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2236 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2237 | << Name.getSourceRange() |
| 2238 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2239 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2240 | } else { |
| 2241 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2242 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2243 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2246 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2247 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2248 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2249 | switch (Name.getKind()) { |
| 2250 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2251 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2252 | Name.Identifier)); |
| 2253 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2254 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2255 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2256 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2257 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2258 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2259 | |
| 2260 | case UnqualifiedId::IK_LiteralOperatorId: |
| 2261 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 2262 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2263 | default: |
| 2264 | break; |
| 2265 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2266 | |
| 2267 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2268 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2269 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2270 | << Name.getSourceRange() |
| 2271 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2272 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2275 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2276 | const TemplateArgumentLoc &AL, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2277 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2278 | const TemplateArgument &Arg = AL.getArgument(); |
| 2279 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2280 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2281 | switch(Arg.getKind()) { |
| 2282 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2283 | // C++ [temp.arg.type]p1: |
| 2284 | // A template-argument for a template-parameter which is a |
| 2285 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2286 | break; |
| 2287 | case TemplateArgument::Template: { |
| 2288 | // We have a template type parameter but the template argument |
| 2289 | // is a template without any arguments. |
| 2290 | SourceRange SR = AL.getSourceRange(); |
| 2291 | TemplateName Name = Arg.getAsTemplate(); |
| 2292 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 2293 | << Name << SR; |
| 2294 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 2295 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2296 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2297 | return true; |
| 2298 | } |
| 2299 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2300 | // We have a template type parameter but the template argument |
| 2301 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 2302 | SourceRange SR = AL.getSourceRange(); |
| 2303 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2304 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2305 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2306 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2307 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2308 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2309 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2310 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2311 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2312 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2313 | // Add the converted template type argument. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2314 | Converted.push_back( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2315 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2316 | return false; |
| 2317 | } |
| 2318 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2319 | /// \brief Substitute template arguments into the default template argument for |
| 2320 | /// the given template type parameter. |
| 2321 | /// |
| 2322 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2323 | /// the substitution. |
| 2324 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2325 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2326 | /// for. |
| 2327 | /// |
| 2328 | /// \param TemplateLoc the location of the template name that started the |
| 2329 | /// template-id we are checking. |
| 2330 | /// |
| 2331 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2332 | /// terminates the template-id. |
| 2333 | /// |
| 2334 | /// \param Param the template template parameter whose default we are |
| 2335 | /// substituting into. |
| 2336 | /// |
| 2337 | /// \param Converted the list of template arguments provided for template |
| 2338 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2339 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2340 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2341 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2342 | TemplateDecl *Template, |
| 2343 | SourceLocation TemplateLoc, |
| 2344 | SourceLocation RAngleLoc, |
| 2345 | TemplateTypeParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2346 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2347 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2348 | |
| 2349 | // If the argument type is dependent, instantiate it now based |
| 2350 | // on the previously-computed template arguments. |
| 2351 | if (ArgType->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2352 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2353 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2354 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2355 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2356 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2357 | |
| 2358 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2359 | Template, Converted.data(), |
| 2360 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2361 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2362 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2363 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2364 | Param->getDefaultArgumentLoc(), |
| 2365 | Param->getDeclName()); |
| 2366 | } |
| 2367 | |
| 2368 | return ArgType; |
| 2369 | } |
| 2370 | |
| 2371 | /// \brief Substitute template arguments into the default template argument for |
| 2372 | /// the given non-type template parameter. |
| 2373 | /// |
| 2374 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2375 | /// the substitution. |
| 2376 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2377 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2378 | /// for. |
| 2379 | /// |
| 2380 | /// \param TemplateLoc the location of the template name that started the |
| 2381 | /// template-id we are checking. |
| 2382 | /// |
| 2383 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2384 | /// terminates the template-id. |
| 2385 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2386 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2387 | /// substituting into. |
| 2388 | /// |
| 2389 | /// \param Converted the list of template arguments provided for template |
| 2390 | /// parameters that precede \p Param in the template parameter list. |
| 2391 | /// |
| 2392 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2393 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2394 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2395 | TemplateDecl *Template, |
| 2396 | SourceLocation TemplateLoc, |
| 2397 | SourceLocation RAngleLoc, |
| 2398 | NonTypeTemplateParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2399 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2400 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2401 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2402 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2403 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2404 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2405 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2406 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2407 | Template, Converted.data(), |
| 2408 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2409 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2410 | |
| 2411 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2412 | } |
| 2413 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2414 | /// \brief Substitute template arguments into the default template argument for |
| 2415 | /// the given template template parameter. |
| 2416 | /// |
| 2417 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2418 | /// the substitution. |
| 2419 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2420 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2421 | /// for. |
| 2422 | /// |
| 2423 | /// \param TemplateLoc the location of the template name that started the |
| 2424 | /// template-id we are checking. |
| 2425 | /// |
| 2426 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2427 | /// terminates the template-id. |
| 2428 | /// |
| 2429 | /// \param Param the template template parameter whose default we are |
| 2430 | /// substituting into. |
| 2431 | /// |
| 2432 | /// \param Converted the list of template arguments provided for template |
| 2433 | /// parameters that precede \p Param in the template parameter list. |
| 2434 | /// |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2435 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 2436 | /// source-location information) that precedes the template name. |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2437 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2438 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2439 | static TemplateName |
| 2440 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2441 | TemplateDecl *Template, |
| 2442 | SourceLocation TemplateLoc, |
| 2443 | SourceLocation RAngleLoc, |
| 2444 | TemplateTemplateParmDecl *Param, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2445 | llvm::SmallVectorImpl<TemplateArgument> &Converted, |
| 2446 | NestedNameSpecifierLoc &QualifierLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2447 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2448 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2449 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2450 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2451 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2452 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2453 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2454 | Template, Converted.data(), |
| 2455 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2456 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2457 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2458 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2459 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2460 | if (QualifierLoc) { |
| 2461 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2462 | AllTemplateArgs); |
| 2463 | if (!QualifierLoc) |
| 2464 | return TemplateName(); |
| 2465 | } |
| 2466 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2467 | return SemaRef.SubstTemplateName(QualifierLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2468 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2469 | Param->getDefaultArgument().getTemplateNameLoc(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2470 | AllTemplateArgs); |
| 2471 | } |
| 2472 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2473 | /// \brief If the given template parameter has a default template |
| 2474 | /// argument, substitute into that default template argument and |
| 2475 | /// return the corresponding template argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2476 | TemplateArgumentLoc |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2477 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2478 | SourceLocation TemplateLoc, |
| 2479 | SourceLocation RAngleLoc, |
| 2480 | Decl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2481 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 2482 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2483 | if (!TypeParm->hasDefaultArgument()) |
| 2484 | return TemplateArgumentLoc(); |
| 2485 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2486 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2487 | TemplateLoc, |
| 2488 | RAngleLoc, |
| 2489 | TypeParm, |
| 2490 | Converted); |
| 2491 | if (DI) |
| 2492 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2493 | |
| 2494 | return TemplateArgumentLoc(); |
| 2495 | } |
| 2496 | |
| 2497 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2498 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2499 | if (!NonTypeParm->hasDefaultArgument()) |
| 2500 | return TemplateArgumentLoc(); |
| 2501 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2502 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2503 | TemplateLoc, |
| 2504 | RAngleLoc, |
| 2505 | NonTypeParm, |
| 2506 | Converted); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2507 | if (Arg.isInvalid()) |
| 2508 | return TemplateArgumentLoc(); |
| 2509 | |
| 2510 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2511 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2512 | } |
| 2513 | |
| 2514 | TemplateTemplateParmDecl *TempTempParm |
| 2515 | = cast<TemplateTemplateParmDecl>(Param); |
| 2516 | if (!TempTempParm->hasDefaultArgument()) |
| 2517 | return TemplateArgumentLoc(); |
| 2518 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2519 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2520 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2521 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2522 | TemplateLoc, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2523 | RAngleLoc, |
| 2524 | TempTempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2525 | Converted, |
| 2526 | QualifierLoc); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2527 | if (TName.isNull()) |
| 2528 | return TemplateArgumentLoc(); |
| 2529 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2530 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2531 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2532 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2533 | } |
| 2534 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2535 | /// \brief Check that the given template argument corresponds to the given |
| 2536 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2537 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2538 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2539 | /// checked. |
| 2540 | /// |
| 2541 | /// \param Arg The template argument. |
| 2542 | /// |
| 2543 | /// \param Template The template in which the template argument resides. |
| 2544 | /// |
| 2545 | /// \param TemplateLoc The location of the template name for the template |
| 2546 | /// whose argument list we're matching. |
| 2547 | /// |
| 2548 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2549 | /// the template argument list. |
| 2550 | /// |
| 2551 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2552 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2553 | /// |
| 2554 | /// \param Converted The checked, converted argument will be added to the |
| 2555 | /// end of this small vector. |
| 2556 | /// |
| 2557 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2558 | /// explicitly written, deduced, etc. |
| 2559 | /// |
| 2560 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2561 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2562 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2563 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2564 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2565 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2566 | unsigned ArgumentPackIndex, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2567 | llvm::SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2568 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2569 | // Check template type parameters. |
| 2570 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2571 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2572 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2573 | // Check non-type template parameters. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2574 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2575 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2576 | // with the template arguments we've seen thus far. But if the |
| 2577 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2578 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2579 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2580 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2581 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2582 | if (NTTPType->isDependentType() && |
| 2583 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2584 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2585 | // Do substitution on the type of the non-type template parameter. |
| 2586 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2587 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2588 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2589 | |
| 2590 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2591 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2592 | NTTPType = SubstType(NTTPType, |
| 2593 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2594 | NTTP->getLocation(), |
| 2595 | NTTP->getDeclName()); |
| 2596 | // If that worked, check the non-type template parameter type |
| 2597 | // for validity. |
| 2598 | if (!NTTPType.isNull()) |
| 2599 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2600 | NTTP->getLocation()); |
| 2601 | if (NTTPType.isNull()) |
| 2602 | return true; |
| 2603 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2604 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2605 | switch (Arg.getArgument().getKind()) { |
| 2606 | case TemplateArgument::Null: |
| 2607 | assert(false && "Should never see a NULL template argument here"); |
| 2608 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2609 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2610 | case TemplateArgument::Expression: { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2611 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2612 | ExprResult Res = |
| 2613 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 2614 | Result, CTAK); |
| 2615 | if (Res.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2616 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2617 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2618 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2619 | break; |
| 2620 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2621 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2622 | case TemplateArgument::Declaration: |
| 2623 | case TemplateArgument::Integral: |
| 2624 | // We've already checked this template argument, so just copy |
| 2625 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2626 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2627 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2628 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2629 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2630 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2631 | // We were given a template template argument. It may not be ill-formed; |
| 2632 | // see below. |
| 2633 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2634 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2635 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2636 | // We have a template argument such as \c T::template X, which we |
| 2637 | // parsed as a template template argument. However, since we now |
| 2638 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2639 | // template name into an expression. |
| 2640 | |
| 2641 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2642 | Arg.getTemplateNameLoc()); |
| 2643 | |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2644 | CXXScopeSpec SS; |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2645 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2646 | ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2647 | SS.getWithLocInContext(Context), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2648 | NameInfo)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2649 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2650 | // If we parsed the template argument as a pack expansion, create a |
| 2651 | // pack expansion expression. |
| 2652 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2653 | E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc()); |
| 2654 | if (E.isInvalid()) |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2655 | return true; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2656 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2657 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2658 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2659 | E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result); |
| 2660 | if (E.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2661 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2662 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2663 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2664 | break; |
| 2665 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2666 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2667 | // We have a template argument that actually does refer to a class |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2668 | // template, alias template, or template template parameter, and |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2669 | // therefore cannot be a non-type template argument. |
| 2670 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2671 | << Arg.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2672 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2673 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2674 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2675 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2676 | case TemplateArgument::Type: { |
| 2677 | // We have a non-type template parameter but the template |
| 2678 | // argument is a type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2679 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2680 | // C++ [temp.arg]p2: |
| 2681 | // In a template-argument, an ambiguity between a type-id and |
| 2682 | // an expression is resolved to a type-id, regardless of the |
| 2683 | // form of the corresponding template-parameter. |
| 2684 | // |
| 2685 | // We warn specifically about this case, since it can be rather |
| 2686 | // confusing for users. |
| 2687 | QualType T = Arg.getArgument().getAsType(); |
| 2688 | SourceRange SR = Arg.getSourceRange(); |
| 2689 | if (T->isFunctionType()) |
| 2690 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2691 | else |
| 2692 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2693 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2694 | return true; |
| 2695 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2696 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2697 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2698 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2699 | break; |
| 2700 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2701 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2702 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2706 | // Check template template parameters. |
| 2707 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2708 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2709 | // Substitute into the template parameter list of the template |
| 2710 | // template parameter, since previously-supplied template arguments |
| 2711 | // may appear within the template template parameter. |
| 2712 | { |
| 2713 | // Set up a template instantiation context. |
| 2714 | LocalInstantiationScope Scope(*this); |
| 2715 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2716 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2717 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2718 | |
| 2719 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2720 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2721 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2722 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2723 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2724 | if (!TempParm) |
| 2725 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2726 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2727 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2728 | switch (Arg.getArgument().getKind()) { |
| 2729 | case TemplateArgument::Null: |
| 2730 | assert(false && "Should never see a NULL template argument here"); |
| 2731 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2732 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2733 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2734 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2735 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2736 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2737 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2738 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2739 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2740 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2741 | case TemplateArgument::Expression: |
| 2742 | case TemplateArgument::Type: |
| 2743 | // We have a template template parameter but the template |
| 2744 | // argument does not refer to a template. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2745 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
| 2746 | << getLangOptions().CPlusPlus0x; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2747 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2748 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2749 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2750 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2751 | "Declaration argument with template template parameter"); |
| 2752 | break; |
| 2753 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2754 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2755 | "Integral argument with template template parameter"); |
| 2756 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2757 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2758 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2759 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2760 | break; |
| 2761 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2762 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2763 | return false; |
| 2764 | } |
| 2765 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2766 | /// \brief Check that the given template argument list is well-formed |
| 2767 | /// for specializing the given template. |
| 2768 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2769 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2770 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2771 | bool PartialTemplateArgs, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2772 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2773 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2774 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2775 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2776 | bool Invalid = false; |
| 2777 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2778 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2779 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2780 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2781 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2782 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2783 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2784 | (NumArgs < Params->getMinRequiredArguments() && |
| 2785 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2786 | // FIXME: point at either the first arg beyond what we can handle, |
| 2787 | // or the '>', depending on whether we have too many or too few |
| 2788 | // arguments. |
| 2789 | SourceRange Range; |
| 2790 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2791 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2792 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2793 | << (NumArgs > NumParams) |
| 2794 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2795 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2796 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2797 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2798 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2799 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2800 | Invalid = true; |
| 2801 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2802 | |
| 2803 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2804 | // [...] The type and form of each template-argument specified in |
| 2805 | // a template-id shall match the type and form specified for the |
| 2806 | // corresponding parameter declared by the template in its |
| 2807 | // template-parameter-list. |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2808 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2809 | llvm::SmallVector<TemplateArgument, 2> ArgumentPack; |
| 2810 | TemplateParameterList::iterator Param = Params->begin(), |
| 2811 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2812 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 2813 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2814 | while (Param != ParamEnd) { |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2815 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 2816 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2817 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2818 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2819 | // If we have an expanded parameter pack, make sure we don't have too |
| 2820 | // many arguments. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2821 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2822 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2823 | if (NTTP->isExpandedParameterPack() && |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2824 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 2825 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2826 | << true |
| 2827 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2828 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2829 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2830 | << Template; |
| 2831 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2832 | << Params->getSourceRange(); |
| 2833 | return true; |
| 2834 | } |
| 2835 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2836 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2837 | // Check the template argument we were given. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2838 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2839 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2840 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2841 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2842 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2843 | if ((*Param)->isTemplateParameterPack()) { |
| 2844 | // The template parameter was a template parameter pack, so take the |
| 2845 | // deduced argument and place it on the argument pack. Note that we |
| 2846 | // stay on the same template parameter so that we can deduce more |
| 2847 | // arguments. |
| 2848 | ArgumentPack.push_back(Converted.back()); |
| 2849 | Converted.pop_back(); |
| 2850 | } else { |
| 2851 | // Move to the next template parameter. |
| 2852 | ++Param; |
| 2853 | } |
| 2854 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2855 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2856 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2857 | |
| 2858 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2859 | // arguments, just break out now and we'll fill in the argument pack below. |
| 2860 | if ((*Param)->isTemplateParameterPack()) |
| 2861 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2862 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2863 | // We have a default template argument that we will use. |
| 2864 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2865 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2866 | // Retrieve the default template argument from the template |
| 2867 | // parameter. For each kind of template parameter, we substitute the |
| 2868 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2869 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2870 | // the default argument. |
| 2871 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2872 | if (!TTP->hasDefaultArgument()) { |
| 2873 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2874 | break; |
| 2875 | } |
| 2876 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2877 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2878 | Template, |
| 2879 | TemplateLoc, |
| 2880 | RAngleLoc, |
| 2881 | TTP, |
| 2882 | Converted); |
| 2883 | if (!ArgType) |
| 2884 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2885 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2886 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2887 | ArgType); |
| 2888 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2889 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2890 | if (!NTTP->hasDefaultArgument()) { |
| 2891 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2892 | break; |
| 2893 | } |
| 2894 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2895 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2896 | TemplateLoc, |
| 2897 | RAngleLoc, |
| 2898 | NTTP, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2899 | Converted); |
| 2900 | if (E.isInvalid()) |
| 2901 | return true; |
| 2902 | |
| 2903 | Expr *Ex = E.takeAs<Expr>(); |
| 2904 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2905 | } else { |
| 2906 | TemplateTemplateParmDecl *TempParm |
| 2907 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2908 | |
| 2909 | if (!TempParm->hasDefaultArgument()) { |
| 2910 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2911 | break; |
| 2912 | } |
| 2913 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2914 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2915 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2916 | TemplateLoc, |
| 2917 | RAngleLoc, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2918 | TempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2919 | Converted, |
| 2920 | QualifierLoc); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2921 | if (Name.isNull()) |
| 2922 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2923 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2924 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 2925 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2926 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2927 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2928 | // Introduce an instantiation record that describes where we are using |
| 2929 | // the default template argument. |
| 2930 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2931 | Converted.data(), Converted.size(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2932 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2933 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2934 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2935 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2936 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2937 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2938 | |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2939 | // Core issue 150 (assumed resolution): if this is a template template |
| 2940 | // parameter, keep track of the default template arguments from the |
| 2941 | // template definition. |
| 2942 | if (isTemplateTemplateParameter) |
| 2943 | TemplateArgs.addArgument(Arg); |
| 2944 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2945 | // Move to the next template parameter and argument. |
| 2946 | ++Param; |
| 2947 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2948 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2949 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2950 | // Form argument packs for each of the parameter packs remaining. |
| 2951 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2952 | // If we're checking a partial list of template arguments, don't fill |
| 2953 | // in arguments for non-template parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2954 | |
| 2955 | if ((*Param)->isTemplateParameterPack()) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2956 | if (PartialTemplateArgs && ArgumentPack.empty()) { |
| 2957 | Converted.push_back(TemplateArgument()); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2958 | } else if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2959 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2960 | else { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2961 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 2962 | ArgumentPack.data(), |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2963 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2964 | ArgumentPack.clear(); |
| 2965 | } |
| 2966 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2967 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2968 | ++Param; |
| 2969 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2970 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2971 | return Invalid; |
| 2972 | } |
| 2973 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2974 | namespace { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2975 | class UnnamedLocalNoLinkageFinder |
| 2976 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2977 | { |
| 2978 | Sema &S; |
| 2979 | SourceRange SR; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2980 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2981 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2982 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2983 | public: |
| 2984 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 2985 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2986 | bool Visit(QualType T) { |
| 2987 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2988 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2989 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2990 | #define TYPE(Class, Parent) \ |
| 2991 | bool Visit##Class##Type(const Class##Type *); |
| 2992 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 2993 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2994 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 2995 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2996 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2997 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2998 | bool VisitTagDecl(const TagDecl *Tag); |
| 2999 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3000 | }; |
| 3001 | } |
| 3002 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3003 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3004 | return false; |
| 3005 | } |
| 3006 | |
| 3007 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3008 | return Visit(T->getElementType()); |
| 3009 | } |
| 3010 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3011 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3012 | return Visit(T->getPointeeType()); |
| 3013 | } |
| 3014 | |
| 3015 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3016 | const BlockPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3017 | return Visit(T->getPointeeType()); |
| 3018 | } |
| 3019 | |
| 3020 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3021 | const LValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3022 | return Visit(T->getPointeeType()); |
| 3023 | } |
| 3024 | |
| 3025 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3026 | const RValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3027 | return Visit(T->getPointeeType()); |
| 3028 | } |
| 3029 | |
| 3030 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3031 | const MemberPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3032 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3033 | } |
| 3034 | |
| 3035 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3036 | const ConstantArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3037 | return Visit(T->getElementType()); |
| 3038 | } |
| 3039 | |
| 3040 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3041 | const IncompleteArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3042 | return Visit(T->getElementType()); |
| 3043 | } |
| 3044 | |
| 3045 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3046 | const VariableArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3047 | return Visit(T->getElementType()); |
| 3048 | } |
| 3049 | |
| 3050 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3051 | const DependentSizedArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3052 | return Visit(T->getElementType()); |
| 3053 | } |
| 3054 | |
| 3055 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3056 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3057 | return Visit(T->getElementType()); |
| 3058 | } |
| 3059 | |
| 3060 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3061 | return Visit(T->getElementType()); |
| 3062 | } |
| 3063 | |
| 3064 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 3065 | return Visit(T->getElementType()); |
| 3066 | } |
| 3067 | |
| 3068 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 3069 | const FunctionProtoType* T) { |
| 3070 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3071 | AEnd = T->arg_type_end(); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3072 | A != AEnd; ++A) { |
| 3073 | if (Visit(*A)) |
| 3074 | return true; |
| 3075 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3076 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3077 | return Visit(T->getResultType()); |
| 3078 | } |
| 3079 | |
| 3080 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 3081 | const FunctionNoProtoType* T) { |
| 3082 | return Visit(T->getResultType()); |
| 3083 | } |
| 3084 | |
| 3085 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 3086 | const UnresolvedUsingType*) { |
| 3087 | return false; |
| 3088 | } |
| 3089 | |
| 3090 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 3091 | return false; |
| 3092 | } |
| 3093 | |
| 3094 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 3095 | return Visit(T->getUnderlyingType()); |
| 3096 | } |
| 3097 | |
| 3098 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 3099 | return false; |
| 3100 | } |
| 3101 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3102 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 3103 | return Visit(T->getDeducedType()); |
| 3104 | } |
| 3105 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3106 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 3107 | return VisitTagDecl(T->getDecl()); |
| 3108 | } |
| 3109 | |
| 3110 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 3111 | return VisitTagDecl(T->getDecl()); |
| 3112 | } |
| 3113 | |
| 3114 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 3115 | const TemplateTypeParmType*) { |
| 3116 | return false; |
| 3117 | } |
| 3118 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3119 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 3120 | const SubstTemplateTypeParmPackType *) { |
| 3121 | return false; |
| 3122 | } |
| 3123 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3124 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 3125 | const TemplateSpecializationType*) { |
| 3126 | return false; |
| 3127 | } |
| 3128 | |
| 3129 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 3130 | const InjectedClassNameType* T) { |
| 3131 | return VisitTagDecl(T->getDecl()); |
| 3132 | } |
| 3133 | |
| 3134 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 3135 | const DependentNameType* T) { |
| 3136 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3137 | } |
| 3138 | |
| 3139 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 3140 | const DependentTemplateSpecializationType* T) { |
| 3141 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3142 | } |
| 3143 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3144 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 3145 | const PackExpansionType* T) { |
| 3146 | return Visit(T->getPattern()); |
| 3147 | } |
| 3148 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3149 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 3150 | return false; |
| 3151 | } |
| 3152 | |
| 3153 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 3154 | const ObjCInterfaceType *) { |
| 3155 | return false; |
| 3156 | } |
| 3157 | |
| 3158 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 3159 | const ObjCObjectPointerType *) { |
| 3160 | return false; |
| 3161 | } |
| 3162 | |
| 3163 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 3164 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
| 3165 | S.Diag(SR.getBegin(), diag::ext_template_arg_local_type) |
| 3166 | << S.Context.getTypeDeclType(Tag) << SR; |
| 3167 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3170 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3171 | S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR; |
| 3172 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 3173 | return true; |
| 3174 | } |
| 3175 | |
| 3176 | return false; |
| 3177 | } |
| 3178 | |
| 3179 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 3180 | NestedNameSpecifier *NNS) { |
| 3181 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 3182 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3183 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3184 | switch (NNS->getKind()) { |
| 3185 | case NestedNameSpecifier::Identifier: |
| 3186 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3187 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3188 | case NestedNameSpecifier::Global: |
| 3189 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3190 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3191 | case NestedNameSpecifier::TypeSpec: |
| 3192 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3193 | return Visit(QualType(NNS->getAsType(), 0)); |
| 3194 | } |
Fariborz Jahanian | 7b1ec6c | 2010-10-13 16:19:16 +0000 | [diff] [blame] | 3195 | return false; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3199 | /// \brief Check a template argument against its corresponding |
| 3200 | /// template type parameter. |
| 3201 | /// |
| 3202 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 3203 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3204 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3205 | TypeSourceInfo *ArgInfo) { |
| 3206 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3207 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 3208 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 3209 | |
| 3210 | if (Arg->isVariablyModifiedType()) { |
| 3211 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3212 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3213 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3214 | } |
| 3215 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3216 | // C++03 [temp.arg.type]p2: |
| 3217 | // A local type, a type with no linkage, an unnamed type or a type |
| 3218 | // compounded from any of these types shall not be used as a |
| 3219 | // template-argument for a template type-parameter. |
| 3220 | // |
| 3221 | // C++0x allows these, and even in C++03 we allow them as an extension with |
| 3222 | // a warning. |
Douglas Gregor | db4d4bb | 2010-10-13 18:05:20 +0000 | [diff] [blame] | 3223 | if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3224 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 3225 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 3226 | } |
| 3227 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3228 | return false; |
| 3229 | } |
| 3230 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3231 | /// \brief Checks whether the given template argument is the address |
| 3232 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3233 | static bool |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3234 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 3235 | NonTypeTemplateParmDecl *Param, |
| 3236 | QualType ParamType, |
| 3237 | Expr *ArgIn, |
| 3238 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3239 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3240 | Expr *Arg = ArgIn; |
| 3241 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3242 | |
| 3243 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3244 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3245 | Arg = Cast->getSubExpr(); |
| 3246 | |
| 3247 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3248 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3249 | // A template-argument for a non-type, non-template |
| 3250 | // template-parameter shall be one of: [...] |
| 3251 | // |
| 3252 | // -- the address of an object or function with external |
| 3253 | // linkage, including function templates and function |
| 3254 | // template-ids but excluding non-static class members, |
| 3255 | // expressed as & id-expression where the & is optional if |
| 3256 | // the name refers to a function or array, or if the |
| 3257 | // corresponding template-parameter is a reference; or |
| 3258 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3259 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3260 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3261 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3262 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3263 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3264 | if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3265 | S.Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3266 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3267 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3268 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | Arg = Parens->getSubExpr(); |
| 3272 | } |
| 3273 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3274 | bool AddressTaken = false; |
| 3275 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3276 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3277 | if (UnOp->getOpcode() == UO_AddrOf) { |
Francois Pichet | 11f98d0 | 2011-04-28 05:12:34 +0000 | [diff] [blame] | 3278 | // Support &__uuidof(class_with_uuid) as a non-type template argument. |
| 3279 | // Very common in Microsoft COM headers. |
| 3280 | if (S.getLangOptions().Microsoft && |
| 3281 | isa<CXXUuidofExpr>(UnOp->getSubExpr())) { |
| 3282 | Converted = TemplateArgument(ArgIn); |
| 3283 | return false; |
| 3284 | } |
| 3285 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3286 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3287 | AddressTaken = true; |
| 3288 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 3289 | } |
Francois Pichet | a343a41 | 2011-04-29 09:08:14 +0000 | [diff] [blame] | 3290 | } else { |
| 3291 | if (S.getLangOptions().Microsoft && isa<CXXUuidofExpr>(Arg)) { |
| 3292 | Converted = TemplateArgument(ArgIn); |
| 3293 | return false; |
| 3294 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3295 | DRE = dyn_cast<DeclRefExpr>(Arg); |
Francois Pichet | a343a41 | 2011-04-29 09:08:14 +0000 | [diff] [blame] | 3296 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3297 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3298 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 3299 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3300 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3301 | return true; |
| 3302 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3303 | |
| 3304 | // Stop checking the precise nature of the argument if it is value dependent, |
| 3305 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3306 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3307 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3308 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3309 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3310 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3311 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 3312 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3313 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3314 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3315 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3316 | return true; |
| 3317 | } |
| 3318 | |
| 3319 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3320 | |
| 3321 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3322 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 3323 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3324 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3325 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3326 | return true; |
| 3327 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3328 | |
| 3329 | // Cannot refer to non-static member functions |
| 3330 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3331 | if (!Method->isStatic()) { |
| 3332 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3333 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3334 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3335 | return true; |
| 3336 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3337 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3338 | // Functions must have external linkage. |
| 3339 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3340 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3341 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3342 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3343 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3344 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3345 | << true; |
| 3346 | return true; |
| 3347 | } |
| 3348 | |
| 3349 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3350 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3351 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3352 | // If the template parameter has pointer type, the function decays. |
| 3353 | if (ParamType->isPointerType() && !AddressTaken) |
| 3354 | ArgType = S.Context.getPointerType(Func->getType()); |
| 3355 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 3356 | // If we originally had an address-of operator, but the |
| 3357 | // parameter has reference type, complain and (if things look |
| 3358 | // like they will work) drop the address-of operator. |
| 3359 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 3360 | ParamType.getNonReferenceType())) { |
| 3361 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3362 | << ParamType; |
| 3363 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3364 | return true; |
| 3365 | } |
| 3366 | |
| 3367 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3368 | << ParamType |
| 3369 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3370 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3371 | |
| 3372 | ArgType = Func->getType(); |
| 3373 | } |
| 3374 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3375 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3376 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3377 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3378 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3379 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3380 | << true; |
| 3381 | return true; |
| 3382 | } |
| 3383 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3384 | // A value of reference type is not an object. |
| 3385 | if (Var->getType()->isReferenceType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3386 | S.Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3387 | diag::err_template_arg_reference_var) |
| 3388 | << Var->getType() << Arg->getSourceRange(); |
| 3389 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3390 | return true; |
| 3391 | } |
| 3392 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3393 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3394 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3395 | |
| 3396 | // If the template parameter has pointer type, we must have taken |
| 3397 | // the address of this object. |
| 3398 | if (ParamType->isReferenceType()) { |
| 3399 | if (AddressTaken) { |
| 3400 | // If we originally had an address-of operator, but the |
| 3401 | // parameter has reference type, complain and (if things look |
| 3402 | // like they will work) drop the address-of operator. |
| 3403 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3404 | ParamType.getNonReferenceType())) { |
| 3405 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3406 | << ParamType; |
| 3407 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3408 | return true; |
| 3409 | } |
| 3410 | |
| 3411 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3412 | << ParamType |
| 3413 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3414 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3415 | |
| 3416 | ArgType = Var->getType(); |
| 3417 | } |
| 3418 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3419 | if (Var->getType()->isArrayType()) { |
| 3420 | // Array-to-pointer decay. |
| 3421 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3422 | } else { |
| 3423 | // If the template parameter has pointer type but the address of |
| 3424 | // this object was not taken, complain and (possibly) recover by |
| 3425 | // taking the address of the entity. |
| 3426 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3427 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3428 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3429 | << ParamType; |
| 3430 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3431 | return true; |
| 3432 | } |
| 3433 | |
| 3434 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3435 | << ParamType |
| 3436 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3437 | |
| 3438 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3439 | } |
| 3440 | } |
| 3441 | } else { |
| 3442 | // We found something else, but we don't know specifically what it is. |
| 3443 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3444 | diag::err_template_arg_not_object_or_func) |
| 3445 | << Arg->getSourceRange(); |
| 3446 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3447 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3448 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3449 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3450 | if (ParamType->isPointerType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3451 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3452 | S.IsQualificationConversion(ArgType, ParamType, false)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3453 | // For pointer-to-object types, qualification conversions are |
| 3454 | // permitted. |
| 3455 | } else { |
| 3456 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3457 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3458 | // C++ [temp.arg.nontype]p5b3: |
| 3459 | // For a non-type template-parameter of type reference to |
| 3460 | // object, no conversions apply. The type referred to by the |
| 3461 | // reference may be more cv-qualified than the (otherwise |
| 3462 | // identical) type of the template- argument. The |
| 3463 | // template-parameter is bound directly to the |
| 3464 | // template-argument, which shall be an lvalue. |
| 3465 | |
| 3466 | // FIXME: Other qualifiers? |
| 3467 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3468 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3469 | |
| 3470 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 3471 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3472 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3473 | << ParamType << Arg->getType() |
| 3474 | << Arg->getSourceRange(); |
| 3475 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3476 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3477 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3478 | } |
| 3479 | } |
| 3480 | |
| 3481 | // At this point, the template argument refers to an object or |
| 3482 | // function with external linkage. We now need to check whether the |
| 3483 | // argument and parameter types are compatible. |
| 3484 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3485 | ParamType.getNonReferenceType())) { |
| 3486 | // We can't perform this conversion or binding. |
| 3487 | if (ParamType->isReferenceType()) |
| 3488 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 3489 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 3490 | else |
| 3491 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 3492 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 3493 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3494 | return true; |
| 3495 | } |
| 3496 | } |
| 3497 | |
| 3498 | // Create the template argument. |
| 3499 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 3500 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3501 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
| 3504 | /// \brief Checks whether the given template argument is a pointer to |
| 3505 | /// member constant according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3506 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3507 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3508 | bool Invalid = false; |
| 3509 | |
| 3510 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3511 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3512 | Arg = Cast->getSubExpr(); |
| 3513 | |
| 3514 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3515 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3516 | // A template-argument for a non-type, non-template |
| 3517 | // template-parameter shall be one of: [...] |
| 3518 | // |
| 3519 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3520 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3521 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3522 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3523 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3524 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3525 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3526 | if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3527 | Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3528 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3529 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3530 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
| 3533 | Arg = Parens->getSubExpr(); |
| 3534 | } |
| 3535 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3536 | // A pointer-to-member constant written &Class::member. |
| 3537 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3538 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3539 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3540 | if (DRE && !DRE->getQualifier()) |
| 3541 | DRE = 0; |
| 3542 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3543 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3544 | // A constant of pointer-to-member type. |
| 3545 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3546 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3547 | if (VD->getType()->isMemberPointerType()) { |
| 3548 | if (isa<NonTypeTemplateParmDecl>(VD) || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3549 | (isa<VarDecl>(VD) && |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3550 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3551 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3552 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3553 | else |
| 3554 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3555 | return Invalid; |
| 3556 | } |
| 3557 | } |
| 3558 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3559 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3560 | DRE = 0; |
| 3561 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3562 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3563 | if (!DRE) |
| 3564 | return Diag(Arg->getSourceRange().getBegin(), |
| 3565 | diag::err_template_arg_not_pointer_to_member_form) |
| 3566 | << Arg->getSourceRange(); |
| 3567 | |
| 3568 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3569 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3570 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3571 | "Only non-static member pointers can make it here"); |
| 3572 | |
| 3573 | // Okay: this is the address of a non-static member, and therefore |
| 3574 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3575 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3576 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3577 | else |
| 3578 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3579 | return Invalid; |
| 3580 | } |
| 3581 | |
| 3582 | // 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] | 3583 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3584 | diag::err_template_arg_not_pointer_to_member_form) |
| 3585 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3586 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3587 | diag::note_template_arg_refers_here); |
| 3588 | return true; |
| 3589 | } |
| 3590 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3591 | /// \brief Check a template argument against its corresponding |
| 3592 | /// non-type template parameter. |
| 3593 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3594 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3595 | /// If an error occurred, it returns ExprError(); otherwise, it |
| 3596 | /// returns the converted template argument. \p |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3597 | /// InstantiatedParamType is the type of the non-type template |
| 3598 | /// parameter after it has been instantiated. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3599 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 3600 | QualType InstantiatedParamType, Expr *Arg, |
| 3601 | TemplateArgument &Converted, |
| 3602 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3603 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3604 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3605 | // If either the parameter has a dependent type or the argument is |
| 3606 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3607 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3608 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3609 | Converted = TemplateArgument(Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3610 | return Owned(Arg); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3611 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3612 | |
| 3613 | // C++ [temp.arg.nontype]p5: |
| 3614 | // The following conversions are performed on each expression used |
| 3615 | // as a non-type template-argument. If a non-type |
| 3616 | // template-argument cannot be converted to the type of the |
| 3617 | // corresponding template-parameter then the program is |
| 3618 | // ill-formed. |
| 3619 | // |
| 3620 | // -- for a non-type template-parameter of integral or |
| 3621 | // enumeration type, integral promotions (4.5) and integral |
| 3622 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3623 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3624 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3625 | if (ParamType->isIntegralOrEnumerationType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3626 | // C++ [temp.arg.nontype]p1: |
| 3627 | // A template-argument for a non-type, non-template |
| 3628 | // template-parameter shall be one of: |
| 3629 | // |
| 3630 | // -- an integral constant-expression of integral or enumeration |
| 3631 | // type; or |
| 3632 | // -- the name of a non-type template-parameter; or |
| 3633 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3634 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3635 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3636 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3637 | diag::err_template_arg_not_integral_or_enumeral) |
| 3638 | << ArgType << Arg->getSourceRange(); |
| 3639 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3640 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3641 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3642 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3643 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 3644 | << ArgType << Arg->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3645 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3646 | } |
| 3647 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3648 | // From here on out, all we care about are the unqualified forms |
| 3649 | // of the parameter and argument types. |
| 3650 | ParamType = ParamType.getUnqualifiedType(); |
| 3651 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3652 | |
| 3653 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3654 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3655 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3656 | } else if (CTAK == CTAK_Deduced) { |
| 3657 | // C++ [temp.deduct.type]p17: |
| 3658 | // If, in the declaration of a function template with a non-type |
| 3659 | // template-parameter, the non-type template- parameter is used |
| 3660 | // in an expression in the function parameter-list and, if the |
| 3661 | // corresponding template-argument is deduced, the |
| 3662 | // template-argument type shall match the type of the |
| 3663 | // template-parameter exactly, except that a template-argument |
| 3664 | // deduced from an array bound may be of any integral type. |
| 3665 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3666 | << ArgType << ParamType; |
| 3667 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3668 | return ExprError(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3669 | } else if (ParamType->isBooleanType()) { |
| 3670 | // This is an integral-to-boolean conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3671 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3672 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3673 | !ParamType->isEnumeralType()) { |
| 3674 | // This is an integral promotion or conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3675 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3676 | } else { |
| 3677 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3678 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3679 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3680 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3681 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3682 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3683 | } |
| 3684 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3685 | // Add the value of this argument to the list of converted |
| 3686 | // arguments. We use the bitwidth and signedness of the template |
| 3687 | // parameter. |
| 3688 | if (Arg->isValueDependent()) { |
| 3689 | // The argument is value-dependent. Create a new |
| 3690 | // TemplateArgument with the converted expression. |
| 3691 | Converted = TemplateArgument(Arg); |
| 3692 | return Owned(Arg); |
| 3693 | } |
| 3694 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3695 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3696 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3697 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3698 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3699 | if (ParamType->isBooleanType()) { |
| 3700 | // Value must be zero or one. |
| 3701 | Value = Value != 0; |
| 3702 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 3703 | if (Value.getBitWidth() != AllowedBits) |
| 3704 | Value = Value.extOrTrunc(AllowedBits); |
| 3705 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
| 3706 | } else { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3707 | llvm::APSInt OldValue = Value; |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3708 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3709 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3710 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3711 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3712 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3713 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3714 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3715 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3716 | // Complain if an unsigned parameter received a negative value. |
| 3717 | if (IntegerType->isUnsignedIntegerType() |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3718 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3719 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3720 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3721 | << Arg->getSourceRange(); |
| 3722 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3723 | } |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3724 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3725 | // Complain if we overflowed the template parameter's type. |
| 3726 | unsigned RequiredBits; |
| 3727 | if (IntegerType->isUnsignedIntegerType()) |
| 3728 | RequiredBits = OldValue.getActiveBits(); |
| 3729 | else if (OldValue.isUnsigned()) |
| 3730 | RequiredBits = OldValue.getActiveBits() + 1; |
| 3731 | else |
| 3732 | RequiredBits = OldValue.getMinSignedBits(); |
| 3733 | if (RequiredBits > AllowedBits) { |
| 3734 | Diag(Arg->getSourceRange().getBegin(), |
| 3735 | diag::warn_template_arg_too_large) |
| 3736 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3737 | << Arg->getSourceRange(); |
| 3738 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3739 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3740 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3741 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3742 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3743 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3744 | : IntegerType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3745 | return Owned(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3746 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3747 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3748 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 3749 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3750 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 3751 | // from a template argument of type std::nullptr_t to a non-type |
| 3752 | // template parameter of type pointer to object, pointer to |
| 3753 | // function, or pointer-to-member, respectively. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3754 | if (ArgType->isNullPtrType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3755 | (ParamType->isPointerType() || ParamType->isMemberPointerType())) { |
| 3756 | Converted = TemplateArgument((NamedDecl *)0); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3757 | return Owned(Arg); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3760 | // Handle pointer-to-function, reference-to-function, and |
| 3761 | // pointer-to-member-function all in (roughly) the same way. |
| 3762 | if (// -- For a non-type template-parameter of type pointer to |
| 3763 | // function, only the function-to-pointer conversion (4.3) is |
| 3764 | // applied. If the template-argument represents a set of |
| 3765 | // overloaded functions (or a pointer to such), the matching |
| 3766 | // function is selected from the set (13.4). |
| 3767 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3768 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3769 | // -- For a non-type template-parameter of type reference to |
| 3770 | // function, no conversions apply. If the template-argument |
| 3771 | // represents a set of overloaded functions, the matching |
| 3772 | // function is selected from the set (13.4). |
| 3773 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3774 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3775 | // -- For a non-type template-parameter of type pointer to |
| 3776 | // member function, no conversions apply. If the |
| 3777 | // template-argument represents a set of overloaded member |
| 3778 | // functions, the matching member function is selected from |
| 3779 | // the set (13.4). |
| 3780 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3781 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3782 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3783 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3784 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3785 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3786 | true, |
| 3787 | FoundResult)) { |
| 3788 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3789 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3790 | |
| 3791 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3792 | ArgType = Arg->getType(); |
| 3793 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3794 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3795 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3796 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3797 | if (!ParamType->isMemberPointerType()) { |
| 3798 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3799 | ParamType, |
| 3800 | Arg, Converted)) |
| 3801 | return ExprError(); |
| 3802 | return Owned(Arg); |
| 3803 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3804 | |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3805 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(), |
| 3806 | false)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3807 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3808 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 3809 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3810 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3811 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3812 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3813 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3814 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3815 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3816 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3817 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3818 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 3819 | return ExprError(); |
| 3820 | return Owned(Arg); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 3823 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3824 | // -- for a non-type template-parameter of type pointer to |
| 3825 | // object, qualification conversions (4.4) and the |
| 3826 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3827 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3828 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3829 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3830 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3831 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3832 | ParamType, |
| 3833 | Arg, Converted)) |
| 3834 | return ExprError(); |
| 3835 | return Owned(Arg); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3836 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3837 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3838 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3839 | // -- For a non-type template-parameter of type reference to |
| 3840 | // object, no conversions apply. The type referred to by the |
| 3841 | // reference may be more cv-qualified than the (otherwise |
| 3842 | // identical) type of the template-argument. The |
| 3843 | // template-parameter is bound directly to the |
| 3844 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3845 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3846 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3847 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3848 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3849 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 3850 | ParamRefType->getPointeeType(), |
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 | b86b057 | 2009-02-11 01:18:59 +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 (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3863 | ParamType, |
| 3864 | Arg, Converted)) |
| 3865 | return ExprError(); |
| 3866 | return Owned(Arg); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3867 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3868 | |
| 3869 | // -- For a non-type template-parameter of type pointer to data |
| 3870 | // member, qualification conversions (4.4) are applied. |
| 3871 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 3872 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 3873 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3874 | // Types match exactly: nothing more to do here. |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3875 | } else if (IsQualificationConversion(ArgType, ParamType, false)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3876 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3877 | } else { |
| 3878 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3879 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3880 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3881 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3882 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3883 | return ExprError(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3886 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 3887 | return ExprError(); |
| 3888 | return Owned(Arg); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
| 3891 | /// \brief Check a template argument against its corresponding |
| 3892 | /// template template parameter. |
| 3893 | /// |
| 3894 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 3895 | /// It returns true if an error occurred, and false otherwise. |
| 3896 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3897 | const TemplateArgumentLoc &Arg) { |
| 3898 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 3899 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 3900 | if (!Template) { |
| 3901 | // Any dependent template name is fine. |
| 3902 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 3903 | return false; |
| 3904 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3905 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3906 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3907 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3908 | // the name of a class template or an alias template, expressed as an |
| 3909 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3910 | // primary class templates are considered when matching the |
| 3911 | // template template argument with the corresponding parameter; |
| 3912 | // partial specializations are not considered even if their |
| 3913 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3914 | // |
| 3915 | // Note that we also allow template template parameters here, which |
| 3916 | // will happen when we are dealing with, e.g., class template |
| 3917 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3918 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3919 | !isa<TemplateTemplateParmDecl>(Template) && |
| 3920 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3921 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3922 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3923 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3924 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3925 | << Template; |
| 3926 | } |
| 3927 | |
| 3928 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 3929 | Param->getTemplateParameters(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3930 | true, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3931 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3932 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3933 | } |
| 3934 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3935 | /// \brief Given a non-type template argument that refers to a |
| 3936 | /// declaration and the type of its corresponding non-type template |
| 3937 | /// parameter, produce an expression that properly refers to that |
| 3938 | /// declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3939 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3940 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 3941 | QualType ParamType, |
| 3942 | SourceLocation Loc) { |
| 3943 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 3944 | "Only declaration template arguments permitted here"); |
| 3945 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 3946 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3947 | if (VD->getDeclContext()->isRecord() && |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3948 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 3949 | // If the value is a class member, we might have a pointer-to-member. |
| 3950 | // Determine whether the non-type template template parameter is of |
| 3951 | // pointer-to-member type. If so, we need to build an appropriate |
| 3952 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 3953 | // would refer to the member itself. |
| 3954 | if (ParamType->isMemberPointerType()) { |
| 3955 | QualType ClassType |
| 3956 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 3957 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3958 | = NestedNameSpecifier::Create(Context, 0, false, |
| 3959 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3960 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 3961 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3962 | |
| 3963 | // The actual value-ness of this is unimportant, but for |
| 3964 | // internal consistency's sake, references to instance methods |
| 3965 | // are r-values. |
| 3966 | ExprValueKind VK = VK_LValue; |
| 3967 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 3968 | VK = VK_RValue; |
| 3969 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3970 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3971 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3972 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3973 | Loc, |
| 3974 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3975 | if (RefExpr.isInvalid()) |
| 3976 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3977 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3978 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3979 | |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3980 | // We might need to perform a trailing qualification conversion, since |
| 3981 | // the element type on the parameter could be more qualified than the |
| 3982 | // element type in the expression we constructed. |
| 3983 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3984 | ParamType.getUnqualifiedType(), false)) |
| 3985 | RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3986 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3987 | assert(!RefExpr.isInvalid() && |
| 3988 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3989 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3990 | return move(RefExpr); |
| 3991 | } |
| 3992 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3993 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3994 | QualType T = VD->getType().getNonReferenceType(); |
| 3995 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3996 | // When the non-type template parameter is a pointer, take the |
| 3997 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3998 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3999 | if (RefExpr.isInvalid()) |
| 4000 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4001 | |
| 4002 | if (T->isFunctionType() || T->isArrayType()) { |
| 4003 | // Decay functions and arrays. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4004 | RefExpr = DefaultFunctionArrayConversion(RefExpr.take()); |
| 4005 | if (RefExpr.isInvalid()) |
| 4006 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4007 | |
| 4008 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4009 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4010 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4011 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4012 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4015 | ExprValueKind VK = VK_RValue; |
| 4016 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4017 | // If the non-type template parameter has reference type, qualify the |
| 4018 | // resulting declaration reference with the extra qualifiers on the |
| 4019 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4020 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 4021 | VK = VK_LValue; |
| 4022 | T = Context.getQualifiedType(T, |
| 4023 | TargetRef->getPointeeType().getQualifiers()); |
| 4024 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4025 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4026 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4027 | } |
| 4028 | |
| 4029 | /// \brief Construct a new expression that refers to the given |
| 4030 | /// integral template argument with the given source-location |
| 4031 | /// information. |
| 4032 | /// |
| 4033 | /// This routine takes care of the mapping from an integral template |
| 4034 | /// argument (which may have any integral type) to the appropriate |
| 4035 | /// literal value. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4036 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4037 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 4038 | SourceLocation Loc) { |
| 4039 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4040 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4041 | QualType T = Arg.getIntegralType(); |
| 4042 | if (T->isCharType() || T->isWideCharType()) |
| 4043 | return Owned(new (Context) CharacterLiteral( |
| 4044 | Arg.getAsIntegral()->getZExtValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4045 | T->isWideCharType(), T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4046 | if (T->isBooleanType()) |
| 4047 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 4048 | Arg.getAsIntegral()->getBoolValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4049 | T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4050 | |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4051 | // If this is an enum type that we're instantiating, we need to use an integer |
| 4052 | // type the same size as the enumerator. We don't want to build an |
| 4053 | // IntegerLiteral with enum type. |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4054 | QualType BT; |
| 4055 | if (const EnumType *ET = T->getAs<EnumType>()) |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4056 | BT = ET->getDecl()->getIntegerType(); |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4057 | else |
| 4058 | BT = T; |
| 4059 | |
| 4060 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
Douglas Gregor | 8f5667d | 2011-02-18 02:12:44 +0000 | [diff] [blame] | 4061 | if (T->isEnumeralType()) { |
| 4062 | // FIXME: This is a hack. We need a better way to handle substituted |
| 4063 | // non-type template parameters. |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4064 | E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0, |
| 4065 | Context.getTrivialTypeSourceInfo(T, Loc), |
| 4066 | Loc, Loc); |
Douglas Gregor | 8f5667d | 2011-02-18 02:12:44 +0000 | [diff] [blame] | 4067 | } |
| 4068 | |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4069 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4070 | } |
| 4071 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4072 | /// \brief Match two template parameters within template parameter lists. |
| 4073 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 4074 | bool Complain, |
| 4075 | Sema::TemplateParameterListEqualKind Kind, |
| 4076 | SourceLocation TemplateArgLoc) { |
| 4077 | // Check the actual kind (type, non-type, template). |
| 4078 | if (Old->getKind() != New->getKind()) { |
| 4079 | if (Complain) { |
| 4080 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 4081 | if (TemplateArgLoc.isValid()) { |
| 4082 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4083 | NextDiag = diag::note_template_param_different_kind; |
| 4084 | } |
| 4085 | S.Diag(New->getLocation(), NextDiag) |
| 4086 | << (Kind != Sema::TPL_TemplateMatch); |
| 4087 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 4088 | << (Kind != Sema::TPL_TemplateMatch); |
| 4089 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4090 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4091 | return false; |
| 4092 | } |
| 4093 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4094 | // Check that both are parameter packs are neither are parameter packs. |
| 4095 | // However, if we are matching a template template argument to a |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4096 | // template template parameter, the template template parameter can have |
| 4097 | // a parameter pack where the template template argument does not. |
| 4098 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 4099 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4100 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4101 | if (Complain) { |
| 4102 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 4103 | if (TemplateArgLoc.isValid()) { |
| 4104 | S.Diag(TemplateArgLoc, |
| 4105 | diag::err_template_arg_template_params_mismatch); |
| 4106 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 4107 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4108 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4109 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 4110 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 4111 | : 2; |
| 4112 | S.Diag(New->getLocation(), NextDiag) |
| 4113 | << ParamKind << New->isParameterPack(); |
| 4114 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 4115 | << ParamKind << Old->isParameterPack(); |
| 4116 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4117 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4118 | return false; |
| 4119 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4120 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4121 | // For non-type template parameters, check the type of the parameter. |
| 4122 | if (NonTypeTemplateParmDecl *OldNTTP |
| 4123 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 4124 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4125 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4126 | // If we are matching a template template argument to a template |
| 4127 | // template parameter and one of the non-type template parameter types |
| 4128 | // is dependent, then we must wait until template instantiation time |
| 4129 | // to actually compare the arguments. |
| 4130 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4131 | (OldNTTP->getType()->isDependentType() || |
| 4132 | NewNTTP->getType()->isDependentType())) |
| 4133 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4134 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4135 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 4136 | if (Complain) { |
| 4137 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 4138 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4139 | S.Diag(TemplateArgLoc, |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4140 | diag::err_template_arg_template_params_mismatch); |
| 4141 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 4142 | } |
| 4143 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 4144 | << NewNTTP->getType() |
| 4145 | << (Kind != Sema::TPL_TemplateMatch); |
| 4146 | S.Diag(OldNTTP->getLocation(), |
| 4147 | diag::note_template_nontype_parm_prev_declaration) |
| 4148 | << OldNTTP->getType(); |
| 4149 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4150 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4151 | return false; |
| 4152 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4153 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4154 | return true; |
| 4155 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4156 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4157 | // For template template parameters, check the template parameter types. |
| 4158 | // The template parameter lists of template template |
| 4159 | // parameters must agree. |
| 4160 | if (TemplateTemplateParmDecl *OldTTP |
| 4161 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4162 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4163 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 4164 | OldTTP->getTemplateParameters(), |
| 4165 | Complain, |
| 4166 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4167 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4168 | : Kind), |
| 4169 | TemplateArgLoc); |
| 4170 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4171 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4172 | return true; |
| 4173 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4174 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4175 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 4176 | /// lists. |
| 4177 | static |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4178 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4179 | TemplateParameterList *New, |
| 4180 | TemplateParameterList *Old, |
| 4181 | Sema::TemplateParameterListEqualKind Kind, |
| 4182 | SourceLocation TemplateArgLoc) { |
| 4183 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 4184 | if (TemplateArgLoc.isValid()) { |
| 4185 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4186 | NextDiag = diag::note_template_param_list_different_arity; |
| 4187 | } |
| 4188 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 4189 | << (New->size() > Old->size()) |
| 4190 | << (Kind != Sema::TPL_TemplateMatch) |
| 4191 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 4192 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 4193 | << (Kind != Sema::TPL_TemplateMatch) |
| 4194 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 4195 | } |
| 4196 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4197 | /// \brief Determine whether the given template parameter lists are |
| 4198 | /// equivalent. |
| 4199 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4200 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4201 | /// source code as part of a new template declaration. |
| 4202 | /// |
| 4203 | /// \param Old The old template parameter list, typically found via |
| 4204 | /// name lookup of the template declared with this template parameter |
| 4205 | /// list. |
| 4206 | /// |
| 4207 | /// \param Complain If true, this routine will produce a diagnostic if |
| 4208 | /// the template parameter lists are not equivalent. |
| 4209 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4210 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4211 | /// |
| 4212 | /// \param TemplateArgLoc If this source location is valid, then we |
| 4213 | /// are actually checking the template parameter list of a template |
| 4214 | /// argument (New) against the template parameter list of its |
| 4215 | /// corresponding template template parameter (Old). We produce |
| 4216 | /// slightly different diagnostics in this scenario. |
| 4217 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4218 | /// \returns True if the template parameter lists are equal, false |
| 4219 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4220 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4221 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 4222 | TemplateParameterList *Old, |
| 4223 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4224 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4225 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4226 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 4227 | if (Complain) |
| 4228 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4229 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4230 | |
| 4231 | return false; |
| 4232 | } |
| 4233 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4234 | // C++0x [temp.arg.template]p3: |
| 4235 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4236 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4237 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4238 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4239 | // template-parameter-list of P. [...] |
| 4240 | TemplateParameterList::iterator NewParm = New->begin(); |
| 4241 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4242 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4243 | OldParmEnd = Old->end(); |
| 4244 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 4245 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 4246 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4247 | if (NewParm == NewParmEnd) { |
| 4248 | if (Complain) |
| 4249 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4250 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4251 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4252 | return false; |
| 4253 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4254 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4255 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4256 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4257 | return false; |
| 4258 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4259 | ++NewParm; |
| 4260 | continue; |
| 4261 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4262 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4263 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4264 | // [...] When P's template- parameter-list contains a template parameter |
| 4265 | // pack (14.5.3), the template parameter pack will match zero or more |
| 4266 | // template parameters or template parameter packs in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4267 | // template-parameter-list of A with the same type and form as the |
| 4268 | // template parameter pack in P (ignoring whether those template |
| 4269 | // parameters are template parameter packs). |
| 4270 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 4271 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4272 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4273 | return false; |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4274 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4275 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4276 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4277 | // Make sure we exhausted all of the arguments. |
| 4278 | if (NewParm != NewParmEnd) { |
| 4279 | if (Complain) |
| 4280 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4281 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4282 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4283 | return false; |
| 4284 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4285 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4286 | return true; |
| 4287 | } |
| 4288 | |
| 4289 | /// \brief Check whether a template can be declared within this scope. |
| 4290 | /// |
| 4291 | /// If the template declaration is valid in this scope, returns |
| 4292 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4293 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4294 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4295 | // Find the nearest enclosing declaration scope. |
| 4296 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4297 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4298 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4299 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4300 | // C++ [temp]p2: |
| 4301 | // A template-declaration can appear only as a namespace scope or |
| 4302 | // class scope declaration. |
| 4303 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4304 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 4305 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4306 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4307 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4308 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4309 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4310 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4311 | |
| 4312 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 4313 | return false; |
| 4314 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4315 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4316 | diag::err_template_outside_namespace_or_class_scope) |
| 4317 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4318 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4319 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4320 | /// \brief Determine what kind of template specialization the given declaration |
| 4321 | /// is. |
| 4322 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 4323 | if (!D) |
| 4324 | return TSK_Undeclared; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4325 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4326 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 4327 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4328 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 4329 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4330 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 4331 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4332 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4333 | return TSK_Undeclared; |
| 4334 | } |
| 4335 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4336 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4337 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4338 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4339 | /// This routine determines whether a template specialization can be declared |
| 4340 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4341 | /// |
| 4342 | /// \param S the semantic analysis object for which this check is being |
| 4343 | /// performed. |
| 4344 | /// |
| 4345 | /// \param Specialized the entity being specialized or instantiated, which |
| 4346 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4347 | /// a member of a class template (member function, static data member, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4348 | /// member class). |
| 4349 | /// |
| 4350 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 4351 | /// |
| 4352 | /// \param Loc the location of the explicit specialization or instantiation of |
| 4353 | /// this entity. |
| 4354 | /// |
| 4355 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 4356 | /// a class template. |
| 4357 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4358 | /// \returns true if there was an error that we cannot recover from, false |
| 4359 | /// otherwise. |
| 4360 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 4361 | NamedDecl *Specialized, |
| 4362 | NamedDecl *PrevDecl, |
| 4363 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4364 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4365 | // Keep these "kind" numbers in sync with the %select statements in the |
| 4366 | // various diagnostics emitted by this routine. |
| 4367 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4368 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4369 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4370 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4371 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4372 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4373 | EntityKind = 3; |
| 4374 | else if (isa<VarDecl>(Specialized)) |
| 4375 | EntityKind = 4; |
| 4376 | else if (isa<RecordDecl>(Specialized)) |
| 4377 | EntityKind = 5; |
| 4378 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4379 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 4380 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4381 | return true; |
| 4382 | } |
| 4383 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4384 | // C++ [temp.expl.spec]p2: |
| 4385 | // An explicit specialization shall be declared in the namespace |
| 4386 | // of which the template is a member, or, for member templates, in |
| 4387 | // the namespace of which the enclosing class or enclosing class |
| 4388 | // template is a member. An explicit specialization of a member |
| 4389 | // function, member class or static data member of a class |
| 4390 | // template shall be declared in the namespace of which the class |
| 4391 | // template is a member. Such a declaration may also be a |
| 4392 | // definition. If the declaration is not a definition, the |
| 4393 | // specialization may be defined later in the name- space in which |
| 4394 | // the explicit specialization was declared, or in a namespace |
| 4395 | // that encloses the one in which the explicit specialization was |
| 4396 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4397 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4398 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4399 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4400 | return true; |
| 4401 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4402 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4403 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 4404 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4405 | << Specialized; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4406 | return true; |
| 4407 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4408 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4409 | // C++ [temp.class.spec]p6: |
| 4410 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4411 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4412 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4413 | bool ComplainedAboutScope = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4414 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4415 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4416 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4417 | if ((!PrevDecl || |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4418 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4419 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4420 | // C++ [temp.exp.spec]p2: |
| 4421 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4422 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4423 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4424 | // An explicit specialization of a member function, member class or |
| 4425 | // static data member of a class template shall be declared in the |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4426 | // namespace of which the class template is a member. |
| 4427 | // |
| 4428 | // C++0x [temp.expl.spec]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4429 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4430 | // the specialized template. |
| 4431 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) && |
| 4432 | !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) { |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4433 | bool IsCPlusPlus0xExtension |
| 4434 | = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4435 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4436 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4437 | ? diag::ext_template_spec_decl_out_of_scope_global |
| 4438 | : diag::err_template_spec_decl_out_of_scope_global) |
| 4439 | << EntityKind << Specialized; |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4440 | else if (isa<NamespaceDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4441 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4442 | ? diag::ext_template_spec_decl_out_of_scope |
| 4443 | : diag::err_template_spec_decl_out_of_scope) |
| 4444 | << EntityKind << Specialized |
| 4445 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4446 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4447 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 4448 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4449 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4450 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4451 | |
| 4452 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4453 | // namespace. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4454 | // Note that HandleDeclarator() performs this check for explicit |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4455 | // specializations of function templates, static data members, and member |
| 4456 | // functions, so we skip the check here for those kinds of entities. |
| 4457 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4458 | // Should we refactor that check, so that it occurs later? |
| 4459 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4460 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 4461 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4462 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 4463 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 4464 | << EntityKind << Specialized; |
| 4465 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 4466 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 4467 | << EntityKind << Specialized |
| 4468 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4469 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4470 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4471 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4472 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4473 | // FIXME: check for specialization-after-instantiation errors and such. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4474 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4475 | return false; |
| 4476 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4477 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4478 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 4479 | /// that checks non-type template partial specialization arguments. |
| 4480 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 4481 | NonTypeTemplateParmDecl *Param, |
| 4482 | const TemplateArgument *Args, |
| 4483 | unsigned NumArgs) { |
| 4484 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4485 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 4486 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4487 | Args[I].pack_begin(), |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4488 | Args[I].pack_size())) |
| 4489 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4490 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4491 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4492 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4493 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4494 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4495 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4496 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4497 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4498 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4499 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4500 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4501 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4502 | |
| 4503 | // Strip off any implicit casts we added as part of type checking. |
| 4504 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4505 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4506 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4507 | // C++ [temp.class.spec]p8: |
| 4508 | // A non-type argument is non-specialized if it is the name of a |
| 4509 | // non-type parameter. All other non-type arguments are |
| 4510 | // specialized. |
| 4511 | // |
| 4512 | // Below, we check the two conditions that only apply to |
| 4513 | // specialized non-type arguments, so skip any non-specialized |
| 4514 | // arguments. |
| 4515 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4516 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4517 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4518 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4519 | // C++ [temp.class.spec]p9: |
| 4520 | // Within the argument list of a class template partial |
| 4521 | // specialization, the following restrictions apply: |
| 4522 | // -- A partially specialized non-type argument expression |
| 4523 | // shall not involve a template parameter of the partial |
| 4524 | // specialization except when the argument expression is a |
| 4525 | // simple identifier. |
| 4526 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4527 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4528 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4529 | << ArgExpr->getSourceRange(); |
| 4530 | return true; |
| 4531 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4532 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4533 | // -- The type of a template parameter corresponding to a |
| 4534 | // specialized non-type argument shall not be dependent on a |
| 4535 | // parameter of the specialization. |
| 4536 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4537 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4538 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 4539 | << Param->getType() |
| 4540 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4541 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4542 | return true; |
| 4543 | } |
| 4544 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4545 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4546 | return false; |
| 4547 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4548 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4549 | /// \brief Check the non-type template arguments of a class template |
| 4550 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 4551 | /// |
| 4552 | /// \param TemplateParams the template parameters of the primary class |
| 4553 | /// template. |
| 4554 | /// |
| 4555 | /// \param TemplateArg the template arguments of the class template |
| 4556 | /// partial specialization. |
| 4557 | /// |
| 4558 | /// \returns true if there was an error, false otherwise. |
| 4559 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 4560 | TemplateParameterList *TemplateParams, |
| 4561 | llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
| 4562 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 4563 | |
| 4564 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4565 | NonTypeTemplateParmDecl *Param |
| 4566 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 4567 | if (!Param) |
| 4568 | continue; |
| 4569 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4570 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4571 | &ArgList[I], 1)) |
| 4572 | return true; |
| 4573 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4574 | |
| 4575 | return false; |
| 4576 | } |
| 4577 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4578 | /// \brief Retrieve the previous declaration of the given declaration. |
| 4579 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 4580 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4581 | return VD->getPreviousDeclaration(); |
| 4582 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 4583 | return FD->getPreviousDeclaration(); |
| 4584 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 4585 | return TD->getPreviousDeclaration(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4586 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4587 | return TD->getPreviousDeclaration(); |
| 4588 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 4589 | return FTD->getPreviousDeclaration(); |
| 4590 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 4591 | return CTD->getPreviousDeclaration(); |
| 4592 | return 0; |
| 4593 | } |
| 4594 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4595 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4596 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4597 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4598 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4599 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4600 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4601 | SourceLocation TemplateNameLoc, |
| 4602 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4603 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4604 | SourceLocation RAngleLoc, |
| 4605 | AttributeList *Attr, |
| 4606 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4607 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4608 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4609 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 4610 | // store the location of the outermost template keyword in the declaration. |
| 4611 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
| 4612 | ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation(); |
| 4613 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4614 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4615 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4616 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4617 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4618 | |
| 4619 | if (!ClassTemplate) { |
| 4620 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4621 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4622 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4623 | return true; |
| 4624 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4625 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4626 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4627 | bool isPartialSpecialization = false; |
| 4628 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4629 | // Check the validity of the template headers that introduce this |
| 4630 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4631 | // FIXME: We probably shouldn't complain about these headers for |
| 4632 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4633 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4634 | TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 4635 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, |
| 4636 | TemplateNameLoc, |
| 4637 | SS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4638 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4639 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4640 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4641 | isExplicitSpecialization, |
| 4642 | Invalid); |
| 4643 | if (Invalid) |
| 4644 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4645 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4646 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4647 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4648 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4649 | if (TUK == TUK_Friend) { |
| 4650 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4651 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4652 | return true; |
| 4653 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4654 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4655 | // C++ [temp.class.spec]p10: |
| 4656 | // The template parameter list of a specialization shall not |
| 4657 | // contain default template argument values. |
| 4658 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4659 | Decl *Param = TemplateParams->getParam(I); |
| 4660 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4661 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4662 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4663 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4664 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4665 | } |
| 4666 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4667 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4668 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4669 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4670 | diag::err_default_arg_in_partial_spec) |
| 4671 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4672 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4673 | } |
| 4674 | } else { |
| 4675 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4676 | if (TTP->hasDefaultArgument()) { |
| 4677 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4678 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4679 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4680 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4681 | } |
| 4682 | } |
| 4683 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4684 | } else if (TemplateParams) { |
| 4685 | if (TUK == TUK_Friend) |
| 4686 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4687 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4688 | SourceRange(TemplateParams->getTemplateLoc(), |
| 4689 | TemplateParams->getRAngleLoc())) |
| 4690 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4691 | else |
| 4692 | isExplicitSpecialization = true; |
| 4693 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4694 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4695 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4696 | isExplicitSpecialization = true; |
| 4697 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4698 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4699 | // Check that the specialization uses the same tag kind as the |
| 4700 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4701 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4702 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4703 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4704 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4705 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4706 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4707 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4708 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4709 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4710 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4711 | diag::note_previous_use); |
| 4712 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4713 | } |
| 4714 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4715 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4716 | TemplateArgumentListInfo TemplateArgs; |
| 4717 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 4718 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4719 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4720 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4721 | // Check for unexpanded parameter packs in any of the template arguments. |
| 4722 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4723 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4724 | UPPC_PartialSpecialization)) |
| 4725 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4726 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4727 | // Check that the template argument list is well-formed for this |
| 4728 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4729 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4730 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4731 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4732 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4733 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4734 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4735 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4736 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4737 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4738 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4739 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4740 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4741 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4742 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4743 | return true; |
| 4744 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4745 | if (!Name.isDependent() && |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4746 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4747 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4748 | TemplateArgs.size())) { |
| 4749 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 4750 | << ClassTemplate->getDeclName(); |
| 4751 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4752 | } |
| 4753 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4754 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4755 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4756 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 4757 | |
| 4758 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4759 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4760 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4761 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 4762 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4763 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4764 | else |
| 4765 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4766 | = ClassTemplate->findSpecialization(Converted.data(), |
| 4767 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4768 | |
| 4769 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4770 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4771 | // Check whether we can declare a class template specialization in |
| 4772 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4773 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4774 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 4775 | TemplateNameLoc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4776 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4777 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4778 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4779 | // The canonical type |
| 4780 | QualType CanonType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4781 | if (PrevDecl && |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4782 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4783 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4784 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4785 | // arguments was referenced but not declared, or we're only |
| 4786 | // referencing this specialization as a friend, reuse that |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4787 | // declaration node as our own, updating its source location and |
| 4788 | // the list of outer template parameters to reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4789 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4790 | Specialization->setLocation(TemplateNameLoc); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4791 | if (TemplateParameterLists.size() > 0) { |
| 4792 | Specialization->setTemplateParameterListsInfo(Context, |
| 4793 | TemplateParameterLists.size(), |
| 4794 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4795 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4796 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4797 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4798 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4799 | // Build the canonical type that describes the converted template |
| 4800 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4801 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4802 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4803 | Converted.data(), |
| 4804 | Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4805 | |
| 4806 | if (Context.hasSameType(CanonType, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4807 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 4808 | // C++ [temp.class.spec]p9b3: |
| 4809 | // |
| 4810 | // -- The argument list of the specialization shall not be identical |
| 4811 | // to the implicit argument list of the primary template. |
| 4812 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 4813 | << (TUK == TUK_Definition) |
| 4814 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 4815 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 4816 | ClassTemplate->getIdentifier(), |
| 4817 | TemplateNameLoc, |
| 4818 | Attr, |
| 4819 | TemplateParams, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 4820 | AS_none, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4821 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 4822 | (TemplateParameterList**) TemplateParameterLists.release()); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4823 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4824 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4825 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4826 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 4827 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4828 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4829 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4830 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4831 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4832 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4833 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 4834 | TemplateParams, |
| 4835 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4836 | Converted.data(), |
| 4837 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4838 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4839 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4840 | PrevPartial, |
| 4841 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4842 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4843 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4844 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4845 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4846 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4847 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4848 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4849 | if (!PrevPartial) |
| 4850 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4851 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4852 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4853 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4854 | // template specialization, make a note of that. |
| 4855 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 4856 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4857 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4858 | // Check that all of the template parameters of the class template |
| 4859 | // partial specialization are deducible from the template |
| 4860 | // arguments. If not, this class template partial specialization |
| 4861 | // will never be used. |
| 4862 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 4863 | DeducibleParams.resize(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4864 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4865 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4866 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4867 | unsigned NumNonDeducible = 0; |
| 4868 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 4869 | if (!DeducibleParams[I]) |
| 4870 | ++NumNonDeducible; |
| 4871 | |
| 4872 | if (NumNonDeducible) { |
| 4873 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 4874 | << (NumNonDeducible > 1) |
| 4875 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 4876 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 4877 | if (!DeducibleParams[I]) { |
| 4878 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 4879 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4880 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4881 | diag::note_partial_spec_unused_parameter) |
| 4882 | << Param->getDeclName(); |
| 4883 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4884 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4885 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 4886 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4887 | } |
| 4888 | } |
| 4889 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4890 | } else { |
| 4891 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4892 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4893 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4894 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4895 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4896 | KWLoc, TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4897 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4898 | Converted.data(), |
| 4899 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4900 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4901 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4902 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4903 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4904 | TemplateParameterLists.size(), |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4905 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4906 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4907 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4908 | if (!PrevDecl) |
| 4909 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4910 | |
| 4911 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4912 | } |
| 4913 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4914 | // C++ [temp.expl.spec]p6: |
| 4915 | // 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] | 4916 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4917 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4918 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4919 | // use occurs; no diagnostic is required. |
| 4920 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4921 | bool Okay = false; |
| 4922 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4923 | // Is there any previous explicit specialization declaration? |
| 4924 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 4925 | Okay = true; |
| 4926 | break; |
| 4927 | } |
| 4928 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4929 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4930 | if (!Okay) { |
| 4931 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 4932 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 4933 | << Context.getTypeDeclType(Specialization) << Range; |
| 4934 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4935 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4936 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4937 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4938 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4939 | return true; |
| 4940 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4941 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4942 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4943 | // If this is not a friend, note that this is an explicit specialization. |
| 4944 | if (TUK != TUK_Friend) |
| 4945 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4946 | |
| 4947 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4948 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4949 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4950 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4951 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4952 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4953 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 4954 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4955 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4956 | } |
| 4957 | } |
| 4958 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 4959 | if (Attr) |
| 4960 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 4961 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 4962 | // Build the fully-sugared type for this class template |
| 4963 | // specialization as the user wrote in the specialization |
| 4964 | // itself. This means that we'll pretty-print the type retrieved |
| 4965 | // from the specialization's declaration the way that the user |
| 4966 | // actually wrote the specialization, rather than formatting the |
| 4967 | // name based on the "canonical" representation used to store the |
| 4968 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4969 | TypeSourceInfo *WrittenTy |
| 4970 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 4971 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4972 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4973 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4974 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4975 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4976 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4977 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4978 | // C++ [temp.expl.spec]p9: |
| 4979 | // A template explicit specialization is in the scope of the |
| 4980 | // namespace in which the template was defined. |
| 4981 | // |
| 4982 | // We actually implement this paragraph where we set the semantic |
| 4983 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 4984 | // but we also maintain the lexical context where the actual |
| 4985 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4986 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4987 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4988 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4989 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4990 | Specialization->startDefinition(); |
| 4991 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4992 | if (TUK == TUK_Friend) { |
| 4993 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 4994 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 4995 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4996 | /*FIXME:*/KWLoc); |
| 4997 | Friend->setAccess(AS_public); |
| 4998 | CurContext->addDecl(Friend); |
| 4999 | } else { |
| 5000 | // Add the specialization into its lexical context, so that it can |
| 5001 | // be seen when iterating through the list of declarations in that |
| 5002 | // context. However, specializations are not found by name lookup. |
| 5003 | CurContext->addDecl(Specialization); |
| 5004 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5005 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5006 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5007 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5008 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5009 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5010 | Declarator &D) { |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5011 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 5012 | } |
| 5013 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5014 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5015 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5016 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5017 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5018 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5019 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5020 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5021 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5022 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5023 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5024 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5025 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5026 | Decl *DP = HandleDeclarator(ParentScope, D, |
| 5027 | move(TemplateParameterLists), |
| 5028 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5029 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5030 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5031 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5032 | FunctionTemplate->getTemplatedDecl()); |
| 5033 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 5034 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 5035 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5036 | } |
| 5037 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5038 | /// \brief Strips various properties off an implicit instantiation |
| 5039 | /// that has just been explicitly specialized. |
| 5040 | static void StripImplicitInstantiation(NamedDecl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5041 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5042 | |
| 5043 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5044 | FD->setInlineSpecified(false); |
| 5045 | } |
| 5046 | } |
| 5047 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5048 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5049 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5050 | /// for those cases where they are required and determining whether the |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5051 | /// new specialization/instantiation will have any effect. |
| 5052 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5053 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5054 | /// instantiation. |
| 5055 | /// |
| 5056 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 5057 | /// |
| 5058 | /// \param PrevDecl the previous declaration of the entity. |
| 5059 | /// |
| 5060 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 5061 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5062 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5063 | /// declaration was instantiated (either implicitly or explicitly). |
| 5064 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5065 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5066 | /// specialization or instantiation has no effect and should be ignored. |
| 5067 | /// |
| 5068 | /// \returns true if there was an error that should prevent the introduction of |
| 5069 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5070 | bool |
| 5071 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 5072 | TemplateSpecializationKind NewTSK, |
| 5073 | NamedDecl *PrevDecl, |
| 5074 | TemplateSpecializationKind PrevTSK, |
| 5075 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5076 | bool &HasNoEffect) { |
| 5077 | HasNoEffect = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5078 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5079 | switch (NewTSK) { |
| 5080 | case TSK_Undeclared: |
| 5081 | case TSK_ImplicitInstantiation: |
| 5082 | assert(false && "Don't check implicit instantiations here"); |
| 5083 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5084 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5085 | case TSK_ExplicitSpecialization: |
| 5086 | switch (PrevTSK) { |
| 5087 | case TSK_Undeclared: |
| 5088 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5089 | // Okay, we're just specializing something that is either already |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5090 | // explicitly specialized or has merely been mentioned without any |
| 5091 | // instantiation. |
| 5092 | return false; |
| 5093 | |
| 5094 | case TSK_ImplicitInstantiation: |
| 5095 | if (PrevPointOfInstantiation.isInvalid()) { |
| 5096 | // The declaration itself has not actually been instantiated, so it is |
| 5097 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5098 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5099 | return false; |
| 5100 | } |
| 5101 | // Fall through |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5102 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5103 | case TSK_ExplicitInstantiationDeclaration: |
| 5104 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5105 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 5106 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5107 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5108 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5109 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5110 | // 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] | 5111 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5112 | // before the first use of that specialization that would cause an |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5113 | // implicit instantiation to take place, in every translation unit in |
| 5114 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5115 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 5116 | // Is there any previous explicit specialization declaration? |
| 5117 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 5118 | return false; |
| 5119 | } |
| 5120 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5121 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5122 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5123 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5124 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5125 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5126 | return true; |
| 5127 | } |
| 5128 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5129 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5130 | case TSK_ExplicitInstantiationDeclaration: |
| 5131 | switch (PrevTSK) { |
| 5132 | case TSK_ExplicitInstantiationDeclaration: |
| 5133 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5134 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5135 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5136 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5137 | case TSK_Undeclared: |
| 5138 | case TSK_ImplicitInstantiation: |
| 5139 | // We're explicitly instantiating something that may have already been |
| 5140 | // implicitly instantiated; that's fine. |
| 5141 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5142 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5143 | case TSK_ExplicitSpecialization: |
| 5144 | // C++0x [temp.explicit]p4: |
| 5145 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5146 | // of a template appears after a declaration of an explicit |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5147 | // specialization for that template, the explicit instantiation has no |
| 5148 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5149 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5150 | return 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 | case TSK_ExplicitInstantiationDefinition: |
| 5153 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5154 | // If an entity is the subject of both an explicit instantiation |
| 5155 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5156 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5157 | Diag(NewLoc, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5158 | diag::err_explicit_instantiation_declaration_after_definition); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5159 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5160 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5161 | assert(PrevPointOfInstantiation.isValid() && |
| 5162 | "Explicit instantiation without point of instantiation?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5163 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5164 | return false; |
| 5165 | } |
| 5166 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5167 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5168 | case TSK_ExplicitInstantiationDefinition: |
| 5169 | switch (PrevTSK) { |
| 5170 | case TSK_Undeclared: |
| 5171 | case TSK_ImplicitInstantiation: |
| 5172 | // We're explicitly instantiating something that may have already been |
| 5173 | // implicitly instantiated; that's fine. |
| 5174 | return false; |
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_ExplicitSpecialization: |
| 5177 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 5178 | // For a given set of template parameters, if an explicit |
| 5179 | // instantiation of a template appears after a declaration of |
| 5180 | // an explicit specialization for that template, the explicit |
| 5181 | // instantiation has no effect. |
| 5182 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5183 | // 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] | 5184 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5185 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5186 | if (!getLangOptions().CPlusPlus0x) { |
| 5187 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5188 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5189 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5190 | diag::note_previous_template_specialization); |
| 5191 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5192 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5193 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5194 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5195 | case TSK_ExplicitInstantiationDeclaration: |
| 5196 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5197 | // were previously asked to suppress instantiations. That's fine. |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5198 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5199 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5200 | case TSK_ExplicitInstantiationDefinition: |
| 5201 | // C++0x [temp.spec]p5: |
| 5202 | // For a given template and a given set of template-arguments, |
| 5203 | // - an explicit instantiation definition shall appear at most once |
| 5204 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5205 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5206 | << PrevDecl; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5207 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5208 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5209 | HasNoEffect = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5210 | return false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5211 | } |
| 5212 | break; |
| 5213 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5214 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5215 | assert(false && "Missing specialization/instantiation case?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5216 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5217 | return false; |
| 5218 | } |
| 5219 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5220 | /// \brief Perform semantic analysis for the given dependent function |
| 5221 | /// template specialization. The only possible way to get a dependent |
| 5222 | /// function template specialization is with a friend declaration, |
| 5223 | /// like so: |
| 5224 | /// |
| 5225 | /// template <class T> void foo(T); |
| 5226 | /// template <class T> class A { |
| 5227 | /// friend void foo<>(T); |
| 5228 | /// }; |
| 5229 | /// |
| 5230 | /// There really isn't any useful analysis we can do here, so we |
| 5231 | /// just store the information. |
| 5232 | bool |
| 5233 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 5234 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 5235 | LookupResult &Previous) { |
| 5236 | // Remove anything from Previous that isn't a function template in |
| 5237 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5238 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5239 | LookupResult::Filter F = Previous.makeFilter(); |
| 5240 | while (F.hasNext()) { |
| 5241 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 5242 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5243 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 5244 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5245 | F.erase(); |
| 5246 | } |
| 5247 | F.done(); |
| 5248 | |
| 5249 | // Should this be diagnosed here? |
| 5250 | if (Previous.empty()) return true; |
| 5251 | |
| 5252 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 5253 | ExplicitTemplateArgs); |
| 5254 | return false; |
| 5255 | } |
| 5256 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5257 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5258 | /// specialization. |
| 5259 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5260 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5261 | /// explicit function template specialization. On successful completion, |
| 5262 | /// the function declaration \p FD will become a function template |
| 5263 | /// specialization. |
| 5264 | /// |
| 5265 | /// \param FD the function declaration, which will be updated to become a |
| 5266 | /// function template specialization. |
| 5267 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5268 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 5269 | /// if any. Note that this may be valid info even when 0 arguments are |
| 5270 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 5271 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5272 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5273 | /// \param PrevDecl the set of declarations that may be specialized by |
| 5274 | /// this function specialization. |
| 5275 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5276 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5277 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5278 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5279 | // The set of function template specializations that could match this |
| 5280 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5281 | UnresolvedSet<8> Candidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5282 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5283 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5284 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5285 | I != E; ++I) { |
| 5286 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 5287 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5288 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5289 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5290 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 5291 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5292 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5293 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5294 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5295 | // A trailing template-argument can be left unspecified in the |
| 5296 | // template-id naming an explicit function template specialization |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5297 | // provided it can be deduced from the function argument type. |
| 5298 | // Perform template argument deduction to determine whether we may be |
| 5299 | // specializing this template. |
| 5300 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5301 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5302 | FunctionDecl *Specialization = 0; |
| 5303 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5304 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5305 | FD->getType(), |
| 5306 | Specialization, |
| 5307 | Info)) { |
| 5308 | // FIXME: Template argument deduction failed; record why it failed, so |
| 5309 | // that we can provide nifty diagnostics. |
| 5310 | (void)TDK; |
| 5311 | continue; |
| 5312 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5313 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5314 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5315 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5316 | } |
| 5317 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5318 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5319 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5320 | UnresolvedSetIterator Result |
| 5321 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5322 | TPOC_Other, 0, FD->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5323 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5324 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5325 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5326 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5327 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5328 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5329 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5330 | |
| 5331 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 5332 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | abfb405 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 5333 | |
| 5334 | FunctionTemplateSpecializationInfo *SpecInfo |
| 5335 | = Specialization->getTemplateSpecializationInfo(); |
| 5336 | assert(SpecInfo && "Function template specialization info missing?"); |
| 5337 | { |
| 5338 | // Note: do not overwrite location info if previous template |
| 5339 | // specialization kind was explicit. |
| 5340 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
| 5341 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) |
| 5342 | Specialization->setLocation(FD->getLocation()); |
| 5343 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5344 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5345 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5346 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5347 | |
| 5348 | // If this is a friend declaration, then we're not really declaring |
| 5349 | // an explicit specialization. |
| 5350 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5351 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5352 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5353 | if (!isFriend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5354 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5355 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5356 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5357 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5358 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5359 | |
| 5360 | // C++ [temp.expl.spec]p6: |
| 5361 | // 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] | 5362 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5363 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5364 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5365 | // use occurs; no diagnostic is required. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5366 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5367 | if (!isFriend && |
| 5368 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5369 | TSK_ExplicitSpecialization, |
| 5370 | Specialization, |
| 5371 | SpecInfo->getTemplateSpecializationKind(), |
| 5372 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5373 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5374 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5375 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5376 | // Mark the prior declaration as an explicit specialization, so that later |
| 5377 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5378 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5379 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5380 | MarkUnusedFileScopedDecl(Specialization); |
| 5381 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5382 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5383 | // Turn the given function declaration into a function template |
| 5384 | // specialization, with the template arguments from the previous |
| 5385 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5386 | // Take copies of (semantic and syntactic) template argument lists. |
| 5387 | const TemplateArgumentList* TemplArgs = new (Context) |
| 5388 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
| 5389 | const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs |
| 5390 | ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5391 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5392 | TemplArgs, /*InsertPos=*/0, |
| 5393 | SpecInfo->getTemplateSpecializationKind(), |
| 5394 | TemplArgsAsWritten); |
| 5395 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5396 | // The "previous declaration" for this function template specialization is |
| 5397 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5398 | Previous.clear(); |
| 5399 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5400 | return false; |
| 5401 | } |
| 5402 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5403 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5404 | /// specialization. |
| 5405 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5406 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5407 | /// explicit member function specialization. On successful completion, |
| 5408 | /// the function declaration \p FD will become a member function |
| 5409 | /// specialization. |
| 5410 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5411 | /// \param Member the member declaration, which will be updated to become a |
| 5412 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5413 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5414 | /// \param Previous the set of declarations, one of which may be specialized |
| 5415 | /// by this function specialization; the set will be modified to contain the |
| 5416 | /// redeclared member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5417 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5418 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5419 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5420 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5421 | // Try to find the member we are instantiating. |
| 5422 | NamedDecl *Instantiation = 0; |
| 5423 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5424 | MemberSpecializationInfo *MSInfo = 0; |
| 5425 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5426 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5427 | // Nowhere to look anyway. |
| 5428 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5429 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5430 | I != E; ++I) { |
| 5431 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5432 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5433 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 5434 | Instantiation = Method; |
| 5435 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5436 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5437 | break; |
| 5438 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5439 | } |
| 5440 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5441 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5442 | VarDecl *PrevVar; |
| 5443 | if (Previous.isSingleResult() && |
| 5444 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5445 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5446 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5447 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5448 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5449 | } |
| 5450 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5451 | CXXRecordDecl *PrevRecord; |
| 5452 | if (Previous.isSingleResult() && |
| 5453 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 5454 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5455 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5456 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5457 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5458 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5459 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5460 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5461 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5462 | // specializations are always out-of-line, the caller will complain about |
| 5463 | // this mismatch later. |
| 5464 | return false; |
| 5465 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5466 | |
| 5467 | // If this is a friend, just bail out here before we start turning |
| 5468 | // things into explicit specializations. |
| 5469 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 5470 | // Preserve instantiation information. |
| 5471 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 5472 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 5473 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5474 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5475 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 5476 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 5477 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5478 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5479 | } |
| 5480 | |
| 5481 | Previous.clear(); |
| 5482 | Previous.addDecl(Instantiation); |
| 5483 | return false; |
| 5484 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5485 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5486 | // Make sure that this is a specialization of a member. |
| 5487 | if (!InstantiatedFrom) { |
| 5488 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 5489 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5490 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 5491 | return true; |
| 5492 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5493 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5494 | // C++ [temp.expl.spec]p6: |
| 5495 | // 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] | 5496 | // explicitly specialized then that spe- cialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5497 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5498 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5499 | // use occurs; no diagnostic is required. |
| 5500 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5501 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5502 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5503 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 5504 | TSK_ExplicitSpecialization, |
| 5505 | Instantiation, |
| 5506 | MSInfo->getTemplateSpecializationKind(), |
| 5507 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5508 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5509 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5510 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5511 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5512 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5513 | InstantiatedFrom, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5514 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5515 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5516 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 5517 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5518 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5519 | // the original declaration to note that it is an explicit specialization |
| 5520 | // (if it was previously an implicit instantiation). This latter step |
| 5521 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5522 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5523 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 5524 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 5525 | TSK_ImplicitInstantiation) { |
| 5526 | InstantiationFunction->setTemplateSpecializationKind( |
| 5527 | TSK_ExplicitSpecialization); |
| 5528 | InstantiationFunction->setLocation(Member->getLocation()); |
| 5529 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5530 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5531 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 5532 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5533 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5534 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5535 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5536 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 5537 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 5538 | TSK_ImplicitInstantiation) { |
| 5539 | InstantiationVar->setTemplateSpecializationKind( |
| 5540 | TSK_ExplicitSpecialization); |
| 5541 | InstantiationVar->setLocation(Member->getLocation()); |
| 5542 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5543 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5544 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 5545 | cast<VarDecl>(InstantiatedFrom), |
| 5546 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5547 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5548 | } else { |
| 5549 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5550 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 5551 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 5552 | TSK_ImplicitInstantiation) { |
| 5553 | InstantiationClass->setTemplateSpecializationKind( |
| 5554 | TSK_ExplicitSpecialization); |
| 5555 | InstantiationClass->setLocation(Member->getLocation()); |
| 5556 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5557 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5558 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5559 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5560 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5561 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5562 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5563 | // Save the caller the trouble of having to figure out which declaration |
| 5564 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5565 | Previous.clear(); |
| 5566 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5567 | return false; |
| 5568 | } |
| 5569 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5570 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5571 | /// |
| 5572 | /// \returns true if a serious error occurs, false otherwise. |
| 5573 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5574 | SourceLocation InstLoc, |
| 5575 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5576 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5577 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5578 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5579 | if (CurContext->isRecord()) { |
| 5580 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5581 | << D; |
| 5582 | return true; |
| 5583 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5584 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5585 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5586 | // An explicit instantiation shall appear in an enclosing namespace of its |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5587 | // template. |
| 5588 | // |
| 5589 | // 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] | 5590 | if (S.getLangOptions().CPlusPlus0x && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5591 | !CurContext->Encloses(OrigContext)) { |
| 5592 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5593 | S.Diag(InstLoc, |
| 5594 | S.getLangOptions().CPlusPlus0x? |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5595 | diag::err_explicit_instantiation_out_of_scope |
| 5596 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5597 | << D << NS; |
| 5598 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5599 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5600 | S.getLangOptions().CPlusPlus0x? |
| 5601 | diag::err_explicit_instantiation_must_be_global |
| 5602 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5603 | << D; |
| 5604 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5605 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5606 | } |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5607 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5608 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5609 | // If the name declared in the explicit instantiation is an unqualified |
| 5610 | // name, the explicit instantiation shall appear in the namespace where |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5611 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 5612 | // namespace from its enclosing namespace set. |
| 5613 | if (WasQualifiedName) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5614 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5615 | |
| 5616 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5617 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5618 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5619 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5620 | S.getLangOptions().CPlusPlus0x? |
| 5621 | diag::err_explicit_instantiation_unqualified_wrong_namespace |
| 5622 | : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5623 | << D << OrigContext; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5624 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5625 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5626 | } |
| 5627 | |
| 5628 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5629 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5630 | if (!SS.isSet()) |
| 5631 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5632 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5633 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5634 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5635 | // or a static data member of a class template specialization, the name of |
| 5636 | // the class template specialization in the qualified-id for the member |
| 5637 | // name shall be a simple-template-id. |
| 5638 | // |
| 5639 | // C++98 has the same restriction, just worded differently. |
| 5640 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5641 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5642 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5643 | if (isa<TemplateSpecializationType>(T)) |
| 5644 | return true; |
| 5645 | |
| 5646 | return false; |
| 5647 | } |
| 5648 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5649 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5650 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5651 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5652 | SourceLocation ExternLoc, |
| 5653 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5654 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5655 | SourceLocation KWLoc, |
| 5656 | const CXXScopeSpec &SS, |
| 5657 | TemplateTy TemplateD, |
| 5658 | SourceLocation TemplateNameLoc, |
| 5659 | SourceLocation LAngleLoc, |
| 5660 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5661 | SourceLocation RAngleLoc, |
| 5662 | AttributeList *Attr) { |
| 5663 | // Find the class template we're specializing |
| 5664 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5665 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5666 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5667 | |
| 5668 | // Check that the specialization uses the same tag kind as the |
| 5669 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5670 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5671 | assert(Kind != TTK_Enum && |
| 5672 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5673 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5674 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5675 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5676 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5677 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5678 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5679 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5680 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5681 | diag::note_previous_use); |
| 5682 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5683 | } |
| 5684 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5685 | // C++0x [temp.explicit]p2: |
| 5686 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5687 | // definition and an explicit instantiation declaration. An explicit |
| 5688 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5689 | TemplateSpecializationKind TSK |
| 5690 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5691 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5692 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5693 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5694 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5695 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5696 | |
| 5697 | // Check that the template argument list is well-formed for this |
| 5698 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5699 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5700 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5701 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5702 | return true; |
| 5703 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5704 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5705 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5706 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5707 | // Find the class template specialization declaration that |
| 5708 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5709 | void *InsertPos = 0; |
| 5710 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5711 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5712 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5713 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5714 | TemplateSpecializationKind PrevDecl_TSK |
| 5715 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 5716 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5717 | // C++0x [temp.explicit]p2: |
| 5718 | // [...] An explicit instantiation shall appear in an enclosing |
| 5719 | // namespace of its template. [...] |
| 5720 | // |
| 5721 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5722 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 5723 | SS.isSet())) |
| 5724 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5725 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5726 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5727 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5728 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5729 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5730 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5731 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5732 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5733 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5734 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5735 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5736 | // Even though HasNoEffect == true means that this explicit instantiation |
| 5737 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 5738 | |
| 5739 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 5740 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5741 | // Since the only prior class template specialization with these |
| 5742 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5743 | // declaration node as our own, updating the source location |
| 5744 | // for the template name to reflect our new declaration. |
| 5745 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5746 | Specialization = PrevDecl; |
| 5747 | Specialization->setLocation(TemplateNameLoc); |
| 5748 | PrevDecl = 0; |
| 5749 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5750 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5751 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5752 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5753 | // Create a new class template specialization declaration node for |
| 5754 | // this explicit specialization. |
| 5755 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5756 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5757 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5758 | KWLoc, TemplateNameLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5759 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5760 | Converted.data(), |
| 5761 | Converted.size(), |
| 5762 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5763 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5764 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5765 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5766 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5767 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5768 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5769 | } |
| 5770 | |
| 5771 | // Build the fully-sugared type for this explicit instantiation as |
| 5772 | // the user wrote in the explicit instantiation itself. This means |
| 5773 | // that we'll pretty-print the type retrieved from the |
| 5774 | // specialization's declaration the way that the user actually wrote |
| 5775 | // the explicit instantiation, rather than formatting the name based |
| 5776 | // on the "canonical" representation used to store the template |
| 5777 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5778 | TypeSourceInfo *WrittenTy |
| 5779 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5780 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5781 | Context.getTypeDeclType(Specialization)); |
| 5782 | Specialization->setTypeAsWritten(WrittenTy); |
| 5783 | TemplateArgsIn.release(); |
| 5784 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5785 | // Set source locations for keywords. |
| 5786 | Specialization->setExternLoc(ExternLoc); |
| 5787 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 5788 | |
| 5789 | // Add the explicit instantiation into its lexical context. However, |
| 5790 | // since explicit instantiations are never found by name lookup, we |
| 5791 | // just put it into the declaration context directly. |
| 5792 | Specialization->setLexicalDeclContext(CurContext); |
| 5793 | CurContext->addDecl(Specialization); |
| 5794 | |
| 5795 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 5796 | if (HasNoEffect) { |
| 5797 | // Set the template specialization kind. |
| 5798 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5799 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5800 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5801 | |
| 5802 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5803 | // A definition of a class template or class member template |
| 5804 | // shall be in scope at the point of the explicit instantiation of |
| 5805 | // the class template or class member template. |
| 5806 | // |
| 5807 | // This check comes when we actually try to perform the |
| 5808 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5809 | ClassTemplateSpecializationDecl *Def |
| 5810 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5811 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5812 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5813 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5814 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5815 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5816 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 5817 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5818 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5819 | // Instantiate the members of this class template specialization. |
| 5820 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5821 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5822 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 5823 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 5824 | |
| 5825 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 5826 | // TSK_ExplicitInstantiationDefinition |
| 5827 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 5828 | TSK == TSK_ExplicitInstantiationDefinition) |
| 5829 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5830 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5831 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5832 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5833 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5834 | // Set the template specialization kind. |
| 5835 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5836 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5837 | } |
| 5838 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5839 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5840 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5841 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5842 | SourceLocation ExternLoc, |
| 5843 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5844 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5845 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5846 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5847 | IdentifierInfo *Name, |
| 5848 | SourceLocation NameLoc, |
| 5849 | AttributeList *Attr) { |
| 5850 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 5851 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5852 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5853 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5854 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
| 5855 | MultiTemplateParamsArg(*this, 0, 0), |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 5856 | Owned, IsDependent, false, false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5857 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5858 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 5859 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5860 | if (!TagD) |
| 5861 | return true; |
| 5862 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5863 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5864 | if (Tag->isEnum()) { |
| 5865 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 5866 | << Context.getTypeDeclType(Tag); |
| 5867 | return true; |
| 5868 | } |
| 5869 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 5870 | if (Tag->isInvalidDecl()) |
| 5871 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5872 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5873 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 5874 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 5875 | if (!Pattern) { |
| 5876 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 5877 | << Context.getTypeDeclType(Record); |
| 5878 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 5879 | return true; |
| 5880 | } |
| 5881 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5882 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5883 | // If the explicit instantiation is for a class or member class, the |
| 5884 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5885 | // simple-template-id. |
| 5886 | // |
| 5887 | // C++98 has the same restriction, just worded differently. |
| 5888 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5889 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5890 | << Record << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5891 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5892 | // C++0x [temp.explicit]p2: |
| 5893 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5894 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5895 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 5896 | TemplateSpecializationKind TSK |
| 5897 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5898 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5899 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5900 | // C++0x [temp.explicit]p2: |
| 5901 | // [...] An explicit instantiation shall appear in an enclosing |
| 5902 | // namespace of its template. [...] |
| 5903 | // |
| 5904 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5905 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5906 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5907 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5908 | CXXRecordDecl *PrevDecl |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5909 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5910 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5911 | PrevDecl = Record; |
| 5912 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5913 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5914 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5915 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5916 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5917 | PrevDecl, |
| 5918 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5919 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5920 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5921 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5922 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5923 | return TagD; |
| 5924 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5925 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5926 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5927 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5928 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5929 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5930 | // 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] | 5931 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5932 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5933 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5934 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 5935 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 5936 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5937 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 5938 | << Pattern; |
| 5939 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5940 | } else { |
| 5941 | if (InstantiateClass(NameLoc, Record, Def, |
| 5942 | getTemplateInstantiationArgs(Record), |
| 5943 | TSK)) |
| 5944 | return true; |
| 5945 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5946 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5947 | if (!RecordDef) |
| 5948 | return true; |
| 5949 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5950 | } |
| 5951 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5952 | // Instantiate all of the members of the class. |
| 5953 | InstantiateClassMembers(NameLoc, RecordDef, |
| 5954 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5955 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5956 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 5957 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 5958 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5959 | // FIXME: We don't have any representation for explicit instantiations of |
| 5960 | // member classes. Such a representation is not needed for compilation, but it |
| 5961 | // should be available for clients that want to see all of the declarations in |
| 5962 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5963 | return TagD; |
| 5964 | } |
| 5965 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5966 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 5967 | SourceLocation ExternLoc, |
| 5968 | SourceLocation TemplateLoc, |
| 5969 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5970 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5971 | // TODO: check if/when DNInfo should replace Name. |
| 5972 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 5973 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5974 | if (!Name) { |
| 5975 | if (!D.isInvalidType()) |
| 5976 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 5977 | diag::err_explicit_instantiation_requires_name) |
| 5978 | << D.getDeclSpec().getSourceRange() |
| 5979 | << D.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5980 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5981 | return true; |
| 5982 | } |
| 5983 | |
| 5984 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 5985 | // we find one that is. |
| 5986 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5987 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5988 | S = S->getParent(); |
| 5989 | |
| 5990 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 5991 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 5992 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5993 | if (R.isNull()) |
| 5994 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5995 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5996 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 5997 | // Cannot explicitly instantiate a typedef. |
| 5998 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 5999 | << Name; |
| 6000 | return true; |
| 6001 | } |
| 6002 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6003 | // C++0x [temp.explicit]p1: |
| 6004 | // [...] An explicit instantiation of a function template shall not use the |
| 6005 | // inline or constexpr specifiers. |
| 6006 | // Presumably, this also applies to member functions of class templates as |
| 6007 | // well. |
| 6008 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6009 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6010 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6011 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6012 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6013 | // FIXME: check for constexpr specifier. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6014 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6015 | // C++0x [temp.explicit]p2: |
| 6016 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6017 | // definition and an explicit instantiation declaration. An explicit |
| 6018 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6019 | TemplateSpecializationKind TSK |
| 6020 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6021 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6022 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6023 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6024 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6025 | |
| 6026 | if (!R->isFunctionType()) { |
| 6027 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6028 | // A [...] static data member of a class template can be explicitly |
| 6029 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6030 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6031 | if (Previous.isAmbiguous()) |
| 6032 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6033 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6034 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6035 | if (!Prev || !Prev->isStaticDataMember()) { |
| 6036 | // We expect to see a data data member here. |
| 6037 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 6038 | << Name; |
| 6039 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6040 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6041 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6042 | return true; |
| 6043 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6044 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6045 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 6046 | // FIXME: Check for explicit specialization? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6047 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6048 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 6049 | << Prev; |
| 6050 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 6051 | // FIXME: Can we provide a note showing where this was declared? |
| 6052 | return true; |
| 6053 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6054 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6055 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6056 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6057 | // or a static data member of a class template specialization, the name of |
| 6058 | // the class template specialization in the qualified-id for the member |
| 6059 | // name shall be a simple-template-id. |
| 6060 | // |
| 6061 | // C++98 has the same restriction, just worded differently. |
| 6062 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6063 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6064 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6065 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6066 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6067 | // Check the scope of this explicit instantiation. |
| 6068 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6069 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6070 | // Verify that it is okay to explicitly instantiate here. |
| 6071 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 6072 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6073 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6074 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6075 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6076 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6077 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6078 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6079 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6080 | return (Decl*) 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6081 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6082 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6083 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6084 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6085 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6086 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6087 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6088 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6089 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6090 | |
| 6091 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6092 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6093 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6094 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6095 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 6096 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6097 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 6098 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6099 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 6100 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6101 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6102 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6103 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 6104 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6105 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6106 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6107 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6108 | // A [...] function [...] can be explicitly instantiated from its template. |
| 6109 | // A member function [...] of a class template can be explicitly |
| 6110 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6111 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6112 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6113 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6114 | P != PEnd; ++P) { |
| 6115 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6116 | if (!HasExplicitTemplateArgs) { |
| 6117 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 6118 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 6119 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6120 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6121 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6122 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 6123 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6124 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6125 | } |
| 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 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 6129 | if (!FunTmpl) |
| 6130 | continue; |
| 6131 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6132 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6133 | FunctionDecl *Specialization = 0; |
| 6134 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6135 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6136 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6137 | R, Specialization, Info)) { |
| 6138 | // FIXME: Keep track of almost-matches? |
| 6139 | (void)TDK; |
| 6140 | continue; |
| 6141 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6142 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6143 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6144 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6145 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6146 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6147 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6148 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6149 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6150 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 6151 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 6152 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6153 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6154 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6155 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6156 | |
| 6157 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 6158 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6159 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6160 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6161 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6162 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 6163 | << Specialization |
| 6164 | << (Specialization->getTemplateSpecializationKind() == |
| 6165 | TSK_ExplicitSpecialization); |
| 6166 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 6167 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6168 | } |
| 6169 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6170 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6171 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 6172 | PrevDecl = Specialization; |
| 6173 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6174 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6175 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6176 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6177 | PrevDecl, |
| 6178 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6179 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6180 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6181 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6182 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6183 | // FIXME: We may still want to build some representation of this |
| 6184 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6185 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6186 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6187 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 6188 | |
| 6189 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6190 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6191 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6192 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6193 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6194 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6195 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6196 | // or a static data member of a class template specialization, the name of |
| 6197 | // the class template specialization in the qualified-id for the member |
| 6198 | // name shall be a simple-template-id. |
| 6199 | // |
| 6200 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6201 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6202 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6203 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6204 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6205 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6206 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6207 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6208 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6209 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6210 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6211 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6212 | D.getIdentifierLoc(), |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6213 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6214 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6215 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6216 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6217 | } |
| 6218 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6219 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6220 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 6221 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 6222 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 6223 | // This has to hold, because SS is expected to be defined. |
| 6224 | assert(Name && "Expected a name in a dependent tag"); |
| 6225 | |
| 6226 | NestedNameSpecifier *NNS |
| 6227 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6228 | if (!NNS) |
| 6229 | return true; |
| 6230 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6231 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 6232 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6233 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 6234 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6235 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6236 | return true; |
| 6237 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6238 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6239 | // Create the resulting type. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6240 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6241 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 6242 | |
| 6243 | // Create type-source location information for this type. |
| 6244 | TypeLocBuilder TLB; |
| 6245 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
| 6246 | TL.setKeywordLoc(TagLoc); |
| 6247 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6248 | TL.setNameLoc(NameLoc); |
| 6249 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6250 | } |
| 6251 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6252 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6253 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6254 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6255 | SourceLocation IdLoc) { |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6256 | if (SS.isInvalid()) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6257 | return true; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6258 | |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6259 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 6260 | !getLangOptions().CPlusPlus0x) |
| 6261 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6262 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6263 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6264 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6265 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 6266 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 6267 | if (T.isNull()) |
| 6268 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6269 | |
| 6270 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6271 | if (isa<DependentNameType>(T)) { |
| 6272 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6273 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6274 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6275 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6276 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6277 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6278 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6279 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6280 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6281 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6282 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6283 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6284 | } |
| 6285 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6286 | TypeResult |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6287 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6288 | const CXXScopeSpec &SS, |
| 6289 | SourceLocation TemplateLoc, |
| 6290 | TemplateTy TemplateIn, |
| 6291 | SourceLocation TemplateNameLoc, |
| 6292 | SourceLocation LAngleLoc, |
| 6293 | ASTTemplateArgsPtr TemplateArgsIn, |
| 6294 | SourceLocation RAngleLoc) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6295 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 6296 | !getLangOptions().CPlusPlus0x) |
| 6297 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6298 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6299 | |
| 6300 | // Translate the parser's template argument list in our AST format. |
| 6301 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 6302 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 6303 | |
| 6304 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6305 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 6306 | // Construct a dependent template specialization type. |
| 6307 | assert(DTN && "dependent template has non-dependent name?"); |
| 6308 | assert(DTN->getQualifier() |
| 6309 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 6310 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 6311 | DTN->getQualifier(), |
| 6312 | DTN->getIdentifier(), |
| 6313 | TemplateArgs); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6314 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6315 | // Create source-location information for this type. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6316 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6317 | DependentTemplateSpecializationTypeLoc SpecTL |
| 6318 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6319 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6320 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6321 | SpecTL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 6322 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6323 | SpecTL.setNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6324 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6325 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6326 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 6327 | } |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6328 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6329 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 6330 | if (T.isNull()) |
| 6331 | return true; |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6332 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6333 | // Provide source-location information for the template specialization |
| 6334 | // type. |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6335 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6336 | TemplateSpecializationTypeLoc SpecTL |
| 6337 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
| 6338 | |
| 6339 | // FIXME: No place to set the location of the 'template' keyword! |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6340 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6341 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6342 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6343 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6344 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 6345 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6346 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 6347 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 6348 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6349 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6350 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6351 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 6352 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 6353 | } |
| 6354 | |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6355 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6356 | /// \brief Build the type that describes a C++ typename specifier, |
| 6357 | /// e.g., "typename T::type". |
| 6358 | QualType |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6359 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 6360 | SourceLocation KeywordLoc, |
| 6361 | NestedNameSpecifierLoc QualifierLoc, |
| 6362 | const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6363 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6364 | CXXScopeSpec SS; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6365 | SS.Adopt(QualifierLoc); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6366 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6367 | DeclContext *Ctx = computeDeclContext(SS); |
| 6368 | if (!Ctx) { |
| 6369 | // If the nested-name-specifier is dependent and couldn't be |
| 6370 | // resolved to a type, build a typename type. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6371 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 6372 | return Context.getDependentNameType(Keyword, |
| 6373 | QualifierLoc.getNestedNameSpecifier(), |
| 6374 | &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 6375 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6376 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6377 | // If the nested-name-specifier refers to the current instantiation, |
| 6378 | // the "typename" keyword itself is superfluous. In C++03, the |
| 6379 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 6380 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 6381 | // 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] | 6382 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6383 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 6384 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6385 | |
| 6386 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6387 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6388 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6389 | unsigned DiagID = 0; |
| 6390 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6391 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6392 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6393 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6394 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6395 | |
| 6396 | case LookupResult::FoundUnresolvedValue: { |
| 6397 | // We found a using declaration that is a value. Most likely, the using |
| 6398 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6399 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6400 | IILoc); |
| 6401 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 6402 | << Name << Ctx << FullRange; |
| 6403 | if (UnresolvedUsingValueDecl *Using |
| 6404 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6405 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6406 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 6407 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 6408 | } |
| 6409 | } |
| 6410 | // Fall through to create a dependent typename type, from which we can recover |
| 6411 | // better. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6412 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 6413 | case LookupResult::NotFoundInCurrentInstantiation: |
| 6414 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6415 | return Context.getDependentNameType(Keyword, |
| 6416 | QualifierLoc.getNestedNameSpecifier(), |
| 6417 | &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6418 | |
| 6419 | case LookupResult::Found: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6420 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6421 | // We found a type. Build an ElaboratedType, since the |
| 6422 | // typename-specifier was just sugar. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6423 | return Context.getElaboratedType(ETK_Typename, |
| 6424 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6425 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6426 | } |
| 6427 | |
| 6428 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6429 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6430 | break; |
| 6431 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6432 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6433 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6434 | return QualType(); |
| 6435 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6436 | case LookupResult::FoundOverloaded: |
| 6437 | DiagID = diag::err_typename_nested_not_type; |
| 6438 | Referenced = *Result.begin(); |
| 6439 | break; |
| 6440 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 6441 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6442 | return QualType(); |
| 6443 | } |
| 6444 | |
| 6445 | // If we get here, it's because name lookup did not find a |
| 6446 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6447 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6448 | IILoc); |
| 6449 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6450 | if (Referenced) |
| 6451 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 6452 | << Name; |
| 6453 | return QualType(); |
| 6454 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6455 | |
| 6456 | namespace { |
| 6457 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 6458 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6459 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6460 | SourceLocation Loc; |
| 6461 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6462 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6463 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 6464 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6465 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6466 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6467 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6468 | DeclarationName Entity) |
| 6469 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6470 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6471 | |
| 6472 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6473 | /// transformed. |
| 6474 | /// |
| 6475 | /// For the purposes of type reconstruction, a type has already been |
| 6476 | /// transformed if it is NULL or if it is not dependent. |
| 6477 | bool AlreadyTransformed(QualType T) { |
| 6478 | return T.isNull() || !T->isDependentType(); |
| 6479 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6480 | |
| 6481 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6482 | /// rebuilt. |
| 6483 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6484 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6485 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 6486 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6487 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6488 | /// \brief Sets the "base" location and entity when that |
| 6489 | /// information is known based on another transformation. |
| 6490 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 6491 | this->Loc = Loc; |
| 6492 | this->Entity = Entity; |
| 6493 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6494 | }; |
| 6495 | } |
| 6496 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6497 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 6498 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6499 | /// 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] | 6500 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6501 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6502 | /// partial specialization thereof). This routine will rebuild that type now |
| 6503 | /// that we have entered the declarator's scope, which may produce different |
| 6504 | /// canonical types, e.g., |
| 6505 | /// |
| 6506 | /// \code |
| 6507 | /// template<typename T> |
| 6508 | /// struct X { |
| 6509 | /// typedef T* pointer; |
| 6510 | /// pointer data(); |
| 6511 | /// }; |
| 6512 | /// |
| 6513 | /// template<typename T> |
| 6514 | /// typename X<T>::pointer X<T>::data() { ... } |
| 6515 | /// \endcode |
| 6516 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 6517 | /// 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] | 6518 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 6519 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6520 | /// 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] | 6521 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 6522 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6523 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 6524 | SourceLocation Loc, |
| 6525 | DeclarationName Name) { |
| 6526 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6527 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6528 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6529 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 6530 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 6531 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6532 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6533 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6534 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 6535 | DeclarationName()); |
| 6536 | return Rebuilder.TransformExpr(E); |
| 6537 | } |
| 6538 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6539 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6540 | if (SS.isInvalid()) |
| 6541 | return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6542 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6543 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6544 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 6545 | DeclarationName()); |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6546 | NestedNameSpecifierLoc Rebuilt |
| 6547 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 6548 | if (!Rebuilt) |
| 6549 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6550 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6551 | SS.Adopt(Rebuilt); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6552 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6553 | } |
| 6554 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6555 | /// \brief Produces a formatted string that describes the binding of |
| 6556 | /// template parameters to template arguments. |
| 6557 | std::string |
| 6558 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6559 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6560 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6561 | } |
| 6562 | |
| 6563 | std::string |
| 6564 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6565 | const TemplateArgument *Args, |
| 6566 | unsigned NumArgs) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6567 | llvm::SmallString<128> Str; |
| 6568 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6569 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6570 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6571 | return std::string(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6572 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6573 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6574 | if (I >= NumArgs) |
| 6575 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6576 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6577 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6578 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6579 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6580 | Out << ", "; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6581 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6582 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6583 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6584 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6585 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6586 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6587 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6588 | Out << " = "; |
| 6589 | Args[I].print(Context.PrintingPolicy, Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6590 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6591 | |
| 6592 | Out << ']'; |
| 6593 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6594 | } |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 6595 | |
| 6596 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) { |
| 6597 | if (!FD) |
| 6598 | return; |
| 6599 | FD->setLateTemplateParsed(Flag); |
| 6600 | } |
| 6601 | |
| 6602 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 6603 | DeclContext *DC = CurContext; |
| 6604 | |
| 6605 | while (DC) { |
| 6606 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 6607 | const FunctionDecl *FD = RD->isLocalClass(); |
| 6608 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 6609 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 6610 | return false; |
| 6611 | |
| 6612 | DC = DC->getParent(); |
| 6613 | } |
| 6614 | return false; |
| 6615 | } |