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 | |
Douglas Gregor | 2ccd89c | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1016 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1017 | NewTemplate->setModulePrivate(); |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1018 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1019 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1020 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1021 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1022 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1023 | (void)T; |
| 1024 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1025 | // 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] | 1026 | // class template, make a note of that. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1027 | if (PrevClassTemplate && |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1028 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1029 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1030 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1031 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1032 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1033 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1034 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1035 | // Set the lexical context of these templates |
| 1036 | NewClass->setLexicalDeclContext(CurContext); |
| 1037 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1038 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1039 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1040 | NewClass->startDefinition(); |
| 1041 | |
| 1042 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1043 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1044 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1045 | if (TUK != TUK_Friend) |
| 1046 | PushOnScopeChains(NewTemplate, S); |
| 1047 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1048 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1049 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1050 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1051 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1052 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1053 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1054 | PrevClassTemplate != NULL); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1055 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1056 | // Friend templates are visible in fairly strange ways. |
| 1057 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1058 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1059 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 1060 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1061 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1062 | /* AddToContext = */ false); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1063 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1064 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1065 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1066 | NewClass->getLocation(), |
| 1067 | NewTemplate, |
| 1068 | /*FIXME:*/NewClass->getLocation()); |
| 1069 | Friend->setAccess(AS_public); |
| 1070 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1071 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1072 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1073 | if (Invalid) { |
| 1074 | NewTemplate->setInvalidDecl(); |
| 1075 | NewClass->setInvalidDecl(); |
| 1076 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1077 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1080 | /// \brief Diagnose the presence of a default template argument on a |
| 1081 | /// template parameter, which is ill-formed in certain contexts. |
| 1082 | /// |
| 1083 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1084 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1085 | Sema::TemplateParamListContext TPC, |
| 1086 | SourceLocation ParamLoc, |
| 1087 | SourceRange DefArgRange) { |
| 1088 | switch (TPC) { |
| 1089 | case Sema::TPC_ClassTemplate: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1090 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1091 | return false; |
| 1092 | |
| 1093 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1094 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1095 | // C++ [temp.param]p9: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1096 | // A default template-argument shall not be specified in a |
| 1097 | // function template declaration or a function template |
| 1098 | // definition [...] |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1099 | // If a friend function template declaration specifies a default |
| 1100 | // template-argument, that declaration shall be a definition and shall be |
| 1101 | // the only declaration of the function template in the translation unit. |
| 1102 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1103 | S.Diag(ParamLoc, S.getLangOptions().CPlusPlus0x ? |
| 1104 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1105 | : diag::ext_template_parameter_default_in_function_template) |
| 1106 | << DefArgRange; |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1107 | return false; |
| 1108 | |
| 1109 | case Sema::TPC_ClassTemplateMember: |
| 1110 | // C++0x [temp.param]p9: |
| 1111 | // A default template-argument shall not be specified in the |
| 1112 | // template-parameter-lists of the definition of a member of a |
| 1113 | // class template that appears outside of the member's class. |
| 1114 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1115 | << DefArgRange; |
| 1116 | return true; |
| 1117 | |
| 1118 | case Sema::TPC_FriendFunctionTemplate: |
| 1119 | // C++ [temp.param]p9: |
| 1120 | // A default template-argument shall not be specified in a |
| 1121 | // friend template declaration. |
| 1122 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1123 | << DefArgRange; |
| 1124 | return true; |
| 1125 | |
| 1126 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1127 | // for friend function templates if there is only a single |
| 1128 | // declaration (and it is a definition). Strange! |
| 1129 | } |
| 1130 | |
| 1131 | return false; |
| 1132 | } |
| 1133 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1134 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1135 | /// of a template template parameter, recursively. |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1136 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1137 | TemplateTemplateParmDecl *TTP) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1138 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1139 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1140 | NamedDecl *P = Params->getParam(I); |
| 1141 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1142 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1143 | NTTP->getTypeSourceInfo(), |
| 1144 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1145 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1146 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1147 | continue; |
| 1148 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1149 | |
| 1150 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1151 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1152 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1153 | return true; |
| 1154 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1155 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1156 | return false; |
| 1157 | } |
| 1158 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1159 | /// \brief Checks the validity of a template parameter list, possibly |
| 1160 | /// considering the template parameter list from a previous |
| 1161 | /// declaration. |
| 1162 | /// |
| 1163 | /// If an "old" template parameter list is provided, it must be |
| 1164 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1165 | /// template parameter list. |
| 1166 | /// |
| 1167 | /// \param NewParams Template parameter list for a new template |
| 1168 | /// declaration. This template parameter list will be updated with any |
| 1169 | /// default arguments that are carried through from the previous |
| 1170 | /// template parameter list. |
| 1171 | /// |
| 1172 | /// \param OldParams If provided, template parameter list from a |
| 1173 | /// previous declaration of the same template. Default template |
| 1174 | /// arguments will be merged from the old template parameter list to |
| 1175 | /// the new template parameter list. |
| 1176 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1177 | /// \param TPC Describes the context in which we are checking the given |
| 1178 | /// template parameter list. |
| 1179 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1180 | /// \returns true if an error occurred, false otherwise. |
| 1181 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1182 | TemplateParameterList *OldParams, |
| 1183 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1184 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1185 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1186 | // C++ [temp.param]p10: |
| 1187 | // The set of default template-arguments available for use with a |
| 1188 | // template declaration or definition is obtained by merging the |
| 1189 | // default arguments from the definition (if in scope) and all |
| 1190 | // declarations in scope in the same way default function |
| 1191 | // arguments are (8.3.6). |
| 1192 | bool SawDefaultArgument = false; |
| 1193 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1194 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1195 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1196 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1197 | if (OldParams) |
| 1198 | OldParam = OldParams->begin(); |
| 1199 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1200 | bool RemoveDefaultArguments = false; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1201 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1202 | NewParamEnd = NewParams->end(); |
| 1203 | NewParam != NewParamEnd; ++NewParam) { |
| 1204 | // Variables used to diagnose redundant default arguments |
| 1205 | bool RedundantDefaultArg = false; |
| 1206 | SourceLocation OldDefaultLoc; |
| 1207 | SourceLocation NewDefaultLoc; |
| 1208 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1209 | // Variable used to diagnose missing default arguments |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1210 | bool MissingDefaultArg = false; |
| 1211 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1212 | // Variable used to diagnose non-final parameter packs |
| 1213 | bool SawParameterPack = false; |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1214 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1215 | if (TemplateTypeParmDecl *NewTypeParm |
| 1216 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1217 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1218 | if (NewTypeParm->hasDefaultArgument() && |
| 1219 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1220 | NewTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1221 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1222 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1223 | NewTypeParm->removeDefaultArgument(); |
| 1224 | |
| 1225 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1226 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1227 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1229 | if (NewTypeParm->isParameterPack()) { |
| 1230 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1231 | "Parameter packs can't have a default argument!"); |
| 1232 | SawParameterPack = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1233 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1234 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1235 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1236 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1237 | SawDefaultArgument = true; |
| 1238 | RedundantDefaultArg = true; |
| 1239 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1240 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1241 | // Merge the default argument from the old declaration to the |
| 1242 | // new declaration. |
| 1243 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1244 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1245 | true); |
| 1246 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1247 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1248 | SawDefaultArgument = true; |
| 1249 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1250 | } else if (SawDefaultArgument) |
| 1251 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1252 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1253 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1254 | // Check for unexpanded parameter packs. |
| 1255 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1256 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1257 | UPPC_NonTypeTemplateParameterType)) { |
| 1258 | Invalid = true; |
| 1259 | continue; |
| 1260 | } |
| 1261 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1262 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1263 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1264 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1265 | NewNonTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1266 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1267 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1270 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1271 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1272 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1273 | if (NewNonTypeParm->isParameterPack()) { |
| 1274 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1275 | "Parameter packs can't have a default argument!"); |
| 1276 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1277 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1278 | NewNonTypeParm->hasDefaultArgument()) { |
| 1279 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1280 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1281 | SawDefaultArgument = true; |
| 1282 | RedundantDefaultArg = true; |
| 1283 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1284 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1285 | // Merge the default argument from the old declaration to the |
| 1286 | // new declaration. |
| 1287 | SawDefaultArgument = true; |
| 1288 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1289 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1290 | // parameter. |
| 1291 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1292 | OldNonTypeParm->getDefaultArgument(), |
| 1293 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1294 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1295 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1296 | SawDefaultArgument = true; |
| 1297 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1298 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1299 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1300 | } else { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1301 | TemplateTemplateParmDecl *NewTemplateParm |
| 1302 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1303 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1304 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1305 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1306 | Invalid = true; |
| 1307 | continue; |
| 1308 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1309 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1310 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1311 | if (NewTemplateParm->hasDefaultArgument() && |
| 1312 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1313 | NewTemplateParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1314 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1315 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1316 | |
| 1317 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1318 | TemplateTemplateParmDecl *OldTemplateParm |
| 1319 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1320 | if (NewTemplateParm->isParameterPack()) { |
| 1321 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1322 | "Parameter packs can't have a default argument!"); |
| 1323 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1324 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1325 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1326 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1327 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1328 | SawDefaultArgument = true; |
| 1329 | RedundantDefaultArg = true; |
| 1330 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1331 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1332 | // Merge the default argument from the old declaration to the |
| 1333 | // new declaration. |
| 1334 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1335 | // FIXME: We need to create a new kind of "default argument" expression |
| 1336 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1337 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1338 | OldTemplateParm->getDefaultArgument(), |
| 1339 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1340 | PreviousDefaultArgLoc |
| 1341 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1342 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1343 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1344 | PreviousDefaultArgLoc |
| 1345 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1346 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1347 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1350 | // C++0x [temp.param]p11: |
| 1351 | // If a template parameter of a primary class template or alias template |
| 1352 | // is a template parameter pack, it shall be the last template parameter. |
| 1353 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
| 1354 | (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) { |
| 1355 | Diag((*NewParam)->getLocation(), |
| 1356 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1357 | Invalid = true; |
| 1358 | } |
| 1359 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1360 | if (RedundantDefaultArg) { |
| 1361 | // C++ [temp.param]p12: |
| 1362 | // A template-parameter shall not be given default arguments |
| 1363 | // by two different declarations in the same scope. |
| 1364 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1365 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1366 | Invalid = true; |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1367 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1368 | // C++ [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1369 | // If a template-parameter of a class template has a default |
| 1370 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1371 | // have a default template-argument supplied or be a template parameter |
| 1372 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1373 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1374 | diag::err_template_param_default_arg_missing); |
| 1375 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1376 | Invalid = true; |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1377 | RemoveDefaultArguments = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | // If we have an old template parameter list that we're merging |
| 1381 | // in, move on to the next parameter. |
| 1382 | if (OldParams) |
| 1383 | ++OldParam; |
| 1384 | } |
| 1385 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1386 | // We were missing some default arguments at the end of the list, so remove |
| 1387 | // all of the default arguments. |
| 1388 | if (RemoveDefaultArguments) { |
| 1389 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1390 | NewParamEnd = NewParams->end(); |
| 1391 | NewParam != NewParamEnd; ++NewParam) { |
| 1392 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1393 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1394 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1395 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1396 | NTTP->removeDefaultArgument(); |
| 1397 | else |
| 1398 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1399 | } |
| 1400 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1401 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1402 | return Invalid; |
| 1403 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1404 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1405 | namespace { |
| 1406 | |
| 1407 | /// A class which looks for a use of a certain level of template |
| 1408 | /// parameter. |
| 1409 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1410 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1411 | |
| 1412 | unsigned Depth; |
| 1413 | bool Match; |
| 1414 | |
| 1415 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1416 | NamedDecl *ND = Params->getParam(0); |
| 1417 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1418 | Depth = PD->getDepth(); |
| 1419 | } else if (NonTypeTemplateParmDecl *PD = |
| 1420 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1421 | Depth = PD->getDepth(); |
| 1422 | } else { |
| 1423 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | bool Matches(unsigned ParmDepth) { |
| 1428 | if (ParmDepth >= Depth) { |
| 1429 | Match = true; |
| 1430 | return true; |
| 1431 | } |
| 1432 | return false; |
| 1433 | } |
| 1434 | |
| 1435 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1436 | return !Matches(T->getDepth()); |
| 1437 | } |
| 1438 | |
| 1439 | bool TraverseTemplateName(TemplateName N) { |
| 1440 | if (TemplateTemplateParmDecl *PD = |
| 1441 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1442 | if (Matches(PD->getDepth())) return false; |
| 1443 | return super::TraverseTemplateName(N); |
| 1444 | } |
| 1445 | |
| 1446 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1447 | if (NonTypeTemplateParmDecl *PD = |
| 1448 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1449 | if (PD->getDepth() == Depth) { |
| 1450 | Match = true; |
| 1451 | return false; |
| 1452 | } |
| 1453 | } |
| 1454 | return super::VisitDeclRefExpr(E); |
| 1455 | } |
Douglas Gregor | 18c8339 | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1456 | |
| 1457 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1458 | return TraverseType(T->getInjectedSpecializationType()); |
| 1459 | } |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1460 | }; |
| 1461 | } |
| 1462 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1463 | /// Determines whether a given type depends on the given parameter |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1464 | /// list. |
| 1465 | static bool |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1466 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1467 | DependencyChecker Checker(Params); |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1468 | Checker.TraverseType(T); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1469 | return Checker.Match; |
| 1470 | } |
| 1471 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1472 | // Find the source range corresponding to the named type in the given |
| 1473 | // nested-name-specifier, if any. |
| 1474 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1475 | QualType T, |
| 1476 | const CXXScopeSpec &SS) { |
| 1477 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1478 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1479 | if (const Type *CurType = NNS->getAsType()) { |
| 1480 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1481 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1482 | } else |
| 1483 | break; |
| 1484 | |
| 1485 | NNSLoc = NNSLoc.getPrefix(); |
| 1486 | } |
| 1487 | |
| 1488 | return SourceRange(); |
| 1489 | } |
| 1490 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1491 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1492 | /// specifier, returning the template parameter list that applies to the |
| 1493 | /// name. |
| 1494 | /// |
| 1495 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1496 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | /// |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1498 | /// \param DeclLoc The location of the declaration itself. |
| 1499 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1500 | /// \param SS the scope specifier that will be matched to the given template |
| 1501 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1502 | /// being declared. |
| 1503 | /// |
| 1504 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1505 | /// innermost template parameter lists. |
| 1506 | /// |
| 1507 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1508 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1509 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1510 | /// matching template parameters to scope specifiers in friend |
| 1511 | /// declarations. |
| 1512 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1513 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1514 | /// declared is an explicit specialization, false otherwise. |
| 1515 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1517 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1518 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1520 | /// 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] | 1521 | /// itself a template). |
| 1522 | TemplateParameterList * |
| 1523 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1524 | SourceLocation DeclLoc, |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1525 | const CXXScopeSpec &SS, |
| 1526 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1527 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1528 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1529 | bool &IsExplicitSpecialization, |
| 1530 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1531 | IsExplicitSpecialization = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1532 | Invalid = false; |
| 1533 | |
| 1534 | // The sequence of nested types to which we will match up the template |
| 1535 | // parameter lists. We first build this list by starting with the type named |
| 1536 | // 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] | 1537 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1538 | QualType T; |
Douglas Gregor | 714c992 | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1539 | if (SS.getScopeRep()) { |
| 1540 | if (CXXRecordDecl *Record |
| 1541 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1542 | T = Context.getTypeDeclType(Record); |
| 1543 | else |
| 1544 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1545 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1546 | |
| 1547 | // If we found an explicit specialization that prevents us from needing |
| 1548 | // 'template<>' headers, this will be set to the location of that |
| 1549 | // explicit specialization. |
| 1550 | SourceLocation ExplicitSpecLoc; |
| 1551 | |
| 1552 | while (!T.isNull()) { |
| 1553 | NestedTypes.push_back(T); |
| 1554 | |
| 1555 | // Retrieve the parent of a record type. |
| 1556 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1557 | // If this type is an explicit specialization, we're done. |
| 1558 | if (ClassTemplateSpecializationDecl *Spec |
| 1559 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1560 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1561 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1562 | ExplicitSpecLoc = Spec->getLocation(); |
| 1563 | break; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1564 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1565 | } else if (Record->getTemplateSpecializationKind() |
| 1566 | == TSK_ExplicitSpecialization) { |
| 1567 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1568 | break; |
| 1569 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1570 | |
| 1571 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1572 | T = Context.getTypeDeclType(Parent); |
| 1573 | else |
| 1574 | T = QualType(); |
| 1575 | continue; |
| 1576 | } |
| 1577 | |
| 1578 | if (const TemplateSpecializationType *TST |
| 1579 | = T->getAs<TemplateSpecializationType>()) { |
| 1580 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1581 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1582 | T = Context.getTypeDeclType(Parent); |
| 1583 | else |
| 1584 | T = QualType(); |
| 1585 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1586 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | // Look one step prior in a dependent template specialization type. |
| 1590 | if (const DependentTemplateSpecializationType *DependentTST |
| 1591 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1592 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1593 | T = QualType(NNS->getAsType(), 0); |
| 1594 | else |
| 1595 | T = QualType(); |
| 1596 | continue; |
| 1597 | } |
| 1598 | |
| 1599 | // Look one step prior in a dependent name type. |
| 1600 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1601 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1602 | T = QualType(NNS->getAsType(), 0); |
| 1603 | else |
| 1604 | T = QualType(); |
| 1605 | continue; |
| 1606 | } |
| 1607 | |
| 1608 | // Retrieve the parent of an enumeration type. |
| 1609 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1610 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1611 | // check here. |
| 1612 | EnumDecl *Enum = EnumT->getDecl(); |
| 1613 | |
| 1614 | // Get to the parent type. |
| 1615 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1616 | T = Context.getTypeDeclType(Parent); |
| 1617 | else |
| 1618 | T = QualType(); |
| 1619 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1620 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1621 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1622 | T = QualType(); |
| 1623 | } |
| 1624 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1625 | // to the innermost while checking template-parameter-lists. |
| 1626 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1627 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1628 | // C++0x [temp.expl.spec]p17: |
| 1629 | // A member or a member template may be nested within many |
| 1630 | // enclosing class templates. In an explicit specialization for |
| 1631 | // such a member, the member declaration shall be preceded by a |
| 1632 | // template<> for each enclosing class template that is |
| 1633 | // explicitly specialized. |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1634 | bool SawNonEmptyTemplateParameterList = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1635 | unsigned ParamIdx = 0; |
| 1636 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1637 | ++TypeIdx) { |
| 1638 | T = NestedTypes[TypeIdx]; |
| 1639 | |
| 1640 | // Whether we expect a 'template<>' header. |
| 1641 | bool NeedEmptyTemplateHeader = false; |
| 1642 | |
| 1643 | // Whether we expect a template header with parameters. |
| 1644 | bool NeedNonemptyTemplateHeader = false; |
| 1645 | |
| 1646 | // For a dependent type, the set of template parameters that we |
| 1647 | // expect to see. |
| 1648 | TemplateParameterList *ExpectedTemplateParams = 0; |
| 1649 | |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1650 | // C++0x [temp.expl.spec]p15: |
| 1651 | // A member or a member template may be nested within many enclosing |
| 1652 | // class templates. In an explicit specialization for such a member, the |
| 1653 | // member declaration shall be preceded by a template<> for each |
| 1654 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1655 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1656 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1657 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1658 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1659 | NeedNonemptyTemplateHeader = true; |
| 1660 | } else if (Record->isDependentType()) { |
| 1661 | if (Record->getDescribedClassTemplate()) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1662 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1663 | ->getTemplateParameters(); |
| 1664 | NeedNonemptyTemplateHeader = true; |
| 1665 | } |
| 1666 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1667 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1668 | // C++0x [temp.expl.spec]p4: |
| 1669 | // Members of an explicitly specialized class template are defined |
| 1670 | // in the same manner as members of normal classes, and not using |
| 1671 | // the template<> syntax. |
| 1672 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1673 | NeedEmptyTemplateHeader = true; |
| 1674 | else |
Douglas Gregor | 95ea450 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1675 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1676 | } else if (Record->getTemplateSpecializationKind()) { |
| 1677 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1678 | != TSK_ExplicitSpecialization && |
| 1679 | TypeIdx == NumTypes - 1) |
| 1680 | IsExplicitSpecialization = true; |
| 1681 | |
| 1682 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1683 | } |
| 1684 | } else if (const TemplateSpecializationType *TST |
| 1685 | = T->getAs<TemplateSpecializationType>()) { |
| 1686 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1687 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1688 | NeedNonemptyTemplateHeader = true; |
| 1689 | } |
| 1690 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1691 | // FIXME: We actually could/should check the template arguments here |
| 1692 | // against the corresponding template parameter list. |
| 1693 | NeedNonemptyTemplateHeader = false; |
| 1694 | } |
| 1695 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1696 | // C++ [temp.expl.spec]p16: |
| 1697 | // In an explicit specialization declaration for a member of a class |
| 1698 | // template or a member template that ap- pears in namespace scope, the |
| 1699 | // member template and some of its enclosing class templates may remain |
| 1700 | // unspecialized, except that the declaration shall not explicitly |
| 1701 | // specialize a class member template if its en- closing class templates |
| 1702 | // are not explicitly specialized as well. |
| 1703 | if (ParamIdx < NumParamLists) { |
| 1704 | if (ParamLists[ParamIdx]->size() == 0) { |
| 1705 | if (SawNonEmptyTemplateParameterList) { |
| 1706 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1707 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1708 | Invalid = true; |
| 1709 | IsExplicitSpecialization = false; |
| 1710 | return 0; |
| 1711 | } |
| 1712 | } else |
| 1713 | SawNonEmptyTemplateParameterList = true; |
| 1714 | } |
| 1715 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1716 | if (NeedEmptyTemplateHeader) { |
| 1717 | // If we're on the last of the types, and we need a 'template<>' header |
| 1718 | // here, then it's an explicit specialization. |
| 1719 | if (TypeIdx == NumTypes - 1) |
| 1720 | IsExplicitSpecialization = true; |
| 1721 | |
| 1722 | if (ParamIdx < NumParamLists) { |
| 1723 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1724 | // The header has template parameters when it shouldn't. Complain. |
| 1725 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1726 | diag::err_template_param_list_matches_nontemplate) |
| 1727 | << T |
| 1728 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1729 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1730 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1731 | Invalid = true; |
| 1732 | return 0; |
| 1733 | } |
| 1734 | |
| 1735 | // Consume this template header. |
| 1736 | ++ParamIdx; |
| 1737 | continue; |
| 1738 | } |
| 1739 | |
| 1740 | if (!IsFriend) { |
| 1741 | // We don't have a template header, but we should. |
| 1742 | SourceLocation ExpectedTemplateLoc; |
| 1743 | if (NumParamLists > 0) |
| 1744 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1745 | else |
| 1746 | ExpectedTemplateLoc = DeclStartLoc; |
| 1747 | |
| 1748 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1749 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS) |
| 1750 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1751 | } |
| 1752 | |
| 1753 | continue; |
| 1754 | } |
| 1755 | |
| 1756 | if (NeedNonemptyTemplateHeader) { |
| 1757 | // In friend declarations we can have template-ids which don't |
| 1758 | // depend on the corresponding template parameter lists. But |
| 1759 | // assume that empty parameter lists are supposed to match this |
| 1760 | // template-id. |
| 1761 | if (IsFriend && T->isDependentType()) { |
| 1762 | if (ParamIdx < NumParamLists && |
| 1763 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
| 1764 | ExpectedTemplateParams = 0; |
| 1765 | else |
| 1766 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1768 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1769 | if (ParamIdx < NumParamLists) { |
| 1770 | // Check the template parameter list, if we can. |
| 1771 | if (ExpectedTemplateParams && |
| 1772 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1773 | ExpectedTemplateParams, |
| 1774 | true, TPL_TemplateMatch)) |
| 1775 | Invalid = true; |
| 1776 | |
| 1777 | if (!Invalid && |
| 1778 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1779 | TPC_ClassTemplateMember)) |
| 1780 | Invalid = true; |
| 1781 | |
| 1782 | ++ParamIdx; |
| 1783 | continue; |
| 1784 | } |
| 1785 | |
| 1786 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1787 | << T |
| 1788 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1789 | Invalid = true; |
| 1790 | continue; |
| 1791 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1792 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1793 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1794 | // If there were at least as many template-ids as there were template |
| 1795 | // parameter lists, then there are no template parameter lists remaining for |
| 1796 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1797 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1798 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1800 | // If there were too many template parameter lists, complain about that now. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1801 | if (ParamIdx < NumParamLists - 1) { |
| 1802 | bool HasAnyExplicitSpecHeader = false; |
| 1803 | bool AllExplicitSpecHeaders = true; |
| 1804 | for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) { |
| 1805 | if (ParamLists[I]->size() == 0) |
| 1806 | HasAnyExplicitSpecHeader = true; |
| 1807 | else |
| 1808 | AllExplicitSpecHeaders = false; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1809 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1810 | |
| 1811 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1812 | AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers |
| 1813 | : diag::err_template_spec_extra_headers) |
| 1814 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1815 | ParamLists[NumParamLists - 2]->getRAngleLoc()); |
| 1816 | |
| 1817 | // If there was a specialization somewhere, such that 'template<>' is |
| 1818 | // not required, and there were any 'template<>' headers, note where the |
| 1819 | // specialization occurred. |
| 1820 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1821 | Diag(ExplicitSpecLoc, |
| 1822 | diag::note_explicit_template_spec_does_not_need_header) |
| 1823 | << NestedTypes.back(); |
| 1824 | |
| 1825 | // We have a template parameter list with no corresponding scope, which |
| 1826 | // means that the resulting template declaration can't be instantiated |
| 1827 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1828 | if (!AllExplicitSpecHeaders) |
| 1829 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1830 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1831 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1832 | // C++ [temp.expl.spec]p16: |
| 1833 | // In an explicit specialization declaration for a member of a class |
| 1834 | // template or a member template that ap- pears in namespace scope, the |
| 1835 | // member template and some of its enclosing class templates may remain |
| 1836 | // unspecialized, except that the declaration shall not explicitly |
| 1837 | // specialize a class member template if its en- closing class templates |
| 1838 | // are not explicitly specialized as well. |
| 1839 | if (ParamLists[NumParamLists - 1]->size() == 0 && |
| 1840 | SawNonEmptyTemplateParameterList) { |
| 1841 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1842 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1843 | Invalid = true; |
| 1844 | IsExplicitSpecialization = false; |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1848 | // Return the last template parameter list, which corresponds to the |
| 1849 | // entity being declared. |
| 1850 | return ParamLists[NumParamLists - 1]; |
| 1851 | } |
| 1852 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1853 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1854 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1855 | Diag(Template->getLocation(), diag::note_template_declared_here) |
| 1856 | << (isa<FunctionTemplateDecl>(Template)? 0 |
| 1857 | : isa<ClassTemplateDecl>(Template)? 1 |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1858 | : isa<TypeAliasTemplateDecl>(Template)? 2 |
| 1859 | : 3) |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1860 | << Template->getDeclName(); |
| 1861 | return; |
| 1862 | } |
| 1863 | |
| 1864 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1865 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1866 | IEnd = OST->end(); |
| 1867 | I != IEnd; ++I) |
| 1868 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1869 | << 0 << (*I)->getDeclName(); |
| 1870 | |
| 1871 | return; |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1876 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1877 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1878 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1879 | DependentTemplateName *DTN |
| 1880 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1881 | if (DTN && DTN->isIdentifier()) |
| 1882 | // When building a template-id where the template-name is dependent, |
| 1883 | // assume the template is a type template. Either our assumption is |
| 1884 | // correct, or the code is ill-formed and will be diagnosed when the |
| 1885 | // dependent name is substituted. |
| 1886 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 1887 | DTN->getQualifier(), |
| 1888 | DTN->getIdentifier(), |
| 1889 | TemplateArgs); |
| 1890 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1891 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1892 | if (!Template || isa<FunctionTemplateDecl>(Template)) { |
| 1893 | // We might have a substituted template template parameter pack. If so, |
| 1894 | // build a template specialization type for it. |
| 1895 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 1896 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1897 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1898 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 1899 | << Name; |
| 1900 | NoteAllFoundTemplates(Name); |
| 1901 | return QualType(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1902 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1903 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1904 | // Check that the template argument list is well-formed for this |
| 1905 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1906 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1907 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1908 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1909 | return QualType(); |
| 1910 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1911 | assert((Converted.size() == Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1912 | "Converted template argument list is too short!"); |
| 1913 | |
| 1914 | QualType CanonType; |
| 1915 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1916 | bool InstantiationDependent = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1917 | if (TypeAliasTemplateDecl *AliasTemplate |
| 1918 | = dyn_cast<TypeAliasTemplateDecl>(Template)) { |
| 1919 | // Find the canonical type for this type alias template specialization. |
| 1920 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 1921 | if (Pattern->isInvalidDecl()) |
| 1922 | return QualType(); |
| 1923 | |
| 1924 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1925 | Converted.data(), Converted.size()); |
| 1926 | |
| 1927 | // Only substitute for the innermost template argument list. |
| 1928 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 1804174 | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 1929 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | aff37b4 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 1930 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 1931 | for (unsigned I = 0; I < Depth; ++I) |
| 1932 | TemplateArgLists.addOuterTemplateArguments(0, 0); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1933 | |
| 1934 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
| 1935 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 1936 | TemplateArgLists, AliasTemplate->getLocation(), |
| 1937 | AliasTemplate->getDeclName()); |
| 1938 | if (CanonType.isNull()) |
| 1939 | return QualType(); |
| 1940 | } else if (Name.isDependent() || |
| 1941 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1942 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1943 | // This class template specialization is a dependent |
| 1944 | // type. Therefore, its canonical type is another class template |
| 1945 | // specialization type that contains all of the converted |
| 1946 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1947 | // A<T, T> have identical types when A is declared as: |
| 1948 | // |
| 1949 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1950 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1951 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1952 | Converted.data(), |
| 1953 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1954 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1955 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1956 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1957 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1958 | // build the canonical type and return that to us. |
| 1959 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1960 | |
| 1961 | // This might work out to be a current instantiation, in which |
| 1962 | // case the canonical type needs to be the InjectedClassNameType. |
| 1963 | // |
| 1964 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1965 | // changes to CurContext don't change the set of current |
| 1966 | // instantiations. |
| 1967 | if (isa<ClassTemplateDecl>(Template)) { |
| 1968 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1969 | // If we get out to a namespace, we're done. |
| 1970 | if (Ctx->isFileContext()) break; |
| 1971 | |
| 1972 | // If this isn't a record, keep looking. |
| 1973 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1974 | if (!Record) continue; |
| 1975 | |
| 1976 | // Look for one of the two cases with InjectedClassNameTypes |
| 1977 | // and check whether it's the same template. |
| 1978 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1979 | !Record->getDescribedClassTemplate()) |
| 1980 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1981 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1982 | // Fetch the injected class name type and check whether its |
| 1983 | // injected type is equal to the type we just built. |
| 1984 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1985 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 1986 | ->getInjectedSpecializationType(); |
| 1987 | |
| 1988 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 1989 | continue; |
| 1990 | |
| 1991 | // If so, the canonical type of this TST is the injected |
| 1992 | // class name type of the record we just found. |
| 1993 | assert(ICNT.isCanonical()); |
| 1994 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1995 | break; |
| 1996 | } |
| 1997 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1999 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2000 | // Find the class template specialization declaration that |
| 2001 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2002 | void *InsertPos = 0; |
| 2003 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2004 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2005 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2006 | if (!Decl) { |
| 2007 | // This is the first time we have referenced this class template |
| 2008 | // specialization. Create the canonical declaration and add it to |
| 2009 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2010 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2011 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2012 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 09d8212 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2013 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2014 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2015 | ClassTemplate, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2016 | Converted.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2017 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2018 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2019 | Decl->setLexicalDeclContext(CurContext); |
| 2020 | } |
| 2021 | |
| 2022 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2023 | assert(isa<RecordType>(CanonType) && |
| 2024 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2025 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2026 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2027 | // Build the fully-sugared type for this class template |
| 2028 | // specialization, which refers back to the class template |
| 2029 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2030 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2033 | TypeResult |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2034 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, |
| 2035 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2036 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2037 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2038 | SourceLocation RAngleLoc) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2039 | if (SS.isInvalid()) |
| 2040 | return true; |
| 2041 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2042 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2043 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2044 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2045 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2046 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2047 | |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2048 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2049 | QualType T = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2050 | DTN->getQualifier(), |
| 2051 | DTN->getIdentifier(), |
| 2052 | TemplateArgs); |
| 2053 | |
| 2054 | // Build type-source information. |
| 2055 | TypeLocBuilder TLB; |
| 2056 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2057 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2058 | SpecTL.setKeywordLoc(SourceLocation()); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2059 | SpecTL.setNameLoc(TemplateLoc); |
| 2060 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2061 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 2062 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2063 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2064 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2065 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2066 | } |
| 2067 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2068 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2069 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2070 | |
| 2071 | if (Result.isNull()) |
| 2072 | return true; |
| 2073 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2074 | // Build type-source information. |
| 2075 | TypeLocBuilder TLB; |
| 2076 | TemplateSpecializationTypeLoc SpecTL |
| 2077 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 2078 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2079 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2080 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2081 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2082 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2083 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2084 | if (SS.isNotEmpty()) { |
| 2085 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2086 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2087 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 2088 | ElabTL.setKeywordLoc(SourceLocation()); |
| 2089 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2090 | } |
| 2091 | |
| 2092 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2093 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2094 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2095 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2096 | TypeSpecifierType TagSpec, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2097 | SourceLocation TagLoc, |
| 2098 | CXXScopeSpec &SS, |
| 2099 | TemplateTy TemplateD, |
| 2100 | SourceLocation TemplateLoc, |
| 2101 | SourceLocation LAngleLoc, |
| 2102 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2103 | SourceLocation RAngleLoc) { |
| 2104 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
| 2105 | |
| 2106 | // Translate the parser's template argument list in our AST format. |
| 2107 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2108 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2109 | |
| 2110 | // Determine the tag kind |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2111 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2112 | ElaboratedTypeKeyword Keyword |
| 2113 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2115 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2116 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2117 | DTN->getQualifier(), |
| 2118 | DTN->getIdentifier(), |
| 2119 | TemplateArgs); |
| 2120 | |
| 2121 | // Build type-source information. |
| 2122 | TypeLocBuilder TLB; |
| 2123 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2124 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2125 | SpecTL.setKeywordLoc(TagLoc); |
| 2126 | SpecTL.setNameLoc(TemplateLoc); |
| 2127 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2128 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2129 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2130 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2131 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2132 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2133 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2134 | |
| 2135 | if (TypeAliasTemplateDecl *TAT = |
| 2136 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2137 | // C++0x [dcl.type.elab]p2: |
| 2138 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2139 | // resolves to an alias template specialization, the |
| 2140 | // elaborated-type-specifier is ill-formed. |
| 2141 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2142 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2143 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2144 | |
| 2145 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2146 | if (Result.isNull()) |
Matt Beaumont-Gay | 3a51d41 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2147 | return TypeResult(true); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2148 | |
| 2149 | // Check the tag kind |
| 2150 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2151 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2152 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2153 | IdentifierInfo *Id = D->getIdentifier(); |
| 2154 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2155 | |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2156 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 2157 | TagLoc, *Id)) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2158 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2159 | << Result |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2160 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2161 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2162 | } |
| 2163 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2164 | |
| 2165 | // Provide source-location information for the template specialization. |
| 2166 | TypeLocBuilder TLB; |
| 2167 | TemplateSpecializationTypeLoc SpecTL |
| 2168 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 2169 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2170 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2171 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2172 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2173 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2174 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2175 | // Construct an elaborated type containing the nested-name-specifier (if any) |
| 2176 | // and keyword. |
| 2177 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2178 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 2179 | ElabTL.setKeywordLoc(TagLoc); |
| 2180 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2181 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2184 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2185 | LookupResult &R, |
| 2186 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2187 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2188 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2189 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2190 | // 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] | 2191 | // though. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2192 | // foo<int> could identify a single function unambiguously |
| 2193 | // This approach does NOT work, since f<int>(1); |
| 2194 | // gets resolved prior to resorting to overload resolution |
| 2195 | // i.e., template<class T> void f(double); |
| 2196 | // vs template<class T, class U> void f(U); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2197 | |
| 2198 | // These should be filtered out by our callers. |
| 2199 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2200 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2201 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2202 | // We don't want lookup warnings at this point. |
| 2203 | R.suppressDiagnostics(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2204 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2205 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2206 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2207 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2208 | R.getLookupNameInfo(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2209 | RequiresADL, TemplateArgs, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2210 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2211 | |
| 2212 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2215 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2216 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2217 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2218 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2219 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2220 | DeclContext *DC; |
| 2221 | if (!(DC = computeDeclContext(SS, false)) || |
| 2222 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2223 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2224 | return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2225 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2226 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2227 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2228 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 2229 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2230 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2231 | if (R.isAmbiguous()) |
| 2232 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2233 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2234 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2235 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2236 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2237 | return ExprError(); |
| 2238 | } |
| 2239 | |
| 2240 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2241 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 2242 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 2243 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2244 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2245 | return ExprError(); |
| 2246 | } |
| 2247 | |
| 2248 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2251 | /// \brief Form a dependent template name. |
| 2252 | /// |
| 2253 | /// This action forms a dependent template name given the template |
| 2254 | /// name and its (presumably dependent) scope specifier. For |
| 2255 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2256 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2257 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2258 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2259 | SourceLocation TemplateKWLoc, |
| 2260 | CXXScopeSpec &SS, |
| 2261 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2262 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2263 | bool EnteringContext, |
| 2264 | TemplateTy &Result) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2265 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 2266 | Diag(TemplateKWLoc, |
| 2267 | getLangOptions().CPlusPlus0x ? |
| 2268 | diag::warn_cxx98_compat_template_outside_of_template : |
| 2269 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2270 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2271 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2272 | DeclContext *LookupCtx = 0; |
| 2273 | if (SS.isSet()) |
| 2274 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2275 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2276 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2277 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2278 | // C++0x [temp.names]p5: |
| 2279 | // If a name prefixed by the keyword template is not the name of |
| 2280 | // a template, the program is ill-formed. [Note: the keyword |
| 2281 | // template may not be applied to non-template members of class |
| 2282 | // templates. -end note ] [ Note: as is the case with the |
| 2283 | // typename prefix, the template prefix is allowed in cases |
| 2284 | // where it is not strictly necessary; i.e., when the |
| 2285 | // nested-name-specifier or the expression on the left of the -> |
| 2286 | // or . is not dependent on a template-parameter, or the use |
| 2287 | // does not appear in the scope of a template. -end note] |
| 2288 | // |
| 2289 | // Note: C++03 was more strict here, because it banned the use of |
| 2290 | // the "template" keyword prior to a template-name that was not a |
| 2291 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2292 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2293 | // 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] | 2294 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2295 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 2296 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2297 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2298 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2299 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | d078bd2 | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2300 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2301 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2302 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2303 | } else if (TNK == TNK_Non_template) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2304 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2305 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2306 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2307 | << Name.getSourceRange() |
| 2308 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2309 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2310 | } else { |
| 2311 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2312 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2313 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2316 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2317 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2318 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2319 | switch (Name.getKind()) { |
| 2320 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2321 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2322 | Name.Identifier)); |
| 2323 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2324 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2325 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2326 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2327 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2328 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2329 | |
| 2330 | case UnqualifiedId::IK_LiteralOperatorId: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2331 | llvm_unreachable( |
| 2332 | "We don't support these; Parse shouldn't have allowed propagation"); |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2333 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2334 | default: |
| 2335 | break; |
| 2336 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2337 | |
| 2338 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2339 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2340 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2341 | << Name.getSourceRange() |
| 2342 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2343 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2346 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2347 | const TemplateArgumentLoc &AL, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2348 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2349 | const TemplateArgument &Arg = AL.getArgument(); |
| 2350 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2351 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2352 | switch(Arg.getKind()) { |
| 2353 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2354 | // C++ [temp.arg.type]p1: |
| 2355 | // A template-argument for a template-parameter which is a |
| 2356 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2357 | break; |
| 2358 | case TemplateArgument::Template: { |
| 2359 | // We have a template type parameter but the template argument |
| 2360 | // is a template without any arguments. |
| 2361 | SourceRange SR = AL.getSourceRange(); |
| 2362 | TemplateName Name = Arg.getAsTemplate(); |
| 2363 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 2364 | << Name << SR; |
| 2365 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 2366 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2367 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2368 | return true; |
| 2369 | } |
| 2370 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2371 | // We have a template type parameter but the template argument |
| 2372 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 2373 | SourceRange SR = AL.getSourceRange(); |
| 2374 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2375 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2376 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2377 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2378 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2379 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2380 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2381 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2382 | return true; |
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 | // Add the converted template type argument. |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 2385 | QualType ArgType = Context.getCanonicalType(Arg.getAsType()); |
| 2386 | |
| 2387 | // Objective-C ARC: |
| 2388 | // If an explicitly-specified template argument type is a lifetime type |
| 2389 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
| 2390 | if (getLangOptions().ObjCAutoRefCount && |
| 2391 | ArgType->isObjCLifetimeType() && |
| 2392 | !ArgType.getObjCLifetime()) { |
| 2393 | Qualifiers Qs; |
| 2394 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 2395 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 2396 | } |
| 2397 | |
| 2398 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2399 | return false; |
| 2400 | } |
| 2401 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2402 | /// \brief Substitute template arguments into the default template argument for |
| 2403 | /// the given template type parameter. |
| 2404 | /// |
| 2405 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2406 | /// the substitution. |
| 2407 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2408 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2409 | /// for. |
| 2410 | /// |
| 2411 | /// \param TemplateLoc the location of the template name that started the |
| 2412 | /// template-id we are checking. |
| 2413 | /// |
| 2414 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2415 | /// terminates the template-id. |
| 2416 | /// |
| 2417 | /// \param Param the template template parameter whose default we are |
| 2418 | /// substituting into. |
| 2419 | /// |
| 2420 | /// \param Converted the list of template arguments provided for template |
| 2421 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2422 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2423 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2424 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2425 | TemplateDecl *Template, |
| 2426 | SourceLocation TemplateLoc, |
| 2427 | SourceLocation RAngleLoc, |
| 2428 | TemplateTypeParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2429 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2430 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2431 | |
| 2432 | // If the argument type is dependent, instantiate it now based |
| 2433 | // on the previously-computed template arguments. |
| 2434 | if (ArgType->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2435 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2436 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2437 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2438 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2439 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2440 | |
| 2441 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2442 | Template, Converted.data(), |
| 2443 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2444 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2445 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2446 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2447 | Param->getDefaultArgumentLoc(), |
| 2448 | Param->getDeclName()); |
| 2449 | } |
| 2450 | |
| 2451 | return ArgType; |
| 2452 | } |
| 2453 | |
| 2454 | /// \brief Substitute template arguments into the default template argument for |
| 2455 | /// the given non-type template parameter. |
| 2456 | /// |
| 2457 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2458 | /// the substitution. |
| 2459 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2460 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2461 | /// for. |
| 2462 | /// |
| 2463 | /// \param TemplateLoc the location of the template name that started the |
| 2464 | /// template-id we are checking. |
| 2465 | /// |
| 2466 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2467 | /// terminates the template-id. |
| 2468 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2469 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2470 | /// substituting into. |
| 2471 | /// |
| 2472 | /// \param Converted the list of template arguments provided for template |
| 2473 | /// parameters that precede \p Param in the template parameter list. |
| 2474 | /// |
| 2475 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2476 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2477 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2478 | TemplateDecl *Template, |
| 2479 | SourceLocation TemplateLoc, |
| 2480 | SourceLocation RAngleLoc, |
| 2481 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2482 | SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2483 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2484 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2485 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2486 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2487 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2488 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2489 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2490 | Template, Converted.data(), |
| 2491 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2492 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2493 | |
| 2494 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2495 | } |
| 2496 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2497 | /// \brief Substitute template arguments into the default template argument for |
| 2498 | /// the given template template parameter. |
| 2499 | /// |
| 2500 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2501 | /// the substitution. |
| 2502 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2503 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2504 | /// for. |
| 2505 | /// |
| 2506 | /// \param TemplateLoc the location of the template name that started the |
| 2507 | /// template-id we are checking. |
| 2508 | /// |
| 2509 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2510 | /// terminates the template-id. |
| 2511 | /// |
| 2512 | /// \param Param the template template parameter whose default we are |
| 2513 | /// substituting into. |
| 2514 | /// |
| 2515 | /// \param Converted the list of template arguments provided for template |
| 2516 | /// parameters that precede \p Param in the template parameter list. |
| 2517 | /// |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2518 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 2519 | /// source-location information) that precedes the template name. |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2520 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2521 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2522 | static TemplateName |
| 2523 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2524 | TemplateDecl *Template, |
| 2525 | SourceLocation TemplateLoc, |
| 2526 | SourceLocation RAngleLoc, |
| 2527 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2528 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2529 | NestedNameSpecifierLoc &QualifierLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2530 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2531 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2532 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2533 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2534 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2535 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2536 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2537 | Template, Converted.data(), |
| 2538 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2539 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2540 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2541 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2542 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2543 | if (QualifierLoc) { |
| 2544 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2545 | AllTemplateArgs); |
| 2546 | if (!QualifierLoc) |
| 2547 | return TemplateName(); |
| 2548 | } |
| 2549 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2550 | return SemaRef.SubstTemplateName(QualifierLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2551 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2552 | Param->getDefaultArgument().getTemplateNameLoc(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2553 | AllTemplateArgs); |
| 2554 | } |
| 2555 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2556 | /// \brief If the given template parameter has a default template |
| 2557 | /// argument, substitute into that default template argument and |
| 2558 | /// return the corresponding template argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2559 | TemplateArgumentLoc |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2560 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2561 | SourceLocation TemplateLoc, |
| 2562 | SourceLocation RAngleLoc, |
| 2563 | Decl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2564 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2565 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2566 | if (!TypeParm->hasDefaultArgument()) |
| 2567 | return TemplateArgumentLoc(); |
| 2568 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2569 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2570 | TemplateLoc, |
| 2571 | RAngleLoc, |
| 2572 | TypeParm, |
| 2573 | Converted); |
| 2574 | if (DI) |
| 2575 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2576 | |
| 2577 | return TemplateArgumentLoc(); |
| 2578 | } |
| 2579 | |
| 2580 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2581 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2582 | if (!NonTypeParm->hasDefaultArgument()) |
| 2583 | return TemplateArgumentLoc(); |
| 2584 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2585 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2586 | TemplateLoc, |
| 2587 | RAngleLoc, |
| 2588 | NonTypeParm, |
| 2589 | Converted); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2590 | if (Arg.isInvalid()) |
| 2591 | return TemplateArgumentLoc(); |
| 2592 | |
| 2593 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2594 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2595 | } |
| 2596 | |
| 2597 | TemplateTemplateParmDecl *TempTempParm |
| 2598 | = cast<TemplateTemplateParmDecl>(Param); |
| 2599 | if (!TempTempParm->hasDefaultArgument()) |
| 2600 | return TemplateArgumentLoc(); |
| 2601 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2602 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2603 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2604 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2605 | TemplateLoc, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2606 | RAngleLoc, |
| 2607 | TempTempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2608 | Converted, |
| 2609 | QualifierLoc); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2610 | if (TName.isNull()) |
| 2611 | return TemplateArgumentLoc(); |
| 2612 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2613 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2614 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2615 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2616 | } |
| 2617 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2618 | /// \brief Check that the given template argument corresponds to the given |
| 2619 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2620 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2621 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2622 | /// checked. |
| 2623 | /// |
| 2624 | /// \param Arg The template argument. |
| 2625 | /// |
| 2626 | /// \param Template The template in which the template argument resides. |
| 2627 | /// |
| 2628 | /// \param TemplateLoc The location of the template name for the template |
| 2629 | /// whose argument list we're matching. |
| 2630 | /// |
| 2631 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2632 | /// the template argument list. |
| 2633 | /// |
| 2634 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2635 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2636 | /// |
| 2637 | /// \param Converted The checked, converted argument will be added to the |
| 2638 | /// end of this small vector. |
| 2639 | /// |
| 2640 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2641 | /// explicitly written, deduced, etc. |
| 2642 | /// |
| 2643 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2644 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2645 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2646 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2647 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2648 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2649 | unsigned ArgumentPackIndex, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2650 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2651 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2652 | // Check template type parameters. |
| 2653 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2654 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2655 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2656 | // Check non-type template parameters. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2657 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2658 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2659 | // with the template arguments we've seen thus far. But if the |
| 2660 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2661 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2662 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2663 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2664 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2665 | if (NTTPType->isDependentType() && |
| 2666 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2667 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2668 | // Do substitution on the type of the non-type template parameter. |
| 2669 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2670 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2671 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2672 | |
| 2673 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2674 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2675 | NTTPType = SubstType(NTTPType, |
| 2676 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2677 | NTTP->getLocation(), |
| 2678 | NTTP->getDeclName()); |
| 2679 | // If that worked, check the non-type template parameter type |
| 2680 | // for validity. |
| 2681 | if (!NTTPType.isNull()) |
| 2682 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2683 | NTTP->getLocation()); |
| 2684 | if (NTTPType.isNull()) |
| 2685 | return true; |
| 2686 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2687 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2688 | switch (Arg.getArgument().getKind()) { |
| 2689 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2690 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2691 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2692 | case TemplateArgument::Expression: { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2693 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2694 | ExprResult Res = |
| 2695 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 2696 | Result, CTAK); |
| 2697 | if (Res.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2698 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2699 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2700 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2701 | break; |
| 2702 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2703 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2704 | case TemplateArgument::Declaration: |
| 2705 | case TemplateArgument::Integral: |
| 2706 | // We've already checked this template argument, so just copy |
| 2707 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2708 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2709 | break; |
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::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2712 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2713 | // We were given a template template argument. It may not be ill-formed; |
| 2714 | // see below. |
| 2715 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2716 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2717 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2718 | // We have a template argument such as \c T::template X, which we |
| 2719 | // parsed as a template template argument. However, since we now |
| 2720 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2721 | // template name into an expression. |
| 2722 | |
| 2723 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2724 | Arg.getTemplateNameLoc()); |
| 2725 | |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2726 | CXXScopeSpec SS; |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2727 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2728 | ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2729 | SS.getWithLocInContext(Context), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2730 | NameInfo)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2731 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2732 | // If we parsed the template argument as a pack expansion, create a |
| 2733 | // pack expansion expression. |
| 2734 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2735 | E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc()); |
| 2736 | if (E.isInvalid()) |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2737 | return true; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2738 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2739 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2740 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2741 | E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result); |
| 2742 | if (E.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2743 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2744 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2745 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2746 | break; |
| 2747 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2748 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2749 | // We have a template argument that actually does refer to a class |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2750 | // template, alias template, or template template parameter, and |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2751 | // therefore cannot be a non-type template argument. |
| 2752 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2753 | << Arg.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2754 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2755 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2756 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2757 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2758 | case TemplateArgument::Type: { |
| 2759 | // We have a non-type template parameter but the template |
| 2760 | // argument is a type. |
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 | // C++ [temp.arg]p2: |
| 2763 | // In a template-argument, an ambiguity between a type-id and |
| 2764 | // an expression is resolved to a type-id, regardless of the |
| 2765 | // form of the corresponding template-parameter. |
| 2766 | // |
| 2767 | // We warn specifically about this case, since it can be rather |
| 2768 | // confusing for users. |
| 2769 | QualType T = Arg.getArgument().getAsType(); |
| 2770 | SourceRange SR = Arg.getSourceRange(); |
| 2771 | if (T->isFunctionType()) |
| 2772 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2773 | else |
| 2774 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2775 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2776 | return true; |
| 2777 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2778 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2779 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2780 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2781 | break; |
| 2782 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2783 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2784 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2788 | // Check template template parameters. |
| 2789 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
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 | // Substitute into the template parameter list of the template |
| 2792 | // template parameter, since previously-supplied template arguments |
| 2793 | // may appear within the template template parameter. |
| 2794 | { |
| 2795 | // Set up a template instantiation context. |
| 2796 | LocalInstantiationScope Scope(*this); |
| 2797 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2798 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2799 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2800 | |
| 2801 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2802 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2803 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2804 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2805 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2806 | if (!TempParm) |
| 2807 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2808 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2809 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2810 | switch (Arg.getArgument().getKind()) { |
| 2811 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2812 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2813 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2814 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2815 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2816 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2817 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2818 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2819 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2820 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2821 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2822 | case TemplateArgument::Expression: |
| 2823 | case TemplateArgument::Type: |
| 2824 | // We have a template template parameter but the template |
| 2825 | // argument does not refer to a template. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2826 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
| 2827 | << getLangOptions().CPlusPlus0x; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2828 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2829 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2830 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2831 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2832 | "Declaration argument with template template parameter"); |
| 2833 | break; |
| 2834 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2835 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2836 | "Integral argument with template template parameter"); |
| 2837 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2838 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2839 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2840 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2841 | break; |
| 2842 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2843 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2844 | return false; |
| 2845 | } |
| 2846 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2847 | /// \brief Check that the given template argument list is well-formed |
| 2848 | /// for specializing the given template. |
| 2849 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2850 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2851 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2852 | bool PartialTemplateArgs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2853 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2854 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2855 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2856 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2857 | bool Invalid = false; |
| 2858 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2859 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2860 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2861 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2862 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2863 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2864 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2865 | (NumArgs < Params->getMinRequiredArguments() && |
| 2866 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2867 | // FIXME: point at either the first arg beyond what we can handle, |
| 2868 | // or the '>', depending on whether we have too many or too few |
| 2869 | // arguments. |
| 2870 | SourceRange Range; |
| 2871 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2872 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2873 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2874 | << (NumArgs > NumParams) |
| 2875 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2876 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2877 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2878 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2879 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2880 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2881 | Invalid = true; |
| 2882 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2883 | |
| 2884 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2885 | // [...] The type and form of each template-argument specified in |
| 2886 | // a template-id shall match the type and form specified for the |
| 2887 | // corresponding parameter declared by the template in its |
| 2888 | // template-parameter-list. |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2889 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2890 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2891 | TemplateParameterList::iterator Param = Params->begin(), |
| 2892 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2893 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 2894 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2895 | while (Param != ParamEnd) { |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2896 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2897 | // If we have an expanded parameter pack, make sure we don't have too |
| 2898 | // many arguments. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2899 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2900 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2901 | if (NTTP->isExpandedParameterPack() && |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2902 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 2903 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2904 | << true |
| 2905 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2906 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2907 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2908 | << Template; |
| 2909 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2910 | << Params->getSourceRange(); |
| 2911 | return true; |
| 2912 | } |
| 2913 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2914 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2915 | // Check the template argument we were given. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2916 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2917 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2918 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2919 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2920 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2921 | if ((*Param)->isTemplateParameterPack()) { |
| 2922 | // The template parameter was a template parameter pack, so take the |
| 2923 | // deduced argument and place it on the argument pack. Note that we |
| 2924 | // stay on the same template parameter so that we can deduce more |
| 2925 | // arguments. |
| 2926 | ArgumentPack.push_back(Converted.back()); |
| 2927 | Converted.pop_back(); |
| 2928 | } else { |
| 2929 | // Move to the next template parameter. |
| 2930 | ++Param; |
| 2931 | } |
| 2932 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2933 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2934 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2935 | |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2936 | // If we're checking a partial template argument list, we're done. |
| 2937 | if (PartialTemplateArgs) { |
| 2938 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 2939 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 2940 | ArgumentPack.data(), |
| 2941 | ArgumentPack.size())); |
| 2942 | |
| 2943 | return Invalid; |
| 2944 | } |
| 2945 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2946 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2947 | // arguments, just break out now and we'll fill in the argument pack below. |
| 2948 | if ((*Param)->isTemplateParameterPack()) |
| 2949 | break; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2950 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2951 | // We have a default template argument that we will use. |
| 2952 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2953 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2954 | // Retrieve the default template argument from the template |
| 2955 | // parameter. For each kind of template parameter, we substitute the |
| 2956 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2957 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2958 | // the default argument. |
| 2959 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2960 | if (!TTP->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2961 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2962 | break; |
| 2963 | } |
| 2964 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2965 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2966 | Template, |
| 2967 | TemplateLoc, |
| 2968 | RAngleLoc, |
| 2969 | TTP, |
| 2970 | Converted); |
| 2971 | if (!ArgType) |
| 2972 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2973 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2974 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2975 | ArgType); |
| 2976 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2977 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2978 | if (!NTTP->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2979 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2980 | break; |
| 2981 | } |
| 2982 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2983 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2984 | TemplateLoc, |
| 2985 | RAngleLoc, |
| 2986 | NTTP, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2987 | Converted); |
| 2988 | if (E.isInvalid()) |
| 2989 | return true; |
| 2990 | |
| 2991 | Expr *Ex = E.takeAs<Expr>(); |
| 2992 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2993 | } else { |
| 2994 | TemplateTemplateParmDecl *TempParm |
| 2995 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2996 | |
| 2997 | if (!TempParm->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2998 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2999 | break; |
| 3000 | } |
| 3001 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3002 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3003 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3004 | TemplateLoc, |
| 3005 | RAngleLoc, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3006 | TempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3007 | Converted, |
| 3008 | QualifierLoc); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3009 | if (Name.isNull()) |
| 3010 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3011 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3012 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3013 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3014 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3015 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3016 | // Introduce an instantiation record that describes where we are using |
| 3017 | // the default template argument. |
| 3018 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3019 | Converted.data(), Converted.size(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3020 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3021 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3022 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3023 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3024 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3025 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3026 | |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3027 | // Core issue 150 (assumed resolution): if this is a template template |
| 3028 | // parameter, keep track of the default template arguments from the |
| 3029 | // template definition. |
| 3030 | if (isTemplateTemplateParameter) |
| 3031 | TemplateArgs.addArgument(Arg); |
| 3032 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3033 | // Move to the next template parameter and argument. |
| 3034 | ++Param; |
| 3035 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3036 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3037 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3038 | // Form argument packs for each of the parameter packs remaining. |
| 3039 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3040 | // If we're checking a partial list of template arguments, don't fill |
| 3041 | // in arguments for non-template parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3042 | |
| 3043 | if ((*Param)->isTemplateParameterPack()) { |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 3044 | if (!HasParameterPack) |
| 3045 | return true; |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3046 | if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3047 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3048 | else { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3049 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3050 | ArgumentPack.data(), |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3051 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3052 | ArgumentPack.clear(); |
| 3053 | } |
| 3054 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3055 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3056 | ++Param; |
| 3057 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3058 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3059 | return Invalid; |
| 3060 | } |
| 3061 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3062 | namespace { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3063 | class UnnamedLocalNoLinkageFinder |
| 3064 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3065 | { |
| 3066 | Sema &S; |
| 3067 | SourceRange SR; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3068 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3069 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3070 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3071 | public: |
| 3072 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3073 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3074 | bool Visit(QualType T) { |
| 3075 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3076 | } |
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 | #define TYPE(Class, Parent) \ |
| 3079 | bool Visit##Class##Type(const Class##Type *); |
| 3080 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 3081 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3082 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 3083 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3084 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3085 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3086 | bool VisitTagDecl(const TagDecl *Tag); |
| 3087 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3088 | }; |
| 3089 | } |
| 3090 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3091 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3092 | return false; |
| 3093 | } |
| 3094 | |
| 3095 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3096 | return Visit(T->getElementType()); |
| 3097 | } |
| 3098 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3099 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3100 | return Visit(T->getPointeeType()); |
| 3101 | } |
| 3102 | |
| 3103 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3104 | const BlockPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3105 | return Visit(T->getPointeeType()); |
| 3106 | } |
| 3107 | |
| 3108 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3109 | const LValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3110 | return Visit(T->getPointeeType()); |
| 3111 | } |
| 3112 | |
| 3113 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3114 | const RValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3115 | return Visit(T->getPointeeType()); |
| 3116 | } |
| 3117 | |
| 3118 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3119 | const MemberPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3120 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3121 | } |
| 3122 | |
| 3123 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3124 | const ConstantArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3125 | return Visit(T->getElementType()); |
| 3126 | } |
| 3127 | |
| 3128 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3129 | const IncompleteArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3130 | return Visit(T->getElementType()); |
| 3131 | } |
| 3132 | |
| 3133 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3134 | const VariableArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3135 | return Visit(T->getElementType()); |
| 3136 | } |
| 3137 | |
| 3138 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3139 | const DependentSizedArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3140 | return Visit(T->getElementType()); |
| 3141 | } |
| 3142 | |
| 3143 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3144 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3145 | return Visit(T->getElementType()); |
| 3146 | } |
| 3147 | |
| 3148 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3149 | return Visit(T->getElementType()); |
| 3150 | } |
| 3151 | |
| 3152 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 3153 | return Visit(T->getElementType()); |
| 3154 | } |
| 3155 | |
| 3156 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 3157 | const FunctionProtoType* T) { |
| 3158 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3159 | AEnd = T->arg_type_end(); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3160 | A != AEnd; ++A) { |
| 3161 | if (Visit(*A)) |
| 3162 | return true; |
| 3163 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3164 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3165 | return Visit(T->getResultType()); |
| 3166 | } |
| 3167 | |
| 3168 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 3169 | const FunctionNoProtoType* T) { |
| 3170 | return Visit(T->getResultType()); |
| 3171 | } |
| 3172 | |
| 3173 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 3174 | const UnresolvedUsingType*) { |
| 3175 | return false; |
| 3176 | } |
| 3177 | |
| 3178 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 3179 | return false; |
| 3180 | } |
| 3181 | |
| 3182 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 3183 | return Visit(T->getUnderlyingType()); |
| 3184 | } |
| 3185 | |
| 3186 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 3187 | return false; |
| 3188 | } |
| 3189 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3190 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 3191 | const UnaryTransformType*) { |
| 3192 | return false; |
| 3193 | } |
| 3194 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3195 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 3196 | return Visit(T->getDeducedType()); |
| 3197 | } |
| 3198 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3199 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 3200 | return VisitTagDecl(T->getDecl()); |
| 3201 | } |
| 3202 | |
| 3203 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 3204 | return VisitTagDecl(T->getDecl()); |
| 3205 | } |
| 3206 | |
| 3207 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 3208 | const TemplateTypeParmType*) { |
| 3209 | return false; |
| 3210 | } |
| 3211 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3212 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 3213 | const SubstTemplateTypeParmPackType *) { |
| 3214 | return false; |
| 3215 | } |
| 3216 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3217 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 3218 | const TemplateSpecializationType*) { |
| 3219 | return false; |
| 3220 | } |
| 3221 | |
| 3222 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 3223 | const InjectedClassNameType* T) { |
| 3224 | return VisitTagDecl(T->getDecl()); |
| 3225 | } |
| 3226 | |
| 3227 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 3228 | const DependentNameType* T) { |
| 3229 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3230 | } |
| 3231 | |
| 3232 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 3233 | const DependentTemplateSpecializationType* T) { |
| 3234 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3235 | } |
| 3236 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3237 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 3238 | const PackExpansionType* T) { |
| 3239 | return Visit(T->getPattern()); |
| 3240 | } |
| 3241 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3242 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 3243 | return false; |
| 3244 | } |
| 3245 | |
| 3246 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 3247 | const ObjCInterfaceType *) { |
| 3248 | return false; |
| 3249 | } |
| 3250 | |
| 3251 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 3252 | const ObjCObjectPointerType *) { |
| 3253 | return false; |
| 3254 | } |
| 3255 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3256 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 3257 | return Visit(T->getValueType()); |
| 3258 | } |
| 3259 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3260 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 3261 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3262 | S.Diag(SR.getBegin(), |
| 3263 | S.getLangOptions().CPlusPlus0x ? |
| 3264 | diag::warn_cxx98_compat_template_arg_local_type : |
| 3265 | diag::ext_template_arg_local_type) |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3266 | << S.Context.getTypeDeclType(Tag) << SR; |
| 3267 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3268 | } |
| 3269 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3270 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3271 | S.Diag(SR.getBegin(), |
| 3272 | S.getLangOptions().CPlusPlus0x ? |
| 3273 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 3274 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3275 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 3276 | return true; |
| 3277 | } |
| 3278 | |
| 3279 | return false; |
| 3280 | } |
| 3281 | |
| 3282 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 3283 | NestedNameSpecifier *NNS) { |
| 3284 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 3285 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3286 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3287 | switch (NNS->getKind()) { |
| 3288 | case NestedNameSpecifier::Identifier: |
| 3289 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3290 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3291 | case NestedNameSpecifier::Global: |
| 3292 | return false; |
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 | case NestedNameSpecifier::TypeSpec: |
| 3295 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3296 | return Visit(QualType(NNS->getAsType(), 0)); |
| 3297 | } |
Fariborz Jahanian | 7b1ec6c | 2010-10-13 16:19:16 +0000 | [diff] [blame] | 3298 | return false; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3299 | } |
| 3300 | |
| 3301 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3302 | /// \brief Check a template argument against its corresponding |
| 3303 | /// template type parameter. |
| 3304 | /// |
| 3305 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 3306 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3307 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3308 | TypeSourceInfo *ArgInfo) { |
| 3309 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3310 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 3311 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 3312 | |
| 3313 | if (Arg->isVariablyModifiedType()) { |
| 3314 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3315 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3316 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3319 | // C++03 [temp.arg.type]p2: |
| 3320 | // A local type, a type with no linkage, an unnamed type or a type |
| 3321 | // compounded from any of these types shall not be used as a |
| 3322 | // template-argument for a template type-parameter. |
| 3323 | // |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3324 | // 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] | 3325 | // a warning. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3326 | if (LangOpts.CPlusPlus0x ? |
| 3327 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 3328 | SR.getBegin()) != DiagnosticsEngine::Ignored || |
| 3329 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type, |
| 3330 | SR.getBegin()) != DiagnosticsEngine::Ignored : |
| 3331 | Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3332 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 3333 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 3334 | } |
| 3335 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3336 | return false; |
| 3337 | } |
| 3338 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3339 | /// \brief Checks whether the given template argument is the address |
| 3340 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3341 | static bool |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3342 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 3343 | NonTypeTemplateParmDecl *Param, |
| 3344 | QualType ParamType, |
| 3345 | Expr *ArgIn, |
| 3346 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3347 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3348 | Expr *Arg = ArgIn; |
| 3349 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3350 | |
| 3351 | // See through any implicit casts we added to fix the type. |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3352 | Arg = Arg->IgnoreImpCasts(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3353 | |
| 3354 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3355 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3356 | // A template-argument for a non-type, non-template |
| 3357 | // template-parameter shall be one of: [...] |
| 3358 | // |
| 3359 | // -- the address of an object or function with external |
| 3360 | // linkage, including function templates and function |
| 3361 | // template-ids but excluding non-static class members, |
| 3362 | // expressed as & id-expression where the & is optional if |
| 3363 | // the name refers to a function or array, or if the |
| 3364 | // corresponding template-parameter is a reference; or |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3365 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3366 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3367 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3368 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3369 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3370 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3371 | S.Diag(Arg->getSourceRange().getBegin(), |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3372 | S.getLangOptions().CPlusPlus0x ? |
| 3373 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3374 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3375 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3376 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3377 | } |
| 3378 | |
| 3379 | Arg = Parens->getSubExpr(); |
| 3380 | } |
| 3381 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3382 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3383 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3384 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3385 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3386 | bool AddressTaken = false; |
| 3387 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3388 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3389 | if (UnOp->getOpcode() == UO_AddrOf) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3390 | Arg = UnOp->getSubExpr(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3391 | AddressTaken = true; |
| 3392 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 3393 | } |
Francois Pichet | a343a41 | 2011-04-29 09:08:14 +0000 | [diff] [blame] | 3394 | } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3395 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3396 | if (S.getLangOptions().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3397 | Converted = TemplateArgument(ArgIn); |
| 3398 | return false; |
| 3399 | } |
| 3400 | |
| 3401 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3402 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3403 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3404 | |
| 3405 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3406 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3407 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 3408 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3409 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3410 | return true; |
| 3411 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3412 | |
| 3413 | // Stop checking the precise nature of the argument if it is value dependent, |
| 3414 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3415 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3416 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3417 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3418 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3419 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3420 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 3421 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3422 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3423 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3424 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3425 | return true; |
| 3426 | } |
| 3427 | |
| 3428 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3429 | |
| 3430 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3431 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 3432 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3433 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3434 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3435 | return true; |
| 3436 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3437 | |
| 3438 | // Cannot refer to non-static member functions |
| 3439 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3440 | if (!Method->isStatic()) { |
| 3441 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3442 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3443 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3444 | return true; |
| 3445 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3446 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3447 | // Functions must have external linkage. |
| 3448 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3449 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3450 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3451 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3452 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3453 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3454 | << true; |
| 3455 | return true; |
| 3456 | } |
| 3457 | |
| 3458 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3459 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3460 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3461 | // If the template parameter has pointer type, the function decays. |
| 3462 | if (ParamType->isPointerType() && !AddressTaken) |
| 3463 | ArgType = S.Context.getPointerType(Func->getType()); |
| 3464 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 3465 | // If we originally had an address-of operator, but the |
| 3466 | // parameter has reference type, complain and (if things look |
| 3467 | // like they will work) drop the address-of operator. |
| 3468 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 3469 | ParamType.getNonReferenceType())) { |
| 3470 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3471 | << ParamType; |
| 3472 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3473 | return true; |
| 3474 | } |
| 3475 | |
| 3476 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3477 | << ParamType |
| 3478 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3479 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3480 | |
| 3481 | ArgType = Func->getType(); |
| 3482 | } |
| 3483 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3484 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3485 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3486 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3487 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3488 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3489 | << true; |
| 3490 | return true; |
| 3491 | } |
| 3492 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3493 | // A value of reference type is not an object. |
| 3494 | if (Var->getType()->isReferenceType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3495 | S.Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3496 | diag::err_template_arg_reference_var) |
| 3497 | << Var->getType() << Arg->getSourceRange(); |
| 3498 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3499 | return true; |
| 3500 | } |
| 3501 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3502 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3503 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3504 | |
| 3505 | // If the template parameter has pointer type, we must have taken |
| 3506 | // the address of this object. |
| 3507 | if (ParamType->isReferenceType()) { |
| 3508 | if (AddressTaken) { |
| 3509 | // If we originally had an address-of operator, but the |
| 3510 | // parameter has reference type, complain and (if things look |
| 3511 | // like they will work) drop the address-of operator. |
| 3512 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3513 | ParamType.getNonReferenceType())) { |
| 3514 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3515 | << ParamType; |
| 3516 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3517 | return true; |
| 3518 | } |
| 3519 | |
| 3520 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3521 | << ParamType |
| 3522 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3523 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3524 | |
| 3525 | ArgType = Var->getType(); |
| 3526 | } |
| 3527 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3528 | if (Var->getType()->isArrayType()) { |
| 3529 | // Array-to-pointer decay. |
| 3530 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3531 | } else { |
| 3532 | // If the template parameter has pointer type but the address of |
| 3533 | // this object was not taken, complain and (possibly) recover by |
| 3534 | // taking the address of the entity. |
| 3535 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3536 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3537 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3538 | << ParamType; |
| 3539 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3540 | return true; |
| 3541 | } |
| 3542 | |
| 3543 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3544 | << ParamType |
| 3545 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3546 | |
| 3547 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3548 | } |
| 3549 | } |
| 3550 | } else { |
| 3551 | // We found something else, but we don't know specifically what it is. |
| 3552 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3553 | diag::err_template_arg_not_object_or_func) |
| 3554 | << Arg->getSourceRange(); |
| 3555 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3556 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3557 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3558 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3559 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3560 | if (ParamType->isPointerType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3561 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3562 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 3563 | ObjCLifetimeConversion)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3564 | // For pointer-to-object types, qualification conversions are |
| 3565 | // permitted. |
| 3566 | } else { |
| 3567 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3568 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3569 | // C++ [temp.arg.nontype]p5b3: |
| 3570 | // For a non-type template-parameter of type reference to |
| 3571 | // object, no conversions apply. The type referred to by the |
| 3572 | // reference may be more cv-qualified than the (otherwise |
| 3573 | // identical) type of the template- argument. The |
| 3574 | // template-parameter is bound directly to the |
| 3575 | // template-argument, which shall be an lvalue. |
| 3576 | |
| 3577 | // FIXME: Other qualifiers? |
| 3578 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3579 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3580 | |
| 3581 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 3582 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3583 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3584 | << ParamType << Arg->getType() |
| 3585 | << Arg->getSourceRange(); |
| 3586 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3587 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3588 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3589 | } |
| 3590 | } |
| 3591 | |
| 3592 | // At this point, the template argument refers to an object or |
| 3593 | // function with external linkage. We now need to check whether the |
| 3594 | // argument and parameter types are compatible. |
| 3595 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3596 | ParamType.getNonReferenceType())) { |
| 3597 | // We can't perform this conversion or binding. |
| 3598 | if (ParamType->isReferenceType()) |
| 3599 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3600 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3601 | else |
| 3602 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3603 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3604 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3605 | return true; |
| 3606 | } |
| 3607 | } |
| 3608 | |
| 3609 | // Create the template argument. |
| 3610 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 3611 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3612 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3613 | } |
| 3614 | |
| 3615 | /// \brief Checks whether the given template argument is a pointer to |
| 3616 | /// member constant according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3617 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3618 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3619 | bool Invalid = false; |
| 3620 | |
| 3621 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3622 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3623 | Arg = Cast->getSubExpr(); |
| 3624 | |
| 3625 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3626 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3627 | // A template-argument for a non-type, non-template |
| 3628 | // template-parameter shall be one of: [...] |
| 3629 | // |
| 3630 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3631 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3632 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3633 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3634 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3635 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3636 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3637 | if (!Invalid && !ExtraParens) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3638 | Diag(Arg->getSourceRange().getBegin(), |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3639 | getLangOptions().CPlusPlus0x ? |
| 3640 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3641 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3642 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3643 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
| 3646 | Arg = Parens->getSubExpr(); |
| 3647 | } |
| 3648 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3649 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3650 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3651 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3652 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3653 | // A pointer-to-member constant written &Class::member. |
| 3654 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3655 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3656 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3657 | if (DRE && !DRE->getQualifier()) |
| 3658 | DRE = 0; |
| 3659 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3660 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3661 | // A constant of pointer-to-member type. |
| 3662 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3663 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3664 | if (VD->getType()->isMemberPointerType()) { |
| 3665 | if (isa<NonTypeTemplateParmDecl>(VD) || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3666 | (isa<VarDecl>(VD) && |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3667 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3668 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3669 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3670 | else |
| 3671 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3672 | return Invalid; |
| 3673 | } |
| 3674 | } |
| 3675 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3676 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3677 | DRE = 0; |
| 3678 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3679 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3680 | if (!DRE) |
| 3681 | return Diag(Arg->getSourceRange().getBegin(), |
| 3682 | diag::err_template_arg_not_pointer_to_member_form) |
| 3683 | << Arg->getSourceRange(); |
| 3684 | |
| 3685 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3686 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3687 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3688 | "Only non-static member pointers can make it here"); |
| 3689 | |
| 3690 | // Okay: this is the address of a non-static member, and therefore |
| 3691 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3692 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3693 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3694 | else |
| 3695 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3696 | return Invalid; |
| 3697 | } |
| 3698 | |
| 3699 | // 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] | 3700 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3701 | diag::err_template_arg_not_pointer_to_member_form) |
| 3702 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3703 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3704 | diag::note_template_arg_refers_here); |
| 3705 | return true; |
| 3706 | } |
| 3707 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3708 | /// \brief Check a template argument against its corresponding |
| 3709 | /// non-type template parameter. |
| 3710 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3711 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3712 | /// If an error occurred, it returns ExprError(); otherwise, it |
| 3713 | /// returns the converted template argument. \p |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3714 | /// InstantiatedParamType is the type of the non-type template |
| 3715 | /// parameter after it has been instantiated. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3716 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 3717 | QualType InstantiatedParamType, Expr *Arg, |
| 3718 | TemplateArgument &Converted, |
| 3719 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3720 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3721 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3722 | // If either the parameter has a dependent type or the argument is |
| 3723 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3724 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3725 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3726 | Converted = TemplateArgument(Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3727 | return Owned(Arg); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3728 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3729 | |
| 3730 | // C++ [temp.arg.nontype]p5: |
| 3731 | // The following conversions are performed on each expression used |
| 3732 | // as a non-type template-argument. If a non-type |
| 3733 | // template-argument cannot be converted to the type of the |
| 3734 | // corresponding template-parameter then the program is |
| 3735 | // ill-formed. |
| 3736 | // |
| 3737 | // -- for a non-type template-parameter of integral or |
| 3738 | // enumeration type, integral promotions (4.5) and integral |
| 3739 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3740 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3741 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 3742 | // FIXME: In C++11, the argument is a converted constant expression of the |
| 3743 | // type of the template parameter. |
| 3744 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 3745 | if (ArgResult.isInvalid()) |
| 3746 | return ExprError(); |
| 3747 | Arg = ArgResult.take(); |
| 3748 | |
| 3749 | QualType ArgType = Arg->getType(); |
| 3750 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3751 | // C++ [temp.arg.nontype]p1: |
| 3752 | // A template-argument for a non-type, non-template |
| 3753 | // template-parameter shall be one of: |
| 3754 | // |
| 3755 | // -- an integral constant-expression of integral or enumeration |
| 3756 | // type; or |
| 3757 | // -- the name of a non-type template-parameter; or |
| 3758 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3759 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3760 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3761 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3762 | diag::err_template_arg_not_integral_or_enumeral) |
| 3763 | << ArgType << Arg->getSourceRange(); |
| 3764 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3765 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3766 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3767 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3768 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 3769 | << ArgType << Arg->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3770 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3771 | } |
| 3772 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3773 | // From here on out, all we care about are the unqualified forms |
| 3774 | // of the parameter and argument types. |
| 3775 | ParamType = ParamType.getUnqualifiedType(); |
| 3776 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3777 | |
| 3778 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3779 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3780 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3781 | } else if (CTAK == CTAK_Deduced) { |
| 3782 | // C++ [temp.deduct.type]p17: |
| 3783 | // If, in the declaration of a function template with a non-type |
| 3784 | // template-parameter, the non-type template- parameter is used |
| 3785 | // in an expression in the function parameter-list and, if the |
| 3786 | // corresponding template-argument is deduced, the |
| 3787 | // template-argument type shall match the type of the |
| 3788 | // template-parameter exactly, except that a template-argument |
| 3789 | // deduced from an array bound may be of any integral type. |
| 3790 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3791 | << ArgType << ParamType; |
| 3792 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3793 | return ExprError(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3794 | } else if (ParamType->isBooleanType()) { |
| 3795 | // This is an integral-to-boolean conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3796 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3797 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3798 | !ParamType->isEnumeralType()) { |
| 3799 | // This is an integral promotion or conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3800 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3801 | } else { |
| 3802 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3803 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3804 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3805 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3806 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3807 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3808 | } |
| 3809 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3810 | // Add the value of this argument to the list of converted |
| 3811 | // arguments. We use the bitwidth and signedness of the template |
| 3812 | // parameter. |
| 3813 | if (Arg->isValueDependent()) { |
| 3814 | // The argument is value-dependent. Create a new |
| 3815 | // TemplateArgument with the converted expression. |
| 3816 | Converted = TemplateArgument(Arg); |
| 3817 | return Owned(Arg); |
| 3818 | } |
| 3819 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3820 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3821 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3822 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3823 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3824 | if (ParamType->isBooleanType()) { |
| 3825 | // Value must be zero or one. |
| 3826 | Value = Value != 0; |
| 3827 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 3828 | if (Value.getBitWidth() != AllowedBits) |
| 3829 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3830 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3831 | } else { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3832 | llvm::APSInt OldValue = Value; |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3833 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3834 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3835 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3836 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3837 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3838 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3839 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3840 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3841 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3842 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3843 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3844 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3845 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3846 | << Arg->getSourceRange(); |
| 3847 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3848 | } |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3849 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3850 | // Complain if we overflowed the template parameter's type. |
| 3851 | unsigned RequiredBits; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3852 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3853 | RequiredBits = OldValue.getActiveBits(); |
| 3854 | else if (OldValue.isUnsigned()) |
| 3855 | RequiredBits = OldValue.getActiveBits() + 1; |
| 3856 | else |
| 3857 | RequiredBits = OldValue.getMinSignedBits(); |
| 3858 | if (RequiredBits > AllowedBits) { |
| 3859 | Diag(Arg->getSourceRange().getBegin(), |
| 3860 | diag::warn_template_arg_too_large) |
| 3861 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3862 | << Arg->getSourceRange(); |
| 3863 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3864 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3865 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3866 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3867 | Converted = TemplateArgument(Value, |
Douglas Gregor | 6b63f55 | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 3868 | ParamType->isEnumeralType() |
| 3869 | ? Context.getCanonicalType(ParamType) |
| 3870 | : IntegerType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3871 | return Owned(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3872 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3873 | |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 3874 | QualType ArgType = Arg->getType(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3875 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 3876 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3877 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 3878 | // from a template argument of type std::nullptr_t to a non-type |
| 3879 | // template parameter of type pointer to object, pointer to |
| 3880 | // function, or pointer-to-member, respectively. |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 3881 | if (ArgType->isNullPtrType()) { |
| 3882 | if (ParamType->isPointerType() || ParamType->isMemberPointerType()) { |
| 3883 | Converted = TemplateArgument((NamedDecl *)0); |
| 3884 | return Owned(Arg); |
| 3885 | } |
| 3886 | |
| 3887 | if (ParamType->isNullPtrType()) { |
| 3888 | llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true); |
| 3889 | Converted = TemplateArgument(Zero, Context.NullPtrTy); |
| 3890 | return Owned(Arg); |
| 3891 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3894 | // Handle pointer-to-function, reference-to-function, and |
| 3895 | // pointer-to-member-function all in (roughly) the same way. |
| 3896 | if (// -- For a non-type template-parameter of type pointer to |
| 3897 | // function, only the function-to-pointer conversion (4.3) is |
| 3898 | // applied. If the template-argument represents a set of |
| 3899 | // overloaded functions (or a pointer to such), the matching |
| 3900 | // function is selected from the set (13.4). |
| 3901 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3902 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3903 | // -- For a non-type template-parameter of type reference to |
| 3904 | // function, no conversions apply. If the template-argument |
| 3905 | // represents a set of overloaded functions, the matching |
| 3906 | // function is selected from the set (13.4). |
| 3907 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3908 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3909 | // -- For a non-type template-parameter of type pointer to |
| 3910 | // member function, no conversions apply. If the |
| 3911 | // template-argument represents a set of overloaded member |
| 3912 | // functions, the matching member function is selected from |
| 3913 | // the set (13.4). |
| 3914 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3915 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3916 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3917 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3918 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3919 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3920 | true, |
| 3921 | FoundResult)) { |
| 3922 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3923 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3924 | |
| 3925 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3926 | ArgType = Arg->getType(); |
| 3927 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3928 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3929 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3930 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3931 | if (!ParamType->isMemberPointerType()) { |
| 3932 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3933 | ParamType, |
| 3934 | Arg, Converted)) |
| 3935 | return ExprError(); |
| 3936 | return Owned(Arg); |
| 3937 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3938 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3939 | bool ObjCLifetimeConversion; |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3940 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3941 | false, ObjCLifetimeConversion)) { |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 3942 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, |
| 3943 | Arg->getValueKind()).take(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3944 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 3945 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3946 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3947 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3948 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3949 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3950 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3951 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3952 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3953 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3954 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 3955 | return ExprError(); |
| 3956 | return Owned(Arg); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3957 | } |
| 3958 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 3959 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3960 | // -- for a non-type template-parameter of type pointer to |
| 3961 | // object, qualification conversions (4.4) and the |
| 3962 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3963 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3964 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3965 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3966 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3967 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3968 | ParamType, |
| 3969 | Arg, Converted)) |
| 3970 | return ExprError(); |
| 3971 | return Owned(Arg); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3972 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3973 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3974 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3975 | // -- For a non-type template-parameter of type reference to |
| 3976 | // object, no conversions apply. The type referred to by the |
| 3977 | // reference may be more cv-qualified than the (otherwise |
| 3978 | // identical) type of the template-argument. The |
| 3979 | // template-parameter is bound directly to the |
| 3980 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3981 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3982 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3983 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3984 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3985 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 3986 | ParamRefType->getPointeeType(), |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3987 | true, |
| 3988 | FoundResult)) { |
| 3989 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3990 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3991 | |
| 3992 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3993 | ArgType = Arg->getType(); |
| 3994 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3995 | return ExprError(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3996 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3997 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3998 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3999 | ParamType, |
| 4000 | Arg, Converted)) |
| 4001 | return ExprError(); |
| 4002 | return Owned(Arg); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4003 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4004 | |
| 4005 | // -- For a non-type template-parameter of type pointer to data |
| 4006 | // member, qualification conversions (4.4) are applied. |
| 4007 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 4008 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4009 | bool ObjCLifetimeConversion; |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 4010 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4011 | // Types match exactly: nothing more to do here. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4012 | } else if (IsQualificationConversion(ArgType, ParamType, false, |
| 4013 | ObjCLifetimeConversion)) { |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 4014 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, |
| 4015 | Arg->getValueKind()).take(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4016 | } else { |
| 4017 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4018 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4019 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4020 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4021 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4022 | return ExprError(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4025 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 4026 | return ExprError(); |
| 4027 | return Owned(Arg); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
| 4030 | /// \brief Check a template argument against its corresponding |
| 4031 | /// template template parameter. |
| 4032 | /// |
| 4033 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 4034 | /// It returns true if an error occurred, and false otherwise. |
| 4035 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4036 | const TemplateArgumentLoc &Arg) { |
| 4037 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 4038 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 4039 | if (!Template) { |
| 4040 | // Any dependent template name is fine. |
| 4041 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 4042 | return false; |
| 4043 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4044 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4045 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4046 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4047 | // the name of a class template or an alias template, expressed as an |
| 4048 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4049 | // primary class templates are considered when matching the |
| 4050 | // template template argument with the corresponding parameter; |
| 4051 | // partial specializations are not considered even if their |
| 4052 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4053 | // |
| 4054 | // Note that we also allow template template parameters here, which |
| 4055 | // will happen when we are dealing with, e.g., class template |
| 4056 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4057 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4058 | !isa<TemplateTemplateParmDecl>(Template) && |
| 4059 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4060 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4061 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4062 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4063 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4064 | << Template; |
| 4065 | } |
| 4066 | |
| 4067 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 4068 | Param->getTemplateParameters(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4069 | true, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4070 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4071 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4072 | } |
| 4073 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4074 | /// \brief Given a non-type template argument that refers to a |
| 4075 | /// declaration and the type of its corresponding non-type template |
| 4076 | /// parameter, produce an expression that properly refers to that |
| 4077 | /// declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4078 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4079 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 4080 | QualType ParamType, |
| 4081 | SourceLocation Loc) { |
| 4082 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 4083 | "Only declaration template arguments permitted here"); |
| 4084 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 4085 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4086 | if (VD->getDeclContext()->isRecord() && |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4087 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 4088 | // If the value is a class member, we might have a pointer-to-member. |
| 4089 | // Determine whether the non-type template template parameter is of |
| 4090 | // pointer-to-member type. If so, we need to build an appropriate |
| 4091 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 4092 | // would refer to the member itself. |
| 4093 | if (ParamType->isMemberPointerType()) { |
| 4094 | QualType ClassType |
| 4095 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 4096 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4097 | = NestedNameSpecifier::Create(Context, 0, false, |
| 4098 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4099 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 4100 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4101 | |
| 4102 | // The actual value-ness of this is unimportant, but for |
| 4103 | // internal consistency's sake, references to instance methods |
| 4104 | // are r-values. |
| 4105 | ExprValueKind VK = VK_LValue; |
| 4106 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 4107 | VK = VK_RValue; |
| 4108 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4109 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4110 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4111 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4112 | Loc, |
| 4113 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4114 | if (RefExpr.isInvalid()) |
| 4115 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4116 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4117 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4118 | |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4119 | // We might need to perform a trailing qualification conversion, since |
| 4120 | // the element type on the parameter could be more qualified than the |
| 4121 | // element type in the expression we constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4122 | bool ObjCLifetimeConversion; |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4123 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4124 | ParamType.getUnqualifiedType(), false, |
| 4125 | ObjCLifetimeConversion)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4126 | RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4127 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4128 | assert(!RefExpr.isInvalid() && |
| 4129 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4130 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4131 | return move(RefExpr); |
| 4132 | } |
| 4133 | } |
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 | QualType T = VD->getType().getNonReferenceType(); |
| 4136 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4137 | // When the non-type template parameter is a pointer, take the |
| 4138 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4139 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4140 | if (RefExpr.isInvalid()) |
| 4141 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4142 | |
| 4143 | if (T->isFunctionType() || T->isArrayType()) { |
| 4144 | // Decay functions and arrays. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4145 | RefExpr = DefaultFunctionArrayConversion(RefExpr.take()); |
| 4146 | if (RefExpr.isInvalid()) |
| 4147 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4148 | |
| 4149 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4150 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4151 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4152 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4153 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4154 | } |
| 4155 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4156 | ExprValueKind VK = VK_RValue; |
| 4157 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4158 | // If the non-type template parameter has reference type, qualify the |
| 4159 | // resulting declaration reference with the extra qualifiers on the |
| 4160 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4161 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 4162 | VK = VK_LValue; |
| 4163 | T = Context.getQualifiedType(T, |
| 4164 | TargetRef->getPointeeType().getQualifiers()); |
| 4165 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4166 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4167 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | /// \brief Construct a new expression that refers to the given |
| 4171 | /// integral template argument with the given source-location |
| 4172 | /// information. |
| 4173 | /// |
| 4174 | /// This routine takes care of the mapping from an integral template |
| 4175 | /// argument (which may have any integral type) to the appropriate |
| 4176 | /// literal value. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4177 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4178 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 4179 | SourceLocation Loc) { |
| 4180 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4181 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4182 | QualType T = Arg.getIntegralType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4183 | if (T->isAnyCharacterType()) { |
| 4184 | CharacterLiteral::CharacterKind Kind; |
| 4185 | if (T->isWideCharType()) |
| 4186 | Kind = CharacterLiteral::Wide; |
| 4187 | else if (T->isChar16Type()) |
| 4188 | Kind = CharacterLiteral::UTF16; |
| 4189 | else if (T->isChar32Type()) |
| 4190 | Kind = CharacterLiteral::UTF32; |
| 4191 | else |
| 4192 | Kind = CharacterLiteral::Ascii; |
| 4193 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4194 | return Owned(new (Context) CharacterLiteral( |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4195 | Arg.getAsIntegral()->getZExtValue(), |
| 4196 | Kind, T, Loc)); |
| 4197 | } |
| 4198 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4199 | if (T->isBooleanType()) |
| 4200 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 4201 | Arg.getAsIntegral()->getBoolValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4202 | T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4203 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4204 | if (T->isNullPtrType()) |
| 4205 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 4206 | |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4207 | // If this is an enum type that we're instantiating, we need to use an integer |
| 4208 | // type the same size as the enumerator. We don't want to build an |
| 4209 | // IntegerLiteral with enum type. |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4210 | QualType BT; |
| 4211 | if (const EnumType *ET = T->getAs<EnumType>()) |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4212 | BT = ET->getDecl()->getIntegerType(); |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4213 | else |
| 4214 | BT = T; |
| 4215 | |
John McCall | 4e9272d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 4216 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
| 4217 | if (T->isEnumeralType()) { |
| 4218 | // FIXME: This is a hack. We need a better way to handle substituted |
| 4219 | // non-type template parameters. |
| 4220 | E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0, |
| 4221 | Context.getTrivialTypeSourceInfo(T, Loc), |
| 4222 | Loc, Loc); |
| 4223 | } |
| 4224 | |
| 4225 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4226 | } |
| 4227 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4228 | /// \brief Match two template parameters within template parameter lists. |
| 4229 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 4230 | bool Complain, |
| 4231 | Sema::TemplateParameterListEqualKind Kind, |
| 4232 | SourceLocation TemplateArgLoc) { |
| 4233 | // Check the actual kind (type, non-type, template). |
| 4234 | if (Old->getKind() != New->getKind()) { |
| 4235 | if (Complain) { |
| 4236 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 4237 | if (TemplateArgLoc.isValid()) { |
| 4238 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4239 | NextDiag = diag::note_template_param_different_kind; |
| 4240 | } |
| 4241 | S.Diag(New->getLocation(), NextDiag) |
| 4242 | << (Kind != Sema::TPL_TemplateMatch); |
| 4243 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 4244 | << (Kind != Sema::TPL_TemplateMatch); |
| 4245 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4246 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4247 | return false; |
| 4248 | } |
| 4249 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4250 | // Check that both are parameter packs are neither are parameter packs. |
| 4251 | // However, if we are matching a template template argument to a |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4252 | // template template parameter, the template template parameter can have |
| 4253 | // a parameter pack where the template template argument does not. |
| 4254 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 4255 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4256 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4257 | if (Complain) { |
| 4258 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 4259 | if (TemplateArgLoc.isValid()) { |
| 4260 | S.Diag(TemplateArgLoc, |
| 4261 | diag::err_template_arg_template_params_mismatch); |
| 4262 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 4263 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4264 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4265 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 4266 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 4267 | : 2; |
| 4268 | S.Diag(New->getLocation(), NextDiag) |
| 4269 | << ParamKind << New->isParameterPack(); |
| 4270 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 4271 | << ParamKind << Old->isParameterPack(); |
| 4272 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4273 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4274 | return false; |
| 4275 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4276 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4277 | // For non-type template parameters, check the type of the parameter. |
| 4278 | if (NonTypeTemplateParmDecl *OldNTTP |
| 4279 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 4280 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4281 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4282 | // If we are matching a template template argument to a template |
| 4283 | // template parameter and one of the non-type template parameter types |
| 4284 | // is dependent, then we must wait until template instantiation time |
| 4285 | // to actually compare the arguments. |
| 4286 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4287 | (OldNTTP->getType()->isDependentType() || |
| 4288 | NewNTTP->getType()->isDependentType())) |
| 4289 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4290 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4291 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 4292 | if (Complain) { |
| 4293 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 4294 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4295 | S.Diag(TemplateArgLoc, |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4296 | diag::err_template_arg_template_params_mismatch); |
| 4297 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 4298 | } |
| 4299 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 4300 | << NewNTTP->getType() |
| 4301 | << (Kind != Sema::TPL_TemplateMatch); |
| 4302 | S.Diag(OldNTTP->getLocation(), |
| 4303 | diag::note_template_nontype_parm_prev_declaration) |
| 4304 | << OldNTTP->getType(); |
| 4305 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4306 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4307 | return false; |
| 4308 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4309 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4310 | return true; |
| 4311 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4312 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4313 | // For template template parameters, check the template parameter types. |
| 4314 | // The template parameter lists of template template |
| 4315 | // parameters must agree. |
| 4316 | if (TemplateTemplateParmDecl *OldTTP |
| 4317 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4318 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4319 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 4320 | OldTTP->getTemplateParameters(), |
| 4321 | Complain, |
| 4322 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4323 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4324 | : Kind), |
| 4325 | TemplateArgLoc); |
| 4326 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4327 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4328 | return true; |
| 4329 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4330 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4331 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 4332 | /// lists. |
| 4333 | static |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4334 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4335 | TemplateParameterList *New, |
| 4336 | TemplateParameterList *Old, |
| 4337 | Sema::TemplateParameterListEqualKind Kind, |
| 4338 | SourceLocation TemplateArgLoc) { |
| 4339 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 4340 | if (TemplateArgLoc.isValid()) { |
| 4341 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4342 | NextDiag = diag::note_template_param_list_different_arity; |
| 4343 | } |
| 4344 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 4345 | << (New->size() > Old->size()) |
| 4346 | << (Kind != Sema::TPL_TemplateMatch) |
| 4347 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 4348 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 4349 | << (Kind != Sema::TPL_TemplateMatch) |
| 4350 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 4351 | } |
| 4352 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4353 | /// \brief Determine whether the given template parameter lists are |
| 4354 | /// equivalent. |
| 4355 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4356 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4357 | /// source code as part of a new template declaration. |
| 4358 | /// |
| 4359 | /// \param Old The old template parameter list, typically found via |
| 4360 | /// name lookup of the template declared with this template parameter |
| 4361 | /// list. |
| 4362 | /// |
| 4363 | /// \param Complain If true, this routine will produce a diagnostic if |
| 4364 | /// the template parameter lists are not equivalent. |
| 4365 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4366 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4367 | /// |
| 4368 | /// \param TemplateArgLoc If this source location is valid, then we |
| 4369 | /// are actually checking the template parameter list of a template |
| 4370 | /// argument (New) against the template parameter list of its |
| 4371 | /// corresponding template template parameter (Old). We produce |
| 4372 | /// slightly different diagnostics in this scenario. |
| 4373 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4374 | /// \returns True if the template parameter lists are equal, false |
| 4375 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4376 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4377 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 4378 | TemplateParameterList *Old, |
| 4379 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4380 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4381 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4382 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 4383 | if (Complain) |
| 4384 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4385 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4386 | |
| 4387 | return false; |
| 4388 | } |
| 4389 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4390 | // C++0x [temp.arg.template]p3: |
| 4391 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4392 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4393 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4394 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4395 | // template-parameter-list of P. [...] |
| 4396 | TemplateParameterList::iterator NewParm = New->begin(); |
| 4397 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4398 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4399 | OldParmEnd = Old->end(); |
| 4400 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 4401 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 4402 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4403 | if (NewParm == NewParmEnd) { |
| 4404 | if (Complain) |
| 4405 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4406 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4407 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4408 | return false; |
| 4409 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4410 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4411 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4412 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4413 | return false; |
| 4414 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4415 | ++NewParm; |
| 4416 | continue; |
| 4417 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4418 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4419 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4420 | // [...] When P's template- parameter-list contains a template parameter |
| 4421 | // pack (14.5.3), the template parameter pack will match zero or more |
| 4422 | // template parameters or template parameter packs in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4423 | // template-parameter-list of A with the same type and form as the |
| 4424 | // template parameter pack in P (ignoring whether those template |
| 4425 | // parameters are template parameter packs). |
| 4426 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 4427 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4428 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4429 | return false; |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4430 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4431 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4432 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4433 | // Make sure we exhausted all of the arguments. |
| 4434 | if (NewParm != NewParmEnd) { |
| 4435 | if (Complain) |
| 4436 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4437 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4438 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4439 | return false; |
| 4440 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4441 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4442 | return true; |
| 4443 | } |
| 4444 | |
| 4445 | /// \brief Check whether a template can be declared within this scope. |
| 4446 | /// |
| 4447 | /// If the template declaration is valid in this scope, returns |
| 4448 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4449 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4450 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 4451 | if (!S) |
| 4452 | return false; |
| 4453 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4454 | // Find the nearest enclosing declaration scope. |
| 4455 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4456 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4457 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4458 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4459 | // C++ [temp]p2: |
| 4460 | // A template-declaration can appear only as a namespace scope or |
| 4461 | // class scope declaration. |
| 4462 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4463 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 4464 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4465 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4466 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4467 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4468 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4469 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4470 | |
| 4471 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 4472 | return false; |
| 4473 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4474 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4475 | diag::err_template_outside_namespace_or_class_scope) |
| 4476 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4477 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4478 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4479 | /// \brief Determine what kind of template specialization the given declaration |
| 4480 | /// is. |
| 4481 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 4482 | if (!D) |
| 4483 | return TSK_Undeclared; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4484 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4485 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 4486 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4487 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 4488 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4489 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 4490 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4491 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4492 | return TSK_Undeclared; |
| 4493 | } |
| 4494 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4495 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4496 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4497 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4498 | /// This routine determines whether a template specialization can be declared |
| 4499 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4500 | /// |
| 4501 | /// \param S the semantic analysis object for which this check is being |
| 4502 | /// performed. |
| 4503 | /// |
| 4504 | /// \param Specialized the entity being specialized or instantiated, which |
| 4505 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4506 | /// a member of a class template (member function, static data member, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4507 | /// member class). |
| 4508 | /// |
| 4509 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 4510 | /// |
| 4511 | /// \param Loc the location of the explicit specialization or instantiation of |
| 4512 | /// this entity. |
| 4513 | /// |
| 4514 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 4515 | /// a class template. |
| 4516 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4517 | /// \returns true if there was an error that we cannot recover from, false |
| 4518 | /// otherwise. |
| 4519 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 4520 | NamedDecl *Specialized, |
| 4521 | NamedDecl *PrevDecl, |
| 4522 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4523 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4524 | // Keep these "kind" numbers in sync with the %select statements in the |
| 4525 | // various diagnostics emitted by this routine. |
| 4526 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4527 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4528 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4529 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4530 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4531 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4532 | EntityKind = 3; |
| 4533 | else if (isa<VarDecl>(Specialized)) |
| 4534 | EntityKind = 4; |
| 4535 | else if (isa<RecordDecl>(Specialized)) |
| 4536 | EntityKind = 5; |
| 4537 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4538 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 4539 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4540 | return true; |
| 4541 | } |
| 4542 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4543 | // C++ [temp.expl.spec]p2: |
| 4544 | // An explicit specialization shall be declared in the namespace |
| 4545 | // of which the template is a member, or, for member templates, in |
| 4546 | // the namespace of which the enclosing class or enclosing class |
| 4547 | // template is a member. An explicit specialization of a member |
| 4548 | // function, member class or static data member of a class |
| 4549 | // template shall be declared in the namespace of which the class |
| 4550 | // template is a member. Such a declaration may also be a |
| 4551 | // definition. If the declaration is not a definition, the |
| 4552 | // specialization may be defined later in the name- space in which |
| 4553 | // the explicit specialization was declared, or in a namespace |
| 4554 | // that encloses the one in which the explicit specialization was |
| 4555 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4556 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4557 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4558 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4559 | return true; |
| 4560 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4561 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4562 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4563 | if (S.getLangOptions().MicrosoftExt) { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4564 | // Do not warn for class scope explicit specialization during |
| 4565 | // instantiation, warning was already emitted during pattern |
| 4566 | // semantic analysis. |
| 4567 | if (!S.ActiveTemplateInstantiations.size()) |
| 4568 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 4569 | << Specialized; |
| 4570 | } else { |
| 4571 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4572 | << Specialized; |
| 4573 | return true; |
| 4574 | } |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4575 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4576 | |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4577 | if (S.CurContext->isRecord() && |
| 4578 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 4579 | // Make sure that we're specializing in the right record context. |
| 4580 | // Otherwise, things can go horribly wrong. |
| 4581 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4582 | << Specialized; |
| 4583 | return true; |
| 4584 | } |
| 4585 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4586 | // C++ [temp.class.spec]p6: |
| 4587 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4588 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4589 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4590 | bool ComplainedAboutScope = false; |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4591 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4592 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4593 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4594 | if ((!PrevDecl || |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4595 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4596 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4597 | // C++ [temp.exp.spec]p2: |
| 4598 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4599 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4600 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4601 | // An explicit specialization of a member function, member class or |
| 4602 | // static data member of a class template shall be declared in the |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4603 | // namespace of which the class template is a member. |
| 4604 | // |
| 4605 | // C++0x [temp.expl.spec]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4606 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4607 | // the specialized template. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4608 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
| 4609 | bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext); |
| 4610 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
| 4611 | assert(!IsCPlusPlus0xExtension && |
| 4612 | "DC encloses TU but isn't in enclosing namespace set"); |
| 4613 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4614 | << EntityKind << Specialized; |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4615 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 4616 | int Diag; |
| 4617 | if (!IsCPlusPlus0xExtension) |
| 4618 | Diag = diag::err_template_spec_decl_out_of_scope; |
| 4619 | else if (!S.getLangOptions().CPlusPlus0x) |
| 4620 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 4621 | else |
| 4622 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 4623 | S.Diag(Loc, Diag) |
| 4624 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 4625 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4626 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4627 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4628 | ComplainedAboutScope = |
| 4629 | !(IsCPlusPlus0xExtension && S.getLangOptions().CPlusPlus0x); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4630 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4631 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4632 | |
| 4633 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4634 | // namespace. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4635 | // Note that HandleDeclarator() performs this check for explicit |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4636 | // specializations of function templates, static data members, and member |
| 4637 | // functions, so we skip the check here for those kinds of entities. |
| 4638 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4639 | // Should we refactor that check, so that it occurs later? |
| 4640 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4641 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 4642 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4643 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 4644 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 4645 | << EntityKind << Specialized; |
| 4646 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 4647 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 4648 | << EntityKind << Specialized |
| 4649 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4650 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4651 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4652 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4653 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4654 | // FIXME: check for specialization-after-instantiation errors and such. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4655 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4656 | return false; |
| 4657 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4658 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4659 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 4660 | /// that checks non-type template partial specialization arguments. |
| 4661 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 4662 | NonTypeTemplateParmDecl *Param, |
| 4663 | const TemplateArgument *Args, |
| 4664 | unsigned NumArgs) { |
| 4665 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4666 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 4667 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4668 | Args[I].pack_begin(), |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4669 | Args[I].pack_size())) |
| 4670 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4671 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4672 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4673 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4674 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4675 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4676 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4677 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4678 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4679 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4680 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4681 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4682 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4683 | |
| 4684 | // Strip off any implicit casts we added as part of type checking. |
| 4685 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4686 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4687 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4688 | // C++ [temp.class.spec]p8: |
| 4689 | // A non-type argument is non-specialized if it is the name of a |
| 4690 | // non-type parameter. All other non-type arguments are |
| 4691 | // specialized. |
| 4692 | // |
| 4693 | // Below, we check the two conditions that only apply to |
| 4694 | // specialized non-type arguments, so skip any non-specialized |
| 4695 | // arguments. |
| 4696 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4697 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4698 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4699 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4700 | // C++ [temp.class.spec]p9: |
| 4701 | // Within the argument list of a class template partial |
| 4702 | // specialization, the following restrictions apply: |
| 4703 | // -- A partially specialized non-type argument expression |
| 4704 | // shall not involve a template parameter of the partial |
| 4705 | // specialization except when the argument expression is a |
| 4706 | // simple identifier. |
| 4707 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4708 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4709 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4710 | << ArgExpr->getSourceRange(); |
| 4711 | return true; |
| 4712 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4713 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4714 | // -- The type of a template parameter corresponding to a |
| 4715 | // specialized non-type argument shall not be dependent on a |
| 4716 | // parameter of the specialization. |
| 4717 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4718 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4719 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 4720 | << Param->getType() |
| 4721 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4722 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4723 | return true; |
| 4724 | } |
| 4725 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4726 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4727 | return false; |
| 4728 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4729 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4730 | /// \brief Check the non-type template arguments of a class template |
| 4731 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 4732 | /// |
| 4733 | /// \param TemplateParams the template parameters of the primary class |
| 4734 | /// template. |
| 4735 | /// |
| 4736 | /// \param TemplateArg the template arguments of the class template |
| 4737 | /// partial specialization. |
| 4738 | /// |
| 4739 | /// \returns true if there was an error, false otherwise. |
| 4740 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 4741 | TemplateParameterList *TemplateParams, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4742 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4743 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 4744 | |
| 4745 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4746 | NonTypeTemplateParmDecl *Param |
| 4747 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 4748 | if (!Param) |
| 4749 | continue; |
| 4750 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4751 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4752 | &ArgList[I], 1)) |
| 4753 | return true; |
| 4754 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4755 | |
| 4756 | return false; |
| 4757 | } |
| 4758 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4759 | /// \brief Retrieve the previous declaration of the given declaration. |
| 4760 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 4761 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4762 | return VD->getPreviousDeclaration(); |
| 4763 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 4764 | return FD->getPreviousDeclaration(); |
| 4765 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 4766 | return TD->getPreviousDeclaration(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4767 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4768 | return TD->getPreviousDeclaration(); |
| 4769 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 4770 | return FTD->getPreviousDeclaration(); |
| 4771 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 4772 | return CTD->getPreviousDeclaration(); |
| 4773 | return 0; |
| 4774 | } |
| 4775 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4776 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4777 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4778 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4779 | SourceLocation KWLoc, |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 4780 | SourceLocation ModulePrivateLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4781 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4782 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4783 | SourceLocation TemplateNameLoc, |
| 4784 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4785 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4786 | SourceLocation RAngleLoc, |
| 4787 | AttributeList *Attr, |
| 4788 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4789 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4790 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4791 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 4792 | // store the location of the outermost template keyword in the declaration. |
| 4793 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
| 4794 | ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation(); |
| 4795 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4796 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4797 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4798 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4799 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4800 | |
| 4801 | if (!ClassTemplate) { |
| 4802 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4803 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4804 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4805 | return true; |
| 4806 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4807 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4808 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4809 | bool isPartialSpecialization = false; |
| 4810 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4811 | // Check the validity of the template headers that introduce this |
| 4812 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4813 | // FIXME: We probably shouldn't complain about these headers for |
| 4814 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4815 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4816 | TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 4817 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, |
| 4818 | TemplateNameLoc, |
| 4819 | SS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4820 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4821 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4822 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4823 | isExplicitSpecialization, |
| 4824 | Invalid); |
| 4825 | if (Invalid) |
| 4826 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4827 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4828 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4829 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4830 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4831 | if (TUK == TUK_Friend) { |
| 4832 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4833 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4834 | return true; |
| 4835 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4836 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4837 | // C++ [temp.class.spec]p10: |
| 4838 | // The template parameter list of a specialization shall not |
| 4839 | // contain default template argument values. |
| 4840 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4841 | Decl *Param = TemplateParams->getParam(I); |
| 4842 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4843 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4844 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4845 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4846 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4847 | } |
| 4848 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4849 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4850 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4851 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4852 | diag::err_default_arg_in_partial_spec) |
| 4853 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4854 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4855 | } |
| 4856 | } else { |
| 4857 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4858 | if (TTP->hasDefaultArgument()) { |
| 4859 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4860 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4861 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4862 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4863 | } |
| 4864 | } |
| 4865 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4866 | } else if (TemplateParams) { |
| 4867 | if (TUK == TUK_Friend) |
| 4868 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4869 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4870 | SourceRange(TemplateParams->getTemplateLoc(), |
| 4871 | TemplateParams->getRAngleLoc())) |
| 4872 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4873 | else |
| 4874 | isExplicitSpecialization = true; |
| 4875 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4876 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4877 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4878 | isExplicitSpecialization = true; |
| 4879 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4880 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4881 | // Check that the specialization uses the same tag kind as the |
| 4882 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4883 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4884 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4885 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 4886 | Kind, TUK == TUK_Definition, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4887 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4888 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4889 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4890 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4891 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4892 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4893 | diag::note_previous_use); |
| 4894 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4895 | } |
| 4896 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4897 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4898 | TemplateArgumentListInfo TemplateArgs; |
| 4899 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 4900 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4901 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4902 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4903 | // Check for unexpanded parameter packs in any of the template arguments. |
| 4904 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4905 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4906 | UPPC_PartialSpecialization)) |
| 4907 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4908 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4909 | // Check that the template argument list is well-formed for this |
| 4910 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4911 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4912 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4913 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4914 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4915 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4916 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4917 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4918 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4919 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4920 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4921 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4922 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4923 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4924 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4925 | return true; |
| 4926 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4927 | bool InstantiationDependent; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4928 | if (!Name.isDependent() && |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4929 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4930 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4931 | TemplateArgs.size(), |
| 4932 | InstantiationDependent)) { |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4933 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 4934 | << ClassTemplate->getDeclName(); |
| 4935 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4936 | } |
| 4937 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4938 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4939 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4940 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 4941 | |
| 4942 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4943 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4944 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4945 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 4946 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4947 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4948 | else |
| 4949 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4950 | = ClassTemplate->findSpecialization(Converted.data(), |
| 4951 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4952 | |
| 4953 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4954 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4955 | // Check whether we can declare a class template specialization in |
| 4956 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4957 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4958 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 4959 | TemplateNameLoc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4960 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4961 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4962 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4963 | // The canonical type |
| 4964 | QualType CanonType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4965 | if (PrevDecl && |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4966 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4967 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4968 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4969 | // arguments was referenced but not declared, or we're only |
| 4970 | // referencing this specialization as a friend, reuse that |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4971 | // declaration node as our own, updating its source location and |
| 4972 | // the list of outer template parameters to reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4973 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4974 | Specialization->setLocation(TemplateNameLoc); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4975 | if (TemplateParameterLists.size() > 0) { |
| 4976 | Specialization->setTemplateParameterListsInfo(Context, |
| 4977 | TemplateParameterLists.size(), |
| 4978 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4979 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4980 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4981 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4982 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4983 | // Build the canonical type that describes the converted template |
| 4984 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4985 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4986 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4987 | Converted.data(), |
| 4988 | Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4989 | |
| 4990 | if (Context.hasSameType(CanonType, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4991 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 4992 | // C++ [temp.class.spec]p9b3: |
| 4993 | // |
| 4994 | // -- The argument list of the specialization shall not be identical |
| 4995 | // to the implicit argument list of the primary template. |
| 4996 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 4997 | << (TUK == TUK_Definition) |
| 4998 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4999 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 5000 | ClassTemplate->getIdentifier(), |
| 5001 | TemplateNameLoc, |
| 5002 | Attr, |
| 5003 | TemplateParams, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 5004 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5005 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 5006 | (TemplateParameterList**) TemplateParameterLists.release()); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5007 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5008 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5009 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5010 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 5011 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5012 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5013 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5014 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5015 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5016 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5017 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 5018 | TemplateParams, |
| 5019 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5020 | Converted.data(), |
| 5021 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5022 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5023 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5024 | PrevPartial, |
| 5025 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5026 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5027 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5028 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5029 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5030 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5031 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5032 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5033 | if (!PrevPartial) |
| 5034 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5035 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5036 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5037 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5038 | // template specialization, make a note of that. |
| 5039 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 5040 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5041 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5042 | // Check that all of the template parameters of the class template |
| 5043 | // partial specialization are deducible from the template |
| 5044 | // arguments. If not, this class template partial specialization |
| 5045 | // will never be used. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5046 | SmallVector<bool, 8> DeducibleParams; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5047 | DeducibleParams.resize(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5048 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5049 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5050 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5051 | unsigned NumNonDeducible = 0; |
| 5052 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 5053 | if (!DeducibleParams[I]) |
| 5054 | ++NumNonDeducible; |
| 5055 | |
| 5056 | if (NumNonDeducible) { |
| 5057 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 5058 | << (NumNonDeducible > 1) |
| 5059 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 5060 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 5061 | if (!DeducibleParams[I]) { |
| 5062 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 5063 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5064 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5065 | diag::note_partial_spec_unused_parameter) |
| 5066 | << Param->getDeclName(); |
| 5067 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5068 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5069 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 5070 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5071 | } |
| 5072 | } |
| 5073 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5074 | } else { |
| 5075 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5076 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5077 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5078 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5079 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5080 | KWLoc, TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5081 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5082 | Converted.data(), |
| 5083 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5084 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5085 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5086 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5087 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5088 | TemplateParameterLists.size(), |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5089 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5090 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5091 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5092 | if (!PrevDecl) |
| 5093 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5094 | |
| 5095 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5096 | } |
| 5097 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5098 | // C++ [temp.expl.spec]p6: |
| 5099 | // 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] | 5100 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5101 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5102 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5103 | // use occurs; no diagnostic is required. |
| 5104 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5105 | bool Okay = false; |
| 5106 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 5107 | // Is there any previous explicit specialization declaration? |
| 5108 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5109 | Okay = true; |
| 5110 | break; |
| 5111 | } |
| 5112 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5113 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5114 | if (!Okay) { |
| 5115 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 5116 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 5117 | << Context.getTypeDeclType(Specialization) << Range; |
| 5118 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5119 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5120 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5121 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5122 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5123 | return true; |
| 5124 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5125 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5126 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5127 | // If this is not a friend, note that this is an explicit specialization. |
| 5128 | if (TUK != TUK_Friend) |
| 5129 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5130 | |
| 5131 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5132 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5133 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5134 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5135 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5136 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5137 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 5138 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5139 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5140 | } |
| 5141 | } |
| 5142 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 5143 | if (Attr) |
| 5144 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 5145 | |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5146 | if (ModulePrivateLoc.isValid()) |
| 5147 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 5148 | << (isPartialSpecialization? 1 : 0) |
| 5149 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 5150 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 5151 | // Build the fully-sugared type for this class template |
| 5152 | // specialization as the user wrote in the specialization |
| 5153 | // itself. This means that we'll pretty-print the type retrieved |
| 5154 | // from the specialization's declaration the way that the user |
| 5155 | // actually wrote the specialization, rather than formatting the |
| 5156 | // name based on the "canonical" representation used to store the |
| 5157 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5158 | TypeSourceInfo *WrittenTy |
| 5159 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5160 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5161 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5162 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5163 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5164 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5165 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5166 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5167 | // C++ [temp.expl.spec]p9: |
| 5168 | // A template explicit specialization is in the scope of the |
| 5169 | // namespace in which the template was defined. |
| 5170 | // |
| 5171 | // We actually implement this paragraph where we set the semantic |
| 5172 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 5173 | // but we also maintain the lexical context where the actual |
| 5174 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5175 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5176 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5177 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5178 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5179 | Specialization->startDefinition(); |
| 5180 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5181 | if (TUK == TUK_Friend) { |
| 5182 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 5183 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5184 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5185 | /*FIXME:*/KWLoc); |
| 5186 | Friend->setAccess(AS_public); |
| 5187 | CurContext->addDecl(Friend); |
| 5188 | } else { |
| 5189 | // Add the specialization into its lexical context, so that it can |
| 5190 | // be seen when iterating through the list of declarations in that |
| 5191 | // context. However, specializations are not found by name lookup. |
| 5192 | CurContext->addDecl(Specialization); |
| 5193 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5194 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5195 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5196 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5197 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5198 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5199 | Declarator &D) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5200 | return HandleDeclarator(S, D, move(TemplateParameterLists)); |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5201 | } |
| 5202 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5203 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5204 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5205 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5206 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5207 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5208 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5209 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5210 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5211 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5212 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5213 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5214 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 5215 | D.setFunctionDefinitionKind(FDK_Definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5216 | Decl *DP = HandleDeclarator(ParentScope, D, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5217 | move(TemplateParameterLists)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5218 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5219 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5220 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5221 | FunctionTemplate->getTemplatedDecl()); |
| 5222 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 5223 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 5224 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5225 | } |
| 5226 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5227 | /// \brief Strips various properties off an implicit instantiation |
| 5228 | /// that has just been explicitly specialized. |
| 5229 | static void StripImplicitInstantiation(NamedDecl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5230 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5231 | |
| 5232 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5233 | FD->setInlineSpecified(false); |
| 5234 | } |
| 5235 | } |
| 5236 | |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5237 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 5238 | // declaration or definition. |
| 5239 | static SourceLocation DiagLocForExplicitInstantiation( |
| 5240 | NamedDecl* Decl, SourceLocation PointOfInstantiation) { |
| 5241 | // Explicit instantiations following a specialization have no effect and |
| 5242 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 5243 | // until a valid name loc is found. |
| 5244 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
| 5245 | for (NamedDecl *Prev = Decl; Prev && !PrevDiagLoc.isValid(); |
| 5246 | Prev = getPreviousDecl(Prev)) { |
| 5247 | PrevDiagLoc = Prev->getLocation(); |
| 5248 | } |
| 5249 | assert(PrevDiagLoc.isValid() && |
| 5250 | "Explicit instantiation without point of instantiation?"); |
| 5251 | return PrevDiagLoc; |
| 5252 | } |
| 5253 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5254 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5255 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5256 | /// for those cases where they are required and determining whether the |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5257 | /// new specialization/instantiation will have any effect. |
| 5258 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5259 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5260 | /// instantiation. |
| 5261 | /// |
| 5262 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 5263 | /// |
| 5264 | /// \param PrevDecl the previous declaration of the entity. |
| 5265 | /// |
| 5266 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 5267 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5268 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5269 | /// declaration was instantiated (either implicitly or explicitly). |
| 5270 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5271 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5272 | /// specialization or instantiation has no effect and should be ignored. |
| 5273 | /// |
| 5274 | /// \returns true if there was an error that should prevent the introduction of |
| 5275 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5276 | bool |
| 5277 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 5278 | TemplateSpecializationKind NewTSK, |
| 5279 | NamedDecl *PrevDecl, |
| 5280 | TemplateSpecializationKind PrevTSK, |
| 5281 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5282 | bool &HasNoEffect) { |
| 5283 | HasNoEffect = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5284 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5285 | switch (NewTSK) { |
| 5286 | case TSK_Undeclared: |
| 5287 | case TSK_ImplicitInstantiation: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5288 | llvm_unreachable("Don't check implicit instantiations here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5289 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5290 | case TSK_ExplicitSpecialization: |
| 5291 | switch (PrevTSK) { |
| 5292 | case TSK_Undeclared: |
| 5293 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5294 | // Okay, we're just specializing something that is either already |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5295 | // explicitly specialized or has merely been mentioned without any |
| 5296 | // instantiation. |
| 5297 | return false; |
| 5298 | |
| 5299 | case TSK_ImplicitInstantiation: |
| 5300 | if (PrevPointOfInstantiation.isInvalid()) { |
| 5301 | // The declaration itself has not actually been instantiated, so it is |
| 5302 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5303 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5304 | return false; |
| 5305 | } |
| 5306 | // Fall through |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5307 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5308 | case TSK_ExplicitInstantiationDeclaration: |
| 5309 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5310 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 5311 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5312 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5313 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5314 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5315 | // 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] | 5316 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5317 | // before the first use of that specialization that would cause an |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5318 | // implicit instantiation to take place, in every translation unit in |
| 5319 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5320 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 5321 | // Is there any previous explicit specialization declaration? |
| 5322 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 5323 | return false; |
| 5324 | } |
| 5325 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5326 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5327 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5328 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5329 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5330 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5331 | return true; |
| 5332 | } |
| 5333 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5334 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5335 | case TSK_ExplicitInstantiationDeclaration: |
| 5336 | switch (PrevTSK) { |
| 5337 | case TSK_ExplicitInstantiationDeclaration: |
| 5338 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5339 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5340 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5341 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5342 | case TSK_Undeclared: |
| 5343 | case TSK_ImplicitInstantiation: |
| 5344 | // We're explicitly instantiating something that may have already been |
| 5345 | // implicitly instantiated; that's fine. |
| 5346 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5347 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5348 | case TSK_ExplicitSpecialization: |
| 5349 | // C++0x [temp.explicit]p4: |
| 5350 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5351 | // of a template appears after a declaration of an explicit |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5352 | // specialization for that template, the explicit instantiation has no |
| 5353 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5354 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5355 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5356 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5357 | case TSK_ExplicitInstantiationDefinition: |
| 5358 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5359 | // If an entity is the subject of both an explicit instantiation |
| 5360 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5361 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5362 | Diag(NewLoc, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5363 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5364 | |
| 5365 | // Explicit instantiations following a specialization have no effect and |
| 5366 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 5367 | // until a valid name loc is found. |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5368 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 5369 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5370 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5371 | return false; |
| 5372 | } |
| 5373 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5374 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5375 | case TSK_ExplicitInstantiationDefinition: |
| 5376 | switch (PrevTSK) { |
| 5377 | case TSK_Undeclared: |
| 5378 | case TSK_ImplicitInstantiation: |
| 5379 | // We're explicitly instantiating something that may have already been |
| 5380 | // implicitly instantiated; that's fine. |
| 5381 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5382 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5383 | case TSK_ExplicitSpecialization: |
| 5384 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 5385 | // For a given set of template parameters, if an explicit |
| 5386 | // instantiation of a template appears after a declaration of |
| 5387 | // an explicit specialization for that template, the explicit |
| 5388 | // instantiation has no effect. |
| 5389 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5390 | // 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] | 5391 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5392 | // has been explicitly specialized. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5393 | Diag(NewLoc, getLangOptions().CPlusPlus0x ? |
| 5394 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 5395 | diag::ext_explicit_instantiation_after_specialization) |
| 5396 | << PrevDecl; |
| 5397 | Diag(PrevDecl->getLocation(), |
| 5398 | diag::note_previous_template_specialization); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5399 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5400 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5401 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5402 | case TSK_ExplicitInstantiationDeclaration: |
| 5403 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5404 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5405 | |
| 5406 | // C++0x [temp.explicit]p4: |
| 5407 | // For a given set of template parameters, if an explicit instantiation |
| 5408 | // of a template appears after a declaration of an explicit |
| 5409 | // specialization for that template, the explicit instantiation has no |
| 5410 | // effect. |
| 5411 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 5412 | // Is there any previous explicit specialization declaration? |
| 5413 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5414 | HasNoEffect = true; |
| 5415 | break; |
| 5416 | } |
| 5417 | } |
| 5418 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5419 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5420 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5421 | case TSK_ExplicitInstantiationDefinition: |
| 5422 | // C++0x [temp.spec]p5: |
| 5423 | // For a given template and a given set of template-arguments, |
| 5424 | // - an explicit instantiation definition shall appear at most once |
| 5425 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5426 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5427 | << PrevDecl; |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5428 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5429 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5430 | HasNoEffect = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5431 | return false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5432 | } |
| 5433 | break; |
| 5434 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5435 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5436 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5437 | } |
| 5438 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5439 | /// \brief Perform semantic analysis for the given dependent function |
| 5440 | /// template specialization. The only possible way to get a dependent |
| 5441 | /// function template specialization is with a friend declaration, |
| 5442 | /// like so: |
| 5443 | /// |
| 5444 | /// template <class T> void foo(T); |
| 5445 | /// template <class T> class A { |
| 5446 | /// friend void foo<>(T); |
| 5447 | /// }; |
| 5448 | /// |
| 5449 | /// There really isn't any useful analysis we can do here, so we |
| 5450 | /// just store the information. |
| 5451 | bool |
| 5452 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 5453 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 5454 | LookupResult &Previous) { |
| 5455 | // Remove anything from Previous that isn't a function template in |
| 5456 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5457 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5458 | LookupResult::Filter F = Previous.makeFilter(); |
| 5459 | while (F.hasNext()) { |
| 5460 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 5461 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5462 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 5463 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5464 | F.erase(); |
| 5465 | } |
| 5466 | F.done(); |
| 5467 | |
| 5468 | // Should this be diagnosed here? |
| 5469 | if (Previous.empty()) return true; |
| 5470 | |
| 5471 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 5472 | ExplicitTemplateArgs); |
| 5473 | return false; |
| 5474 | } |
| 5475 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5476 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5477 | /// specialization. |
| 5478 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5479 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5480 | /// explicit function template specialization. On successful completion, |
| 5481 | /// the function declaration \p FD will become a function template |
| 5482 | /// specialization. |
| 5483 | /// |
| 5484 | /// \param FD the function declaration, which will be updated to become a |
| 5485 | /// function template specialization. |
| 5486 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5487 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 5488 | /// if any. Note that this may be valid info even when 0 arguments are |
| 5489 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 5490 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5491 | /// |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5492 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5493 | /// this function specialization. |
| 5494 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5495 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5496 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5497 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5498 | // The set of function template specializations that could match this |
| 5499 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5500 | UnresolvedSet<8> Candidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5501 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5502 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5503 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5504 | I != E; ++I) { |
| 5505 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 5506 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5507 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5508 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5509 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 5510 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5511 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5512 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5513 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5514 | // A trailing template-argument can be left unspecified in the |
| 5515 | // template-id naming an explicit function template specialization |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5516 | // provided it can be deduced from the function argument type. |
| 5517 | // Perform template argument deduction to determine whether we may be |
| 5518 | // specializing this template. |
| 5519 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5520 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5521 | FunctionDecl *Specialization = 0; |
| 5522 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5523 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5524 | FD->getType(), |
| 5525 | Specialization, |
| 5526 | Info)) { |
| 5527 | // FIXME: Template argument deduction failed; record why it failed, so |
| 5528 | // that we can provide nifty diagnostics. |
| 5529 | (void)TDK; |
| 5530 | continue; |
| 5531 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5532 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5533 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5534 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5535 | } |
| 5536 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5537 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5538 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5539 | UnresolvedSetIterator Result |
| 5540 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5541 | TPOC_Other, 0, FD->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5542 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5543 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5544 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5545 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5546 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5547 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5548 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5549 | |
| 5550 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 5551 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | abfb405 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 5552 | |
| 5553 | FunctionTemplateSpecializationInfo *SpecInfo |
| 5554 | = Specialization->getTemplateSpecializationInfo(); |
| 5555 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5556 | |
| 5557 | // Note: do not overwrite location info if previous template |
| 5558 | // specialization kind was explicit. |
| 5559 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
| 5560 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) |
| 5561 | Specialization->setLocation(FD->getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5562 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5563 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5564 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5565 | |
| 5566 | // If this is a friend declaration, then we're not really declaring |
| 5567 | // an explicit specialization. |
| 5568 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5569 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5570 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5571 | if (!isFriend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5572 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5573 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5574 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5575 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5576 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5577 | |
| 5578 | // C++ [temp.expl.spec]p6: |
| 5579 | // 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] | 5580 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5581 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5582 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5583 | // use occurs; no diagnostic is required. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5584 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5585 | if (!isFriend && |
| 5586 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5587 | TSK_ExplicitSpecialization, |
| 5588 | Specialization, |
| 5589 | SpecInfo->getTemplateSpecializationKind(), |
| 5590 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5591 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5592 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5593 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5594 | // Mark the prior declaration as an explicit specialization, so that later |
| 5595 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5596 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5597 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5598 | MarkUnusedFileScopedDecl(Specialization); |
| 5599 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5600 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5601 | // Turn the given function declaration into a function template |
| 5602 | // specialization, with the template arguments from the previous |
| 5603 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5604 | // Take copies of (semantic and syntactic) template argument lists. |
| 5605 | const TemplateArgumentList* TemplArgs = new (Context) |
| 5606 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5607 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5608 | TemplArgs, /*InsertPos=*/0, |
| 5609 | SpecInfo->getTemplateSpecializationKind(), |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 5610 | ExplicitTemplateArgs); |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5611 | FD->setStorageClass(Specialization->getStorageClass()); |
| 5612 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5613 | // The "previous declaration" for this function template specialization is |
| 5614 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5615 | Previous.clear(); |
| 5616 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5617 | return false; |
| 5618 | } |
| 5619 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5620 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5621 | /// specialization. |
| 5622 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5623 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5624 | /// explicit member function specialization. On successful completion, |
| 5625 | /// the function declaration \p FD will become a member function |
| 5626 | /// specialization. |
| 5627 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5628 | /// \param Member the member declaration, which will be updated to become a |
| 5629 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5630 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5631 | /// \param Previous the set of declarations, one of which may be specialized |
| 5632 | /// by this function specialization; the set will be modified to contain the |
| 5633 | /// redeclared member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5634 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5635 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5636 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5637 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5638 | // Try to find the member we are instantiating. |
| 5639 | NamedDecl *Instantiation = 0; |
| 5640 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5641 | MemberSpecializationInfo *MSInfo = 0; |
| 5642 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5643 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5644 | // Nowhere to look anyway. |
| 5645 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5646 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5647 | I != E; ++I) { |
| 5648 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5649 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5650 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 5651 | Instantiation = Method; |
| 5652 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5653 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5654 | break; |
| 5655 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5656 | } |
| 5657 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5658 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5659 | VarDecl *PrevVar; |
| 5660 | if (Previous.isSingleResult() && |
| 5661 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5662 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5663 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5664 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5665 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5666 | } |
| 5667 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5668 | CXXRecordDecl *PrevRecord; |
| 5669 | if (Previous.isSingleResult() && |
| 5670 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 5671 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5672 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5673 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5674 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5675 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5676 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5677 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5678 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5679 | // specializations are always out-of-line, the caller will complain about |
| 5680 | // this mismatch later. |
| 5681 | return false; |
| 5682 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5683 | |
| 5684 | // If this is a friend, just bail out here before we start turning |
| 5685 | // things into explicit specializations. |
| 5686 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 5687 | // Preserve instantiation information. |
| 5688 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 5689 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 5690 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5691 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5692 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 5693 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 5694 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5695 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5696 | } |
| 5697 | |
| 5698 | Previous.clear(); |
| 5699 | Previous.addDecl(Instantiation); |
| 5700 | return false; |
| 5701 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5702 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5703 | // Make sure that this is a specialization of a member. |
| 5704 | if (!InstantiatedFrom) { |
| 5705 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 5706 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5707 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 5708 | return true; |
| 5709 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5710 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5711 | // C++ [temp.expl.spec]p6: |
| 5712 | // If a template, a member template or the member of a class template is |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5713 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5714 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5715 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5716 | // use occurs; no diagnostic is required. |
| 5717 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5718 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5719 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5720 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 5721 | TSK_ExplicitSpecialization, |
| 5722 | Instantiation, |
| 5723 | MSInfo->getTemplateSpecializationKind(), |
| 5724 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5725 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5726 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5727 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5728 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5729 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5730 | InstantiatedFrom, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5731 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5732 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5733 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 5734 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5735 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5736 | // the original declaration to note that it is an explicit specialization |
| 5737 | // (if it was previously an implicit instantiation). This latter step |
| 5738 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5739 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5740 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 5741 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 5742 | TSK_ImplicitInstantiation) { |
| 5743 | InstantiationFunction->setTemplateSpecializationKind( |
| 5744 | TSK_ExplicitSpecialization); |
| 5745 | InstantiationFunction->setLocation(Member->getLocation()); |
| 5746 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5747 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5748 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 5749 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5750 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5751 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5752 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5753 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 5754 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 5755 | TSK_ImplicitInstantiation) { |
| 5756 | InstantiationVar->setTemplateSpecializationKind( |
| 5757 | TSK_ExplicitSpecialization); |
| 5758 | InstantiationVar->setLocation(Member->getLocation()); |
| 5759 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5760 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5761 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 5762 | cast<VarDecl>(InstantiatedFrom), |
| 5763 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5764 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5765 | } else { |
| 5766 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5767 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 5768 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 5769 | TSK_ImplicitInstantiation) { |
| 5770 | InstantiationClass->setTemplateSpecializationKind( |
| 5771 | TSK_ExplicitSpecialization); |
| 5772 | InstantiationClass->setLocation(Member->getLocation()); |
| 5773 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5774 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5775 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5776 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5777 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5778 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5779 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5780 | // Save the caller the trouble of having to figure out which declaration |
| 5781 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5782 | Previous.clear(); |
| 5783 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5784 | return false; |
| 5785 | } |
| 5786 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5787 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5788 | /// |
| 5789 | /// \returns true if a serious error occurs, false otherwise. |
| 5790 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5791 | SourceLocation InstLoc, |
| 5792 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5793 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5794 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5795 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5796 | if (CurContext->isRecord()) { |
| 5797 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5798 | << D; |
| 5799 | return true; |
| 5800 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5801 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5802 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5803 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5804 | // template. If the name declared in the explicit instantiation is an |
| 5805 | // unqualified name, the explicit instantiation shall appear in the |
| 5806 | // namespace where its template is declared or, if that namespace is inline |
| 5807 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5808 | // |
| 5809 | // 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] | 5810 | if (WasQualifiedName) { |
| 5811 | if (CurContext->Encloses(OrigContext)) |
| 5812 | return false; |
| 5813 | } else { |
| 5814 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 5815 | return false; |
| 5816 | } |
| 5817 | |
| 5818 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 5819 | if (WasQualifiedName) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5820 | S.Diag(InstLoc, |
| 5821 | S.getLangOptions().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5822 | diag::err_explicit_instantiation_out_of_scope : |
| 5823 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5824 | << D << NS; |
| 5825 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5826 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5827 | S.getLangOptions().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5828 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 5829 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 5830 | << D << NS; |
| 5831 | } else |
| 5832 | S.Diag(InstLoc, |
| 5833 | S.getLangOptions().CPlusPlus0x? |
| 5834 | diag::err_explicit_instantiation_must_be_global : |
| 5835 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 5836 | << D; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5837 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5838 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5839 | } |
| 5840 | |
| 5841 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5842 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5843 | if (!SS.isSet()) |
| 5844 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5845 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5846 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5847 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5848 | // or a static data member of a class template specialization, the name of |
| 5849 | // the class template specialization in the qualified-id for the member |
| 5850 | // name shall be a simple-template-id. |
| 5851 | // |
| 5852 | // C++98 has the same restriction, just worded differently. |
| 5853 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5854 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5855 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5856 | if (isa<TemplateSpecializationType>(T)) |
| 5857 | return true; |
| 5858 | |
| 5859 | return false; |
| 5860 | } |
| 5861 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5862 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5863 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5864 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5865 | SourceLocation ExternLoc, |
| 5866 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5867 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5868 | SourceLocation KWLoc, |
| 5869 | const CXXScopeSpec &SS, |
| 5870 | TemplateTy TemplateD, |
| 5871 | SourceLocation TemplateNameLoc, |
| 5872 | SourceLocation LAngleLoc, |
| 5873 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5874 | SourceLocation RAngleLoc, |
| 5875 | AttributeList *Attr) { |
| 5876 | // Find the class template we're specializing |
| 5877 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5878 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5879 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5880 | |
| 5881 | // Check that the specialization uses the same tag kind as the |
| 5882 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5883 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5884 | assert(Kind != TTK_Enum && |
| 5885 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5886 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 5887 | Kind, /*isDefinition*/false, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5888 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5889 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5890 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5891 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5892 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5893 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5894 | diag::note_previous_use); |
| 5895 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5896 | } |
| 5897 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5898 | // C++0x [temp.explicit]p2: |
| 5899 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5900 | // definition and an explicit instantiation declaration. An explicit |
| 5901 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5902 | TemplateSpecializationKind TSK |
| 5903 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5904 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5905 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5906 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5907 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5908 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5909 | |
| 5910 | // Check that the template argument list is well-formed for this |
| 5911 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5912 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5913 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5914 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5915 | return true; |
| 5916 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5917 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5918 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5919 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5920 | // Find the class template specialization declaration that |
| 5921 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5922 | void *InsertPos = 0; |
| 5923 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5924 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5925 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5926 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5927 | TemplateSpecializationKind PrevDecl_TSK |
| 5928 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 5929 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5930 | // C++0x [temp.explicit]p2: |
| 5931 | // [...] An explicit instantiation shall appear in an enclosing |
| 5932 | // namespace of its template. [...] |
| 5933 | // |
| 5934 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5935 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 5936 | SS.isSet())) |
| 5937 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5938 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5939 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5940 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5941 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5942 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5943 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5944 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5945 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5946 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5947 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5948 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5949 | // Even though HasNoEffect == true means that this explicit instantiation |
| 5950 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 5951 | |
| 5952 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 5953 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5954 | // Since the only prior class template specialization with these |
| 5955 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5956 | // declaration node as our own, updating the source location |
| 5957 | // for the template name to reflect our new declaration. |
| 5958 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5959 | Specialization = PrevDecl; |
| 5960 | Specialization->setLocation(TemplateNameLoc); |
| 5961 | PrevDecl = 0; |
| 5962 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5963 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5964 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5965 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5966 | // Create a new class template specialization declaration node for |
| 5967 | // this explicit specialization. |
| 5968 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5969 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5970 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5971 | KWLoc, TemplateNameLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5972 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5973 | Converted.data(), |
| 5974 | Converted.size(), |
| 5975 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5976 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5977 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5978 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5979 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5980 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5981 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5982 | } |
| 5983 | |
| 5984 | // Build the fully-sugared type for this explicit instantiation as |
| 5985 | // the user wrote in the explicit instantiation itself. This means |
| 5986 | // that we'll pretty-print the type retrieved from the |
| 5987 | // specialization's declaration the way that the user actually wrote |
| 5988 | // the explicit instantiation, rather than formatting the name based |
| 5989 | // on the "canonical" representation used to store the template |
| 5990 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5991 | TypeSourceInfo *WrittenTy |
| 5992 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5993 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5994 | Context.getTypeDeclType(Specialization)); |
| 5995 | Specialization->setTypeAsWritten(WrittenTy); |
| 5996 | TemplateArgsIn.release(); |
| 5997 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5998 | // Set source locations for keywords. |
| 5999 | Specialization->setExternLoc(ExternLoc); |
| 6000 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 6001 | |
Rafael Espindola | 0257b7f | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 6002 | if (Attr) |
| 6003 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6004 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6005 | // Add the explicit instantiation into its lexical context. However, |
| 6006 | // since explicit instantiations are never found by name lookup, we |
| 6007 | // just put it into the declaration context directly. |
| 6008 | Specialization->setLexicalDeclContext(CurContext); |
| 6009 | CurContext->addDecl(Specialization); |
| 6010 | |
| 6011 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 6012 | if (HasNoEffect) { |
| 6013 | // Set the template specialization kind. |
| 6014 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6015 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 6016 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6017 | |
| 6018 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6019 | // A definition of a class template or class member template |
| 6020 | // shall be in scope at the point of the explicit instantiation of |
| 6021 | // the class template or class member template. |
| 6022 | // |
| 6023 | // This check comes when we actually try to perform the |
| 6024 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6025 | ClassTemplateSpecializationDecl *Def |
| 6026 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6027 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6028 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6029 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6030 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6031 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6032 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 6033 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6034 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6035 | // Instantiate the members of this class template specialization. |
| 6036 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6037 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6038 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 6039 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 6040 | |
| 6041 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 6042 | // TSK_ExplicitInstantiationDefinition |
| 6043 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 6044 | TSK == TSK_ExplicitInstantiationDefinition) |
| 6045 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6046 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6047 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6048 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6049 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6050 | // Set the template specialization kind. |
| 6051 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6052 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6053 | } |
| 6054 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6055 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6056 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6057 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 6058 | SourceLocation ExternLoc, |
| 6059 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6060 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6061 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6062 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6063 | IdentifierInfo *Name, |
| 6064 | SourceLocation NameLoc, |
| 6065 | AttributeList *Attr) { |
| 6066 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 6067 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6068 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6069 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6070 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6071 | /*ModulePrivateLoc=*/SourceLocation(), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6072 | MultiTemplateParamsArg(*this, 0, 0), |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame^] | 6073 | Owned, IsDependent, SourceLocation(), false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6074 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6075 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 6076 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6077 | if (!TagD) |
| 6078 | return true; |
| 6079 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6080 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6081 | if (Tag->isEnum()) { |
| 6082 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 6083 | << Context.getTypeDeclType(Tag); |
| 6084 | return true; |
| 6085 | } |
| 6086 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 6087 | if (Tag->isInvalidDecl()) |
| 6088 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6089 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6090 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 6091 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 6092 | if (!Pattern) { |
| 6093 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 6094 | << Context.getTypeDeclType(Record); |
| 6095 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 6096 | return true; |
| 6097 | } |
| 6098 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6099 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6100 | // If the explicit instantiation is for a class or member class, the |
| 6101 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6102 | // simple-template-id. |
| 6103 | // |
| 6104 | // C++98 has the same restriction, just worded differently. |
| 6105 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6106 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6107 | << Record << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6108 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6109 | // C++0x [temp.explicit]p2: |
| 6110 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6111 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6112 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 6113 | TemplateSpecializationKind TSK |
| 6114 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6115 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6116 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6117 | // C++0x [temp.explicit]p2: |
| 6118 | // [...] An explicit instantiation shall appear in an enclosing |
| 6119 | // namespace of its template. [...] |
| 6120 | // |
| 6121 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6122 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6123 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6124 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6125 | CXXRecordDecl *PrevDecl |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6126 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6127 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6128 | PrevDecl = Record; |
| 6129 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6130 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6131 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6132 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6133 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6134 | PrevDecl, |
| 6135 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6136 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6137 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6138 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6139 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6140 | return TagD; |
| 6141 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6142 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6143 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6144 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6145 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6146 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6147 | // 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] | 6148 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6149 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6150 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6151 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 6152 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 6153 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6154 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 6155 | << Pattern; |
| 6156 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6157 | } else { |
| 6158 | if (InstantiateClass(NameLoc, Record, Def, |
| 6159 | getTemplateInstantiationArgs(Record), |
| 6160 | TSK)) |
| 6161 | return true; |
| 6162 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6163 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6164 | if (!RecordDef) |
| 6165 | return true; |
| 6166 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6167 | } |
| 6168 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6169 | // Instantiate all of the members of the class. |
| 6170 | InstantiateClassMembers(NameLoc, RecordDef, |
| 6171 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6172 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6173 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 6174 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 6175 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6176 | // FIXME: We don't have any representation for explicit instantiations of |
| 6177 | // member classes. Such a representation is not needed for compilation, but it |
| 6178 | // should be available for clients that want to see all of the declarations in |
| 6179 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6180 | return TagD; |
| 6181 | } |
| 6182 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6183 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 6184 | SourceLocation ExternLoc, |
| 6185 | SourceLocation TemplateLoc, |
| 6186 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6187 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6188 | // TODO: check if/when DNInfo should replace Name. |
| 6189 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6190 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6191 | if (!Name) { |
| 6192 | if (!D.isInvalidType()) |
| 6193 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 6194 | diag::err_explicit_instantiation_requires_name) |
| 6195 | << D.getDeclSpec().getSourceRange() |
| 6196 | << D.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6197 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6198 | return true; |
| 6199 | } |
| 6200 | |
| 6201 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 6202 | // we find one that is. |
| 6203 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 6204 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 6205 | S = S->getParent(); |
| 6206 | |
| 6207 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6208 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 6209 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6210 | if (R.isNull()) |
| 6211 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6212 | |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6213 | // C++ [dcl.stc]p1: |
| 6214 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 6215 | // instantiation (14.7.2) directive. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6216 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6217 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 6218 | << Name; |
| 6219 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6220 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 6221 | != DeclSpec::SCS_unspecified) { |
| 6222 | // Complain about then remove the storage class specifier. |
| 6223 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 6224 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6225 | |
| 6226 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6227 | } |
| 6228 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6229 | // C++0x [temp.explicit]p1: |
| 6230 | // [...] An explicit instantiation of a function template shall not use the |
| 6231 | // inline or constexpr specifiers. |
| 6232 | // Presumably, this also applies to member functions of class templates as |
| 6233 | // well. |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6234 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6235 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6236 | getLangOptions().CPlusPlus0x ? |
| 6237 | diag::err_explicit_instantiation_inline : |
| 6238 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | fe6f648 | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 6239 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
| 6240 | if (D.getDeclSpec().isConstexprSpecified()) |
| 6241 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 6242 | // not already specified. |
| 6243 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 6244 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6245 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6246 | // C++0x [temp.explicit]p2: |
| 6247 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6248 | // definition and an explicit instantiation declaration. An explicit |
| 6249 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6250 | TemplateSpecializationKind TSK |
| 6251 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6252 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6253 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6254 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6255 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6256 | |
| 6257 | if (!R->isFunctionType()) { |
| 6258 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6259 | // A [...] static data member of a class template can be explicitly |
| 6260 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6261 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6262 | if (Previous.isAmbiguous()) |
| 6263 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6264 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6265 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6266 | if (!Prev || !Prev->isStaticDataMember()) { |
| 6267 | // We expect to see a data data member here. |
| 6268 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 6269 | << Name; |
| 6270 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6271 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6272 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6273 | return true; |
| 6274 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6275 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6276 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 6277 | // FIXME: Check for explicit specialization? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6278 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6279 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 6280 | << Prev; |
| 6281 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 6282 | // FIXME: Can we provide a note showing where this was declared? |
| 6283 | return true; |
| 6284 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6285 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6286 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6287 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6288 | // or a static data member of a class template specialization, the name of |
| 6289 | // the class template specialization in the qualified-id for the member |
| 6290 | // name shall be a simple-template-id. |
| 6291 | // |
| 6292 | // C++98 has the same restriction, just worded differently. |
| 6293 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6294 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6295 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6296 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6297 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6298 | // Check the scope of this explicit instantiation. |
| 6299 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6300 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6301 | // Verify that it is okay to explicitly instantiate here. |
| 6302 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 6303 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6304 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6305 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6306 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6307 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6308 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6309 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6310 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6311 | return (Decl*) 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6312 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6313 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6314 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6315 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6316 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6317 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6318 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6319 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6320 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6321 | |
| 6322 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6323 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6324 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6325 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6326 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 6327 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6328 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 6329 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6330 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 6331 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6332 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6333 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6334 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 6335 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6336 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6337 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6338 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6339 | // A [...] function [...] can be explicitly instantiated from its template. |
| 6340 | // A member function [...] of a class template can be explicitly |
| 6341 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6342 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6343 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6344 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6345 | P != PEnd; ++P) { |
| 6346 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6347 | if (!HasExplicitTemplateArgs) { |
| 6348 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 6349 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 6350 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6351 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6352 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6353 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 6354 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6355 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6356 | } |
| 6357 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6358 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6359 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 6360 | if (!FunTmpl) |
| 6361 | continue; |
| 6362 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6363 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6364 | FunctionDecl *Specialization = 0; |
| 6365 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6366 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6367 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6368 | R, Specialization, Info)) { |
| 6369 | // FIXME: Keep track of almost-matches? |
| 6370 | (void)TDK; |
| 6371 | continue; |
| 6372 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6373 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6374 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6375 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6376 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6377 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6378 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6379 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6380 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6381 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 6382 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 6383 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6384 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6385 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6386 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6387 | |
| 6388 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 6389 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6390 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6391 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6392 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6393 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 6394 | << Specialization |
| 6395 | << (Specialization->getTemplateSpecializationKind() == |
| 6396 | TSK_ExplicitSpecialization); |
| 6397 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 6398 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6399 | } |
| 6400 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6401 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6402 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 6403 | PrevDecl = Specialization; |
| 6404 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6405 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6406 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6407 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6408 | PrevDecl, |
| 6409 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6410 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6411 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6412 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6413 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6414 | // FIXME: We may still want to build some representation of this |
| 6415 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6416 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6417 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6418 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 6419 | |
| 6420 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 256fc4d | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 6421 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 6422 | if (Attr) |
| 6423 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6424 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6425 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6426 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6427 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6428 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6429 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6430 | // or a static data member of a class template specialization, the name of |
| 6431 | // the class template specialization in the qualified-id for the member |
| 6432 | // name shall be a simple-template-id. |
| 6433 | // |
| 6434 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6435 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6436 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6437 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6438 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6439 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6440 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6441 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6442 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6443 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6444 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6445 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6446 | D.getIdentifierLoc(), |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6447 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6448 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6449 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6450 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6451 | } |
| 6452 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6453 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6454 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 6455 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 6456 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 6457 | // This has to hold, because SS is expected to be defined. |
| 6458 | assert(Name && "Expected a name in a dependent tag"); |
| 6459 | |
| 6460 | NestedNameSpecifier *NNS |
| 6461 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6462 | if (!NNS) |
| 6463 | return true; |
| 6464 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6465 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 6466 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6467 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 6468 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6469 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6470 | return true; |
| 6471 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6472 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6473 | // Create the resulting type. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6474 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6475 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 6476 | |
| 6477 | // Create type-source location information for this type. |
| 6478 | TypeLocBuilder TLB; |
| 6479 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
| 6480 | TL.setKeywordLoc(TagLoc); |
| 6481 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6482 | TL.setNameLoc(NameLoc); |
| 6483 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6484 | } |
| 6485 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6486 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6487 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6488 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6489 | SourceLocation IdLoc) { |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6490 | if (SS.isInvalid()) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6491 | return true; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6492 | |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6493 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6494 | Diag(TypenameLoc, |
| 6495 | getLangOptions().CPlusPlus0x ? |
| 6496 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6497 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6498 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6499 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6500 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6501 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 6502 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 6503 | if (T.isNull()) |
| 6504 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6505 | |
| 6506 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6507 | if (isa<DependentNameType>(T)) { |
| 6508 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6509 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6510 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6511 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6512 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6513 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6514 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6515 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6516 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6517 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6518 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6519 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6520 | } |
| 6521 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6522 | TypeResult |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6523 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6524 | const CXXScopeSpec &SS, |
| 6525 | SourceLocation TemplateLoc, |
| 6526 | TemplateTy TemplateIn, |
| 6527 | SourceLocation TemplateNameLoc, |
| 6528 | SourceLocation LAngleLoc, |
| 6529 | ASTTemplateArgsPtr TemplateArgsIn, |
| 6530 | SourceLocation RAngleLoc) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6531 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6532 | Diag(TypenameLoc, |
| 6533 | getLangOptions().CPlusPlus0x ? |
| 6534 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6535 | diag::ext_typename_outside_of_template) |
| 6536 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6537 | |
| 6538 | // Translate the parser's template argument list in our AST format. |
| 6539 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 6540 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 6541 | |
| 6542 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6543 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 6544 | // Construct a dependent template specialization type. |
| 6545 | assert(DTN && "dependent template has non-dependent name?"); |
| 6546 | assert(DTN->getQualifier() |
| 6547 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 6548 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 6549 | DTN->getQualifier(), |
| 6550 | DTN->getIdentifier(), |
| 6551 | TemplateArgs); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6552 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6553 | // Create source-location information for this type. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6554 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6555 | DependentTemplateSpecializationTypeLoc SpecTL |
| 6556 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6557 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6558 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6559 | SpecTL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 6560 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6561 | SpecTL.setNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6562 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6563 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6564 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 6565 | } |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6566 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6567 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 6568 | if (T.isNull()) |
| 6569 | return true; |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6570 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6571 | // Provide source-location information for the template specialization |
| 6572 | // type. |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6573 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6574 | TemplateSpecializationTypeLoc SpecTL |
| 6575 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
| 6576 | |
| 6577 | // FIXME: No place to set the location of the 'template' keyword! |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6578 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6579 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6580 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6581 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6582 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 6583 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6584 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 6585 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 6586 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6587 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6588 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6589 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 6590 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 6591 | } |
| 6592 | |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6593 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6594 | /// \brief Build the type that describes a C++ typename specifier, |
| 6595 | /// e.g., "typename T::type". |
| 6596 | QualType |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6597 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 6598 | SourceLocation KeywordLoc, |
| 6599 | NestedNameSpecifierLoc QualifierLoc, |
| 6600 | const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6601 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6602 | CXXScopeSpec SS; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6603 | SS.Adopt(QualifierLoc); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6604 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6605 | DeclContext *Ctx = computeDeclContext(SS); |
| 6606 | if (!Ctx) { |
| 6607 | // If the nested-name-specifier is dependent and couldn't be |
| 6608 | // resolved to a type, build a typename type. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6609 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 6610 | return Context.getDependentNameType(Keyword, |
| 6611 | QualifierLoc.getNestedNameSpecifier(), |
| 6612 | &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 6613 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6614 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6615 | // If the nested-name-specifier refers to the current instantiation, |
| 6616 | // the "typename" keyword itself is superfluous. In C++03, the |
| 6617 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 6618 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 6619 | // 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] | 6620 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6621 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 6622 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6623 | |
| 6624 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6625 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6626 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6627 | unsigned DiagID = 0; |
| 6628 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6629 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6630 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6631 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6632 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6633 | |
| 6634 | case LookupResult::FoundUnresolvedValue: { |
| 6635 | // We found a using declaration that is a value. Most likely, the using |
| 6636 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6637 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6638 | IILoc); |
| 6639 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 6640 | << Name << Ctx << FullRange; |
| 6641 | if (UnresolvedUsingValueDecl *Using |
| 6642 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6643 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6644 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 6645 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 6646 | } |
| 6647 | } |
| 6648 | // Fall through to create a dependent typename type, from which we can recover |
| 6649 | // better. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6650 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 6651 | case LookupResult::NotFoundInCurrentInstantiation: |
| 6652 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6653 | return Context.getDependentNameType(Keyword, |
| 6654 | QualifierLoc.getNestedNameSpecifier(), |
| 6655 | &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6656 | |
| 6657 | case LookupResult::Found: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6658 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6659 | // We found a type. Build an ElaboratedType, since the |
| 6660 | // typename-specifier was just sugar. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6661 | return Context.getElaboratedType(ETK_Typename, |
| 6662 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6663 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6664 | } |
| 6665 | |
| 6666 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6667 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6668 | break; |
| 6669 | |
| 6670 | case LookupResult::FoundOverloaded: |
| 6671 | DiagID = diag::err_typename_nested_not_type; |
| 6672 | Referenced = *Result.begin(); |
| 6673 | break; |
| 6674 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 6675 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6676 | return QualType(); |
| 6677 | } |
| 6678 | |
| 6679 | // If we get here, it's because name lookup did not find a |
| 6680 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6681 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6682 | IILoc); |
| 6683 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6684 | if (Referenced) |
| 6685 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 6686 | << Name; |
| 6687 | return QualType(); |
| 6688 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6689 | |
| 6690 | namespace { |
| 6691 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 6692 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6693 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6694 | SourceLocation Loc; |
| 6695 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6696 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6697 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 6698 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6699 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6700 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6701 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6702 | DeclarationName Entity) |
| 6703 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6704 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6705 | |
| 6706 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6707 | /// transformed. |
| 6708 | /// |
| 6709 | /// For the purposes of type reconstruction, a type has already been |
| 6710 | /// transformed if it is NULL or if it is not dependent. |
| 6711 | bool AlreadyTransformed(QualType T) { |
| 6712 | return T.isNull() || !T->isDependentType(); |
| 6713 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6714 | |
| 6715 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6716 | /// rebuilt. |
| 6717 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6718 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6719 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 6720 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6721 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6722 | /// \brief Sets the "base" location and entity when that |
| 6723 | /// information is known based on another transformation. |
| 6724 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 6725 | this->Loc = Loc; |
| 6726 | this->Entity = Entity; |
| 6727 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6728 | }; |
| 6729 | } |
| 6730 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6731 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 6732 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6733 | /// 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] | 6734 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6735 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6736 | /// partial specialization thereof). This routine will rebuild that type now |
| 6737 | /// that we have entered the declarator's scope, which may produce different |
| 6738 | /// canonical types, e.g., |
| 6739 | /// |
| 6740 | /// \code |
| 6741 | /// template<typename T> |
| 6742 | /// struct X { |
| 6743 | /// typedef T* pointer; |
| 6744 | /// pointer data(); |
| 6745 | /// }; |
| 6746 | /// |
| 6747 | /// template<typename T> |
| 6748 | /// typename X<T>::pointer X<T>::data() { ... } |
| 6749 | /// \endcode |
| 6750 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 6751 | /// 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] | 6752 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 6753 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6754 | /// 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] | 6755 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 6756 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6757 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 6758 | SourceLocation Loc, |
| 6759 | DeclarationName Name) { |
| 6760 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6761 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6762 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6763 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 6764 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 6765 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6766 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6767 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6768 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 6769 | DeclarationName()); |
| 6770 | return Rebuilder.TransformExpr(E); |
| 6771 | } |
| 6772 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6773 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6774 | if (SS.isInvalid()) |
| 6775 | return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6776 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6777 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6778 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 6779 | DeclarationName()); |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6780 | NestedNameSpecifierLoc Rebuilt |
| 6781 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 6782 | if (!Rebuilt) |
| 6783 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6784 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6785 | SS.Adopt(Rebuilt); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6786 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6787 | } |
| 6788 | |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 6789 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 6790 | /// instantiation. |
| 6791 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 6792 | TemplateParameterList *Params) { |
| 6793 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 6794 | Decl *Param = Params->getParam(I); |
| 6795 | |
| 6796 | // There is nothing to rebuild in a type parameter. |
| 6797 | if (isa<TemplateTypeParmDecl>(Param)) |
| 6798 | continue; |
| 6799 | |
| 6800 | // Rebuild the template parameter list of a template template parameter. |
| 6801 | if (TemplateTemplateParmDecl *TTP |
| 6802 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 6803 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 6804 | TTP->getTemplateParameters())) |
| 6805 | return true; |
| 6806 | |
| 6807 | continue; |
| 6808 | } |
| 6809 | |
| 6810 | // Rebuild the type of a non-type template parameter. |
| 6811 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 6812 | TypeSourceInfo *NewTSI |
| 6813 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 6814 | NTTP->getLocation(), |
| 6815 | NTTP->getDeclName()); |
| 6816 | if (!NewTSI) |
| 6817 | return true; |
| 6818 | |
| 6819 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 6820 | NTTP->setTypeSourceInfo(NewTSI); |
| 6821 | NTTP->setType(NewTSI->getType()); |
| 6822 | } |
| 6823 | } |
| 6824 | |
| 6825 | return false; |
| 6826 | } |
| 6827 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6828 | /// \brief Produces a formatted string that describes the binding of |
| 6829 | /// template parameters to template arguments. |
| 6830 | std::string |
| 6831 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6832 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6833 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6834 | } |
| 6835 | |
| 6836 | std::string |
| 6837 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6838 | const TemplateArgument *Args, |
| 6839 | unsigned NumArgs) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6840 | llvm::SmallString<128> Str; |
| 6841 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6842 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6843 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6844 | return std::string(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6845 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6846 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6847 | if (I >= NumArgs) |
| 6848 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6849 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6850 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6851 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6852 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6853 | Out << ", "; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6854 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6855 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6856 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6857 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6858 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6859 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6860 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6861 | Out << " = "; |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6862 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6863 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6864 | |
| 6865 | Out << ']'; |
| 6866 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6867 | } |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 6868 | |
| 6869 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) { |
| 6870 | if (!FD) |
| 6871 | return; |
| 6872 | FD->setLateTemplateParsed(Flag); |
| 6873 | } |
| 6874 | |
| 6875 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 6876 | DeclContext *DC = CurContext; |
| 6877 | |
| 6878 | while (DC) { |
| 6879 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 6880 | const FunctionDecl *FD = RD->isLocalClass(); |
| 6881 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 6882 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 6883 | return false; |
| 6884 | |
| 6885 | DC = DC->getParent(); |
| 6886 | } |
| 6887 | return false; |
| 6888 | } |