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(); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 297 | Found.clear(); |
| 298 | if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(), |
| 299 | Found.getLookupKind(), S, &SS, |
| 300 | LookupCtx, false, |
| 301 | CTC_CXXCasts)) { |
| 302 | Found.setLookupName(Corrected.getCorrection()); |
| 303 | if (Corrected.getCorrectionDecl()) |
| 304 | Found.addDecl(Corrected.getCorrectionDecl()); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 305 | FilterAcceptableTemplateNames(Found); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 306 | if (!Found.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 307 | std::string CorrectedStr(Corrected.getAsString(getLangOptions())); |
| 308 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions())); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 309 | if (LookupCtx) |
| 310 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 311 | << Name << LookupCtx << CorrectedQuotedStr << SS.getRange() |
| 312 | << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 313 | else |
| 314 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 315 | << Name << CorrectedQuotedStr |
| 316 | << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 317 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) |
| 318 | Diag(Template->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 319 | << CorrectedQuotedStr; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 320 | } |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 321 | } else { |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 322 | Found.setLookupName(Name); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 326 | FilterAcceptableTemplateNames(Found); |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 327 | if (Found.empty()) { |
| 328 | if (isDependent) |
| 329 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 330 | return; |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 331 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 332 | |
| 333 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
| 334 | // C++ [basic.lookup.classref]p1: |
| 335 | // [...] If the lookup in the class of the object expression finds a |
| 336 | // template, the name is also looked up in the context of the entire |
| 337 | // postfix-expression and [...] |
| 338 | // |
| 339 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 340 | LookupOrdinaryName); |
| 341 | LookupName(FoundOuter, S); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 342 | FilterAcceptableTemplateNames(FoundOuter); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 343 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 344 | if (FoundOuter.empty()) { |
| 345 | // - if the name is not found, the name found in the class of the |
| 346 | // object expression is used, otherwise |
Douglas Gregor | a6d1e76 | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 347 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 348 | FoundOuter.isAmbiguous()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 349 | // - if the name is found in the context of the entire |
| 350 | // postfix-expression and does not name a class template, the name |
| 351 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | a6d1e76 | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 352 | FoundOuter.clear(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 353 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 354 | // - if the name found is a class template, it must refer to the same |
| 355 | // entity as the one found in the class of the object expression, |
| 356 | // otherwise the program is ill-formed. |
| 357 | if (!Found.isSingleResult() || |
| 358 | Found.getFoundDecl()->getCanonicalDecl() |
| 359 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 360 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 21d07e4 | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 361 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 362 | << Found.getLookupName() |
| 363 | << ObjectType; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 364 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 365 | diag::note_ambig_member_ref_object_type) |
| 366 | << ObjectType; |
| 367 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 368 | diag::note_ambig_member_ref_scope); |
| 369 | |
| 370 | // Recover by taking the template that we found in the object |
| 371 | // expression's type. |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 377 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 378 | /// was just parsed. This is only possible with an explicit scope |
| 379 | /// specifier naming a dependent type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 380 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 381 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 382 | const DeclarationNameInfo &NameInfo, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 383 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 384 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 385 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 386 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 387 | if (!isAddressOfOperand && |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 388 | isa<CXXMethodDecl>(DC) && |
| 389 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 390 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 391 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 392 | // Since the 'this' expression is synthesized, we don't need to |
| 393 | // perform the double-lookup check. |
| 394 | NamedDecl *FirstQualifierInScope = 0; |
| 395 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 396 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 397 | /*This*/ 0, ThisType, |
| 398 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 399 | /*Op*/ SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 400 | SS.getWithLocInContext(Context), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 401 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 402 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 403 | TemplateArgs)); |
| 404 | } |
| 405 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 406 | return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 407 | } |
| 408 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 409 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 410 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 411 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 412 | const TemplateArgumentListInfo *TemplateArgs) { |
| 413 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 414 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 415 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 416 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 419 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 420 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 421 | /// declaration at location Loc. Returns true to indicate that this is |
| 422 | /// an error, and false otherwise. |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 423 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 424 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 425 | |
| 426 | // Microsoft Visual C++ permits template parameters to be shadowed. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 427 | if (getLangOptions().MicrosoftExt) |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 428 | return; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 429 | |
| 430 | // C++ [temp.local]p4: |
| 431 | // A template-parameter shall not be redeclared within its |
| 432 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 434 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 435 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 436 | return; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 439 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 440 | /// the parameter D to reference the templated declaration and return a pointer |
| 441 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 442 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 443 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 444 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 445 | return Temp; |
| 446 | } |
| 447 | return 0; |
| 448 | } |
| 449 | |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 450 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 451 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 452 | assert(Kind == Template && |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 453 | "Only template template arguments can be pack expansions here"); |
| 454 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 455 | "Template template argument pack expansion without packs"); |
| 456 | ParsedTemplateArgument Result(*this); |
| 457 | Result.EllipsisLoc = EllipsisLoc; |
| 458 | return Result; |
| 459 | } |
| 460 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 461 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 462 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 463 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 464 | switch (Arg.getKind()) { |
| 465 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 466 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 467 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 468 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 469 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 470 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 471 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 472 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 473 | case ParsedTemplateArgument::NonType: { |
| 474 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 475 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 476 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 477 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 478 | case ParsedTemplateArgument::Template: { |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 479 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 480 | TemplateArgument TArg; |
| 481 | if (Arg.getEllipsisLoc().isValid()) |
| 482 | TArg = TemplateArgument(Template, llvm::Optional<unsigned int>()); |
| 483 | else |
| 484 | TArg = Template; |
| 485 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 486 | Arg.getScopeSpec().getWithLocInContext( |
| 487 | SemaRef.Context), |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 488 | Arg.getLocation(), |
| 489 | Arg.getEllipsisLoc()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 490 | } |
| 491 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 492 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 493 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 494 | return TemplateArgumentLoc(); |
| 495 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 496 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 497 | /// \brief Translates template arguments as provided by the parser |
| 498 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 499 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 500 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 501 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 502 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 503 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 504 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 505 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 506 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 507 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 508 | /// the keyword "typename" was used to declare the type parameter |
| 509 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 510 | /// "class" or "typename" keyword. ParamName is the name of the |
| 511 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 512 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 513 | /// If the type parameter has a default argument, it will be added |
| 514 | /// later via ActOnTypeParameterDefault. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 515 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
| 516 | SourceLocation EllipsisLoc, |
| 517 | SourceLocation KeyLoc, |
| 518 | IdentifierInfo *ParamName, |
| 519 | SourceLocation ParamNameLoc, |
| 520 | unsigned Depth, unsigned Position, |
| 521 | SourceLocation EqualLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 522 | ParsedType DefaultArg) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 523 | assert(S->isTemplateParamScope() && |
| 524 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 525 | bool Invalid = false; |
| 526 | |
| 527 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 528 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 529 | LookupOrdinaryName, |
| 530 | ForRedeclaration); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 531 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 532 | DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl); |
| 533 | PrevDecl = 0; |
| 534 | } |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 537 | SourceLocation Loc = ParamNameLoc; |
| 538 | if (!ParamName) |
| 539 | Loc = KeyLoc; |
| 540 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 541 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 542 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 543 | KeyLoc, Loc, Depth, Position, ParamName, |
| 544 | Typename, Ellipsis); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 545 | Param->setAccess(AS_public); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 546 | if (Invalid) |
| 547 | Param->setInvalidDecl(); |
| 548 | |
| 549 | if (ParamName) { |
| 550 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 551 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 552 | IdResolver.AddDecl(Param); |
| 553 | } |
| 554 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 555 | // C++0x [temp.param]p9: |
| 556 | // A default template-argument may be specified for any kind of |
| 557 | // template-parameter that is not a template parameter pack. |
| 558 | if (DefaultArg && Ellipsis) { |
| 559 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 560 | DefaultArg = ParsedType(); |
| 561 | } |
| 562 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 563 | // Handle the default argument, if provided. |
| 564 | if (DefaultArg) { |
| 565 | TypeSourceInfo *DefaultTInfo; |
| 566 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 567 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 568 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 569 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 570 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 571 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 572 | UPPC_DefaultArgument)) |
| 573 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 574 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 575 | // Check the template argument itself. |
| 576 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 577 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 578 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 579 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 580 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 581 | Param->setDefaultArgument(DefaultTInfo, false); |
| 582 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 583 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 584 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 587 | /// \brief Check that the type of a non-type template parameter is |
| 588 | /// well-formed. |
| 589 | /// |
| 590 | /// \returns the (possibly-promoted) parameter type if valid; |
| 591 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 592 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 593 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 594 | // We don't allow variably-modified types as the type of non-type template |
| 595 | // parameters. |
| 596 | if (T->isVariablyModifiedType()) { |
| 597 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 598 | << T; |
| 599 | return QualType(); |
| 600 | } |
| 601 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 602 | // C++ [temp.param]p4: |
| 603 | // |
| 604 | // A non-type template-parameter shall have one of the following |
| 605 | // (optionally cv-qualified) types: |
| 606 | // |
| 607 | // -- integral or enumeration type, |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 608 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 609 | // -- pointer to object or pointer to function, |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 610 | T->isPointerType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 611 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 612 | T->isReferenceType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 613 | // -- pointer to member, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 614 | T->isMemberPointerType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 615 | // -- std::nullptr_t. |
| 616 | T->isNullPtrType() || |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 617 | // If T is a dependent type, we can't do the check now, so we |
| 618 | // assume that it is well-formed. |
| 619 | T->isDependentType()) |
| 620 | return T; |
| 621 | // C++ [temp.param]p8: |
| 622 | // |
| 623 | // A non-type template-parameter of type "array of T" or |
| 624 | // "function returning T" is adjusted to be of type "pointer to |
| 625 | // T" or "pointer to function returning T", respectively. |
| 626 | else if (T->isArrayType()) |
| 627 | // FIXME: Keep the type prior to promotion? |
| 628 | return Context.getArrayDecayedType(T); |
| 629 | else if (T->isFunctionType()) |
| 630 | // FIXME: Keep the type prior to promotion? |
| 631 | return Context.getPointerType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 632 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 633 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 634 | << T; |
| 635 | |
| 636 | return QualType(); |
| 637 | } |
| 638 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 639 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 640 | unsigned Depth, |
| 641 | unsigned Position, |
| 642 | SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 643 | Expr *Default) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 644 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 645 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 646 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 647 | assert(S->isTemplateParamScope() && |
| 648 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 649 | bool Invalid = false; |
| 650 | |
| 651 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 652 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 653 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 654 | LookupOrdinaryName, |
| 655 | ForRedeclaration); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 656 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 657 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 658 | PrevDecl = 0; |
| 659 | } |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 662 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 663 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 664 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 665 | Invalid = true; |
| 666 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 667 | |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 668 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 669 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 670 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 671 | D.getSourceRange().getBegin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 672 | D.getIdentifierLoc(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 673 | Depth, Position, ParamName, T, |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 674 | IsParameterPack, TInfo); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 675 | Param->setAccess(AS_public); |
| 676 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 677 | if (Invalid) |
| 678 | Param->setInvalidDecl(); |
| 679 | |
| 680 | if (D.getIdentifier()) { |
| 681 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 682 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 683 | IdResolver.AddDecl(Param); |
| 684 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 685 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 686 | // C++0x [temp.param]p9: |
| 687 | // A default template-argument may be specified for any kind of |
| 688 | // template-parameter that is not a template parameter pack. |
| 689 | if (Default && IsParameterPack) { |
| 690 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 691 | Default = 0; |
| 692 | } |
| 693 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 694 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 695 | if (Default) { |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 696 | // Check for unexpanded parameter packs. |
| 697 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 698 | return Param; |
| 699 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 700 | TemplateArgument Converted; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 701 | ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
| 702 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 703 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 704 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 705 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 706 | Default = DefaultRes.take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 707 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 708 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 709 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 710 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 711 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 712 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 713 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 714 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 715 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 716 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 717 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 718 | SourceLocation TmpLoc, |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 719 | TemplateParameterList *Params, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 720 | SourceLocation EllipsisLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 721 | IdentifierInfo *Name, |
| 722 | SourceLocation NameLoc, |
| 723 | unsigned Depth, |
| 724 | unsigned Position, |
| 725 | SourceLocation EqualLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 726 | ParsedTemplateArgument Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 727 | assert(S->isTemplateParamScope() && |
| 728 | "Template template parameter not in template parameter scope!"); |
| 729 | |
| 730 | // Construct the parameter object. |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 731 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 732 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 733 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 734 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 735 | Depth, Position, IsParameterPack, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 736 | Name, Params); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 737 | Param->setAccess(AS_public); |
| 738 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 739 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 740 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 741 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 742 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 743 | IdResolver.AddDecl(Param); |
| 744 | } |
| 745 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 746 | if (Params->size() == 0) { |
| 747 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 748 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 749 | Param->setInvalidDecl(); |
| 750 | } |
| 751 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 752 | // C++0x [temp.param]p9: |
| 753 | // A default template-argument may be specified for any kind of |
| 754 | // template-parameter that is not a template parameter pack. |
| 755 | if (IsParameterPack && !Default.isInvalid()) { |
| 756 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 757 | Default = ParsedTemplateArgument(); |
| 758 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 759 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 760 | if (!Default.isInvalid()) { |
| 761 | // Check only that we have a template template argument. We don't want to |
| 762 | // try to check well-formedness now, because our template template parameter |
| 763 | // might have dependent types in its template parameters, which we wouldn't |
| 764 | // be able to match now. |
| 765 | // |
| 766 | // If none of the template template parameter's template arguments mention |
| 767 | // other template parameters, we could actually perform more checking here. |
| 768 | // However, it isn't worth doing. |
| 769 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 770 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 771 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 772 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 773 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 774 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 775 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 776 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 777 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 778 | DefaultArg.getArgument().getAsTemplate(), |
| 779 | UPPC_DefaultArgument)) |
| 780 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 781 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 782 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 783 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 784 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 785 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 788 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 789 | /// contains the template parameters in Params/NumParams. |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 790 | TemplateParameterList * |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 791 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 792 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 793 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 794 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 795 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 796 | SourceLocation RAngleLoc) { |
| 797 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 798 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 799 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 800 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 801 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 802 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 803 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 804 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 805 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 806 | if (SS.isSet()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 807 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 808 | } |
| 809 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 810 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 811 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 812 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 813 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 814 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 815 | TemplateParameterList *TemplateParams, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 816 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 817 | unsigned NumOuterTemplateParamLists, |
| 818 | TemplateParameterList** OuterTemplateParamLists) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 819 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 820 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 821 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 822 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 823 | |
| 824 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 825 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 826 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 827 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 828 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 829 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 830 | |
| 831 | // There is no such thing as an unnamed class template. |
| 832 | if (!Name) { |
| 833 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 834 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 838 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 839 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 840 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 841 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 842 | SemanticContext = computeDeclContext(SS, true); |
| 843 | if (!SemanticContext) { |
| 844 | // FIXME: Produce a reasonable diagnostic here |
| 845 | return true; |
| 846 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 848 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 849 | return true; |
| 850 | |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 851 | // If we're adding a template to a dependent context, we may need to |
| 852 | // rebuilding some of the types used within the template parameter list, |
| 853 | // now that we know what the current instantiation is. |
| 854 | if (SemanticContext->isDependentContext()) { |
| 855 | ContextRAII SavedContext(*this, SemanticContext); |
| 856 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 857 | Invalid = true; |
| 858 | } |
| 859 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 860 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 861 | } else { |
| 862 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 863 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 864 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 865 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 866 | if (Previous.isAmbiguous()) |
| 867 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 868 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 869 | NamedDecl *PrevDecl = 0; |
| 870 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 871 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 872 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 873 | // If there is a previous declaration with the same name, check |
| 874 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 875 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 876 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 877 | |
| 878 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 879 | // class template partial specialization, or class template specialization. |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 880 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 881 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 882 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 883 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 884 | PrevClassTemplate |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 885 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 886 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 887 | PrevClassTemplate |
| 888 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 889 | ->getSpecializedTemplate(); |
| 890 | } |
| 891 | } |
| 892 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 893 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 894 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 895 | // [...] When looking for a prior declaration of a class or a function |
| 896 | // 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] | 897 | // function is neither a qualified name nor a template-id, scopes outside |
| 898 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 899 | if (!SS.isSet()) { |
| 900 | DeclContext *OutermostContext = CurContext; |
| 901 | while (!OutermostContext->isFileContext()) |
| 902 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 903 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 904 | if (PrevDecl && |
| 905 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 906 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 907 | SemanticContext = PrevDecl->getDeclContext(); |
| 908 | } else { |
| 909 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 910 | // context we computed is the semantic context for our new |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 911 | // declaration. |
| 912 | PrevDecl = PrevClassTemplate = 0; |
| 913 | SemanticContext = OutermostContext; |
| 914 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 915 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 916 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 917 | if (CurContext->isDependentContext()) { |
| 918 | // If this is a dependent context, we don't want to link the friend |
| 919 | // class template to the template in scope, because that would perform |
| 920 | // checking of the template parameter lists that can't be performed |
| 921 | // until the outer context is instantiated. |
| 922 | PrevDecl = PrevClassTemplate = 0; |
| 923 | } |
| 924 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 925 | PrevDecl = PrevClassTemplate = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 926 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 927 | if (PrevClassTemplate) { |
| 928 | // Ensure that the template parameter lists are compatible. |
| 929 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 930 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 931 | /*Complain=*/true, |
| 932 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 933 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 934 | |
| 935 | // C++ [temp.class]p4: |
| 936 | // In a redeclaration, partial specialization, explicit |
| 937 | // specialization or explicit instantiation of a class template, |
| 938 | // the class-key shall agree in kind with the original class |
| 939 | // template declaration (7.1.5.3). |
| 940 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 941 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
| 942 | TUK == TUK_Definition, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 943 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 944 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 945 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 946 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 947 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 950 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 951 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 952 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 953 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 954 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 955 | // FIXME: Would it make sense to try to "forget" the previous |
| 956 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 957 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 958 | } |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 959 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 960 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 961 | // Maybe we will complain about the shadowed template parameter. |
| 962 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 963 | // Just pretend that we didn't see the previous declaration. |
| 964 | PrevDecl = 0; |
| 965 | } else if (PrevDecl) { |
| 966 | // C++ [temp]p5: |
| 967 | // A class template shall not have the same name as any other |
| 968 | // template, class, function, object, enumeration, enumerator, |
| 969 | // namespace, or type in the same scope (3.3), except as specified |
| 970 | // in (14.5.4). |
| 971 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 972 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 973 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 976 | // Check the template parameter list of this declaration, possibly |
| 977 | // merging in the template parameter list from the previous class |
| 978 | // template declaration. |
| 979 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 980 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 981 | (SS.isSet() && SemanticContext && |
Douglas Gregor | 461bf2e | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 982 | SemanticContext->isRecord() && |
| 983 | SemanticContext->isDependentContext()) |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 984 | ? TPC_ClassTemplateMember |
| 985 | : TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 986 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 987 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 988 | if (SS.isSet()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 989 | // 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] | 990 | // template out-of-line. |
| 991 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
Douglas Gregor | ea9f54a | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 992 | !(TUK == TUK_Friend && CurContext->isDependentContext())) { |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 993 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 994 | << Name << SemanticContext << SS.getRange(); |
Douglas Gregor | ea9f54a | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 995 | Invalid = true; |
| 996 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | CXXRecordDecl *NewClass = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1000 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1002 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 1003 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1004 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1005 | if (NumOuterTemplateParamLists > 0) |
| 1006 | NewClass->setTemplateParameterListsInfo(Context, |
| 1007 | NumOuterTemplateParamLists, |
| 1008 | OuterTemplateParamLists); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1009 | |
| 1010 | ClassTemplateDecl *NewTemplate |
| 1011 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1012 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1013 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1014 | NewClass->setDescribedClassTemplate(NewTemplate); |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1015 | |
| 1016 | if (PrevClassTemplate && PrevClassTemplate->isModulePrivate()) { |
| 1017 | NewTemplate->setModulePrivate(); |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 1018 | } else if (ModulePrivateLoc.isValid()) { |
| 1019 | if (PrevClassTemplate && !PrevClassTemplate->isModulePrivate()) |
| 1020 | diagnoseModulePrivateRedeclaration(NewTemplate, PrevClassTemplate, |
| 1021 | ModulePrivateLoc); |
| 1022 | else |
| 1023 | NewTemplate->setModulePrivate(); |
| 1024 | } |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1025 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1026 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1027 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1028 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1029 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1030 | (void)T; |
| 1031 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1032 | // 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] | 1033 | // class template, make a note of that. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1034 | if (PrevClassTemplate && |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1035 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1036 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1037 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1038 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1039 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1040 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1041 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1042 | // Set the lexical context of these templates |
| 1043 | NewClass->setLexicalDeclContext(CurContext); |
| 1044 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1045 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1046 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1047 | NewClass->startDefinition(); |
| 1048 | |
| 1049 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1050 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1051 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1052 | if (TUK != TUK_Friend) |
| 1053 | PushOnScopeChains(NewTemplate, S); |
| 1054 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1055 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1056 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1057 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1058 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1059 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1060 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1061 | PrevClassTemplate != NULL); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1062 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1063 | // Friend templates are visible in fairly strange ways. |
| 1064 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1065 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1066 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 1067 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1068 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1069 | /* AddToContext = */ false); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1070 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1071 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1072 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1073 | NewClass->getLocation(), |
| 1074 | NewTemplate, |
| 1075 | /*FIXME:*/NewClass->getLocation()); |
| 1076 | Friend->setAccess(AS_public); |
| 1077 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1078 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1079 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1080 | if (Invalid) { |
| 1081 | NewTemplate->setInvalidDecl(); |
| 1082 | NewClass->setInvalidDecl(); |
| 1083 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1084 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1087 | /// \brief Diagnose the presence of a default template argument on a |
| 1088 | /// template parameter, which is ill-formed in certain contexts. |
| 1089 | /// |
| 1090 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1091 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1092 | Sema::TemplateParamListContext TPC, |
| 1093 | SourceLocation ParamLoc, |
| 1094 | SourceRange DefArgRange) { |
| 1095 | switch (TPC) { |
| 1096 | case Sema::TPC_ClassTemplate: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1097 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1098 | return false; |
| 1099 | |
| 1100 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1101 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1102 | // C++ [temp.param]p9: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1103 | // A default template-argument shall not be specified in a |
| 1104 | // function template declaration or a function template |
| 1105 | // definition [...] |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1106 | // If a friend function template declaration specifies a default |
| 1107 | // template-argument, that declaration shall be a definition and shall be |
| 1108 | // the only declaration of the function template in the translation unit. |
| 1109 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1110 | S.Diag(ParamLoc, S.getLangOptions().CPlusPlus0x ? |
| 1111 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1112 | : diag::ext_template_parameter_default_in_function_template) |
| 1113 | << DefArgRange; |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1114 | return false; |
| 1115 | |
| 1116 | case Sema::TPC_ClassTemplateMember: |
| 1117 | // C++0x [temp.param]p9: |
| 1118 | // A default template-argument shall not be specified in the |
| 1119 | // template-parameter-lists of the definition of a member of a |
| 1120 | // class template that appears outside of the member's class. |
| 1121 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1122 | << DefArgRange; |
| 1123 | return true; |
| 1124 | |
| 1125 | case Sema::TPC_FriendFunctionTemplate: |
| 1126 | // C++ [temp.param]p9: |
| 1127 | // A default template-argument shall not be specified in a |
| 1128 | // friend template declaration. |
| 1129 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1130 | << DefArgRange; |
| 1131 | return true; |
| 1132 | |
| 1133 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1134 | // for friend function templates if there is only a single |
| 1135 | // declaration (and it is a definition). Strange! |
| 1136 | } |
| 1137 | |
| 1138 | return false; |
| 1139 | } |
| 1140 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1141 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1142 | /// of a template template parameter, recursively. |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1143 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1144 | TemplateTemplateParmDecl *TTP) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1145 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1146 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1147 | NamedDecl *P = Params->getParam(I); |
| 1148 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1149 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1150 | NTTP->getTypeSourceInfo(), |
| 1151 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1152 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1153 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1154 | continue; |
| 1155 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1156 | |
| 1157 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1158 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1159 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1160 | return true; |
| 1161 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1162 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1163 | return false; |
| 1164 | } |
| 1165 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1166 | /// \brief Checks the validity of a template parameter list, possibly |
| 1167 | /// considering the template parameter list from a previous |
| 1168 | /// declaration. |
| 1169 | /// |
| 1170 | /// If an "old" template parameter list is provided, it must be |
| 1171 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1172 | /// template parameter list. |
| 1173 | /// |
| 1174 | /// \param NewParams Template parameter list for a new template |
| 1175 | /// declaration. This template parameter list will be updated with any |
| 1176 | /// default arguments that are carried through from the previous |
| 1177 | /// template parameter list. |
| 1178 | /// |
| 1179 | /// \param OldParams If provided, template parameter list from a |
| 1180 | /// previous declaration of the same template. Default template |
| 1181 | /// arguments will be merged from the old template parameter list to |
| 1182 | /// the new template parameter list. |
| 1183 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1184 | /// \param TPC Describes the context in which we are checking the given |
| 1185 | /// template parameter list. |
| 1186 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1187 | /// \returns true if an error occurred, false otherwise. |
| 1188 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1189 | TemplateParameterList *OldParams, |
| 1190 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1191 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1192 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1193 | // C++ [temp.param]p10: |
| 1194 | // The set of default template-arguments available for use with a |
| 1195 | // template declaration or definition is obtained by merging the |
| 1196 | // default arguments from the definition (if in scope) and all |
| 1197 | // declarations in scope in the same way default function |
| 1198 | // arguments are (8.3.6). |
| 1199 | bool SawDefaultArgument = false; |
| 1200 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1201 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1202 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1203 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1204 | if (OldParams) |
| 1205 | OldParam = OldParams->begin(); |
| 1206 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1207 | bool RemoveDefaultArguments = false; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1208 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1209 | NewParamEnd = NewParams->end(); |
| 1210 | NewParam != NewParamEnd; ++NewParam) { |
| 1211 | // Variables used to diagnose redundant default arguments |
| 1212 | bool RedundantDefaultArg = false; |
| 1213 | SourceLocation OldDefaultLoc; |
| 1214 | SourceLocation NewDefaultLoc; |
| 1215 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1216 | // Variable used to diagnose missing default arguments |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1217 | bool MissingDefaultArg = false; |
| 1218 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1219 | // Variable used to diagnose non-final parameter packs |
| 1220 | bool SawParameterPack = false; |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1221 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1222 | if (TemplateTypeParmDecl *NewTypeParm |
| 1223 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1224 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1225 | if (NewTypeParm->hasDefaultArgument() && |
| 1226 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1227 | NewTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1228 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1229 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1230 | NewTypeParm->removeDefaultArgument(); |
| 1231 | |
| 1232 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1233 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1234 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1235 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1236 | if (NewTypeParm->isParameterPack()) { |
| 1237 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1238 | "Parameter packs can't have a default argument!"); |
| 1239 | SawParameterPack = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1240 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1241 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1242 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1243 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1244 | SawDefaultArgument = true; |
| 1245 | RedundantDefaultArg = true; |
| 1246 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1247 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1248 | // Merge the default argument from the old declaration to the |
| 1249 | // new declaration. |
| 1250 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1251 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1252 | true); |
| 1253 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1254 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1255 | SawDefaultArgument = true; |
| 1256 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1257 | } else if (SawDefaultArgument) |
| 1258 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1259 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1260 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1261 | // Check for unexpanded parameter packs. |
| 1262 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1263 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1264 | UPPC_NonTypeTemplateParameterType)) { |
| 1265 | Invalid = true; |
| 1266 | continue; |
| 1267 | } |
| 1268 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1269 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1270 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1271 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1272 | NewNonTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1273 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1274 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1277 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1278 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1279 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1280 | if (NewNonTypeParm->isParameterPack()) { |
| 1281 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1282 | "Parameter packs can't have a default argument!"); |
| 1283 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1284 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1285 | NewNonTypeParm->hasDefaultArgument()) { |
| 1286 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1287 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1288 | SawDefaultArgument = true; |
| 1289 | RedundantDefaultArg = true; |
| 1290 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1291 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1292 | // Merge the default argument from the old declaration to the |
| 1293 | // new declaration. |
| 1294 | SawDefaultArgument = true; |
| 1295 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1296 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1297 | // parameter. |
| 1298 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1299 | OldNonTypeParm->getDefaultArgument(), |
| 1300 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1301 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1302 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1303 | SawDefaultArgument = true; |
| 1304 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1305 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1306 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1307 | } else { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1308 | TemplateTemplateParmDecl *NewTemplateParm |
| 1309 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1310 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1311 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1312 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1313 | Invalid = true; |
| 1314 | continue; |
| 1315 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1316 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1317 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1318 | if (NewTemplateParm->hasDefaultArgument() && |
| 1319 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1320 | NewTemplateParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1321 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1322 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1323 | |
| 1324 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1325 | TemplateTemplateParmDecl *OldTemplateParm |
| 1326 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1327 | if (NewTemplateParm->isParameterPack()) { |
| 1328 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1329 | "Parameter packs can't have a default argument!"); |
| 1330 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1331 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1332 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1333 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1334 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1335 | SawDefaultArgument = true; |
| 1336 | RedundantDefaultArg = true; |
| 1337 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1338 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1339 | // Merge the default argument from the old declaration to the |
| 1340 | // new declaration. |
| 1341 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1342 | // FIXME: We need to create a new kind of "default argument" expression |
| 1343 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1344 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1345 | OldTemplateParm->getDefaultArgument(), |
| 1346 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1347 | PreviousDefaultArgLoc |
| 1348 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1349 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1350 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1351 | PreviousDefaultArgLoc |
| 1352 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1353 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1354 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1357 | // C++0x [temp.param]p11: |
| 1358 | // If a template parameter of a primary class template or alias template |
| 1359 | // is a template parameter pack, it shall be the last template parameter. |
| 1360 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
| 1361 | (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) { |
| 1362 | Diag((*NewParam)->getLocation(), |
| 1363 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1364 | Invalid = true; |
| 1365 | } |
| 1366 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1367 | if (RedundantDefaultArg) { |
| 1368 | // C++ [temp.param]p12: |
| 1369 | // A template-parameter shall not be given default arguments |
| 1370 | // by two different declarations in the same scope. |
| 1371 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1372 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1373 | Invalid = true; |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1374 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1375 | // C++ [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1376 | // If a template-parameter of a class template has a default |
| 1377 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1378 | // have a default template-argument supplied or be a template parameter |
| 1379 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1380 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1381 | diag::err_template_param_default_arg_missing); |
| 1382 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1383 | Invalid = true; |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1384 | RemoveDefaultArguments = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | // If we have an old template parameter list that we're merging |
| 1388 | // in, move on to the next parameter. |
| 1389 | if (OldParams) |
| 1390 | ++OldParam; |
| 1391 | } |
| 1392 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1393 | // We were missing some default arguments at the end of the list, so remove |
| 1394 | // all of the default arguments. |
| 1395 | if (RemoveDefaultArguments) { |
| 1396 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1397 | NewParamEnd = NewParams->end(); |
| 1398 | NewParam != NewParamEnd; ++NewParam) { |
| 1399 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1400 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1401 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1402 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1403 | NTTP->removeDefaultArgument(); |
| 1404 | else |
| 1405 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1406 | } |
| 1407 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1408 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1409 | return Invalid; |
| 1410 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1411 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1412 | namespace { |
| 1413 | |
| 1414 | /// A class which looks for a use of a certain level of template |
| 1415 | /// parameter. |
| 1416 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1417 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1418 | |
| 1419 | unsigned Depth; |
| 1420 | bool Match; |
| 1421 | |
| 1422 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1423 | NamedDecl *ND = Params->getParam(0); |
| 1424 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1425 | Depth = PD->getDepth(); |
| 1426 | } else if (NonTypeTemplateParmDecl *PD = |
| 1427 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1428 | Depth = PD->getDepth(); |
| 1429 | } else { |
| 1430 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | bool Matches(unsigned ParmDepth) { |
| 1435 | if (ParmDepth >= Depth) { |
| 1436 | Match = true; |
| 1437 | return true; |
| 1438 | } |
| 1439 | return false; |
| 1440 | } |
| 1441 | |
| 1442 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1443 | return !Matches(T->getDepth()); |
| 1444 | } |
| 1445 | |
| 1446 | bool TraverseTemplateName(TemplateName N) { |
| 1447 | if (TemplateTemplateParmDecl *PD = |
| 1448 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1449 | if (Matches(PD->getDepth())) return false; |
| 1450 | return super::TraverseTemplateName(N); |
| 1451 | } |
| 1452 | |
| 1453 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1454 | if (NonTypeTemplateParmDecl *PD = |
| 1455 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1456 | if (PD->getDepth() == Depth) { |
| 1457 | Match = true; |
| 1458 | return false; |
| 1459 | } |
| 1460 | } |
| 1461 | return super::VisitDeclRefExpr(E); |
| 1462 | } |
Douglas Gregor | 18c8339 | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1463 | |
| 1464 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1465 | return TraverseType(T->getInjectedSpecializationType()); |
| 1466 | } |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1467 | }; |
| 1468 | } |
| 1469 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1470 | /// Determines whether a given type depends on the given parameter |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1471 | /// list. |
| 1472 | static bool |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1473 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1474 | DependencyChecker Checker(Params); |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1475 | Checker.TraverseType(T); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1476 | return Checker.Match; |
| 1477 | } |
| 1478 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1479 | // Find the source range corresponding to the named type in the given |
| 1480 | // nested-name-specifier, if any. |
| 1481 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1482 | QualType T, |
| 1483 | const CXXScopeSpec &SS) { |
| 1484 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1485 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1486 | if (const Type *CurType = NNS->getAsType()) { |
| 1487 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1488 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1489 | } else |
| 1490 | break; |
| 1491 | |
| 1492 | NNSLoc = NNSLoc.getPrefix(); |
| 1493 | } |
| 1494 | |
| 1495 | return SourceRange(); |
| 1496 | } |
| 1497 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1498 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1499 | /// specifier, returning the template parameter list that applies to the |
| 1500 | /// name. |
| 1501 | /// |
| 1502 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1503 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1504 | /// |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1505 | /// \param DeclLoc The location of the declaration itself. |
| 1506 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1507 | /// \param SS the scope specifier that will be matched to the given template |
| 1508 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1509 | /// being declared. |
| 1510 | /// |
| 1511 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1512 | /// innermost template parameter lists. |
| 1513 | /// |
| 1514 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1515 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1516 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1517 | /// matching template parameters to scope specifiers in friend |
| 1518 | /// declarations. |
| 1519 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1520 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1521 | /// declared is an explicit specialization, false otherwise. |
| 1522 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1523 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1524 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1525 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1527 | /// 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] | 1528 | /// itself a template). |
| 1529 | TemplateParameterList * |
| 1530 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1531 | SourceLocation DeclLoc, |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1532 | const CXXScopeSpec &SS, |
| 1533 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1534 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1535 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1536 | bool &IsExplicitSpecialization, |
| 1537 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1538 | IsExplicitSpecialization = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1539 | Invalid = false; |
| 1540 | |
| 1541 | // The sequence of nested types to which we will match up the template |
| 1542 | // parameter lists. We first build this list by starting with the type named |
| 1543 | // by the nested-name-specifier and walking out until we run out of types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1544 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1545 | QualType T; |
Douglas Gregor | 714c992 | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1546 | if (SS.getScopeRep()) { |
| 1547 | if (CXXRecordDecl *Record |
| 1548 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1549 | T = Context.getTypeDeclType(Record); |
| 1550 | else |
| 1551 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1552 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1553 | |
| 1554 | // If we found an explicit specialization that prevents us from needing |
| 1555 | // 'template<>' headers, this will be set to the location of that |
| 1556 | // explicit specialization. |
| 1557 | SourceLocation ExplicitSpecLoc; |
| 1558 | |
| 1559 | while (!T.isNull()) { |
| 1560 | NestedTypes.push_back(T); |
| 1561 | |
| 1562 | // Retrieve the parent of a record type. |
| 1563 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1564 | // If this type is an explicit specialization, we're done. |
| 1565 | if (ClassTemplateSpecializationDecl *Spec |
| 1566 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1567 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1568 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1569 | ExplicitSpecLoc = Spec->getLocation(); |
| 1570 | break; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1571 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1572 | } else if (Record->getTemplateSpecializationKind() |
| 1573 | == TSK_ExplicitSpecialization) { |
| 1574 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1575 | break; |
| 1576 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1577 | |
| 1578 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1579 | T = Context.getTypeDeclType(Parent); |
| 1580 | else |
| 1581 | T = QualType(); |
| 1582 | continue; |
| 1583 | } |
| 1584 | |
| 1585 | if (const TemplateSpecializationType *TST |
| 1586 | = T->getAs<TemplateSpecializationType>()) { |
| 1587 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1588 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1589 | T = Context.getTypeDeclType(Parent); |
| 1590 | else |
| 1591 | T = QualType(); |
| 1592 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1593 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | // Look one step prior in a dependent template specialization type. |
| 1597 | if (const DependentTemplateSpecializationType *DependentTST |
| 1598 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1599 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1600 | T = QualType(NNS->getAsType(), 0); |
| 1601 | else |
| 1602 | T = QualType(); |
| 1603 | continue; |
| 1604 | } |
| 1605 | |
| 1606 | // Look one step prior in a dependent name type. |
| 1607 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1608 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1609 | T = QualType(NNS->getAsType(), 0); |
| 1610 | else |
| 1611 | T = QualType(); |
| 1612 | continue; |
| 1613 | } |
| 1614 | |
| 1615 | // Retrieve the parent of an enumeration type. |
| 1616 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1617 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1618 | // check here. |
| 1619 | EnumDecl *Enum = EnumT->getDecl(); |
| 1620 | |
| 1621 | // Get to the parent type. |
| 1622 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1623 | T = Context.getTypeDeclType(Parent); |
| 1624 | else |
| 1625 | T = QualType(); |
| 1626 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1627 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1629 | T = QualType(); |
| 1630 | } |
| 1631 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1632 | // to the innermost while checking template-parameter-lists. |
| 1633 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1634 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1635 | // C++0x [temp.expl.spec]p17: |
| 1636 | // A member or a member template may be nested within many |
| 1637 | // enclosing class templates. In an explicit specialization for |
| 1638 | // such a member, the member declaration shall be preceded by a |
| 1639 | // template<> for each enclosing class template that is |
| 1640 | // explicitly specialized. |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1641 | bool SawNonEmptyTemplateParameterList = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1642 | unsigned ParamIdx = 0; |
| 1643 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1644 | ++TypeIdx) { |
| 1645 | T = NestedTypes[TypeIdx]; |
| 1646 | |
| 1647 | // Whether we expect a 'template<>' header. |
| 1648 | bool NeedEmptyTemplateHeader = false; |
| 1649 | |
| 1650 | // Whether we expect a template header with parameters. |
| 1651 | bool NeedNonemptyTemplateHeader = false; |
| 1652 | |
| 1653 | // For a dependent type, the set of template parameters that we |
| 1654 | // expect to see. |
| 1655 | TemplateParameterList *ExpectedTemplateParams = 0; |
| 1656 | |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1657 | // C++0x [temp.expl.spec]p15: |
| 1658 | // A member or a member template may be nested within many enclosing |
| 1659 | // class templates. In an explicit specialization for such a member, the |
| 1660 | // member declaration shall be preceded by a template<> for each |
| 1661 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1662 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1663 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1664 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1665 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1666 | NeedNonemptyTemplateHeader = true; |
| 1667 | } else if (Record->isDependentType()) { |
| 1668 | if (Record->getDescribedClassTemplate()) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1669 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1670 | ->getTemplateParameters(); |
| 1671 | NeedNonemptyTemplateHeader = true; |
| 1672 | } |
| 1673 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1674 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1675 | // C++0x [temp.expl.spec]p4: |
| 1676 | // Members of an explicitly specialized class template are defined |
| 1677 | // in the same manner as members of normal classes, and not using |
| 1678 | // the template<> syntax. |
| 1679 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1680 | NeedEmptyTemplateHeader = true; |
| 1681 | else |
Douglas Gregor | 95ea450 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1682 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1683 | } else if (Record->getTemplateSpecializationKind()) { |
| 1684 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1685 | != TSK_ExplicitSpecialization && |
| 1686 | TypeIdx == NumTypes - 1) |
| 1687 | IsExplicitSpecialization = true; |
| 1688 | |
| 1689 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1690 | } |
| 1691 | } else if (const TemplateSpecializationType *TST |
| 1692 | = T->getAs<TemplateSpecializationType>()) { |
| 1693 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1694 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1695 | NeedNonemptyTemplateHeader = true; |
| 1696 | } |
| 1697 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1698 | // FIXME: We actually could/should check the template arguments here |
| 1699 | // against the corresponding template parameter list. |
| 1700 | NeedNonemptyTemplateHeader = false; |
| 1701 | } |
| 1702 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1703 | // C++ [temp.expl.spec]p16: |
| 1704 | // In an explicit specialization declaration for a member of a class |
| 1705 | // template or a member template that ap- pears in namespace scope, the |
| 1706 | // member template and some of its enclosing class templates may remain |
| 1707 | // unspecialized, except that the declaration shall not explicitly |
| 1708 | // specialize a class member template if its en- closing class templates |
| 1709 | // are not explicitly specialized as well. |
| 1710 | if (ParamIdx < NumParamLists) { |
| 1711 | if (ParamLists[ParamIdx]->size() == 0) { |
| 1712 | if (SawNonEmptyTemplateParameterList) { |
| 1713 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1714 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1715 | Invalid = true; |
| 1716 | IsExplicitSpecialization = false; |
| 1717 | return 0; |
| 1718 | } |
| 1719 | } else |
| 1720 | SawNonEmptyTemplateParameterList = true; |
| 1721 | } |
| 1722 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1723 | if (NeedEmptyTemplateHeader) { |
| 1724 | // If we're on the last of the types, and we need a 'template<>' header |
| 1725 | // here, then it's an explicit specialization. |
| 1726 | if (TypeIdx == NumTypes - 1) |
| 1727 | IsExplicitSpecialization = true; |
| 1728 | |
| 1729 | if (ParamIdx < NumParamLists) { |
| 1730 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1731 | // The header has template parameters when it shouldn't. Complain. |
| 1732 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1733 | diag::err_template_param_list_matches_nontemplate) |
| 1734 | << T |
| 1735 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1736 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1737 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1738 | Invalid = true; |
| 1739 | return 0; |
| 1740 | } |
| 1741 | |
| 1742 | // Consume this template header. |
| 1743 | ++ParamIdx; |
| 1744 | continue; |
| 1745 | } |
| 1746 | |
| 1747 | if (!IsFriend) { |
| 1748 | // We don't have a template header, but we should. |
| 1749 | SourceLocation ExpectedTemplateLoc; |
| 1750 | if (NumParamLists > 0) |
| 1751 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1752 | else |
| 1753 | ExpectedTemplateLoc = DeclStartLoc; |
| 1754 | |
| 1755 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1756 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS) |
| 1757 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1758 | } |
| 1759 | |
| 1760 | continue; |
| 1761 | } |
| 1762 | |
| 1763 | if (NeedNonemptyTemplateHeader) { |
| 1764 | // In friend declarations we can have template-ids which don't |
| 1765 | // depend on the corresponding template parameter lists. But |
| 1766 | // assume that empty parameter lists are supposed to match this |
| 1767 | // template-id. |
| 1768 | if (IsFriend && T->isDependentType()) { |
| 1769 | if (ParamIdx < NumParamLists && |
| 1770 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
| 1771 | ExpectedTemplateParams = 0; |
| 1772 | else |
| 1773 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1774 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1775 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1776 | if (ParamIdx < NumParamLists) { |
| 1777 | // Check the template parameter list, if we can. |
| 1778 | if (ExpectedTemplateParams && |
| 1779 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1780 | ExpectedTemplateParams, |
| 1781 | true, TPL_TemplateMatch)) |
| 1782 | Invalid = true; |
| 1783 | |
| 1784 | if (!Invalid && |
| 1785 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1786 | TPC_ClassTemplateMember)) |
| 1787 | Invalid = true; |
| 1788 | |
| 1789 | ++ParamIdx; |
| 1790 | continue; |
| 1791 | } |
| 1792 | |
| 1793 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1794 | << T |
| 1795 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1796 | Invalid = true; |
| 1797 | continue; |
| 1798 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1799 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1800 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1801 | // If there were at least as many template-ids as there were template |
| 1802 | // parameter lists, then there are no template parameter lists remaining for |
| 1803 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1804 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1805 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1806 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1807 | // If there were too many template parameter lists, complain about that now. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1808 | if (ParamIdx < NumParamLists - 1) { |
| 1809 | bool HasAnyExplicitSpecHeader = false; |
| 1810 | bool AllExplicitSpecHeaders = true; |
| 1811 | for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) { |
| 1812 | if (ParamLists[I]->size() == 0) |
| 1813 | HasAnyExplicitSpecHeader = true; |
| 1814 | else |
| 1815 | AllExplicitSpecHeaders = false; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1816 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1817 | |
| 1818 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1819 | AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers |
| 1820 | : diag::err_template_spec_extra_headers) |
| 1821 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1822 | ParamLists[NumParamLists - 2]->getRAngleLoc()); |
| 1823 | |
| 1824 | // If there was a specialization somewhere, such that 'template<>' is |
| 1825 | // not required, and there were any 'template<>' headers, note where the |
| 1826 | // specialization occurred. |
| 1827 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1828 | Diag(ExplicitSpecLoc, |
| 1829 | diag::note_explicit_template_spec_does_not_need_header) |
| 1830 | << NestedTypes.back(); |
| 1831 | |
| 1832 | // We have a template parameter list with no corresponding scope, which |
| 1833 | // means that the resulting template declaration can't be instantiated |
| 1834 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1835 | if (!AllExplicitSpecHeaders) |
| 1836 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1837 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1839 | // C++ [temp.expl.spec]p16: |
| 1840 | // In an explicit specialization declaration for a member of a class |
| 1841 | // template or a member template that ap- pears in namespace scope, the |
| 1842 | // member template and some of its enclosing class templates may remain |
| 1843 | // unspecialized, except that the declaration shall not explicitly |
| 1844 | // specialize a class member template if its en- closing class templates |
| 1845 | // are not explicitly specialized as well. |
| 1846 | if (ParamLists[NumParamLists - 1]->size() == 0 && |
| 1847 | SawNonEmptyTemplateParameterList) { |
| 1848 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1849 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1850 | Invalid = true; |
| 1851 | IsExplicitSpecialization = false; |
| 1852 | return 0; |
| 1853 | } |
| 1854 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1855 | // Return the last template parameter list, which corresponds to the |
| 1856 | // entity being declared. |
| 1857 | return ParamLists[NumParamLists - 1]; |
| 1858 | } |
| 1859 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1860 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1861 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1862 | Diag(Template->getLocation(), diag::note_template_declared_here) |
| 1863 | << (isa<FunctionTemplateDecl>(Template)? 0 |
| 1864 | : isa<ClassTemplateDecl>(Template)? 1 |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1865 | : isa<TypeAliasTemplateDecl>(Template)? 2 |
| 1866 | : 3) |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1867 | << Template->getDeclName(); |
| 1868 | return; |
| 1869 | } |
| 1870 | |
| 1871 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1872 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1873 | IEnd = OST->end(); |
| 1874 | I != IEnd; ++I) |
| 1875 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1876 | << 0 << (*I)->getDeclName(); |
| 1877 | |
| 1878 | return; |
| 1879 | } |
| 1880 | } |
| 1881 | |
| 1882 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1883 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1884 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1885 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1886 | DependentTemplateName *DTN |
| 1887 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1888 | if (DTN && DTN->isIdentifier()) |
| 1889 | // When building a template-id where the template-name is dependent, |
| 1890 | // assume the template is a type template. Either our assumption is |
| 1891 | // correct, or the code is ill-formed and will be diagnosed when the |
| 1892 | // dependent name is substituted. |
| 1893 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 1894 | DTN->getQualifier(), |
| 1895 | DTN->getIdentifier(), |
| 1896 | TemplateArgs); |
| 1897 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1898 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1899 | if (!Template || isa<FunctionTemplateDecl>(Template)) { |
| 1900 | // We might have a substituted template template parameter pack. If so, |
| 1901 | // build a template specialization type for it. |
| 1902 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 1903 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1904 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1905 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 1906 | << Name; |
| 1907 | NoteAllFoundTemplates(Name); |
| 1908 | return QualType(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1909 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1910 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1911 | // Check that the template argument list is well-formed for this |
| 1912 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1913 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1914 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1915 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1916 | return QualType(); |
| 1917 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1918 | assert((Converted.size() == Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1919 | "Converted template argument list is too short!"); |
| 1920 | |
| 1921 | QualType CanonType; |
| 1922 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1923 | bool InstantiationDependent = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1924 | if (TypeAliasTemplateDecl *AliasTemplate |
| 1925 | = dyn_cast<TypeAliasTemplateDecl>(Template)) { |
| 1926 | // Find the canonical type for this type alias template specialization. |
| 1927 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 1928 | if (Pattern->isInvalidDecl()) |
| 1929 | return QualType(); |
| 1930 | |
| 1931 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1932 | Converted.data(), Converted.size()); |
| 1933 | |
| 1934 | // Only substitute for the innermost template argument list. |
| 1935 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 1804174 | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 1936 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | aff37b4 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 1937 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 1938 | for (unsigned I = 0; I < Depth; ++I) |
| 1939 | TemplateArgLists.addOuterTemplateArguments(0, 0); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1940 | |
| 1941 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
| 1942 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 1943 | TemplateArgLists, AliasTemplate->getLocation(), |
| 1944 | AliasTemplate->getDeclName()); |
| 1945 | if (CanonType.isNull()) |
| 1946 | return QualType(); |
| 1947 | } else if (Name.isDependent() || |
| 1948 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1949 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1950 | // This class template specialization is a dependent |
| 1951 | // type. Therefore, its canonical type is another class template |
| 1952 | // specialization type that contains all of the converted |
| 1953 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1954 | // A<T, T> have identical types when A is declared as: |
| 1955 | // |
| 1956 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1957 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1959 | Converted.data(), |
| 1960 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1961 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1962 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1963 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1964 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1965 | // build the canonical type and return that to us. |
| 1966 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1967 | |
| 1968 | // This might work out to be a current instantiation, in which |
| 1969 | // case the canonical type needs to be the InjectedClassNameType. |
| 1970 | // |
| 1971 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1972 | // changes to CurContext don't change the set of current |
| 1973 | // instantiations. |
| 1974 | if (isa<ClassTemplateDecl>(Template)) { |
| 1975 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1976 | // If we get out to a namespace, we're done. |
| 1977 | if (Ctx->isFileContext()) break; |
| 1978 | |
| 1979 | // If this isn't a record, keep looking. |
| 1980 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1981 | if (!Record) continue; |
| 1982 | |
| 1983 | // Look for one of the two cases with InjectedClassNameTypes |
| 1984 | // and check whether it's the same template. |
| 1985 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1986 | !Record->getDescribedClassTemplate()) |
| 1987 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1988 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1989 | // Fetch the injected class name type and check whether its |
| 1990 | // injected type is equal to the type we just built. |
| 1991 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1992 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 1993 | ->getInjectedSpecializationType(); |
| 1994 | |
| 1995 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 1996 | continue; |
| 1997 | |
| 1998 | // If so, the canonical type of this TST is the injected |
| 1999 | // class name type of the record we just found. |
| 2000 | assert(ICNT.isCanonical()); |
| 2001 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2002 | break; |
| 2003 | } |
| 2004 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2005 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2006 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2007 | // Find the class template specialization declaration that |
| 2008 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2009 | void *InsertPos = 0; |
| 2010 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2011 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2012 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2013 | if (!Decl) { |
| 2014 | // This is the first time we have referenced this class template |
| 2015 | // specialization. Create the canonical declaration and add it to |
| 2016 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2018 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2019 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 09d8212 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2020 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2021 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2022 | ClassTemplate, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2023 | Converted.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2024 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2025 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2026 | Decl->setLexicalDeclContext(CurContext); |
| 2027 | } |
| 2028 | |
| 2029 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2030 | assert(isa<RecordType>(CanonType) && |
| 2031 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2032 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2033 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2034 | // Build the fully-sugared type for this class template |
| 2035 | // specialization, which refers back to the class template |
| 2036 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2037 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2040 | TypeResult |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2041 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, |
| 2042 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2043 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2044 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2045 | SourceLocation RAngleLoc) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2046 | if (SS.isInvalid()) |
| 2047 | return true; |
| 2048 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2049 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2050 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2051 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2052 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2053 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2054 | |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2055 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2056 | QualType T = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2057 | DTN->getQualifier(), |
| 2058 | DTN->getIdentifier(), |
| 2059 | TemplateArgs); |
| 2060 | |
| 2061 | // Build type-source information. |
| 2062 | TypeLocBuilder TLB; |
| 2063 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2064 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2065 | SpecTL.setKeywordLoc(SourceLocation()); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2066 | SpecTL.setNameLoc(TemplateLoc); |
| 2067 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2068 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 2069 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2070 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2071 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2072 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2073 | } |
| 2074 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2075 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2076 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2077 | |
| 2078 | if (Result.isNull()) |
| 2079 | return true; |
| 2080 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2081 | // Build type-source information. |
| 2082 | TypeLocBuilder TLB; |
| 2083 | TemplateSpecializationTypeLoc SpecTL |
| 2084 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 2085 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2086 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2087 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2088 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2089 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2090 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2091 | if (SS.isNotEmpty()) { |
| 2092 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2093 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2094 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 2095 | ElabTL.setKeywordLoc(SourceLocation()); |
| 2096 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2097 | } |
| 2098 | |
| 2099 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2100 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2101 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2102 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2103 | TypeSpecifierType TagSpec, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2104 | SourceLocation TagLoc, |
| 2105 | CXXScopeSpec &SS, |
| 2106 | TemplateTy TemplateD, |
| 2107 | SourceLocation TemplateLoc, |
| 2108 | SourceLocation LAngleLoc, |
| 2109 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2110 | SourceLocation RAngleLoc) { |
| 2111 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
| 2112 | |
| 2113 | // Translate the parser's template argument list in our AST format. |
| 2114 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2115 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2116 | |
| 2117 | // Determine the tag kind |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2118 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2119 | ElaboratedTypeKeyword Keyword |
| 2120 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2121 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2122 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2123 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2124 | DTN->getQualifier(), |
| 2125 | DTN->getIdentifier(), |
| 2126 | TemplateArgs); |
| 2127 | |
| 2128 | // Build type-source information. |
| 2129 | TypeLocBuilder TLB; |
| 2130 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2131 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2132 | SpecTL.setKeywordLoc(TagLoc); |
| 2133 | SpecTL.setNameLoc(TemplateLoc); |
| 2134 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2135 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2136 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2137 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2138 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2139 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2140 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2141 | |
| 2142 | if (TypeAliasTemplateDecl *TAT = |
| 2143 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2144 | // C++0x [dcl.type.elab]p2: |
| 2145 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2146 | // resolves to an alias template specialization, the |
| 2147 | // elaborated-type-specifier is ill-formed. |
| 2148 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2149 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2150 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2151 | |
| 2152 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2153 | if (Result.isNull()) |
Matt Beaumont-Gay | 3a51d41 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2154 | return TypeResult(true); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2155 | |
| 2156 | // Check the tag kind |
| 2157 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2158 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2159 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2160 | IdentifierInfo *Id = D->getIdentifier(); |
| 2161 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2162 | |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2163 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 2164 | TagLoc, *Id)) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2165 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2166 | << Result |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2167 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2168 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2169 | } |
| 2170 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2171 | |
| 2172 | // Provide source-location information for the template specialization. |
| 2173 | TypeLocBuilder TLB; |
| 2174 | TemplateSpecializationTypeLoc SpecTL |
| 2175 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 2176 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2177 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2178 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2179 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2180 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2181 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2182 | // Construct an elaborated type containing the nested-name-specifier (if any) |
| 2183 | // and keyword. |
| 2184 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2185 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 2186 | ElabTL.setKeywordLoc(TagLoc); |
| 2187 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2188 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2191 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2192 | LookupResult &R, |
| 2193 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2194 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2195 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2196 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2197 | // 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] | 2198 | // though. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2199 | // foo<int> could identify a single function unambiguously |
| 2200 | // This approach does NOT work, since f<int>(1); |
| 2201 | // gets resolved prior to resorting to overload resolution |
| 2202 | // i.e., template<class T> void f(double); |
| 2203 | // vs template<class T, class U> void f(U); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2204 | |
| 2205 | // These should be filtered out by our callers. |
| 2206 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2207 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2208 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2209 | // We don't want lookup warnings at this point. |
| 2210 | R.suppressDiagnostics(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2211 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2212 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2213 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2214 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2215 | R.getLookupNameInfo(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2216 | RequiresADL, TemplateArgs, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2217 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2218 | |
| 2219 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2222 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2223 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2224 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2225 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2226 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2227 | DeclContext *DC; |
| 2228 | if (!(DC = computeDeclContext(SS, false)) || |
| 2229 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2230 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2231 | return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2232 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2233 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2234 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2235 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 2236 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2237 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2238 | if (R.isAmbiguous()) |
| 2239 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2240 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2241 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2242 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2243 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2244 | return ExprError(); |
| 2245 | } |
| 2246 | |
| 2247 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2248 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 2249 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 2250 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2251 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2252 | return ExprError(); |
| 2253 | } |
| 2254 | |
| 2255 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2258 | /// \brief Form a dependent template name. |
| 2259 | /// |
| 2260 | /// This action forms a dependent template name given the template |
| 2261 | /// name and its (presumably dependent) scope specifier. For |
| 2262 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2263 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2264 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2265 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2266 | SourceLocation TemplateKWLoc, |
| 2267 | CXXScopeSpec &SS, |
| 2268 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2269 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2270 | bool EnteringContext, |
| 2271 | TemplateTy &Result) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2272 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 2273 | Diag(TemplateKWLoc, |
| 2274 | getLangOptions().CPlusPlus0x ? |
| 2275 | diag::warn_cxx98_compat_template_outside_of_template : |
| 2276 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2277 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2278 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2279 | DeclContext *LookupCtx = 0; |
| 2280 | if (SS.isSet()) |
| 2281 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2282 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2283 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2284 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2285 | // C++0x [temp.names]p5: |
| 2286 | // If a name prefixed by the keyword template is not the name of |
| 2287 | // a template, the program is ill-formed. [Note: the keyword |
| 2288 | // template may not be applied to non-template members of class |
| 2289 | // templates. -end note ] [ Note: as is the case with the |
| 2290 | // typename prefix, the template prefix is allowed in cases |
| 2291 | // where it is not strictly necessary; i.e., when the |
| 2292 | // nested-name-specifier or the expression on the left of the -> |
| 2293 | // or . is not dependent on a template-parameter, or the use |
| 2294 | // does not appear in the scope of a template. -end note] |
| 2295 | // |
| 2296 | // Note: C++03 was more strict here, because it banned the use of |
| 2297 | // the "template" keyword prior to a template-name that was not a |
| 2298 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2299 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2300 | // 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] | 2301 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2302 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 2303 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2304 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2305 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2306 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | d078bd2 | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2307 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2308 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2309 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2310 | } else if (TNK == TNK_Non_template) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2311 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2312 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2313 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2314 | << Name.getSourceRange() |
| 2315 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2316 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2317 | } else { |
| 2318 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2319 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2320 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2323 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2324 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2325 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2326 | switch (Name.getKind()) { |
| 2327 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2328 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2329 | Name.Identifier)); |
| 2330 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2331 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2332 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2333 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2334 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2335 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2336 | |
| 2337 | case UnqualifiedId::IK_LiteralOperatorId: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2338 | llvm_unreachable( |
| 2339 | "We don't support these; Parse shouldn't have allowed propagation"); |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2340 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2341 | default: |
| 2342 | break; |
| 2343 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2344 | |
| 2345 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2346 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2347 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2348 | << Name.getSourceRange() |
| 2349 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2350 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2353 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2354 | const TemplateArgumentLoc &AL, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2355 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2356 | const TemplateArgument &Arg = AL.getArgument(); |
| 2357 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2358 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2359 | switch(Arg.getKind()) { |
| 2360 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2361 | // C++ [temp.arg.type]p1: |
| 2362 | // A template-argument for a template-parameter which is a |
| 2363 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2364 | break; |
| 2365 | case TemplateArgument::Template: { |
| 2366 | // We have a template type parameter but the template argument |
| 2367 | // is a template without any arguments. |
| 2368 | SourceRange SR = AL.getSourceRange(); |
| 2369 | TemplateName Name = Arg.getAsTemplate(); |
| 2370 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 2371 | << Name << SR; |
| 2372 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 2373 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2374 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2375 | return true; |
| 2376 | } |
| 2377 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2378 | // We have a template type parameter but the template argument |
| 2379 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 2380 | SourceRange SR = AL.getSourceRange(); |
| 2381 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2382 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2383 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2384 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2385 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2386 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2387 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2388 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2389 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2390 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2391 | // Add the converted template type argument. |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 2392 | QualType ArgType = Context.getCanonicalType(Arg.getAsType()); |
| 2393 | |
| 2394 | // Objective-C ARC: |
| 2395 | // If an explicitly-specified template argument type is a lifetime type |
| 2396 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
| 2397 | if (getLangOptions().ObjCAutoRefCount && |
| 2398 | ArgType->isObjCLifetimeType() && |
| 2399 | !ArgType.getObjCLifetime()) { |
| 2400 | Qualifiers Qs; |
| 2401 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 2402 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 2403 | } |
| 2404 | |
| 2405 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2406 | return false; |
| 2407 | } |
| 2408 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2409 | /// \brief Substitute template arguments into the default template argument for |
| 2410 | /// the given template type parameter. |
| 2411 | /// |
| 2412 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2413 | /// the substitution. |
| 2414 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2415 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2416 | /// for. |
| 2417 | /// |
| 2418 | /// \param TemplateLoc the location of the template name that started the |
| 2419 | /// template-id we are checking. |
| 2420 | /// |
| 2421 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2422 | /// terminates the template-id. |
| 2423 | /// |
| 2424 | /// \param Param the template template parameter whose default we are |
| 2425 | /// substituting into. |
| 2426 | /// |
| 2427 | /// \param Converted the list of template arguments provided for template |
| 2428 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2429 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2430 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2431 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2432 | TemplateDecl *Template, |
| 2433 | SourceLocation TemplateLoc, |
| 2434 | SourceLocation RAngleLoc, |
| 2435 | TemplateTypeParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2436 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2437 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2438 | |
| 2439 | // If the argument type is dependent, instantiate it now based |
| 2440 | // on the previously-computed template arguments. |
| 2441 | if (ArgType->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2442 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2443 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2444 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2445 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2446 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2447 | |
| 2448 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2449 | Template, Converted.data(), |
| 2450 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2451 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2452 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2453 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2454 | Param->getDefaultArgumentLoc(), |
| 2455 | Param->getDeclName()); |
| 2456 | } |
| 2457 | |
| 2458 | return ArgType; |
| 2459 | } |
| 2460 | |
| 2461 | /// \brief Substitute template arguments into the default template argument for |
| 2462 | /// the given non-type template parameter. |
| 2463 | /// |
| 2464 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2465 | /// the substitution. |
| 2466 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2467 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2468 | /// for. |
| 2469 | /// |
| 2470 | /// \param TemplateLoc the location of the template name that started the |
| 2471 | /// template-id we are checking. |
| 2472 | /// |
| 2473 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2474 | /// terminates the template-id. |
| 2475 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2476 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2477 | /// substituting into. |
| 2478 | /// |
| 2479 | /// \param Converted the list of template arguments provided for template |
| 2480 | /// parameters that precede \p Param in the template parameter list. |
| 2481 | /// |
| 2482 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2483 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2484 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2485 | TemplateDecl *Template, |
| 2486 | SourceLocation TemplateLoc, |
| 2487 | SourceLocation RAngleLoc, |
| 2488 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2489 | SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2490 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2491 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2492 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2493 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2494 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2495 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2496 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2497 | Template, Converted.data(), |
| 2498 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2499 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2500 | |
| 2501 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2502 | } |
| 2503 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2504 | /// \brief Substitute template arguments into the default template argument for |
| 2505 | /// the given template template parameter. |
| 2506 | /// |
| 2507 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2508 | /// the substitution. |
| 2509 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2510 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2511 | /// for. |
| 2512 | /// |
| 2513 | /// \param TemplateLoc the location of the template name that started the |
| 2514 | /// template-id we are checking. |
| 2515 | /// |
| 2516 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2517 | /// terminates the template-id. |
| 2518 | /// |
| 2519 | /// \param Param the template template parameter whose default we are |
| 2520 | /// substituting into. |
| 2521 | /// |
| 2522 | /// \param Converted the list of template arguments provided for template |
| 2523 | /// parameters that precede \p Param in the template parameter list. |
| 2524 | /// |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2525 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 2526 | /// source-location information) that precedes the template name. |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2527 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2528 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2529 | static TemplateName |
| 2530 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2531 | TemplateDecl *Template, |
| 2532 | SourceLocation TemplateLoc, |
| 2533 | SourceLocation RAngleLoc, |
| 2534 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2535 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2536 | NestedNameSpecifierLoc &QualifierLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2537 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2538 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2539 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2540 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2541 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2542 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2543 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2544 | Template, Converted.data(), |
| 2545 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2546 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2547 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2548 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2549 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2550 | if (QualifierLoc) { |
| 2551 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2552 | AllTemplateArgs); |
| 2553 | if (!QualifierLoc) |
| 2554 | return TemplateName(); |
| 2555 | } |
| 2556 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2557 | return SemaRef.SubstTemplateName(QualifierLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2558 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2559 | Param->getDefaultArgument().getTemplateNameLoc(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2560 | AllTemplateArgs); |
| 2561 | } |
| 2562 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2563 | /// \brief If the given template parameter has a default template |
| 2564 | /// argument, substitute into that default template argument and |
| 2565 | /// return the corresponding template argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2566 | TemplateArgumentLoc |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2567 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2568 | SourceLocation TemplateLoc, |
| 2569 | SourceLocation RAngleLoc, |
| 2570 | Decl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2571 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2572 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2573 | if (!TypeParm->hasDefaultArgument()) |
| 2574 | return TemplateArgumentLoc(); |
| 2575 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2576 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2577 | TemplateLoc, |
| 2578 | RAngleLoc, |
| 2579 | TypeParm, |
| 2580 | Converted); |
| 2581 | if (DI) |
| 2582 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2583 | |
| 2584 | return TemplateArgumentLoc(); |
| 2585 | } |
| 2586 | |
| 2587 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2588 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2589 | if (!NonTypeParm->hasDefaultArgument()) |
| 2590 | return TemplateArgumentLoc(); |
| 2591 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2592 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2593 | TemplateLoc, |
| 2594 | RAngleLoc, |
| 2595 | NonTypeParm, |
| 2596 | Converted); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2597 | if (Arg.isInvalid()) |
| 2598 | return TemplateArgumentLoc(); |
| 2599 | |
| 2600 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2601 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2602 | } |
| 2603 | |
| 2604 | TemplateTemplateParmDecl *TempTempParm |
| 2605 | = cast<TemplateTemplateParmDecl>(Param); |
| 2606 | if (!TempTempParm->hasDefaultArgument()) |
| 2607 | return TemplateArgumentLoc(); |
| 2608 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2609 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2610 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2611 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2612 | TemplateLoc, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2613 | RAngleLoc, |
| 2614 | TempTempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2615 | Converted, |
| 2616 | QualifierLoc); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2617 | if (TName.isNull()) |
| 2618 | return TemplateArgumentLoc(); |
| 2619 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2620 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2621 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2622 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2623 | } |
| 2624 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2625 | /// \brief Check that the given template argument corresponds to the given |
| 2626 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2627 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2628 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2629 | /// checked. |
| 2630 | /// |
| 2631 | /// \param Arg The template argument. |
| 2632 | /// |
| 2633 | /// \param Template The template in which the template argument resides. |
| 2634 | /// |
| 2635 | /// \param TemplateLoc The location of the template name for the template |
| 2636 | /// whose argument list we're matching. |
| 2637 | /// |
| 2638 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2639 | /// the template argument list. |
| 2640 | /// |
| 2641 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2642 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2643 | /// |
| 2644 | /// \param Converted The checked, converted argument will be added to the |
| 2645 | /// end of this small vector. |
| 2646 | /// |
| 2647 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2648 | /// explicitly written, deduced, etc. |
| 2649 | /// |
| 2650 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2651 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2652 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2653 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2654 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2655 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2656 | unsigned ArgumentPackIndex, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2657 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2658 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2659 | // Check template type parameters. |
| 2660 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2661 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2662 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2663 | // Check non-type template parameters. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2664 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2665 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2666 | // with the template arguments we've seen thus far. But if the |
| 2667 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2668 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2669 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2670 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2671 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2672 | if (NTTPType->isDependentType() && |
| 2673 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2674 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2675 | // Do substitution on the type of the non-type template parameter. |
| 2676 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2677 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2678 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2679 | |
| 2680 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2681 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2682 | NTTPType = SubstType(NTTPType, |
| 2683 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2684 | NTTP->getLocation(), |
| 2685 | NTTP->getDeclName()); |
| 2686 | // If that worked, check the non-type template parameter type |
| 2687 | // for validity. |
| 2688 | if (!NTTPType.isNull()) |
| 2689 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2690 | NTTP->getLocation()); |
| 2691 | if (NTTPType.isNull()) |
| 2692 | return true; |
| 2693 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2694 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2695 | switch (Arg.getArgument().getKind()) { |
| 2696 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2697 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2698 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2699 | case TemplateArgument::Expression: { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2700 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2701 | ExprResult Res = |
| 2702 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 2703 | Result, CTAK); |
| 2704 | if (Res.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2705 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2706 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2707 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2708 | break; |
| 2709 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2710 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2711 | case TemplateArgument::Declaration: |
| 2712 | case TemplateArgument::Integral: |
| 2713 | // We've already checked this template argument, so just copy |
| 2714 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2715 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2716 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2717 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2718 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2719 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2720 | // We were given a template template argument. It may not be ill-formed; |
| 2721 | // see below. |
| 2722 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2723 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2724 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2725 | // We have a template argument such as \c T::template X, which we |
| 2726 | // parsed as a template template argument. However, since we now |
| 2727 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2728 | // template name into an expression. |
| 2729 | |
| 2730 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2731 | Arg.getTemplateNameLoc()); |
| 2732 | |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2733 | CXXScopeSpec SS; |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2734 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2735 | ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2736 | SS.getWithLocInContext(Context), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2737 | NameInfo)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2738 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2739 | // If we parsed the template argument as a pack expansion, create a |
| 2740 | // pack expansion expression. |
| 2741 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2742 | E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc()); |
| 2743 | if (E.isInvalid()) |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2744 | return true; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2745 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2746 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2747 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2748 | E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result); |
| 2749 | if (E.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2750 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2751 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2752 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2753 | break; |
| 2754 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2755 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2756 | // We have a template argument that actually does refer to a class |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2757 | // template, alias template, or template template parameter, and |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2758 | // therefore cannot be a non-type template argument. |
| 2759 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2760 | << Arg.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2761 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2762 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2763 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2764 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2765 | case TemplateArgument::Type: { |
| 2766 | // We have a non-type template parameter but the template |
| 2767 | // argument is a type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2768 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2769 | // C++ [temp.arg]p2: |
| 2770 | // In a template-argument, an ambiguity between a type-id and |
| 2771 | // an expression is resolved to a type-id, regardless of the |
| 2772 | // form of the corresponding template-parameter. |
| 2773 | // |
| 2774 | // We warn specifically about this case, since it can be rather |
| 2775 | // confusing for users. |
| 2776 | QualType T = Arg.getArgument().getAsType(); |
| 2777 | SourceRange SR = Arg.getSourceRange(); |
| 2778 | if (T->isFunctionType()) |
| 2779 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2780 | else |
| 2781 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2782 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2783 | return true; |
| 2784 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2785 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2786 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2787 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2788 | break; |
| 2789 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2790 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2791 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
| 2794 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2795 | // Check template template parameters. |
| 2796 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2797 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2798 | // Substitute into the template parameter list of the template |
| 2799 | // template parameter, since previously-supplied template arguments |
| 2800 | // may appear within the template template parameter. |
| 2801 | { |
| 2802 | // Set up a template instantiation context. |
| 2803 | LocalInstantiationScope Scope(*this); |
| 2804 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2805 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2806 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2807 | |
| 2808 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2809 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2810 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2811 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2812 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2813 | if (!TempParm) |
| 2814 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2815 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2816 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2817 | switch (Arg.getArgument().getKind()) { |
| 2818 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2819 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2820 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2821 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2822 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2823 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2824 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2825 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2826 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2827 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2828 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2829 | case TemplateArgument::Expression: |
| 2830 | case TemplateArgument::Type: |
| 2831 | // We have a template template parameter but the template |
| 2832 | // argument does not refer to a template. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2833 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
| 2834 | << getLangOptions().CPlusPlus0x; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2835 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2836 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2837 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2838 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2839 | "Declaration argument with template template parameter"); |
| 2840 | break; |
| 2841 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2842 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2843 | "Integral argument with template template parameter"); |
| 2844 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2845 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2846 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2847 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2848 | break; |
| 2849 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2850 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2851 | return false; |
| 2852 | } |
| 2853 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2854 | /// \brief Check that the given template argument list is well-formed |
| 2855 | /// for specializing the given template. |
| 2856 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2857 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2858 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2859 | bool PartialTemplateArgs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2860 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2861 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2862 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2863 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2864 | bool Invalid = false; |
| 2865 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2866 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2867 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2868 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2869 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2870 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2871 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2872 | (NumArgs < Params->getMinRequiredArguments() && |
| 2873 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2874 | // FIXME: point at either the first arg beyond what we can handle, |
| 2875 | // or the '>', depending on whether we have too many or too few |
| 2876 | // arguments. |
| 2877 | SourceRange Range; |
| 2878 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2879 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2880 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2881 | << (NumArgs > NumParams) |
| 2882 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2883 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2884 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2885 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2886 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2887 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2888 | Invalid = true; |
| 2889 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2890 | |
| 2891 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2892 | // [...] The type and form of each template-argument specified in |
| 2893 | // a template-id shall match the type and form specified for the |
| 2894 | // corresponding parameter declared by the template in its |
| 2895 | // template-parameter-list. |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2896 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2897 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2898 | TemplateParameterList::iterator Param = Params->begin(), |
| 2899 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2900 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 2901 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2902 | while (Param != ParamEnd) { |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2903 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2904 | // If we have an expanded parameter pack, make sure we don't have too |
| 2905 | // many arguments. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2906 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2907 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2908 | if (NTTP->isExpandedParameterPack() && |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2909 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 2910 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2911 | << true |
| 2912 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2913 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2914 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2915 | << Template; |
| 2916 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2917 | << Params->getSourceRange(); |
| 2918 | return true; |
| 2919 | } |
| 2920 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2921 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2922 | // Check the template argument we were given. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2923 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2924 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2925 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2926 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2927 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2928 | if ((*Param)->isTemplateParameterPack()) { |
| 2929 | // The template parameter was a template parameter pack, so take the |
| 2930 | // deduced argument and place it on the argument pack. Note that we |
| 2931 | // stay on the same template parameter so that we can deduce more |
| 2932 | // arguments. |
| 2933 | ArgumentPack.push_back(Converted.back()); |
| 2934 | Converted.pop_back(); |
| 2935 | } else { |
| 2936 | // Move to the next template parameter. |
| 2937 | ++Param; |
| 2938 | } |
| 2939 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2940 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2941 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2942 | |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2943 | // If we're checking a partial template argument list, we're done. |
| 2944 | if (PartialTemplateArgs) { |
| 2945 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 2946 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 2947 | ArgumentPack.data(), |
| 2948 | ArgumentPack.size())); |
| 2949 | |
| 2950 | return Invalid; |
| 2951 | } |
| 2952 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2953 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2954 | // arguments, just break out now and we'll fill in the argument pack below. |
| 2955 | if ((*Param)->isTemplateParameterPack()) |
| 2956 | break; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2957 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2958 | // We have a default template argument that we will use. |
| 2959 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2960 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2961 | // Retrieve the default template argument from the template |
| 2962 | // parameter. For each kind of template parameter, we substitute the |
| 2963 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2964 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2965 | // the default argument. |
| 2966 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2967 | if (!TTP->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2968 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2969 | break; |
| 2970 | } |
| 2971 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2972 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2973 | Template, |
| 2974 | TemplateLoc, |
| 2975 | RAngleLoc, |
| 2976 | TTP, |
| 2977 | Converted); |
| 2978 | if (!ArgType) |
| 2979 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2980 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2981 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2982 | ArgType); |
| 2983 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2984 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2985 | if (!NTTP->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2986 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2987 | break; |
| 2988 | } |
| 2989 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2990 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2991 | TemplateLoc, |
| 2992 | RAngleLoc, |
| 2993 | NTTP, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2994 | Converted); |
| 2995 | if (E.isInvalid()) |
| 2996 | return true; |
| 2997 | |
| 2998 | Expr *Ex = E.takeAs<Expr>(); |
| 2999 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 3000 | } else { |
| 3001 | TemplateTemplateParmDecl *TempParm |
| 3002 | = cast<TemplateTemplateParmDecl>(*Param); |
| 3003 | |
| 3004 | if (!TempParm->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3005 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3006 | break; |
| 3007 | } |
| 3008 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3009 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3010 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3011 | TemplateLoc, |
| 3012 | RAngleLoc, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3013 | TempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3014 | Converted, |
| 3015 | QualifierLoc); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3016 | if (Name.isNull()) |
| 3017 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3018 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3019 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3020 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3021 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3022 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3023 | // Introduce an instantiation record that describes where we are using |
| 3024 | // the default template argument. |
| 3025 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3026 | Converted.data(), Converted.size(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3027 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3028 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3029 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3030 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3031 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3032 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3033 | |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3034 | // Core issue 150 (assumed resolution): if this is a template template |
| 3035 | // parameter, keep track of the default template arguments from the |
| 3036 | // template definition. |
| 3037 | if (isTemplateTemplateParameter) |
| 3038 | TemplateArgs.addArgument(Arg); |
| 3039 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3040 | // Move to the next template parameter and argument. |
| 3041 | ++Param; |
| 3042 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3043 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3044 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3045 | // Form argument packs for each of the parameter packs remaining. |
| 3046 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3047 | // If we're checking a partial list of template arguments, don't fill |
| 3048 | // in arguments for non-template parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3049 | |
| 3050 | if ((*Param)->isTemplateParameterPack()) { |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 3051 | if (!HasParameterPack) |
| 3052 | return true; |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3053 | if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3054 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3055 | else { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3056 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3057 | ArgumentPack.data(), |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3058 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3059 | ArgumentPack.clear(); |
| 3060 | } |
| 3061 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3062 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3063 | ++Param; |
| 3064 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3065 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3066 | return Invalid; |
| 3067 | } |
| 3068 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3069 | namespace { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3070 | class UnnamedLocalNoLinkageFinder |
| 3071 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3072 | { |
| 3073 | Sema &S; |
| 3074 | SourceRange SR; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3075 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3076 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3077 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3078 | public: |
| 3079 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3080 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3081 | bool Visit(QualType T) { |
| 3082 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3083 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3084 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3085 | #define TYPE(Class, Parent) \ |
| 3086 | bool Visit##Class##Type(const Class##Type *); |
| 3087 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 3088 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3089 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 3090 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3091 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3092 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3093 | bool VisitTagDecl(const TagDecl *Tag); |
| 3094 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3095 | }; |
| 3096 | } |
| 3097 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3098 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3099 | return false; |
| 3100 | } |
| 3101 | |
| 3102 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3103 | return Visit(T->getElementType()); |
| 3104 | } |
| 3105 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3106 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3107 | return Visit(T->getPointeeType()); |
| 3108 | } |
| 3109 | |
| 3110 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3111 | const BlockPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3112 | return Visit(T->getPointeeType()); |
| 3113 | } |
| 3114 | |
| 3115 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3116 | const LValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3117 | return Visit(T->getPointeeType()); |
| 3118 | } |
| 3119 | |
| 3120 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3121 | const RValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3122 | return Visit(T->getPointeeType()); |
| 3123 | } |
| 3124 | |
| 3125 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3126 | const MemberPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3127 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3128 | } |
| 3129 | |
| 3130 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3131 | const ConstantArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3132 | return Visit(T->getElementType()); |
| 3133 | } |
| 3134 | |
| 3135 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3136 | const IncompleteArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3137 | return Visit(T->getElementType()); |
| 3138 | } |
| 3139 | |
| 3140 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3141 | const VariableArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3142 | return Visit(T->getElementType()); |
| 3143 | } |
| 3144 | |
| 3145 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3146 | const DependentSizedArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3147 | return Visit(T->getElementType()); |
| 3148 | } |
| 3149 | |
| 3150 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3151 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3152 | return Visit(T->getElementType()); |
| 3153 | } |
| 3154 | |
| 3155 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3156 | return Visit(T->getElementType()); |
| 3157 | } |
| 3158 | |
| 3159 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 3160 | return Visit(T->getElementType()); |
| 3161 | } |
| 3162 | |
| 3163 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 3164 | const FunctionProtoType* T) { |
| 3165 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3166 | AEnd = T->arg_type_end(); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3167 | A != AEnd; ++A) { |
| 3168 | if (Visit(*A)) |
| 3169 | return true; |
| 3170 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3171 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3172 | return Visit(T->getResultType()); |
| 3173 | } |
| 3174 | |
| 3175 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 3176 | const FunctionNoProtoType* T) { |
| 3177 | return Visit(T->getResultType()); |
| 3178 | } |
| 3179 | |
| 3180 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 3181 | const UnresolvedUsingType*) { |
| 3182 | return false; |
| 3183 | } |
| 3184 | |
| 3185 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 3186 | return false; |
| 3187 | } |
| 3188 | |
| 3189 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 3190 | return Visit(T->getUnderlyingType()); |
| 3191 | } |
| 3192 | |
| 3193 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 3194 | return false; |
| 3195 | } |
| 3196 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3197 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 3198 | const UnaryTransformType*) { |
| 3199 | return false; |
| 3200 | } |
| 3201 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3202 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 3203 | return Visit(T->getDeducedType()); |
| 3204 | } |
| 3205 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3206 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 3207 | return VisitTagDecl(T->getDecl()); |
| 3208 | } |
| 3209 | |
| 3210 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 3211 | return VisitTagDecl(T->getDecl()); |
| 3212 | } |
| 3213 | |
| 3214 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 3215 | const TemplateTypeParmType*) { |
| 3216 | return false; |
| 3217 | } |
| 3218 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3219 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 3220 | const SubstTemplateTypeParmPackType *) { |
| 3221 | return false; |
| 3222 | } |
| 3223 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3224 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 3225 | const TemplateSpecializationType*) { |
| 3226 | return false; |
| 3227 | } |
| 3228 | |
| 3229 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 3230 | const InjectedClassNameType* T) { |
| 3231 | return VisitTagDecl(T->getDecl()); |
| 3232 | } |
| 3233 | |
| 3234 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 3235 | const DependentNameType* T) { |
| 3236 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3237 | } |
| 3238 | |
| 3239 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 3240 | const DependentTemplateSpecializationType* T) { |
| 3241 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3242 | } |
| 3243 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3244 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 3245 | const PackExpansionType* T) { |
| 3246 | return Visit(T->getPattern()); |
| 3247 | } |
| 3248 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3249 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 3250 | return false; |
| 3251 | } |
| 3252 | |
| 3253 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 3254 | const ObjCInterfaceType *) { |
| 3255 | return false; |
| 3256 | } |
| 3257 | |
| 3258 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 3259 | const ObjCObjectPointerType *) { |
| 3260 | return false; |
| 3261 | } |
| 3262 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3263 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 3264 | return Visit(T->getValueType()); |
| 3265 | } |
| 3266 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3267 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 3268 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3269 | S.Diag(SR.getBegin(), |
| 3270 | S.getLangOptions().CPlusPlus0x ? |
| 3271 | diag::warn_cxx98_compat_template_arg_local_type : |
| 3272 | diag::ext_template_arg_local_type) |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3273 | << S.Context.getTypeDeclType(Tag) << SR; |
| 3274 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3275 | } |
| 3276 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3277 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3278 | S.Diag(SR.getBegin(), |
| 3279 | S.getLangOptions().CPlusPlus0x ? |
| 3280 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 3281 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3282 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 3283 | return true; |
| 3284 | } |
| 3285 | |
| 3286 | return false; |
| 3287 | } |
| 3288 | |
| 3289 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 3290 | NestedNameSpecifier *NNS) { |
| 3291 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 3292 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3293 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3294 | switch (NNS->getKind()) { |
| 3295 | case NestedNameSpecifier::Identifier: |
| 3296 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3297 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3298 | case NestedNameSpecifier::Global: |
| 3299 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3300 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3301 | case NestedNameSpecifier::TypeSpec: |
| 3302 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3303 | return Visit(QualType(NNS->getAsType(), 0)); |
| 3304 | } |
Fariborz Jahanian | 7b1ec6c | 2010-10-13 16:19:16 +0000 | [diff] [blame] | 3305 | return false; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3306 | } |
| 3307 | |
| 3308 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3309 | /// \brief Check a template argument against its corresponding |
| 3310 | /// template type parameter. |
| 3311 | /// |
| 3312 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 3313 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3314 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3315 | TypeSourceInfo *ArgInfo) { |
| 3316 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3317 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 3318 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 3319 | |
| 3320 | if (Arg->isVariablyModifiedType()) { |
| 3321 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3322 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3323 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3326 | // C++03 [temp.arg.type]p2: |
| 3327 | // A local type, a type with no linkage, an unnamed type or a type |
| 3328 | // compounded from any of these types shall not be used as a |
| 3329 | // template-argument for a template type-parameter. |
| 3330 | // |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3331 | // C++11 allows these, and even in C++03 we allow them as an extension with |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3332 | // a warning. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3333 | if (LangOpts.CPlusPlus0x ? |
| 3334 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 3335 | SR.getBegin()) != DiagnosticsEngine::Ignored || |
| 3336 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type, |
| 3337 | SR.getBegin()) != DiagnosticsEngine::Ignored : |
| 3338 | Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3339 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 3340 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 3341 | } |
| 3342 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3343 | return false; |
| 3344 | } |
| 3345 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3346 | /// \brief Checks whether the given template argument is the address |
| 3347 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3348 | static bool |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3349 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 3350 | NonTypeTemplateParmDecl *Param, |
| 3351 | QualType ParamType, |
| 3352 | Expr *ArgIn, |
| 3353 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3354 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3355 | Expr *Arg = ArgIn; |
| 3356 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3357 | |
| 3358 | // See through any implicit casts we added to fix the type. |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3359 | Arg = Arg->IgnoreImpCasts(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3360 | |
| 3361 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3362 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3363 | // A template-argument for a non-type, non-template |
| 3364 | // template-parameter shall be one of: [...] |
| 3365 | // |
| 3366 | // -- the address of an object or function with external |
| 3367 | // linkage, including function templates and function |
| 3368 | // template-ids but excluding non-static class members, |
| 3369 | // expressed as & id-expression where the & is optional if |
| 3370 | // the name refers to a function or array, or if the |
| 3371 | // corresponding template-parameter is a reference; or |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3372 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3373 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3374 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3375 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3376 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3377 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3378 | S.Diag(Arg->getSourceRange().getBegin(), |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3379 | S.getLangOptions().CPlusPlus0x ? |
| 3380 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3381 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3382 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3383 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
| 3386 | Arg = Parens->getSubExpr(); |
| 3387 | } |
| 3388 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3389 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3390 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3391 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3392 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3393 | bool AddressTaken = false; |
| 3394 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3395 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3396 | if (UnOp->getOpcode() == UO_AddrOf) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3397 | Arg = UnOp->getSubExpr(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3398 | AddressTaken = true; |
| 3399 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 3400 | } |
Francois Pichet | a343a41 | 2011-04-29 09:08:14 +0000 | [diff] [blame] | 3401 | } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3402 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3403 | if (S.getLangOptions().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3404 | Converted = TemplateArgument(ArgIn); |
| 3405 | return false; |
| 3406 | } |
| 3407 | |
| 3408 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3409 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3410 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3411 | |
| 3412 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3413 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3414 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 3415 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3416 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3417 | return true; |
| 3418 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3419 | |
| 3420 | // Stop checking the precise nature of the argument if it is value dependent, |
| 3421 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3422 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3423 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3424 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3425 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3426 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3427 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 3428 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3429 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3430 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3431 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3432 | return true; |
| 3433 | } |
| 3434 | |
| 3435 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3436 | |
| 3437 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3438 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 3439 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3440 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3441 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3442 | return true; |
| 3443 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3444 | |
| 3445 | // Cannot refer to non-static member functions |
| 3446 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3447 | if (!Method->isStatic()) { |
| 3448 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3449 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3450 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3451 | return true; |
| 3452 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3453 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3454 | // Functions must have external linkage. |
| 3455 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3456 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3457 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3458 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3459 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3460 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3461 | << true; |
| 3462 | return true; |
| 3463 | } |
| 3464 | |
| 3465 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3466 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3467 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3468 | // If the template parameter has pointer type, the function decays. |
| 3469 | if (ParamType->isPointerType() && !AddressTaken) |
| 3470 | ArgType = S.Context.getPointerType(Func->getType()); |
| 3471 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 3472 | // If we originally had an address-of operator, but the |
| 3473 | // parameter has reference type, complain and (if things look |
| 3474 | // like they will work) drop the address-of operator. |
| 3475 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 3476 | ParamType.getNonReferenceType())) { |
| 3477 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3478 | << ParamType; |
| 3479 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3480 | return true; |
| 3481 | } |
| 3482 | |
| 3483 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3484 | << ParamType |
| 3485 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3486 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3487 | |
| 3488 | ArgType = Func->getType(); |
| 3489 | } |
| 3490 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3491 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3492 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3493 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3494 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3495 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3496 | << true; |
| 3497 | return true; |
| 3498 | } |
| 3499 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3500 | // A value of reference type is not an object. |
| 3501 | if (Var->getType()->isReferenceType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3502 | S.Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3503 | diag::err_template_arg_reference_var) |
| 3504 | << Var->getType() << Arg->getSourceRange(); |
| 3505 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3506 | return true; |
| 3507 | } |
| 3508 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3509 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3510 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3511 | |
| 3512 | // If the template parameter has pointer type, we must have taken |
| 3513 | // the address of this object. |
| 3514 | if (ParamType->isReferenceType()) { |
| 3515 | if (AddressTaken) { |
| 3516 | // If we originally had an address-of operator, but the |
| 3517 | // parameter has reference type, complain and (if things look |
| 3518 | // like they will work) drop the address-of operator. |
| 3519 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3520 | ParamType.getNonReferenceType())) { |
| 3521 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3522 | << ParamType; |
| 3523 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3524 | return true; |
| 3525 | } |
| 3526 | |
| 3527 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3528 | << ParamType |
| 3529 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3530 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3531 | |
| 3532 | ArgType = Var->getType(); |
| 3533 | } |
| 3534 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3535 | if (Var->getType()->isArrayType()) { |
| 3536 | // Array-to-pointer decay. |
| 3537 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3538 | } else { |
| 3539 | // If the template parameter has pointer type but the address of |
| 3540 | // this object was not taken, complain and (possibly) recover by |
| 3541 | // taking the address of the entity. |
| 3542 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3543 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3544 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3545 | << ParamType; |
| 3546 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3547 | return true; |
| 3548 | } |
| 3549 | |
| 3550 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3551 | << ParamType |
| 3552 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3553 | |
| 3554 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3555 | } |
| 3556 | } |
| 3557 | } else { |
| 3558 | // We found something else, but we don't know specifically what it is. |
| 3559 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3560 | diag::err_template_arg_not_object_or_func) |
| 3561 | << Arg->getSourceRange(); |
| 3562 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3563 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3564 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3565 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3566 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3567 | if (ParamType->isPointerType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3568 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3569 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 3570 | ObjCLifetimeConversion)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3571 | // For pointer-to-object types, qualification conversions are |
| 3572 | // permitted. |
| 3573 | } else { |
| 3574 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3575 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3576 | // C++ [temp.arg.nontype]p5b3: |
| 3577 | // For a non-type template-parameter of type reference to |
| 3578 | // object, no conversions apply. The type referred to by the |
| 3579 | // reference may be more cv-qualified than the (otherwise |
| 3580 | // identical) type of the template- argument. The |
| 3581 | // template-parameter is bound directly to the |
| 3582 | // template-argument, which shall be an lvalue. |
| 3583 | |
| 3584 | // FIXME: Other qualifiers? |
| 3585 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3586 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3587 | |
| 3588 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 3589 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3590 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3591 | << ParamType << Arg->getType() |
| 3592 | << Arg->getSourceRange(); |
| 3593 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3594 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3595 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3596 | } |
| 3597 | } |
| 3598 | |
| 3599 | // At this point, the template argument refers to an object or |
| 3600 | // function with external linkage. We now need to check whether the |
| 3601 | // argument and parameter types are compatible. |
| 3602 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3603 | ParamType.getNonReferenceType())) { |
| 3604 | // We can't perform this conversion or binding. |
| 3605 | if (ParamType->isReferenceType()) |
| 3606 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3607 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3608 | else |
| 3609 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3610 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3611 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3612 | return true; |
| 3613 | } |
| 3614 | } |
| 3615 | |
| 3616 | // Create the template argument. |
| 3617 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 3618 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3619 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3620 | } |
| 3621 | |
| 3622 | /// \brief Checks whether the given template argument is a pointer to |
| 3623 | /// member constant according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3624 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3625 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3626 | bool Invalid = false; |
| 3627 | |
| 3628 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3629 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3630 | Arg = Cast->getSubExpr(); |
| 3631 | |
| 3632 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3633 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3634 | // A template-argument for a non-type, non-template |
| 3635 | // template-parameter shall be one of: [...] |
| 3636 | // |
| 3637 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3638 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3639 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3640 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3641 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3642 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3643 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3644 | if (!Invalid && !ExtraParens) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3645 | Diag(Arg->getSourceRange().getBegin(), |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3646 | getLangOptions().CPlusPlus0x ? |
| 3647 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3648 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3649 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3650 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
| 3653 | Arg = Parens->getSubExpr(); |
| 3654 | } |
| 3655 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3656 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3657 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3658 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3659 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3660 | // A pointer-to-member constant written &Class::member. |
| 3661 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3662 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3663 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3664 | if (DRE && !DRE->getQualifier()) |
| 3665 | DRE = 0; |
| 3666 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3667 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3668 | // A constant of pointer-to-member type. |
| 3669 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3670 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3671 | if (VD->getType()->isMemberPointerType()) { |
| 3672 | if (isa<NonTypeTemplateParmDecl>(VD) || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3673 | (isa<VarDecl>(VD) && |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3674 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3675 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3676 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3677 | else |
| 3678 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3679 | return Invalid; |
| 3680 | } |
| 3681 | } |
| 3682 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3683 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3684 | DRE = 0; |
| 3685 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3686 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3687 | if (!DRE) |
| 3688 | return Diag(Arg->getSourceRange().getBegin(), |
| 3689 | diag::err_template_arg_not_pointer_to_member_form) |
| 3690 | << Arg->getSourceRange(); |
| 3691 | |
| 3692 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3693 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3694 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3695 | "Only non-static member pointers can make it here"); |
| 3696 | |
| 3697 | // Okay: this is the address of a non-static member, and therefore |
| 3698 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3699 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3700 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3701 | else |
| 3702 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3703 | return Invalid; |
| 3704 | } |
| 3705 | |
| 3706 | // 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] | 3707 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3708 | diag::err_template_arg_not_pointer_to_member_form) |
| 3709 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3710 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3711 | diag::note_template_arg_refers_here); |
| 3712 | return true; |
| 3713 | } |
| 3714 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3715 | /// \brief Check a template argument against its corresponding |
| 3716 | /// non-type template parameter. |
| 3717 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3718 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3719 | /// If an error occurred, it returns ExprError(); otherwise, it |
| 3720 | /// returns the converted template argument. \p |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3721 | /// InstantiatedParamType is the type of the non-type template |
| 3722 | /// parameter after it has been instantiated. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3723 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 3724 | QualType InstantiatedParamType, Expr *Arg, |
| 3725 | TemplateArgument &Converted, |
| 3726 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3727 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3728 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3729 | // If either the parameter has a dependent type or the argument is |
| 3730 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3731 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3732 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3733 | Converted = TemplateArgument(Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3734 | return Owned(Arg); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3735 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3736 | |
| 3737 | // C++ [temp.arg.nontype]p5: |
| 3738 | // The following conversions are performed on each expression used |
| 3739 | // as a non-type template-argument. If a non-type |
| 3740 | // template-argument cannot be converted to the type of the |
| 3741 | // corresponding template-parameter then the program is |
| 3742 | // ill-formed. |
| 3743 | // |
| 3744 | // -- for a non-type template-parameter of integral or |
| 3745 | // enumeration type, integral promotions (4.5) and integral |
| 3746 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3747 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3748 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 3749 | // FIXME: In C++11, the argument is a converted constant expression of the |
| 3750 | // type of the template parameter. |
| 3751 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 3752 | if (ArgResult.isInvalid()) |
| 3753 | return ExprError(); |
| 3754 | Arg = ArgResult.take(); |
| 3755 | |
| 3756 | QualType ArgType = Arg->getType(); |
| 3757 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3758 | // C++ [temp.arg.nontype]p1: |
| 3759 | // A template-argument for a non-type, non-template |
| 3760 | // template-parameter shall be one of: |
| 3761 | // |
| 3762 | // -- an integral constant-expression of integral or enumeration |
| 3763 | // type; or |
| 3764 | // -- the name of a non-type template-parameter; or |
| 3765 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3766 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3767 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3768 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3769 | diag::err_template_arg_not_integral_or_enumeral) |
| 3770 | << ArgType << Arg->getSourceRange(); |
| 3771 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3772 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3773 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3774 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3775 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 3776 | << ArgType << Arg->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3777 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3780 | // From here on out, all we care about are the unqualified forms |
| 3781 | // of the parameter and argument types. |
| 3782 | ParamType = ParamType.getUnqualifiedType(); |
| 3783 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3784 | |
| 3785 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3786 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3787 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3788 | } else if (CTAK == CTAK_Deduced) { |
| 3789 | // C++ [temp.deduct.type]p17: |
| 3790 | // If, in the declaration of a function template with a non-type |
| 3791 | // template-parameter, the non-type template- parameter is used |
| 3792 | // in an expression in the function parameter-list and, if the |
| 3793 | // corresponding template-argument is deduced, the |
| 3794 | // template-argument type shall match the type of the |
| 3795 | // template-parameter exactly, except that a template-argument |
| 3796 | // deduced from an array bound may be of any integral type. |
| 3797 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3798 | << ArgType << ParamType; |
| 3799 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3800 | return ExprError(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3801 | } else if (ParamType->isBooleanType()) { |
| 3802 | // This is an integral-to-boolean conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3803 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3804 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3805 | !ParamType->isEnumeralType()) { |
| 3806 | // This is an integral promotion or conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3807 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3808 | } else { |
| 3809 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3810 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3811 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3812 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3813 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3814 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3815 | } |
| 3816 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3817 | // Add the value of this argument to the list of converted |
| 3818 | // arguments. We use the bitwidth and signedness of the template |
| 3819 | // parameter. |
| 3820 | if (Arg->isValueDependent()) { |
| 3821 | // The argument is value-dependent. Create a new |
| 3822 | // TemplateArgument with the converted expression. |
| 3823 | Converted = TemplateArgument(Arg); |
| 3824 | return Owned(Arg); |
| 3825 | } |
| 3826 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3827 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3828 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3829 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3830 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3831 | if (ParamType->isBooleanType()) { |
| 3832 | // Value must be zero or one. |
| 3833 | Value = Value != 0; |
| 3834 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 3835 | if (Value.getBitWidth() != AllowedBits) |
| 3836 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3837 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3838 | } else { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3839 | llvm::APSInt OldValue = Value; |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3840 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3841 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3842 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3843 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3844 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3845 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3846 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3847 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3848 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3849 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3850 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3851 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3852 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3853 | << Arg->getSourceRange(); |
| 3854 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3855 | } |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3856 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3857 | // Complain if we overflowed the template parameter's type. |
| 3858 | unsigned RequiredBits; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3859 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3860 | RequiredBits = OldValue.getActiveBits(); |
| 3861 | else if (OldValue.isUnsigned()) |
| 3862 | RequiredBits = OldValue.getActiveBits() + 1; |
| 3863 | else |
| 3864 | RequiredBits = OldValue.getMinSignedBits(); |
| 3865 | if (RequiredBits > AllowedBits) { |
| 3866 | Diag(Arg->getSourceRange().getBegin(), |
| 3867 | diag::warn_template_arg_too_large) |
| 3868 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3869 | << Arg->getSourceRange(); |
| 3870 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3871 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3872 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3873 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3874 | Converted = TemplateArgument(Value, |
Douglas Gregor | 6b63f55 | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 3875 | ParamType->isEnumeralType() |
| 3876 | ? Context.getCanonicalType(ParamType) |
| 3877 | : IntegerType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3878 | return Owned(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3879 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3880 | |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 3881 | QualType ArgType = Arg->getType(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3882 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 3883 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3884 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 3885 | // from a template argument of type std::nullptr_t to a non-type |
| 3886 | // template parameter of type pointer to object, pointer to |
| 3887 | // function, or pointer-to-member, respectively. |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 3888 | if (ArgType->isNullPtrType()) { |
| 3889 | if (ParamType->isPointerType() || ParamType->isMemberPointerType()) { |
| 3890 | Converted = TemplateArgument((NamedDecl *)0); |
| 3891 | return Owned(Arg); |
| 3892 | } |
| 3893 | |
| 3894 | if (ParamType->isNullPtrType()) { |
| 3895 | llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true); |
| 3896 | Converted = TemplateArgument(Zero, Context.NullPtrTy); |
| 3897 | return Owned(Arg); |
| 3898 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3899 | } |
| 3900 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3901 | // Handle pointer-to-function, reference-to-function, and |
| 3902 | // pointer-to-member-function all in (roughly) the same way. |
| 3903 | if (// -- For a non-type template-parameter of type pointer to |
| 3904 | // function, only the function-to-pointer conversion (4.3) is |
| 3905 | // applied. If the template-argument represents a set of |
| 3906 | // overloaded functions (or a pointer to such), the matching |
| 3907 | // function is selected from the set (13.4). |
| 3908 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3909 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3910 | // -- For a non-type template-parameter of type reference to |
| 3911 | // function, no conversions apply. If the template-argument |
| 3912 | // represents a set of overloaded functions, the matching |
| 3913 | // function is selected from the set (13.4). |
| 3914 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3915 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3916 | // -- For a non-type template-parameter of type pointer to |
| 3917 | // member function, no conversions apply. If the |
| 3918 | // template-argument represents a set of overloaded member |
| 3919 | // functions, the matching member function is selected from |
| 3920 | // the set (13.4). |
| 3921 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3922 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3923 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3924 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3925 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3926 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3927 | true, |
| 3928 | FoundResult)) { |
| 3929 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3930 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3931 | |
| 3932 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3933 | ArgType = Arg->getType(); |
| 3934 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3935 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3936 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3937 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3938 | if (!ParamType->isMemberPointerType()) { |
| 3939 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3940 | ParamType, |
| 3941 | Arg, Converted)) |
| 3942 | return ExprError(); |
| 3943 | return Owned(Arg); |
| 3944 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3945 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3946 | bool ObjCLifetimeConversion; |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3947 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3948 | false, ObjCLifetimeConversion)) { |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 3949 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, |
| 3950 | Arg->getValueKind()).take(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3951 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 3952 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3953 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3954 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3955 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3956 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3957 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3958 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3959 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3960 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3961 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 3962 | return ExprError(); |
| 3963 | return Owned(Arg); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 3966 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3967 | // -- for a non-type template-parameter of type pointer to |
| 3968 | // object, qualification conversions (4.4) and the |
| 3969 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3970 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3971 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3972 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3973 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3974 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3975 | ParamType, |
| 3976 | Arg, Converted)) |
| 3977 | return ExprError(); |
| 3978 | return Owned(Arg); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3979 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3980 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3981 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3982 | // -- For a non-type template-parameter of type reference to |
| 3983 | // object, no conversions apply. The type referred to by the |
| 3984 | // reference may be more cv-qualified than the (otherwise |
| 3985 | // identical) type of the template-argument. The |
| 3986 | // template-parameter is bound directly to the |
| 3987 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3988 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3989 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3990 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3991 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3992 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 3993 | ParamRefType->getPointeeType(), |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3994 | true, |
| 3995 | FoundResult)) { |
| 3996 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3997 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3998 | |
| 3999 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 4000 | ArgType = Arg->getType(); |
| 4001 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4002 | return ExprError(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4003 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4004 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4005 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4006 | ParamType, |
| 4007 | Arg, Converted)) |
| 4008 | return ExprError(); |
| 4009 | return Owned(Arg); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4010 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4011 | |
| 4012 | // -- For a non-type template-parameter of type pointer to data |
| 4013 | // member, qualification conversions (4.4) are applied. |
| 4014 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 4015 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4016 | bool ObjCLifetimeConversion; |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 4017 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4018 | // Types match exactly: nothing more to do here. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4019 | } else if (IsQualificationConversion(ArgType, ParamType, false, |
| 4020 | ObjCLifetimeConversion)) { |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 4021 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, |
| 4022 | Arg->getValueKind()).take(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4023 | } else { |
| 4024 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4025 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4026 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4027 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4028 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4029 | return ExprError(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4032 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 4033 | return ExprError(); |
| 4034 | return Owned(Arg); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
| 4037 | /// \brief Check a template argument against its corresponding |
| 4038 | /// template template parameter. |
| 4039 | /// |
| 4040 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 4041 | /// It returns true if an error occurred, and false otherwise. |
| 4042 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4043 | const TemplateArgumentLoc &Arg) { |
| 4044 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 4045 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 4046 | if (!Template) { |
| 4047 | // Any dependent template name is fine. |
| 4048 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 4049 | return false; |
| 4050 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4051 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4052 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4053 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4054 | // the name of a class template or an alias template, expressed as an |
| 4055 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4056 | // primary class templates are considered when matching the |
| 4057 | // template template argument with the corresponding parameter; |
| 4058 | // partial specializations are not considered even if their |
| 4059 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4060 | // |
| 4061 | // Note that we also allow template template parameters here, which |
| 4062 | // will happen when we are dealing with, e.g., class template |
| 4063 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4064 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4065 | !isa<TemplateTemplateParmDecl>(Template) && |
| 4066 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4067 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4068 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4069 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4070 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4071 | << Template; |
| 4072 | } |
| 4073 | |
| 4074 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 4075 | Param->getTemplateParameters(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4076 | true, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4077 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4078 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4081 | /// \brief Given a non-type template argument that refers to a |
| 4082 | /// declaration and the type of its corresponding non-type template |
| 4083 | /// parameter, produce an expression that properly refers to that |
| 4084 | /// declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4085 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4086 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 4087 | QualType ParamType, |
| 4088 | SourceLocation Loc) { |
| 4089 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 4090 | "Only declaration template arguments permitted here"); |
| 4091 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 4092 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4093 | if (VD->getDeclContext()->isRecord() && |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4094 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 4095 | // If the value is a class member, we might have a pointer-to-member. |
| 4096 | // Determine whether the non-type template template parameter is of |
| 4097 | // pointer-to-member type. If so, we need to build an appropriate |
| 4098 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 4099 | // would refer to the member itself. |
| 4100 | if (ParamType->isMemberPointerType()) { |
| 4101 | QualType ClassType |
| 4102 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 4103 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4104 | = NestedNameSpecifier::Create(Context, 0, false, |
| 4105 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4106 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 4107 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4108 | |
| 4109 | // The actual value-ness of this is unimportant, but for |
| 4110 | // internal consistency's sake, references to instance methods |
| 4111 | // are r-values. |
| 4112 | ExprValueKind VK = VK_LValue; |
| 4113 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 4114 | VK = VK_RValue; |
| 4115 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4116 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4117 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4118 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4119 | Loc, |
| 4120 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4121 | if (RefExpr.isInvalid()) |
| 4122 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4123 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4124 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4125 | |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4126 | // We might need to perform a trailing qualification conversion, since |
| 4127 | // the element type on the parameter could be more qualified than the |
| 4128 | // element type in the expression we constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4129 | bool ObjCLifetimeConversion; |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4130 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4131 | ParamType.getUnqualifiedType(), false, |
| 4132 | ObjCLifetimeConversion)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4133 | RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4134 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4135 | assert(!RefExpr.isInvalid() && |
| 4136 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4137 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4138 | return move(RefExpr); |
| 4139 | } |
| 4140 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4141 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4142 | QualType T = VD->getType().getNonReferenceType(); |
| 4143 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4144 | // When the non-type template parameter is a pointer, take the |
| 4145 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4146 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4147 | if (RefExpr.isInvalid()) |
| 4148 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4149 | |
| 4150 | if (T->isFunctionType() || T->isArrayType()) { |
| 4151 | // Decay functions and arrays. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4152 | RefExpr = DefaultFunctionArrayConversion(RefExpr.take()); |
| 4153 | if (RefExpr.isInvalid()) |
| 4154 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4155 | |
| 4156 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4157 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4158 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4159 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4160 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4163 | ExprValueKind VK = VK_RValue; |
| 4164 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4165 | // If the non-type template parameter has reference type, qualify the |
| 4166 | // resulting declaration reference with the extra qualifiers on the |
| 4167 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4168 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 4169 | VK = VK_LValue; |
| 4170 | T = Context.getQualifiedType(T, |
| 4171 | TargetRef->getPointeeType().getQualifiers()); |
| 4172 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4173 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4174 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
| 4177 | /// \brief Construct a new expression that refers to the given |
| 4178 | /// integral template argument with the given source-location |
| 4179 | /// information. |
| 4180 | /// |
| 4181 | /// This routine takes care of the mapping from an integral template |
| 4182 | /// argument (which may have any integral type) to the appropriate |
| 4183 | /// literal value. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4184 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4185 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 4186 | SourceLocation Loc) { |
| 4187 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4188 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4189 | QualType T = Arg.getIntegralType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4190 | if (T->isAnyCharacterType()) { |
| 4191 | CharacterLiteral::CharacterKind Kind; |
| 4192 | if (T->isWideCharType()) |
| 4193 | Kind = CharacterLiteral::Wide; |
| 4194 | else if (T->isChar16Type()) |
| 4195 | Kind = CharacterLiteral::UTF16; |
| 4196 | else if (T->isChar32Type()) |
| 4197 | Kind = CharacterLiteral::UTF32; |
| 4198 | else |
| 4199 | Kind = CharacterLiteral::Ascii; |
| 4200 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4201 | return Owned(new (Context) CharacterLiteral( |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4202 | Arg.getAsIntegral()->getZExtValue(), |
| 4203 | Kind, T, Loc)); |
| 4204 | } |
| 4205 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4206 | if (T->isBooleanType()) |
| 4207 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 4208 | Arg.getAsIntegral()->getBoolValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4209 | T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4210 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4211 | if (T->isNullPtrType()) |
| 4212 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 4213 | |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4214 | // If this is an enum type that we're instantiating, we need to use an integer |
| 4215 | // type the same size as the enumerator. We don't want to build an |
| 4216 | // IntegerLiteral with enum type. |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4217 | QualType BT; |
| 4218 | if (const EnumType *ET = T->getAs<EnumType>()) |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4219 | BT = ET->getDecl()->getIntegerType(); |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4220 | else |
| 4221 | BT = T; |
| 4222 | |
John McCall | 4e9272d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 4223 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
| 4224 | if (T->isEnumeralType()) { |
| 4225 | // FIXME: This is a hack. We need a better way to handle substituted |
| 4226 | // non-type template parameters. |
| 4227 | E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0, |
| 4228 | Context.getTrivialTypeSourceInfo(T, Loc), |
| 4229 | Loc, Loc); |
| 4230 | } |
| 4231 | |
| 4232 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4235 | /// \brief Match two template parameters within template parameter lists. |
| 4236 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 4237 | bool Complain, |
| 4238 | Sema::TemplateParameterListEqualKind Kind, |
| 4239 | SourceLocation TemplateArgLoc) { |
| 4240 | // Check the actual kind (type, non-type, template). |
| 4241 | if (Old->getKind() != New->getKind()) { |
| 4242 | if (Complain) { |
| 4243 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 4244 | if (TemplateArgLoc.isValid()) { |
| 4245 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4246 | NextDiag = diag::note_template_param_different_kind; |
| 4247 | } |
| 4248 | S.Diag(New->getLocation(), NextDiag) |
| 4249 | << (Kind != Sema::TPL_TemplateMatch); |
| 4250 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 4251 | << (Kind != Sema::TPL_TemplateMatch); |
| 4252 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4253 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4254 | return false; |
| 4255 | } |
| 4256 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4257 | // Check that both are parameter packs are neither are parameter packs. |
| 4258 | // However, if we are matching a template template argument to a |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4259 | // template template parameter, the template template parameter can have |
| 4260 | // a parameter pack where the template template argument does not. |
| 4261 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 4262 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4263 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4264 | if (Complain) { |
| 4265 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 4266 | if (TemplateArgLoc.isValid()) { |
| 4267 | S.Diag(TemplateArgLoc, |
| 4268 | diag::err_template_arg_template_params_mismatch); |
| 4269 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 4270 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4271 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4272 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 4273 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 4274 | : 2; |
| 4275 | S.Diag(New->getLocation(), NextDiag) |
| 4276 | << ParamKind << New->isParameterPack(); |
| 4277 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 4278 | << ParamKind << Old->isParameterPack(); |
| 4279 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4280 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4281 | return false; |
| 4282 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4283 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4284 | // For non-type template parameters, check the type of the parameter. |
| 4285 | if (NonTypeTemplateParmDecl *OldNTTP |
| 4286 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 4287 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4288 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4289 | // If we are matching a template template argument to a template |
| 4290 | // template parameter and one of the non-type template parameter types |
| 4291 | // is dependent, then we must wait until template instantiation time |
| 4292 | // to actually compare the arguments. |
| 4293 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4294 | (OldNTTP->getType()->isDependentType() || |
| 4295 | NewNTTP->getType()->isDependentType())) |
| 4296 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4297 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4298 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 4299 | if (Complain) { |
| 4300 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 4301 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4302 | S.Diag(TemplateArgLoc, |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4303 | diag::err_template_arg_template_params_mismatch); |
| 4304 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 4305 | } |
| 4306 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 4307 | << NewNTTP->getType() |
| 4308 | << (Kind != Sema::TPL_TemplateMatch); |
| 4309 | S.Diag(OldNTTP->getLocation(), |
| 4310 | diag::note_template_nontype_parm_prev_declaration) |
| 4311 | << OldNTTP->getType(); |
| 4312 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4313 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4314 | return false; |
| 4315 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4316 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4317 | return true; |
| 4318 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4319 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4320 | // For template template parameters, check the template parameter types. |
| 4321 | // The template parameter lists of template template |
| 4322 | // parameters must agree. |
| 4323 | if (TemplateTemplateParmDecl *OldTTP |
| 4324 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4325 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4326 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 4327 | OldTTP->getTemplateParameters(), |
| 4328 | Complain, |
| 4329 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4330 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4331 | : Kind), |
| 4332 | TemplateArgLoc); |
| 4333 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4334 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4335 | return true; |
| 4336 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4337 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4338 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 4339 | /// lists. |
| 4340 | static |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4341 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4342 | TemplateParameterList *New, |
| 4343 | TemplateParameterList *Old, |
| 4344 | Sema::TemplateParameterListEqualKind Kind, |
| 4345 | SourceLocation TemplateArgLoc) { |
| 4346 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 4347 | if (TemplateArgLoc.isValid()) { |
| 4348 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4349 | NextDiag = diag::note_template_param_list_different_arity; |
| 4350 | } |
| 4351 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 4352 | << (New->size() > Old->size()) |
| 4353 | << (Kind != Sema::TPL_TemplateMatch) |
| 4354 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 4355 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 4356 | << (Kind != Sema::TPL_TemplateMatch) |
| 4357 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 4358 | } |
| 4359 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4360 | /// \brief Determine whether the given template parameter lists are |
| 4361 | /// equivalent. |
| 4362 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4363 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4364 | /// source code as part of a new template declaration. |
| 4365 | /// |
| 4366 | /// \param Old The old template parameter list, typically found via |
| 4367 | /// name lookup of the template declared with this template parameter |
| 4368 | /// list. |
| 4369 | /// |
| 4370 | /// \param Complain If true, this routine will produce a diagnostic if |
| 4371 | /// the template parameter lists are not equivalent. |
| 4372 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4373 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4374 | /// |
| 4375 | /// \param TemplateArgLoc If this source location is valid, then we |
| 4376 | /// are actually checking the template parameter list of a template |
| 4377 | /// argument (New) against the template parameter list of its |
| 4378 | /// corresponding template template parameter (Old). We produce |
| 4379 | /// slightly different diagnostics in this scenario. |
| 4380 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4381 | /// \returns True if the template parameter lists are equal, false |
| 4382 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4383 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4384 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 4385 | TemplateParameterList *Old, |
| 4386 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4387 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4388 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4389 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 4390 | if (Complain) |
| 4391 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4392 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4393 | |
| 4394 | return false; |
| 4395 | } |
| 4396 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4397 | // C++0x [temp.arg.template]p3: |
| 4398 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4399 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4400 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4401 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4402 | // template-parameter-list of P. [...] |
| 4403 | TemplateParameterList::iterator NewParm = New->begin(); |
| 4404 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4405 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4406 | OldParmEnd = Old->end(); |
| 4407 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 4408 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 4409 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4410 | if (NewParm == NewParmEnd) { |
| 4411 | if (Complain) |
| 4412 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4413 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4414 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4415 | return false; |
| 4416 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4417 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4418 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4419 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4420 | return false; |
| 4421 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4422 | ++NewParm; |
| 4423 | continue; |
| 4424 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4425 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4426 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4427 | // [...] When P's template- parameter-list contains a template parameter |
| 4428 | // pack (14.5.3), the template parameter pack will match zero or more |
| 4429 | // template parameters or template parameter packs in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4430 | // template-parameter-list of A with the same type and form as the |
| 4431 | // template parameter pack in P (ignoring whether those template |
| 4432 | // parameters are template parameter packs). |
| 4433 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 4434 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4435 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4436 | return false; |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4437 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4438 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4439 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4440 | // Make sure we exhausted all of the arguments. |
| 4441 | if (NewParm != NewParmEnd) { |
| 4442 | if (Complain) |
| 4443 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4444 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4445 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4446 | return false; |
| 4447 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4448 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4449 | return true; |
| 4450 | } |
| 4451 | |
| 4452 | /// \brief Check whether a template can be declared within this scope. |
| 4453 | /// |
| 4454 | /// If the template declaration is valid in this scope, returns |
| 4455 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4456 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4457 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 4458 | if (!S) |
| 4459 | return false; |
| 4460 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4461 | // Find the nearest enclosing declaration scope. |
| 4462 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4463 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4464 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4465 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4466 | // C++ [temp]p2: |
| 4467 | // A template-declaration can appear only as a namespace scope or |
| 4468 | // class scope declaration. |
| 4469 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4470 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 4471 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4472 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4473 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4474 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4475 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4476 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4477 | |
| 4478 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 4479 | return false; |
| 4480 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4481 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4482 | diag::err_template_outside_namespace_or_class_scope) |
| 4483 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4484 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4485 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4486 | /// \brief Determine what kind of template specialization the given declaration |
| 4487 | /// is. |
| 4488 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 4489 | if (!D) |
| 4490 | return TSK_Undeclared; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4491 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4492 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 4493 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4494 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 4495 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4496 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 4497 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4498 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4499 | return TSK_Undeclared; |
| 4500 | } |
| 4501 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4502 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4503 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4504 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4505 | /// This routine determines whether a template specialization can be declared |
| 4506 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4507 | /// |
| 4508 | /// \param S the semantic analysis object for which this check is being |
| 4509 | /// performed. |
| 4510 | /// |
| 4511 | /// \param Specialized the entity being specialized or instantiated, which |
| 4512 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4513 | /// a member of a class template (member function, static data member, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4514 | /// member class). |
| 4515 | /// |
| 4516 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 4517 | /// |
| 4518 | /// \param Loc the location of the explicit specialization or instantiation of |
| 4519 | /// this entity. |
| 4520 | /// |
| 4521 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 4522 | /// a class template. |
| 4523 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4524 | /// \returns true if there was an error that we cannot recover from, false |
| 4525 | /// otherwise. |
| 4526 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 4527 | NamedDecl *Specialized, |
| 4528 | NamedDecl *PrevDecl, |
| 4529 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4530 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4531 | // Keep these "kind" numbers in sync with the %select statements in the |
| 4532 | // various diagnostics emitted by this routine. |
| 4533 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4534 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4535 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4536 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4537 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4538 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4539 | EntityKind = 3; |
| 4540 | else if (isa<VarDecl>(Specialized)) |
| 4541 | EntityKind = 4; |
| 4542 | else if (isa<RecordDecl>(Specialized)) |
| 4543 | EntityKind = 5; |
| 4544 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4545 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 4546 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4547 | return true; |
| 4548 | } |
| 4549 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4550 | // C++ [temp.expl.spec]p2: |
| 4551 | // An explicit specialization shall be declared in the namespace |
| 4552 | // of which the template is a member, or, for member templates, in |
| 4553 | // the namespace of which the enclosing class or enclosing class |
| 4554 | // template is a member. An explicit specialization of a member |
| 4555 | // function, member class or static data member of a class |
| 4556 | // template shall be declared in the namespace of which the class |
| 4557 | // template is a member. Such a declaration may also be a |
| 4558 | // definition. If the declaration is not a definition, the |
| 4559 | // specialization may be defined later in the name- space in which |
| 4560 | // the explicit specialization was declared, or in a namespace |
| 4561 | // that encloses the one in which the explicit specialization was |
| 4562 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4563 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4564 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4565 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4566 | return true; |
| 4567 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4568 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4569 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4570 | if (S.getLangOptions().MicrosoftExt) { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4571 | // Do not warn for class scope explicit specialization during |
| 4572 | // instantiation, warning was already emitted during pattern |
| 4573 | // semantic analysis. |
| 4574 | if (!S.ActiveTemplateInstantiations.size()) |
| 4575 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 4576 | << Specialized; |
| 4577 | } else { |
| 4578 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4579 | << Specialized; |
| 4580 | return true; |
| 4581 | } |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4582 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4583 | |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4584 | if (S.CurContext->isRecord() && |
| 4585 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 4586 | // Make sure that we're specializing in the right record context. |
| 4587 | // Otherwise, things can go horribly wrong. |
| 4588 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4589 | << Specialized; |
| 4590 | return true; |
| 4591 | } |
| 4592 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4593 | // C++ [temp.class.spec]p6: |
| 4594 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4595 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4596 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4597 | bool ComplainedAboutScope = false; |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4598 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4599 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4600 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4601 | if ((!PrevDecl || |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4602 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4603 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4604 | // C++ [temp.exp.spec]p2: |
| 4605 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4606 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4607 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4608 | // An explicit specialization of a member function, member class or |
| 4609 | // static data member of a class template shall be declared in the |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4610 | // namespace of which the class template is a member. |
| 4611 | // |
| 4612 | // C++0x [temp.expl.spec]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4613 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4614 | // the specialized template. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4615 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
| 4616 | bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext); |
| 4617 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
| 4618 | assert(!IsCPlusPlus0xExtension && |
| 4619 | "DC encloses TU but isn't in enclosing namespace set"); |
| 4620 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4621 | << EntityKind << Specialized; |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4622 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 4623 | int Diag; |
| 4624 | if (!IsCPlusPlus0xExtension) |
| 4625 | Diag = diag::err_template_spec_decl_out_of_scope; |
| 4626 | else if (!S.getLangOptions().CPlusPlus0x) |
| 4627 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 4628 | else |
| 4629 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 4630 | S.Diag(Loc, Diag) |
| 4631 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 4632 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4633 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4634 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4635 | ComplainedAboutScope = |
| 4636 | !(IsCPlusPlus0xExtension && S.getLangOptions().CPlusPlus0x); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4637 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4638 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4639 | |
| 4640 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4641 | // namespace. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4642 | // Note that HandleDeclarator() performs this check for explicit |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4643 | // specializations of function templates, static data members, and member |
| 4644 | // functions, so we skip the check here for those kinds of entities. |
| 4645 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4646 | // Should we refactor that check, so that it occurs later? |
| 4647 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4648 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 4649 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4650 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 4651 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 4652 | << EntityKind << Specialized; |
| 4653 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 4654 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 4655 | << EntityKind << Specialized |
| 4656 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4657 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4658 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4659 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4660 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4661 | // FIXME: check for specialization-after-instantiation errors and such. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4662 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4663 | return false; |
| 4664 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4665 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4666 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 4667 | /// that checks non-type template partial specialization arguments. |
| 4668 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 4669 | NonTypeTemplateParmDecl *Param, |
| 4670 | const TemplateArgument *Args, |
| 4671 | unsigned NumArgs) { |
| 4672 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4673 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 4674 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4675 | Args[I].pack_begin(), |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4676 | Args[I].pack_size())) |
| 4677 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4678 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4679 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4680 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4681 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4682 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4683 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4684 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4685 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4686 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4687 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4688 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4689 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4690 | |
| 4691 | // Strip off any implicit casts we added as part of type checking. |
| 4692 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4693 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4694 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4695 | // C++ [temp.class.spec]p8: |
| 4696 | // A non-type argument is non-specialized if it is the name of a |
| 4697 | // non-type parameter. All other non-type arguments are |
| 4698 | // specialized. |
| 4699 | // |
| 4700 | // Below, we check the two conditions that only apply to |
| 4701 | // specialized non-type arguments, so skip any non-specialized |
| 4702 | // arguments. |
| 4703 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4704 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4705 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4706 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4707 | // C++ [temp.class.spec]p9: |
| 4708 | // Within the argument list of a class template partial |
| 4709 | // specialization, the following restrictions apply: |
| 4710 | // -- A partially specialized non-type argument expression |
| 4711 | // shall not involve a template parameter of the partial |
| 4712 | // specialization except when the argument expression is a |
| 4713 | // simple identifier. |
| 4714 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4715 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4716 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4717 | << ArgExpr->getSourceRange(); |
| 4718 | return true; |
| 4719 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4720 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4721 | // -- The type of a template parameter corresponding to a |
| 4722 | // specialized non-type argument shall not be dependent on a |
| 4723 | // parameter of the specialization. |
| 4724 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4725 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4726 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 4727 | << Param->getType() |
| 4728 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4729 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4730 | return true; |
| 4731 | } |
| 4732 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4733 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4734 | return false; |
| 4735 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4736 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4737 | /// \brief Check the non-type template arguments of a class template |
| 4738 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 4739 | /// |
| 4740 | /// \param TemplateParams the template parameters of the primary class |
| 4741 | /// template. |
| 4742 | /// |
| 4743 | /// \param TemplateArg the template arguments of the class template |
| 4744 | /// partial specialization. |
| 4745 | /// |
| 4746 | /// \returns true if there was an error, false otherwise. |
| 4747 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 4748 | TemplateParameterList *TemplateParams, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4749 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4750 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 4751 | |
| 4752 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4753 | NonTypeTemplateParmDecl *Param |
| 4754 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 4755 | if (!Param) |
| 4756 | continue; |
| 4757 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4758 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4759 | &ArgList[I], 1)) |
| 4760 | return true; |
| 4761 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4762 | |
| 4763 | return false; |
| 4764 | } |
| 4765 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4766 | /// \brief Retrieve the previous declaration of the given declaration. |
| 4767 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 4768 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4769 | return VD->getPreviousDeclaration(); |
| 4770 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 4771 | return FD->getPreviousDeclaration(); |
| 4772 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 4773 | return TD->getPreviousDeclaration(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4774 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4775 | return TD->getPreviousDeclaration(); |
| 4776 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 4777 | return FTD->getPreviousDeclaration(); |
| 4778 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 4779 | return CTD->getPreviousDeclaration(); |
| 4780 | return 0; |
| 4781 | } |
| 4782 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4783 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4784 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4785 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4786 | SourceLocation KWLoc, |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 4787 | SourceLocation ModulePrivateLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4788 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4789 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4790 | SourceLocation TemplateNameLoc, |
| 4791 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4792 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4793 | SourceLocation RAngleLoc, |
| 4794 | AttributeList *Attr, |
| 4795 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4796 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4797 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4798 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 4799 | // store the location of the outermost template keyword in the declaration. |
| 4800 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
| 4801 | ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation(); |
| 4802 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4803 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4804 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4805 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4806 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4807 | |
| 4808 | if (!ClassTemplate) { |
| 4809 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4810 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4811 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4812 | return true; |
| 4813 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4814 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4815 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4816 | bool isPartialSpecialization = false; |
| 4817 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4818 | // Check the validity of the template headers that introduce this |
| 4819 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4820 | // FIXME: We probably shouldn't complain about these headers for |
| 4821 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4822 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4823 | TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 4824 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, |
| 4825 | TemplateNameLoc, |
| 4826 | SS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4827 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4828 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4829 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4830 | isExplicitSpecialization, |
| 4831 | Invalid); |
| 4832 | if (Invalid) |
| 4833 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4834 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4835 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4836 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4837 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4838 | if (TUK == TUK_Friend) { |
| 4839 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4840 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4841 | return true; |
| 4842 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4843 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4844 | // C++ [temp.class.spec]p10: |
| 4845 | // The template parameter list of a specialization shall not |
| 4846 | // contain default template argument values. |
| 4847 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4848 | Decl *Param = TemplateParams->getParam(I); |
| 4849 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4850 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4851 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4852 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4853 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4854 | } |
| 4855 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4856 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4857 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4858 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4859 | diag::err_default_arg_in_partial_spec) |
| 4860 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4861 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4862 | } |
| 4863 | } else { |
| 4864 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4865 | if (TTP->hasDefaultArgument()) { |
| 4866 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4867 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4868 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4869 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4870 | } |
| 4871 | } |
| 4872 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4873 | } else if (TemplateParams) { |
| 4874 | if (TUK == TUK_Friend) |
| 4875 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4876 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4877 | SourceRange(TemplateParams->getTemplateLoc(), |
| 4878 | TemplateParams->getRAngleLoc())) |
| 4879 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4880 | else |
| 4881 | isExplicitSpecialization = true; |
| 4882 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4883 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4884 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4885 | isExplicitSpecialization = true; |
| 4886 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4887 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4888 | // Check that the specialization uses the same tag kind as the |
| 4889 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4890 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4891 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4892 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 4893 | Kind, TUK == TUK_Definition, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4894 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4895 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4896 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4897 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4898 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4899 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4900 | diag::note_previous_use); |
| 4901 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4902 | } |
| 4903 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4904 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4905 | TemplateArgumentListInfo TemplateArgs; |
| 4906 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 4907 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4908 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4909 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4910 | // Check for unexpanded parameter packs in any of the template arguments. |
| 4911 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4912 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4913 | UPPC_PartialSpecialization)) |
| 4914 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4915 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4916 | // Check that the template argument list is well-formed for this |
| 4917 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4918 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4919 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4920 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4921 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4922 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4923 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4924 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4925 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4926 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4927 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4928 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4929 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4930 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4931 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4932 | return true; |
| 4933 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4934 | bool InstantiationDependent; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4935 | if (!Name.isDependent() && |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4936 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4937 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4938 | TemplateArgs.size(), |
| 4939 | InstantiationDependent)) { |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4940 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 4941 | << ClassTemplate->getDeclName(); |
| 4942 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4943 | } |
| 4944 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4945 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4946 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4947 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 4948 | |
| 4949 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4950 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4951 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4952 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 4953 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4954 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4955 | else |
| 4956 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4957 | = ClassTemplate->findSpecialization(Converted.data(), |
| 4958 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4959 | |
| 4960 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4961 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4962 | // Check whether we can declare a class template specialization in |
| 4963 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4964 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4965 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 4966 | TemplateNameLoc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4967 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4968 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4969 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4970 | // The canonical type |
| 4971 | QualType CanonType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4972 | if (PrevDecl && |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4973 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4974 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4975 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4976 | // arguments was referenced but not declared, or we're only |
| 4977 | // referencing this specialization as a friend, reuse that |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4978 | // declaration node as our own, updating its source location and |
| 4979 | // the list of outer template parameters to reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4980 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4981 | Specialization->setLocation(TemplateNameLoc); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4982 | if (TemplateParameterLists.size() > 0) { |
| 4983 | Specialization->setTemplateParameterListsInfo(Context, |
| 4984 | TemplateParameterLists.size(), |
| 4985 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4986 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4987 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4988 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4989 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4990 | // Build the canonical type that describes the converted template |
| 4991 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4992 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4993 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4994 | Converted.data(), |
| 4995 | Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4996 | |
| 4997 | if (Context.hasSameType(CanonType, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4998 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 4999 | // C++ [temp.class.spec]p9b3: |
| 5000 | // |
| 5001 | // -- The argument list of the specialization shall not be identical |
| 5002 | // to the implicit argument list of the primary template. |
| 5003 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 5004 | << (TUK == TUK_Definition) |
| 5005 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5006 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 5007 | ClassTemplate->getIdentifier(), |
| 5008 | TemplateNameLoc, |
| 5009 | Attr, |
| 5010 | TemplateParams, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 5011 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5012 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 5013 | (TemplateParameterList**) TemplateParameterLists.release()); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5014 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5015 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5016 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5017 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 5018 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5019 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5020 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5021 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5022 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5023 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5024 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 5025 | TemplateParams, |
| 5026 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5027 | Converted.data(), |
| 5028 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5029 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5030 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5031 | PrevPartial, |
| 5032 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5033 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5034 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5035 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5036 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5037 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5038 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5039 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5040 | if (!PrevPartial) |
| 5041 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5042 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5043 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5044 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5045 | // template specialization, make a note of that. |
| 5046 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 5047 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5048 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5049 | // Check that all of the template parameters of the class template |
| 5050 | // partial specialization are deducible from the template |
| 5051 | // arguments. If not, this class template partial specialization |
| 5052 | // will never be used. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5053 | SmallVector<bool, 8> DeducibleParams; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5054 | DeducibleParams.resize(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5055 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5056 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5057 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5058 | unsigned NumNonDeducible = 0; |
| 5059 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 5060 | if (!DeducibleParams[I]) |
| 5061 | ++NumNonDeducible; |
| 5062 | |
| 5063 | if (NumNonDeducible) { |
| 5064 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 5065 | << (NumNonDeducible > 1) |
| 5066 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 5067 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 5068 | if (!DeducibleParams[I]) { |
| 5069 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 5070 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5071 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5072 | diag::note_partial_spec_unused_parameter) |
| 5073 | << Param->getDeclName(); |
| 5074 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5075 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5076 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 5077 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5078 | } |
| 5079 | } |
| 5080 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5081 | } else { |
| 5082 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5083 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5084 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5085 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5086 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5087 | KWLoc, TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5088 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5089 | Converted.data(), |
| 5090 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5091 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5092 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5093 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5094 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5095 | TemplateParameterLists.size(), |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5096 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5097 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5098 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5099 | if (!PrevDecl) |
| 5100 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5101 | |
| 5102 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5103 | } |
| 5104 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5105 | // C++ [temp.expl.spec]p6: |
| 5106 | // 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] | 5107 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5108 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5109 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5110 | // use occurs; no diagnostic is required. |
| 5111 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5112 | bool Okay = false; |
| 5113 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 5114 | // Is there any previous explicit specialization declaration? |
| 5115 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5116 | Okay = true; |
| 5117 | break; |
| 5118 | } |
| 5119 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5120 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5121 | if (!Okay) { |
| 5122 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 5123 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 5124 | << Context.getTypeDeclType(Specialization) << Range; |
| 5125 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5126 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5127 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5128 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5129 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5130 | return true; |
| 5131 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5132 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5133 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5134 | // If this is not a friend, note that this is an explicit specialization. |
| 5135 | if (TUK != TUK_Friend) |
| 5136 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5137 | |
| 5138 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5139 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5140 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5141 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5142 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5143 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5144 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 5145 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5146 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5147 | } |
| 5148 | } |
| 5149 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 5150 | if (Attr) |
| 5151 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 5152 | |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5153 | if (ModulePrivateLoc.isValid()) |
| 5154 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 5155 | << (isPartialSpecialization? 1 : 0) |
| 5156 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 5157 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 5158 | // Build the fully-sugared type for this class template |
| 5159 | // specialization as the user wrote in the specialization |
| 5160 | // itself. This means that we'll pretty-print the type retrieved |
| 5161 | // from the specialization's declaration the way that the user |
| 5162 | // actually wrote the specialization, rather than formatting the |
| 5163 | // name based on the "canonical" representation used to store the |
| 5164 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5165 | TypeSourceInfo *WrittenTy |
| 5166 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5167 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5168 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5169 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5170 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5171 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5172 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5173 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5174 | // C++ [temp.expl.spec]p9: |
| 5175 | // A template explicit specialization is in the scope of the |
| 5176 | // namespace in which the template was defined. |
| 5177 | // |
| 5178 | // We actually implement this paragraph where we set the semantic |
| 5179 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 5180 | // but we also maintain the lexical context where the actual |
| 5181 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5182 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5183 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5184 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5185 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5186 | Specialization->startDefinition(); |
| 5187 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5188 | if (TUK == TUK_Friend) { |
| 5189 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 5190 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5191 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5192 | /*FIXME:*/KWLoc); |
| 5193 | Friend->setAccess(AS_public); |
| 5194 | CurContext->addDecl(Friend); |
| 5195 | } else { |
| 5196 | // Add the specialization into its lexical context, so that it can |
| 5197 | // be seen when iterating through the list of declarations in that |
| 5198 | // context. However, specializations are not found by name lookup. |
| 5199 | CurContext->addDecl(Specialization); |
| 5200 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5201 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5202 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5203 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5204 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5205 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5206 | Declarator &D) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5207 | return HandleDeclarator(S, D, move(TemplateParameterLists)); |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5208 | } |
| 5209 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5210 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5211 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5212 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5213 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5214 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5215 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5216 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5217 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5218 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5219 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5220 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5221 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 5222 | D.setFunctionDefinitionKind(FDK_Definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5223 | Decl *DP = HandleDeclarator(ParentScope, D, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5224 | move(TemplateParameterLists)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5225 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5226 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5227 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5228 | FunctionTemplate->getTemplatedDecl()); |
| 5229 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 5230 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 5231 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5232 | } |
| 5233 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5234 | /// \brief Strips various properties off an implicit instantiation |
| 5235 | /// that has just been explicitly specialized. |
| 5236 | static void StripImplicitInstantiation(NamedDecl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5237 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5238 | |
| 5239 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5240 | FD->setInlineSpecified(false); |
| 5241 | } |
| 5242 | } |
| 5243 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5244 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5245 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5246 | /// for those cases where they are required and determining whether the |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5247 | /// new specialization/instantiation will have any effect. |
| 5248 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5249 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5250 | /// instantiation. |
| 5251 | /// |
| 5252 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 5253 | /// |
| 5254 | /// \param PrevDecl the previous declaration of the entity. |
| 5255 | /// |
| 5256 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 5257 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5258 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5259 | /// declaration was instantiated (either implicitly or explicitly). |
| 5260 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5261 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5262 | /// specialization or instantiation has no effect and should be ignored. |
| 5263 | /// |
| 5264 | /// \returns true if there was an error that should prevent the introduction of |
| 5265 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5266 | bool |
| 5267 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 5268 | TemplateSpecializationKind NewTSK, |
| 5269 | NamedDecl *PrevDecl, |
| 5270 | TemplateSpecializationKind PrevTSK, |
| 5271 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5272 | bool &HasNoEffect) { |
| 5273 | HasNoEffect = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5274 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5275 | switch (NewTSK) { |
| 5276 | case TSK_Undeclared: |
| 5277 | case TSK_ImplicitInstantiation: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5278 | llvm_unreachable("Don't check implicit instantiations here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5279 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5280 | case TSK_ExplicitSpecialization: |
| 5281 | switch (PrevTSK) { |
| 5282 | case TSK_Undeclared: |
| 5283 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5284 | // Okay, we're just specializing something that is either already |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5285 | // explicitly specialized or has merely been mentioned without any |
| 5286 | // instantiation. |
| 5287 | return false; |
| 5288 | |
| 5289 | case TSK_ImplicitInstantiation: |
| 5290 | if (PrevPointOfInstantiation.isInvalid()) { |
| 5291 | // The declaration itself has not actually been instantiated, so it is |
| 5292 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5293 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5294 | return false; |
| 5295 | } |
| 5296 | // Fall through |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5297 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5298 | case TSK_ExplicitInstantiationDeclaration: |
| 5299 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5300 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 5301 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5302 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5303 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5304 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5305 | // 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] | 5306 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5307 | // before the first use of that specialization that would cause an |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5308 | // implicit instantiation to take place, in every translation unit in |
| 5309 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5310 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 5311 | // Is there any previous explicit specialization declaration? |
| 5312 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 5313 | return false; |
| 5314 | } |
| 5315 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5316 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5317 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5318 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5319 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5320 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5321 | return true; |
| 5322 | } |
| 5323 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5324 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5325 | case TSK_ExplicitInstantiationDeclaration: |
| 5326 | switch (PrevTSK) { |
| 5327 | case TSK_ExplicitInstantiationDeclaration: |
| 5328 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5329 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5330 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5331 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5332 | case TSK_Undeclared: |
| 5333 | case TSK_ImplicitInstantiation: |
| 5334 | // We're explicitly instantiating something that may have already been |
| 5335 | // implicitly instantiated; that's fine. |
| 5336 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5337 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5338 | case TSK_ExplicitSpecialization: |
| 5339 | // C++0x [temp.explicit]p4: |
| 5340 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5341 | // of a template appears after a declaration of an explicit |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5342 | // specialization for that template, the explicit instantiation has no |
| 5343 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5344 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5345 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5346 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5347 | case TSK_ExplicitInstantiationDefinition: |
| 5348 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5349 | // If an entity is the subject of both an explicit instantiation |
| 5350 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5351 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5352 | Diag(NewLoc, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5353 | diag::err_explicit_instantiation_declaration_after_definition); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5354 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5355 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5356 | assert(PrevPointOfInstantiation.isValid() && |
| 5357 | "Explicit instantiation without point of instantiation?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5358 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5359 | return false; |
| 5360 | } |
| 5361 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5362 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5363 | case TSK_ExplicitInstantiationDefinition: |
| 5364 | switch (PrevTSK) { |
| 5365 | case TSK_Undeclared: |
| 5366 | case TSK_ImplicitInstantiation: |
| 5367 | // We're explicitly instantiating something that may have already been |
| 5368 | // implicitly instantiated; that's fine. |
| 5369 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5370 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5371 | case TSK_ExplicitSpecialization: |
| 5372 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 5373 | // For a given set of template parameters, if an explicit |
| 5374 | // instantiation of a template appears after a declaration of |
| 5375 | // an explicit specialization for that template, the explicit |
| 5376 | // instantiation has no effect. |
| 5377 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5378 | // 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] | 5379 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5380 | // has been explicitly specialized. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5381 | Diag(NewLoc, getLangOptions().CPlusPlus0x ? |
| 5382 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 5383 | diag::ext_explicit_instantiation_after_specialization) |
| 5384 | << PrevDecl; |
| 5385 | Diag(PrevDecl->getLocation(), |
| 5386 | diag::note_previous_template_specialization); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5387 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5388 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5389 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5390 | case TSK_ExplicitInstantiationDeclaration: |
| 5391 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5392 | // were previously asked to suppress instantiations. That's fine. |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5393 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5394 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5395 | case TSK_ExplicitInstantiationDefinition: |
| 5396 | // C++0x [temp.spec]p5: |
| 5397 | // For a given template and a given set of template-arguments, |
| 5398 | // - an explicit instantiation definition shall appear at most once |
| 5399 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5400 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5401 | << PrevDecl; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5402 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5403 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5404 | HasNoEffect = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5405 | return false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5406 | } |
| 5407 | break; |
| 5408 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5409 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5410 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5411 | } |
| 5412 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5413 | /// \brief Perform semantic analysis for the given dependent function |
| 5414 | /// template specialization. The only possible way to get a dependent |
| 5415 | /// function template specialization is with a friend declaration, |
| 5416 | /// like so: |
| 5417 | /// |
| 5418 | /// template <class T> void foo(T); |
| 5419 | /// template <class T> class A { |
| 5420 | /// friend void foo<>(T); |
| 5421 | /// }; |
| 5422 | /// |
| 5423 | /// There really isn't any useful analysis we can do here, so we |
| 5424 | /// just store the information. |
| 5425 | bool |
| 5426 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 5427 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 5428 | LookupResult &Previous) { |
| 5429 | // Remove anything from Previous that isn't a function template in |
| 5430 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5431 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5432 | LookupResult::Filter F = Previous.makeFilter(); |
| 5433 | while (F.hasNext()) { |
| 5434 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 5435 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5436 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 5437 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5438 | F.erase(); |
| 5439 | } |
| 5440 | F.done(); |
| 5441 | |
| 5442 | // Should this be diagnosed here? |
| 5443 | if (Previous.empty()) return true; |
| 5444 | |
| 5445 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 5446 | ExplicitTemplateArgs); |
| 5447 | return false; |
| 5448 | } |
| 5449 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5450 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5451 | /// specialization. |
| 5452 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5453 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5454 | /// explicit function template specialization. On successful completion, |
| 5455 | /// the function declaration \p FD will become a function template |
| 5456 | /// specialization. |
| 5457 | /// |
| 5458 | /// \param FD the function declaration, which will be updated to become a |
| 5459 | /// function template specialization. |
| 5460 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5461 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 5462 | /// if any. Note that this may be valid info even when 0 arguments are |
| 5463 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 5464 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5465 | /// |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5466 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5467 | /// this function specialization. |
| 5468 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5469 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5470 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5471 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5472 | // The set of function template specializations that could match this |
| 5473 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5474 | UnresolvedSet<8> Candidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5475 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5476 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5477 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5478 | I != E; ++I) { |
| 5479 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 5480 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5481 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5482 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5483 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 5484 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5485 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5486 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5487 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5488 | // A trailing template-argument can be left unspecified in the |
| 5489 | // template-id naming an explicit function template specialization |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5490 | // provided it can be deduced from the function argument type. |
| 5491 | // Perform template argument deduction to determine whether we may be |
| 5492 | // specializing this template. |
| 5493 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5494 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5495 | FunctionDecl *Specialization = 0; |
| 5496 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5497 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5498 | FD->getType(), |
| 5499 | Specialization, |
| 5500 | Info)) { |
| 5501 | // FIXME: Template argument deduction failed; record why it failed, so |
| 5502 | // that we can provide nifty diagnostics. |
| 5503 | (void)TDK; |
| 5504 | continue; |
| 5505 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5506 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5507 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5508 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5509 | } |
| 5510 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5511 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5512 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5513 | UnresolvedSetIterator Result |
| 5514 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5515 | TPOC_Other, 0, FD->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5516 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5517 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5518 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5519 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5520 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5521 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5522 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5523 | |
| 5524 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 5525 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | abfb405 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 5526 | |
| 5527 | FunctionTemplateSpecializationInfo *SpecInfo |
| 5528 | = Specialization->getTemplateSpecializationInfo(); |
| 5529 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5530 | |
| 5531 | // Note: do not overwrite location info if previous template |
| 5532 | // specialization kind was explicit. |
| 5533 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
| 5534 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) |
| 5535 | Specialization->setLocation(FD->getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5536 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5537 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5538 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5539 | |
| 5540 | // If this is a friend declaration, then we're not really declaring |
| 5541 | // an explicit specialization. |
| 5542 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5543 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5544 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5545 | if (!isFriend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5546 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5547 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5548 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5549 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5550 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5551 | |
| 5552 | // C++ [temp.expl.spec]p6: |
| 5553 | // 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] | 5554 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5555 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5556 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5557 | // use occurs; no diagnostic is required. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5558 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5559 | if (!isFriend && |
| 5560 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5561 | TSK_ExplicitSpecialization, |
| 5562 | Specialization, |
| 5563 | SpecInfo->getTemplateSpecializationKind(), |
| 5564 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5565 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5566 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5567 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5568 | // Mark the prior declaration as an explicit specialization, so that later |
| 5569 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5570 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5571 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5572 | MarkUnusedFileScopedDecl(Specialization); |
| 5573 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5574 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5575 | // Turn the given function declaration into a function template |
| 5576 | // specialization, with the template arguments from the previous |
| 5577 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5578 | // Take copies of (semantic and syntactic) template argument lists. |
| 5579 | const TemplateArgumentList* TemplArgs = new (Context) |
| 5580 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5581 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5582 | TemplArgs, /*InsertPos=*/0, |
| 5583 | SpecInfo->getTemplateSpecializationKind(), |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 5584 | ExplicitTemplateArgs); |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5585 | FD->setStorageClass(Specialization->getStorageClass()); |
| 5586 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5587 | // The "previous declaration" for this function template specialization is |
| 5588 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5589 | Previous.clear(); |
| 5590 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5591 | return false; |
| 5592 | } |
| 5593 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5594 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5595 | /// specialization. |
| 5596 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5597 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5598 | /// explicit member function specialization. On successful completion, |
| 5599 | /// the function declaration \p FD will become a member function |
| 5600 | /// specialization. |
| 5601 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5602 | /// \param Member the member declaration, which will be updated to become a |
| 5603 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5604 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5605 | /// \param Previous the set of declarations, one of which may be specialized |
| 5606 | /// by this function specialization; the set will be modified to contain the |
| 5607 | /// redeclared member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5608 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5609 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5610 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5611 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5612 | // Try to find the member we are instantiating. |
| 5613 | NamedDecl *Instantiation = 0; |
| 5614 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5615 | MemberSpecializationInfo *MSInfo = 0; |
| 5616 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5617 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5618 | // Nowhere to look anyway. |
| 5619 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5620 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5621 | I != E; ++I) { |
| 5622 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5623 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5624 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 5625 | Instantiation = Method; |
| 5626 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5627 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5628 | break; |
| 5629 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5630 | } |
| 5631 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5632 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5633 | VarDecl *PrevVar; |
| 5634 | if (Previous.isSingleResult() && |
| 5635 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5636 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5637 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5638 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5639 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5640 | } |
| 5641 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5642 | CXXRecordDecl *PrevRecord; |
| 5643 | if (Previous.isSingleResult() && |
| 5644 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 5645 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5646 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5647 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5648 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5649 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5650 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5651 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5652 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5653 | // specializations are always out-of-line, the caller will complain about |
| 5654 | // this mismatch later. |
| 5655 | return false; |
| 5656 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5657 | |
| 5658 | // If this is a friend, just bail out here before we start turning |
| 5659 | // things into explicit specializations. |
| 5660 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 5661 | // Preserve instantiation information. |
| 5662 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 5663 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 5664 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5665 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5666 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 5667 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 5668 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5669 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5670 | } |
| 5671 | |
| 5672 | Previous.clear(); |
| 5673 | Previous.addDecl(Instantiation); |
| 5674 | return false; |
| 5675 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5676 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5677 | // Make sure that this is a specialization of a member. |
| 5678 | if (!InstantiatedFrom) { |
| 5679 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 5680 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5681 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 5682 | return true; |
| 5683 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5684 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5685 | // C++ [temp.expl.spec]p6: |
| 5686 | // 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] | 5687 | // explicitly specialized then that spe- cialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5688 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5689 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5690 | // use occurs; no diagnostic is required. |
| 5691 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5692 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5693 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5694 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 5695 | TSK_ExplicitSpecialization, |
| 5696 | Instantiation, |
| 5697 | MSInfo->getTemplateSpecializationKind(), |
| 5698 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5699 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5700 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5701 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5702 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5703 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5704 | InstantiatedFrom, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5705 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5706 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5707 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 5708 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5709 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5710 | // the original declaration to note that it is an explicit specialization |
| 5711 | // (if it was previously an implicit instantiation). This latter step |
| 5712 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5713 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5714 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 5715 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 5716 | TSK_ImplicitInstantiation) { |
| 5717 | InstantiationFunction->setTemplateSpecializationKind( |
| 5718 | TSK_ExplicitSpecialization); |
| 5719 | InstantiationFunction->setLocation(Member->getLocation()); |
| 5720 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5721 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5722 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 5723 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5724 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5725 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5726 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5727 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 5728 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 5729 | TSK_ImplicitInstantiation) { |
| 5730 | InstantiationVar->setTemplateSpecializationKind( |
| 5731 | TSK_ExplicitSpecialization); |
| 5732 | InstantiationVar->setLocation(Member->getLocation()); |
| 5733 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5734 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5735 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 5736 | cast<VarDecl>(InstantiatedFrom), |
| 5737 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5738 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5739 | } else { |
| 5740 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5741 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 5742 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 5743 | TSK_ImplicitInstantiation) { |
| 5744 | InstantiationClass->setTemplateSpecializationKind( |
| 5745 | TSK_ExplicitSpecialization); |
| 5746 | InstantiationClass->setLocation(Member->getLocation()); |
| 5747 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5748 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5749 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5750 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5751 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5752 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5753 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5754 | // Save the caller the trouble of having to figure out which declaration |
| 5755 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5756 | Previous.clear(); |
| 5757 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5758 | return false; |
| 5759 | } |
| 5760 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5761 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5762 | /// |
| 5763 | /// \returns true if a serious error occurs, false otherwise. |
| 5764 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5765 | SourceLocation InstLoc, |
| 5766 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5767 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5768 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5769 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5770 | if (CurContext->isRecord()) { |
| 5771 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5772 | << D; |
| 5773 | return true; |
| 5774 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5775 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5776 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5777 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5778 | // template. If the name declared in the explicit instantiation is an |
| 5779 | // unqualified name, the explicit instantiation shall appear in the |
| 5780 | // namespace where its template is declared or, if that namespace is inline |
| 5781 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5782 | // |
| 5783 | // This is DR275, which we do not retroactively apply to C++98/03. |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5784 | if (WasQualifiedName) { |
| 5785 | if (CurContext->Encloses(OrigContext)) |
| 5786 | return false; |
| 5787 | } else { |
| 5788 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 5789 | return false; |
| 5790 | } |
| 5791 | |
| 5792 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 5793 | if (WasQualifiedName) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5794 | S.Diag(InstLoc, |
| 5795 | S.getLangOptions().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5796 | diag::err_explicit_instantiation_out_of_scope : |
| 5797 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5798 | << D << NS; |
| 5799 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5800 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5801 | S.getLangOptions().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5802 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 5803 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 5804 | << D << NS; |
| 5805 | } else |
| 5806 | S.Diag(InstLoc, |
| 5807 | S.getLangOptions().CPlusPlus0x? |
| 5808 | diag::err_explicit_instantiation_must_be_global : |
| 5809 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 5810 | << D; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5811 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5812 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5813 | } |
| 5814 | |
| 5815 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5816 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5817 | if (!SS.isSet()) |
| 5818 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5819 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5820 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5821 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5822 | // or a static data member of a class template specialization, the name of |
| 5823 | // the class template specialization in the qualified-id for the member |
| 5824 | // name shall be a simple-template-id. |
| 5825 | // |
| 5826 | // C++98 has the same restriction, just worded differently. |
| 5827 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5828 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5829 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5830 | if (isa<TemplateSpecializationType>(T)) |
| 5831 | return true; |
| 5832 | |
| 5833 | return false; |
| 5834 | } |
| 5835 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5836 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5837 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5838 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5839 | SourceLocation ExternLoc, |
| 5840 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5841 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5842 | SourceLocation KWLoc, |
| 5843 | const CXXScopeSpec &SS, |
| 5844 | TemplateTy TemplateD, |
| 5845 | SourceLocation TemplateNameLoc, |
| 5846 | SourceLocation LAngleLoc, |
| 5847 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5848 | SourceLocation RAngleLoc, |
| 5849 | AttributeList *Attr) { |
| 5850 | // Find the class template we're specializing |
| 5851 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5852 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5853 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5854 | |
| 5855 | // Check that the specialization uses the same tag kind as the |
| 5856 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5857 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5858 | assert(Kind != TTK_Enum && |
| 5859 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5860 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 5861 | Kind, /*isDefinition*/false, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5862 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5863 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5864 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5865 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5866 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5867 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5868 | diag::note_previous_use); |
| 5869 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5870 | } |
| 5871 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5872 | // C++0x [temp.explicit]p2: |
| 5873 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5874 | // definition and an explicit instantiation declaration. An explicit |
| 5875 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5876 | TemplateSpecializationKind TSK |
| 5877 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5878 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5879 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5880 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5881 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5882 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5883 | |
| 5884 | // Check that the template argument list is well-formed for this |
| 5885 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5886 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5887 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5888 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5889 | return true; |
| 5890 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5891 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5892 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5893 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5894 | // Find the class template specialization declaration that |
| 5895 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5896 | void *InsertPos = 0; |
| 5897 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5898 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5899 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5900 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5901 | TemplateSpecializationKind PrevDecl_TSK |
| 5902 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 5903 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5904 | // C++0x [temp.explicit]p2: |
| 5905 | // [...] An explicit instantiation shall appear in an enclosing |
| 5906 | // namespace of its template. [...] |
| 5907 | // |
| 5908 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5909 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 5910 | SS.isSet())) |
| 5911 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5912 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5913 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5914 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5915 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5916 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5917 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5918 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5919 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5920 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5921 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5922 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5923 | // Even though HasNoEffect == true means that this explicit instantiation |
| 5924 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 5925 | |
| 5926 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 5927 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5928 | // Since the only prior class template specialization with these |
| 5929 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5930 | // declaration node as our own, updating the source location |
| 5931 | // for the template name to reflect our new declaration. |
| 5932 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5933 | Specialization = PrevDecl; |
| 5934 | Specialization->setLocation(TemplateNameLoc); |
| 5935 | PrevDecl = 0; |
| 5936 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5937 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5938 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5939 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5940 | // Create a new class template specialization declaration node for |
| 5941 | // this explicit specialization. |
| 5942 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5943 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5944 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5945 | KWLoc, TemplateNameLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5946 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5947 | Converted.data(), |
| 5948 | Converted.size(), |
| 5949 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5950 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5951 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5952 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5953 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5954 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5955 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5956 | } |
| 5957 | |
| 5958 | // Build the fully-sugared type for this explicit instantiation as |
| 5959 | // the user wrote in the explicit instantiation itself. This means |
| 5960 | // that we'll pretty-print the type retrieved from the |
| 5961 | // specialization's declaration the way that the user actually wrote |
| 5962 | // the explicit instantiation, rather than formatting the name based |
| 5963 | // on the "canonical" representation used to store the template |
| 5964 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5965 | TypeSourceInfo *WrittenTy |
| 5966 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5967 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5968 | Context.getTypeDeclType(Specialization)); |
| 5969 | Specialization->setTypeAsWritten(WrittenTy); |
| 5970 | TemplateArgsIn.release(); |
| 5971 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5972 | // Set source locations for keywords. |
| 5973 | Specialization->setExternLoc(ExternLoc); |
| 5974 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 5975 | |
| 5976 | // Add the explicit instantiation into its lexical context. However, |
| 5977 | // since explicit instantiations are never found by name lookup, we |
| 5978 | // just put it into the declaration context directly. |
| 5979 | Specialization->setLexicalDeclContext(CurContext); |
| 5980 | CurContext->addDecl(Specialization); |
| 5981 | |
| 5982 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 5983 | if (HasNoEffect) { |
| 5984 | // Set the template specialization kind. |
| 5985 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5986 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5987 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5988 | |
| 5989 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5990 | // A definition of a class template or class member template |
| 5991 | // shall be in scope at the point of the explicit instantiation of |
| 5992 | // the class template or class member template. |
| 5993 | // |
| 5994 | // This check comes when we actually try to perform the |
| 5995 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5996 | ClassTemplateSpecializationDecl *Def |
| 5997 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5998 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5999 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6000 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6001 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6002 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6003 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 6004 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6005 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6006 | // Instantiate the members of this class template specialization. |
| 6007 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6008 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6009 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 6010 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 6011 | |
| 6012 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 6013 | // TSK_ExplicitInstantiationDefinition |
| 6014 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 6015 | TSK == TSK_ExplicitInstantiationDefinition) |
| 6016 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6017 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6018 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6019 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6020 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6021 | // Set the template specialization kind. |
| 6022 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6023 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6024 | } |
| 6025 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6026 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6027 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6028 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 6029 | SourceLocation ExternLoc, |
| 6030 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6031 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6032 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6033 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6034 | IdentifierInfo *Name, |
| 6035 | SourceLocation NameLoc, |
| 6036 | AttributeList *Attr) { |
| 6037 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 6038 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6039 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6040 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6041 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6042 | /*ModulePrivateLoc=*/SourceLocation(), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6043 | MultiTemplateParamsArg(*this, 0, 0), |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 6044 | Owned, IsDependent, false, false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6045 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6046 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 6047 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6048 | if (!TagD) |
| 6049 | return true; |
| 6050 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6051 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6052 | if (Tag->isEnum()) { |
| 6053 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 6054 | << Context.getTypeDeclType(Tag); |
| 6055 | return true; |
| 6056 | } |
| 6057 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 6058 | if (Tag->isInvalidDecl()) |
| 6059 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6060 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6061 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 6062 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 6063 | if (!Pattern) { |
| 6064 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 6065 | << Context.getTypeDeclType(Record); |
| 6066 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 6067 | return true; |
| 6068 | } |
| 6069 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6070 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6071 | // If the explicit instantiation is for a class or member class, the |
| 6072 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6073 | // simple-template-id. |
| 6074 | // |
| 6075 | // C++98 has the same restriction, just worded differently. |
| 6076 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6077 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6078 | << Record << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6079 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6080 | // C++0x [temp.explicit]p2: |
| 6081 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6082 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6083 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 6084 | TemplateSpecializationKind TSK |
| 6085 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6086 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6087 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6088 | // C++0x [temp.explicit]p2: |
| 6089 | // [...] An explicit instantiation shall appear in an enclosing |
| 6090 | // namespace of its template. [...] |
| 6091 | // |
| 6092 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6093 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6094 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6095 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6096 | CXXRecordDecl *PrevDecl |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6097 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6098 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6099 | PrevDecl = Record; |
| 6100 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6101 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6102 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6103 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6104 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6105 | PrevDecl, |
| 6106 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6107 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6108 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6109 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6110 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6111 | return TagD; |
| 6112 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6113 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6114 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6115 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6116 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6117 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6118 | // 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] | 6119 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6120 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6121 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6122 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 6123 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 6124 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6125 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 6126 | << Pattern; |
| 6127 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6128 | } else { |
| 6129 | if (InstantiateClass(NameLoc, Record, Def, |
| 6130 | getTemplateInstantiationArgs(Record), |
| 6131 | TSK)) |
| 6132 | return true; |
| 6133 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6134 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6135 | if (!RecordDef) |
| 6136 | return true; |
| 6137 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6138 | } |
| 6139 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6140 | // Instantiate all of the members of the class. |
| 6141 | InstantiateClassMembers(NameLoc, RecordDef, |
| 6142 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6143 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6144 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 6145 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 6146 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6147 | // FIXME: We don't have any representation for explicit instantiations of |
| 6148 | // member classes. Such a representation is not needed for compilation, but it |
| 6149 | // should be available for clients that want to see all of the declarations in |
| 6150 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6151 | return TagD; |
| 6152 | } |
| 6153 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6154 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 6155 | SourceLocation ExternLoc, |
| 6156 | SourceLocation TemplateLoc, |
| 6157 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6158 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6159 | // TODO: check if/when DNInfo should replace Name. |
| 6160 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6161 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6162 | if (!Name) { |
| 6163 | if (!D.isInvalidType()) |
| 6164 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 6165 | diag::err_explicit_instantiation_requires_name) |
| 6166 | << D.getDeclSpec().getSourceRange() |
| 6167 | << D.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6168 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6169 | return true; |
| 6170 | } |
| 6171 | |
| 6172 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 6173 | // we find one that is. |
| 6174 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 6175 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 6176 | S = S->getParent(); |
| 6177 | |
| 6178 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6179 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 6180 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6181 | if (R.isNull()) |
| 6182 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6183 | |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6184 | // C++ [dcl.stc]p1: |
| 6185 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 6186 | // instantiation (14.7.2) directive. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6187 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6188 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 6189 | << Name; |
| 6190 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6191 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 6192 | != DeclSpec::SCS_unspecified) { |
| 6193 | // Complain about then remove the storage class specifier. |
| 6194 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 6195 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6196 | |
| 6197 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6198 | } |
| 6199 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6200 | // C++0x [temp.explicit]p1: |
| 6201 | // [...] An explicit instantiation of a function template shall not use the |
| 6202 | // inline or constexpr specifiers. |
| 6203 | // Presumably, this also applies to member functions of class templates as |
| 6204 | // well. |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6205 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6206 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6207 | getLangOptions().CPlusPlus0x ? |
| 6208 | diag::err_explicit_instantiation_inline : |
| 6209 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | fe6f648 | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 6210 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
| 6211 | if (D.getDeclSpec().isConstexprSpecified()) |
| 6212 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 6213 | // not already specified. |
| 6214 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 6215 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6216 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6217 | // C++0x [temp.explicit]p2: |
| 6218 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6219 | // definition and an explicit instantiation declaration. An explicit |
| 6220 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6221 | TemplateSpecializationKind TSK |
| 6222 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6223 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6224 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6225 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6226 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6227 | |
| 6228 | if (!R->isFunctionType()) { |
| 6229 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6230 | // A [...] static data member of a class template can be explicitly |
| 6231 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6232 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6233 | if (Previous.isAmbiguous()) |
| 6234 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6235 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6236 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6237 | if (!Prev || !Prev->isStaticDataMember()) { |
| 6238 | // We expect to see a data data member here. |
| 6239 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 6240 | << Name; |
| 6241 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6242 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6243 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6244 | return true; |
| 6245 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6246 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6247 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 6248 | // FIXME: Check for explicit specialization? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6249 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6250 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 6251 | << Prev; |
| 6252 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 6253 | // FIXME: Can we provide a note showing where this was declared? |
| 6254 | return true; |
| 6255 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6256 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6257 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6258 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6259 | // or a static data member of a class template specialization, the name of |
| 6260 | // the class template specialization in the qualified-id for the member |
| 6261 | // name shall be a simple-template-id. |
| 6262 | // |
| 6263 | // C++98 has the same restriction, just worded differently. |
| 6264 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6265 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6266 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6267 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6268 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6269 | // Check the scope of this explicit instantiation. |
| 6270 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6271 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6272 | // Verify that it is okay to explicitly instantiate here. |
| 6273 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 6274 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6275 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6276 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6277 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6278 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6279 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6280 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6281 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6282 | return (Decl*) 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6283 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6284 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6285 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6286 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6287 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6288 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6289 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6290 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6291 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6292 | |
| 6293 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6294 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6295 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6296 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6297 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 6298 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6299 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 6300 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6301 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 6302 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6303 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6304 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6305 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 6306 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6307 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6308 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6309 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6310 | // A [...] function [...] can be explicitly instantiated from its template. |
| 6311 | // A member function [...] of a class template can be explicitly |
| 6312 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6313 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6314 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6315 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6316 | P != PEnd; ++P) { |
| 6317 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6318 | if (!HasExplicitTemplateArgs) { |
| 6319 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 6320 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 6321 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6322 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6323 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6324 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 6325 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6326 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6327 | } |
| 6328 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6329 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6330 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 6331 | if (!FunTmpl) |
| 6332 | continue; |
| 6333 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6334 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6335 | FunctionDecl *Specialization = 0; |
| 6336 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6337 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6338 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6339 | R, Specialization, Info)) { |
| 6340 | // FIXME: Keep track of almost-matches? |
| 6341 | (void)TDK; |
| 6342 | continue; |
| 6343 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6344 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6345 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6346 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6347 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6348 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6349 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6350 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6351 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6352 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 6353 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 6354 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6355 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6356 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6357 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6358 | |
| 6359 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 6360 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6361 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6362 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6363 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6364 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 6365 | << Specialization |
| 6366 | << (Specialization->getTemplateSpecializationKind() == |
| 6367 | TSK_ExplicitSpecialization); |
| 6368 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 6369 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6370 | } |
| 6371 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6372 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6373 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 6374 | PrevDecl = Specialization; |
| 6375 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6376 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6377 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6378 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6379 | PrevDecl, |
| 6380 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6381 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6382 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6383 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6384 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6385 | // FIXME: We may still want to build some representation of this |
| 6386 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6387 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6388 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6389 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 6390 | |
| 6391 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6392 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6393 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6394 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6395 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6396 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6397 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6398 | // or a static data member of a class template specialization, the name of |
| 6399 | // the class template specialization in the qualified-id for the member |
| 6400 | // name shall be a simple-template-id. |
| 6401 | // |
| 6402 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6403 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6404 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6405 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6406 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6407 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6408 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6409 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6410 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6411 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6412 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6413 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6414 | D.getIdentifierLoc(), |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6415 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6416 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6417 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6418 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6419 | } |
| 6420 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6421 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6422 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 6423 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 6424 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 6425 | // This has to hold, because SS is expected to be defined. |
| 6426 | assert(Name && "Expected a name in a dependent tag"); |
| 6427 | |
| 6428 | NestedNameSpecifier *NNS |
| 6429 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6430 | if (!NNS) |
| 6431 | return true; |
| 6432 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6433 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 6434 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6435 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 6436 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6437 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6438 | return true; |
| 6439 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6440 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6441 | // Create the resulting type. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6442 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6443 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 6444 | |
| 6445 | // Create type-source location information for this type. |
| 6446 | TypeLocBuilder TLB; |
| 6447 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
| 6448 | TL.setKeywordLoc(TagLoc); |
| 6449 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6450 | TL.setNameLoc(NameLoc); |
| 6451 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6452 | } |
| 6453 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6454 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6455 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6456 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6457 | SourceLocation IdLoc) { |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6458 | if (SS.isInvalid()) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6459 | return true; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6460 | |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6461 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6462 | Diag(TypenameLoc, |
| 6463 | getLangOptions().CPlusPlus0x ? |
| 6464 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6465 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6466 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6467 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6468 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6469 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 6470 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 6471 | if (T.isNull()) |
| 6472 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6473 | |
| 6474 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6475 | if (isa<DependentNameType>(T)) { |
| 6476 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6477 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6478 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6479 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6480 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6481 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6482 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6483 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6484 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6485 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6486 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6487 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6488 | } |
| 6489 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6490 | TypeResult |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6491 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6492 | const CXXScopeSpec &SS, |
| 6493 | SourceLocation TemplateLoc, |
| 6494 | TemplateTy TemplateIn, |
| 6495 | SourceLocation TemplateNameLoc, |
| 6496 | SourceLocation LAngleLoc, |
| 6497 | ASTTemplateArgsPtr TemplateArgsIn, |
| 6498 | SourceLocation RAngleLoc) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6499 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6500 | Diag(TypenameLoc, |
| 6501 | getLangOptions().CPlusPlus0x ? |
| 6502 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6503 | diag::ext_typename_outside_of_template) |
| 6504 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6505 | |
| 6506 | // Translate the parser's template argument list in our AST format. |
| 6507 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 6508 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 6509 | |
| 6510 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6511 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 6512 | // Construct a dependent template specialization type. |
| 6513 | assert(DTN && "dependent template has non-dependent name?"); |
| 6514 | assert(DTN->getQualifier() |
| 6515 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 6516 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 6517 | DTN->getQualifier(), |
| 6518 | DTN->getIdentifier(), |
| 6519 | TemplateArgs); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6520 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6521 | // Create source-location information for this type. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6522 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6523 | DependentTemplateSpecializationTypeLoc SpecTL |
| 6524 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6525 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6526 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6527 | SpecTL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 6528 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6529 | SpecTL.setNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6530 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6531 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6532 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 6533 | } |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6534 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6535 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 6536 | if (T.isNull()) |
| 6537 | return true; |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6538 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6539 | // Provide source-location information for the template specialization |
| 6540 | // type. |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6541 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6542 | TemplateSpecializationTypeLoc SpecTL |
| 6543 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
| 6544 | |
| 6545 | // FIXME: No place to set the location of the 'template' keyword! |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6546 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6547 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6548 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6549 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6550 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 6551 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6552 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 6553 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 6554 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6555 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6556 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6557 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 6558 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 6559 | } |
| 6560 | |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6561 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6562 | /// \brief Build the type that describes a C++ typename specifier, |
| 6563 | /// e.g., "typename T::type". |
| 6564 | QualType |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6565 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 6566 | SourceLocation KeywordLoc, |
| 6567 | NestedNameSpecifierLoc QualifierLoc, |
| 6568 | const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6569 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6570 | CXXScopeSpec SS; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6571 | SS.Adopt(QualifierLoc); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6572 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6573 | DeclContext *Ctx = computeDeclContext(SS); |
| 6574 | if (!Ctx) { |
| 6575 | // If the nested-name-specifier is dependent and couldn't be |
| 6576 | // resolved to a type, build a typename type. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6577 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 6578 | return Context.getDependentNameType(Keyword, |
| 6579 | QualifierLoc.getNestedNameSpecifier(), |
| 6580 | &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 6581 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6582 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6583 | // If the nested-name-specifier refers to the current instantiation, |
| 6584 | // the "typename" keyword itself is superfluous. In C++03, the |
| 6585 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 6586 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 6587 | // 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] | 6588 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6589 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 6590 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6591 | |
| 6592 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6593 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6594 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6595 | unsigned DiagID = 0; |
| 6596 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6597 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6598 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6599 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6600 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6601 | |
| 6602 | case LookupResult::FoundUnresolvedValue: { |
| 6603 | // We found a using declaration that is a value. Most likely, the using |
| 6604 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6605 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6606 | IILoc); |
| 6607 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 6608 | << Name << Ctx << FullRange; |
| 6609 | if (UnresolvedUsingValueDecl *Using |
| 6610 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6611 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6612 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 6613 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 6614 | } |
| 6615 | } |
| 6616 | // Fall through to create a dependent typename type, from which we can recover |
| 6617 | // better. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6618 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 6619 | case LookupResult::NotFoundInCurrentInstantiation: |
| 6620 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6621 | return Context.getDependentNameType(Keyword, |
| 6622 | QualifierLoc.getNestedNameSpecifier(), |
| 6623 | &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6624 | |
| 6625 | case LookupResult::Found: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6626 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6627 | // We found a type. Build an ElaboratedType, since the |
| 6628 | // typename-specifier was just sugar. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6629 | return Context.getElaboratedType(ETK_Typename, |
| 6630 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6631 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6632 | } |
| 6633 | |
| 6634 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6635 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6636 | break; |
| 6637 | |
| 6638 | case LookupResult::FoundOverloaded: |
| 6639 | DiagID = diag::err_typename_nested_not_type; |
| 6640 | Referenced = *Result.begin(); |
| 6641 | break; |
| 6642 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 6643 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6644 | return QualType(); |
| 6645 | } |
| 6646 | |
| 6647 | // If we get here, it's because name lookup did not find a |
| 6648 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6649 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6650 | IILoc); |
| 6651 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6652 | if (Referenced) |
| 6653 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 6654 | << Name; |
| 6655 | return QualType(); |
| 6656 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6657 | |
| 6658 | namespace { |
| 6659 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 6660 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6661 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6662 | SourceLocation Loc; |
| 6663 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6664 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6665 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 6666 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6667 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6668 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6669 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6670 | DeclarationName Entity) |
| 6671 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6672 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6673 | |
| 6674 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6675 | /// transformed. |
| 6676 | /// |
| 6677 | /// For the purposes of type reconstruction, a type has already been |
| 6678 | /// transformed if it is NULL or if it is not dependent. |
| 6679 | bool AlreadyTransformed(QualType T) { |
| 6680 | return T.isNull() || !T->isDependentType(); |
| 6681 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6682 | |
| 6683 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6684 | /// rebuilt. |
| 6685 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6686 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6687 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 6688 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6689 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6690 | /// \brief Sets the "base" location and entity when that |
| 6691 | /// information is known based on another transformation. |
| 6692 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 6693 | this->Loc = Loc; |
| 6694 | this->Entity = Entity; |
| 6695 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6696 | }; |
| 6697 | } |
| 6698 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6699 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 6700 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6701 | /// 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] | 6702 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6703 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6704 | /// partial specialization thereof). This routine will rebuild that type now |
| 6705 | /// that we have entered the declarator's scope, which may produce different |
| 6706 | /// canonical types, e.g., |
| 6707 | /// |
| 6708 | /// \code |
| 6709 | /// template<typename T> |
| 6710 | /// struct X { |
| 6711 | /// typedef T* pointer; |
| 6712 | /// pointer data(); |
| 6713 | /// }; |
| 6714 | /// |
| 6715 | /// template<typename T> |
| 6716 | /// typename X<T>::pointer X<T>::data() { ... } |
| 6717 | /// \endcode |
| 6718 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 6719 | /// 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] | 6720 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 6721 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6722 | /// 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] | 6723 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 6724 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6725 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 6726 | SourceLocation Loc, |
| 6727 | DeclarationName Name) { |
| 6728 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6729 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6730 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6731 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 6732 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 6733 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6734 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6735 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6736 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 6737 | DeclarationName()); |
| 6738 | return Rebuilder.TransformExpr(E); |
| 6739 | } |
| 6740 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6741 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6742 | if (SS.isInvalid()) |
| 6743 | return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6744 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6745 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6746 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 6747 | DeclarationName()); |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6748 | NestedNameSpecifierLoc Rebuilt |
| 6749 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 6750 | if (!Rebuilt) |
| 6751 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6752 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6753 | SS.Adopt(Rebuilt); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6754 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6755 | } |
| 6756 | |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 6757 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 6758 | /// instantiation. |
| 6759 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 6760 | TemplateParameterList *Params) { |
| 6761 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 6762 | Decl *Param = Params->getParam(I); |
| 6763 | |
| 6764 | // There is nothing to rebuild in a type parameter. |
| 6765 | if (isa<TemplateTypeParmDecl>(Param)) |
| 6766 | continue; |
| 6767 | |
| 6768 | // Rebuild the template parameter list of a template template parameter. |
| 6769 | if (TemplateTemplateParmDecl *TTP |
| 6770 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 6771 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 6772 | TTP->getTemplateParameters())) |
| 6773 | return true; |
| 6774 | |
| 6775 | continue; |
| 6776 | } |
| 6777 | |
| 6778 | // Rebuild the type of a non-type template parameter. |
| 6779 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 6780 | TypeSourceInfo *NewTSI |
| 6781 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 6782 | NTTP->getLocation(), |
| 6783 | NTTP->getDeclName()); |
| 6784 | if (!NewTSI) |
| 6785 | return true; |
| 6786 | |
| 6787 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 6788 | NTTP->setTypeSourceInfo(NewTSI); |
| 6789 | NTTP->setType(NewTSI->getType()); |
| 6790 | } |
| 6791 | } |
| 6792 | |
| 6793 | return false; |
| 6794 | } |
| 6795 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6796 | /// \brief Produces a formatted string that describes the binding of |
| 6797 | /// template parameters to template arguments. |
| 6798 | std::string |
| 6799 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6800 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6801 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6802 | } |
| 6803 | |
| 6804 | std::string |
| 6805 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6806 | const TemplateArgument *Args, |
| 6807 | unsigned NumArgs) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6808 | llvm::SmallString<128> Str; |
| 6809 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6810 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6811 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6812 | return std::string(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6813 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6814 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6815 | if (I >= NumArgs) |
| 6816 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6817 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6818 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6819 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6820 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6821 | Out << ", "; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6822 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6823 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6824 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6825 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6826 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6827 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6828 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6829 | Out << " = "; |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6830 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6831 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6832 | |
| 6833 | Out << ']'; |
| 6834 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6835 | } |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 6836 | |
| 6837 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) { |
| 6838 | if (!FD) |
| 6839 | return; |
| 6840 | FD->setLateTemplateParsed(Flag); |
| 6841 | } |
| 6842 | |
| 6843 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 6844 | DeclContext *DC = CurContext; |
| 6845 | |
| 6846 | while (DC) { |
| 6847 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 6848 | const FunctionDecl *FD = RD->isLocalClass(); |
| 6849 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 6850 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 6851 | return false; |
| 6852 | |
| 6853 | DC = DC->getParent(); |
| 6854 | } |
| 6855 | return false; |
| 6856 | } |