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 |
| 95 | // is followed by a template-argument-list, the reference refers to the |
| 96 | // class template itself and not a specialization thereof, and is not |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 97 | // ambiguous. |
| 98 | // |
| 99 | // FIXME: Will we eventually have to do the same for alias templates? |
| 100 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
| 101 | if (!ClassTemplates.insert(ClassTmpl)) { |
| 102 | filter.erase(); |
| 103 | continue; |
| 104 | } |
John McCall | 8ba6691 | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 105 | |
| 106 | // FIXME: we promote access to public here as a workaround to |
| 107 | // the fact that LookupResult doesn't let us remember that we |
| 108 | // found this template through a particular injected class name, |
| 109 | // which means we end up doing nasty things to the invariants. |
| 110 | // Pretending that access is public is *much* safer. |
| 111 | filter.replace(Repl, AS_public); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 112 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 113 | } |
| 114 | filter.done(); |
| 115 | } |
| 116 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 117 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) { |
| 118 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
| 119 | if (isAcceptableTemplateName(Context, *I)) |
| 120 | return true; |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 125 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 126 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 127 | bool hasTemplateKeyword, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 128 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 129 | ParsedType ObjectTypePtr, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 130 | bool EnteringContext, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 131 | TemplateTy &TemplateResult, |
| 132 | bool &MemberOfUnknownSpecialization) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 133 | assert(getLangOptions().CPlusPlus && "No template names in C!"); |
| 134 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 135 | DeclarationName TName; |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 136 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 137 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 138 | switch (Name.getKind()) { |
| 139 | case UnqualifiedId::IK_Identifier: |
| 140 | TName = DeclarationName(Name.Identifier); |
| 141 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 142 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 143 | case UnqualifiedId::IK_OperatorFunctionId: |
| 144 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 145 | Name.OperatorFunctionId.Operator); |
| 146 | break; |
| 147 | |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 148 | case UnqualifiedId::IK_LiteralOperatorId: |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 149 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 150 | break; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 151 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 152 | default: |
| 153 | return TNK_Non_template; |
| 154 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 156 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 158 | LookupResult R(*this, TName, Name.getSourceRange().getBegin(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 159 | LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 160 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 161 | MemberOfUnknownSpecialization); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 162 | if (R.empty()) return TNK_Non_template; |
| 163 | if (R.isAmbiguous()) { |
| 164 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 165 | R.suppressDiagnostics(); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 166 | |
| 167 | // FIXME: we might have ambiguous templates, in which case we |
| 168 | // should at least parse them properly! |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 169 | return TNK_Non_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 170 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 171 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 172 | TemplateName Template; |
| 173 | TemplateNameKind TemplateKind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 175 | unsigned ResultCount = R.end() - R.begin(); |
| 176 | if (ResultCount > 1) { |
| 177 | // We assume that we'll preserve the qualifier from a function |
| 178 | // template name in other ways. |
| 179 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 180 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 181 | |
| 182 | // We'll do this lookup again later. |
| 183 | R.suppressDiagnostics(); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 184 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 185 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 186 | |
| 187 | if (SS.isSet() && !SS.isInvalid()) { |
| 188 | NestedNameSpecifier *Qualifier |
| 189 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 190 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 191 | hasTemplateKeyword, TD); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 192 | } else { |
| 193 | Template = TemplateName(TD); |
| 194 | } |
| 195 | |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 196 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 197 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 198 | |
| 199 | // We'll do this lookup again later. |
| 200 | R.suppressDiagnostics(); |
| 201 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 202 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD)); |
| 203 | TemplateKind = TNK_Type_template; |
| 204 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 205 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 207 | TemplateResult = TemplateTy::make(Template); |
| 208 | return TemplateKind; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 209 | } |
| 210 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 211 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 212 | SourceLocation IILoc, |
| 213 | Scope *S, |
| 214 | const CXXScopeSpec *SS, |
| 215 | TemplateTy &SuggestedTemplate, |
| 216 | TemplateNameKind &SuggestedKind) { |
| 217 | // We can't recover unless there's a dependent scope specifier preceding the |
| 218 | // template name. |
Douglas Gregor | d5ab9b0 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 219 | // FIXME: Typo correction? |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 220 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 221 | computeDeclContext(*SS)) |
| 222 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 223 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 224 | // The code is missing a 'template' keyword prior to the dependent template |
| 225 | // name. |
| 226 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 227 | Diag(IILoc, diag::err_template_kw_missing) |
| 228 | << Qualifier << II.getName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 229 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 230 | SuggestedTemplate |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 231 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 232 | SuggestedKind = TNK_Dependent_template_name; |
| 233 | return true; |
| 234 | } |
| 235 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 236 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 237 | Scope *S, CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 238 | QualType ObjectType, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 239 | bool EnteringContext, |
| 240 | bool &MemberOfUnknownSpecialization) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 241 | // Determine where to perform name lookup |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 242 | MemberOfUnknownSpecialization = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 243 | DeclContext *LookupCtx = 0; |
| 244 | bool isDependent = false; |
| 245 | if (!ObjectType.isNull()) { |
| 246 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 247 | // x->B::f, and we are looking into the type of the object. |
| 248 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 249 | LookupCtx = computeDeclContext(ObjectType); |
| 250 | isDependent = ObjectType->isDependentType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 251 | assert((isDependent || !ObjectType->isIncompleteType()) && |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 252 | "Caller should have completed object type"); |
| 253 | } else if (SS.isSet()) { |
| 254 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 255 | // so long into the context associated with the prior nested-name-specifier. |
| 256 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 257 | isDependent = isDependentScopeSpecifier(SS); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 258 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 259 | // The declaration context must be complete. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 260 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 261 | return; |
| 262 | } |
| 263 | |
| 264 | bool ObjectTypeSearchedInScope = false; |
| 265 | if (LookupCtx) { |
| 266 | // Perform "qualified" name lookup into the declaration context we |
| 267 | // computed, which is either the type of the base of a member access |
| 268 | // expression or the declaration context associated with a prior |
| 269 | // nested-name-specifier. |
| 270 | LookupQualifiedName(Found, LookupCtx); |
| 271 | |
| 272 | if (!ObjectType.isNull() && Found.empty()) { |
| 273 | // C++ [basic.lookup.classref]p1: |
| 274 | // In a class member access expression (5.2.5), if the . or -> token is |
| 275 | // immediately followed by an identifier followed by a <, the |
| 276 | // identifier must be looked up to determine whether the < is the |
| 277 | // beginning of a template argument list (14.2) or a less-than operator. |
| 278 | // The identifier is first looked up in the class of the object |
| 279 | // expression. If the identifier is not found, it is then looked up in |
| 280 | // the context of the entire postfix-expression and shall name a class |
| 281 | // or function template. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 282 | if (S) LookupName(Found, S); |
| 283 | ObjectTypeSearchedInScope = true; |
| 284 | } |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 285 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 286 | // We cannot look into a dependent object type or nested nme |
| 287 | // specifier. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 288 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 289 | return; |
| 290 | } else { |
| 291 | // Perform unqualified name lookup in the current scope. |
| 292 | LookupName(Found, S); |
| 293 | } |
| 294 | |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 295 | if (Found.empty() && !isDependent) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 296 | // If we did not find any names, attempt to correct any typos. |
| 297 | DeclarationName Name = Found.getLookupName(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 298 | if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx, |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 299 | false, CTC_CXXCasts)) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 300 | FilterAcceptableTemplateNames(Found); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 301 | if (!Found.empty()) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 302 | if (LookupCtx) |
| 303 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) |
| 304 | << Name << LookupCtx << Found.getLookupName() << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 305 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 306 | Found.getLookupName().getAsString()); |
| 307 | else |
| 308 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) |
| 309 | << Name << Found.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 310 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 311 | Found.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 312 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) |
| 313 | Diag(Template->getLocation(), diag::note_previous_decl) |
| 314 | << Template->getDeclName(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 315 | } |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 316 | } else { |
| 317 | Found.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 318 | Found.setLookupName(Name); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 322 | FilterAcceptableTemplateNames(Found); |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 323 | if (Found.empty()) { |
| 324 | if (isDependent) |
| 325 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 326 | return; |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 327 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 328 | |
| 329 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
| 330 | // C++ [basic.lookup.classref]p1: |
| 331 | // [...] If the lookup in the class of the object expression finds a |
| 332 | // template, the name is also looked up in the context of the entire |
| 333 | // postfix-expression and [...] |
| 334 | // |
| 335 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 336 | LookupOrdinaryName); |
| 337 | LookupName(FoundOuter, S); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 338 | FilterAcceptableTemplateNames(FoundOuter); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 339 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 340 | if (FoundOuter.empty()) { |
| 341 | // - if the name is not found, the name found in the class of the |
| 342 | // object expression is used, otherwise |
| 343 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) { |
| 344 | // - if the name is found in the context of the entire |
| 345 | // postfix-expression and does not name a class template, the name |
| 346 | // found in the class of the object expression is used, otherwise |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 347 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 348 | // - if the name found is a class template, it must refer to the same |
| 349 | // entity as the one found in the class of the object expression, |
| 350 | // otherwise the program is ill-formed. |
| 351 | if (!Found.isSingleResult() || |
| 352 | Found.getFoundDecl()->getCanonicalDecl() |
| 353 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 354 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 21d07e4 | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 355 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 356 | << Found.getLookupName() |
| 357 | << ObjectType; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 358 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 359 | diag::note_ambig_member_ref_object_type) |
| 360 | << ObjectType; |
| 361 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 362 | diag::note_ambig_member_ref_scope); |
| 363 | |
| 364 | // Recover by taking the template that we found in the object |
| 365 | // expression's type. |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 371 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 372 | /// was just parsed. This is only possible with an explicit scope |
| 373 | /// specifier naming a dependent type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 374 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 375 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 376 | const DeclarationNameInfo &NameInfo, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 377 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 378 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 379 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 380 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 381 | if (!isAddressOfOperand && |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 382 | isa<CXXMethodDecl>(DC) && |
| 383 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 384 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 385 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 386 | // Since the 'this' expression is synthesized, we don't need to |
| 387 | // perform the double-lookup check. |
| 388 | NamedDecl *FirstQualifierInScope = 0; |
| 389 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 390 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 391 | /*This*/ 0, ThisType, |
| 392 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 393 | /*Op*/ SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 394 | SS.getWithLocInContext(Context), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 395 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 396 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 397 | TemplateArgs)); |
| 398 | } |
| 399 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 400 | return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 401 | } |
| 402 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 403 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 404 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 405 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 406 | const TemplateArgumentListInfo *TemplateArgs) { |
| 407 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 408 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 409 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 410 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 413 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 414 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 415 | /// declaration at location Loc. Returns true to indicate that this is |
| 416 | /// an error, and false otherwise. |
| 417 | bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 418 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 419 | |
| 420 | // Microsoft Visual C++ permits template parameters to be shadowed. |
| 421 | if (getLangOptions().Microsoft) |
| 422 | return false; |
| 423 | |
| 424 | // C++ [temp.local]p4: |
| 425 | // A template-parameter shall not be redeclared within its |
| 426 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 428 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 429 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
| 430 | return true; |
| 431 | } |
| 432 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 433 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 434 | /// the parameter D to reference the templated declaration and return a pointer |
| 435 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 436 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 437 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 438 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 439 | return Temp; |
| 440 | } |
| 441 | return 0; |
| 442 | } |
| 443 | |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 444 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 445 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 446 | assert(Kind == Template && |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 447 | "Only template template arguments can be pack expansions here"); |
| 448 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 449 | "Template template argument pack expansion without packs"); |
| 450 | ParsedTemplateArgument Result(*this); |
| 451 | Result.EllipsisLoc = EllipsisLoc; |
| 452 | return Result; |
| 453 | } |
| 454 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 455 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 456 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 457 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 458 | switch (Arg.getKind()) { |
| 459 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 460 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 461 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 462 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 463 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 464 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 465 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 466 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 467 | case ParsedTemplateArgument::NonType: { |
| 468 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 469 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 470 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 471 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 472 | case ParsedTemplateArgument::Template: { |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 473 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 474 | TemplateArgument TArg; |
| 475 | if (Arg.getEllipsisLoc().isValid()) |
| 476 | TArg = TemplateArgument(Template, llvm::Optional<unsigned int>()); |
| 477 | else |
| 478 | TArg = Template; |
| 479 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 480 | Arg.getScopeSpec().getWithLocInContext( |
| 481 | SemaRef.Context), |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 482 | Arg.getLocation(), |
| 483 | Arg.getEllipsisLoc()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 486 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 487 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 488 | return TemplateArgumentLoc(); |
| 489 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 490 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 491 | /// \brief Translates template arguments as provided by the parser |
| 492 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 493 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 494 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 495 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 496 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 497 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 498 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 499 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 500 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 501 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 502 | /// the keyword "typename" was used to declare the type parameter |
| 503 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 504 | /// "class" or "typename" keyword. ParamName is the name of the |
| 505 | /// parameter (NULL indicates an unnamed template parameter) and |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 506 | /// ParamName is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 507 | /// If the type parameter has a default argument, it will be added |
| 508 | /// later via ActOnTypeParameterDefault. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 509 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
| 510 | SourceLocation EllipsisLoc, |
| 511 | SourceLocation KeyLoc, |
| 512 | IdentifierInfo *ParamName, |
| 513 | SourceLocation ParamNameLoc, |
| 514 | unsigned Depth, unsigned Position, |
| 515 | SourceLocation EqualLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 516 | ParsedType DefaultArg) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | assert(S->isTemplateParamScope() && |
| 518 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 519 | bool Invalid = false; |
| 520 | |
| 521 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 522 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 523 | LookupOrdinaryName, |
| 524 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 525 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 526 | Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 530 | SourceLocation Loc = ParamNameLoc; |
| 531 | if (!ParamName) |
| 532 | Loc = KeyLoc; |
| 533 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 534 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 535 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 536 | KeyLoc, Loc, Depth, Position, ParamName, |
| 537 | Typename, Ellipsis); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 538 | Param->setAccess(AS_public); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 539 | if (Invalid) |
| 540 | Param->setInvalidDecl(); |
| 541 | |
| 542 | if (ParamName) { |
| 543 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 544 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 545 | IdResolver.AddDecl(Param); |
| 546 | } |
| 547 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 548 | // C++0x [temp.param]p9: |
| 549 | // A default template-argument may be specified for any kind of |
| 550 | // template-parameter that is not a template parameter pack. |
| 551 | if (DefaultArg && Ellipsis) { |
| 552 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 553 | DefaultArg = ParsedType(); |
| 554 | } |
| 555 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 556 | // Handle the default argument, if provided. |
| 557 | if (DefaultArg) { |
| 558 | TypeSourceInfo *DefaultTInfo; |
| 559 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 560 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 561 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 562 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 563 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 564 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 565 | UPPC_DefaultArgument)) |
| 566 | return Param; |
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 | // Check the template argument itself. |
| 569 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 570 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 571 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 572 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 573 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 574 | Param->setDefaultArgument(DefaultTInfo, false); |
| 575 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 576 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 577 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 580 | /// \brief Check that the type of a non-type template parameter is |
| 581 | /// well-formed. |
| 582 | /// |
| 583 | /// \returns the (possibly-promoted) parameter type if valid; |
| 584 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 585 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 586 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 587 | // We don't allow variably-modified types as the type of non-type template |
| 588 | // parameters. |
| 589 | if (T->isVariablyModifiedType()) { |
| 590 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 591 | << T; |
| 592 | return QualType(); |
| 593 | } |
| 594 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 595 | // C++ [temp.param]p4: |
| 596 | // |
| 597 | // A non-type template-parameter shall have one of the following |
| 598 | // (optionally cv-qualified) types: |
| 599 | // |
| 600 | // -- integral or enumeration type, |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 601 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 602 | // -- pointer to object or pointer to function, |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 603 | T->isPointerType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 604 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 605 | T->isReferenceType() || |
| 606 | // -- pointer to member. |
| 607 | T->isMemberPointerType() || |
| 608 | // If T is a dependent type, we can't do the check now, so we |
| 609 | // assume that it is well-formed. |
| 610 | T->isDependentType()) |
| 611 | return T; |
| 612 | // C++ [temp.param]p8: |
| 613 | // |
| 614 | // A non-type template-parameter of type "array of T" or |
| 615 | // "function returning T" is adjusted to be of type "pointer to |
| 616 | // T" or "pointer to function returning T", respectively. |
| 617 | else if (T->isArrayType()) |
| 618 | // FIXME: Keep the type prior to promotion? |
| 619 | return Context.getArrayDecayedType(T); |
| 620 | else if (T->isFunctionType()) |
| 621 | // FIXME: Keep the type prior to promotion? |
| 622 | return Context.getPointerType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 623 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 624 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 625 | << T; |
| 626 | |
| 627 | return QualType(); |
| 628 | } |
| 629 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 630 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 631 | unsigned Depth, |
| 632 | unsigned Position, |
| 633 | SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 634 | Expr *Default) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 635 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 636 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 637 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 638 | assert(S->isTemplateParamScope() && |
| 639 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 640 | bool Invalid = false; |
| 641 | |
| 642 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 643 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 644 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 645 | LookupOrdinaryName, |
| 646 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 647 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 648 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 649 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 652 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 653 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 654 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 655 | Invalid = true; |
| 656 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 657 | |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 658 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 659 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 660 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 661 | D.getSourceRange().getBegin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 662 | D.getIdentifierLoc(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 663 | Depth, Position, ParamName, T, |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 664 | IsParameterPack, TInfo); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 665 | Param->setAccess(AS_public); |
| 666 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 667 | if (Invalid) |
| 668 | Param->setInvalidDecl(); |
| 669 | |
| 670 | if (D.getIdentifier()) { |
| 671 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 672 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 673 | IdResolver.AddDecl(Param); |
| 674 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 675 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 676 | // C++0x [temp.param]p9: |
| 677 | // A default template-argument may be specified for any kind of |
| 678 | // template-parameter that is not a template parameter pack. |
| 679 | if (Default && IsParameterPack) { |
| 680 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 681 | Default = 0; |
| 682 | } |
| 683 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 684 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 685 | if (Default) { |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 686 | // Check for unexpanded parameter packs. |
| 687 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 688 | return Param; |
| 689 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 690 | TemplateArgument Converted; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 691 | ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
| 692 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 693 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 694 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 695 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 696 | Default = DefaultRes.take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 697 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 698 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 699 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 700 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 701 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 702 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 703 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 704 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 705 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 706 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 707 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 708 | SourceLocation TmpLoc, |
| 709 | TemplateParamsTy *Params, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 710 | SourceLocation EllipsisLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 711 | IdentifierInfo *Name, |
| 712 | SourceLocation NameLoc, |
| 713 | unsigned Depth, |
| 714 | unsigned Position, |
| 715 | SourceLocation EqualLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 716 | ParsedTemplateArgument Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 717 | assert(S->isTemplateParamScope() && |
| 718 | "Template template parameter not in template parameter scope!"); |
| 719 | |
| 720 | // Construct the parameter object. |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 721 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 722 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 723 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 724 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 725 | Depth, Position, IsParameterPack, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 726 | Name, Params); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 727 | Param->setAccess(AS_public); |
| 728 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 729 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 730 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 731 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 732 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 733 | IdResolver.AddDecl(Param); |
| 734 | } |
| 735 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 736 | if (Params->size() == 0) { |
| 737 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 738 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 739 | Param->setInvalidDecl(); |
| 740 | } |
| 741 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 742 | // C++0x [temp.param]p9: |
| 743 | // A default template-argument may be specified for any kind of |
| 744 | // template-parameter that is not a template parameter pack. |
| 745 | if (IsParameterPack && !Default.isInvalid()) { |
| 746 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 747 | Default = ParsedTemplateArgument(); |
| 748 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 749 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 750 | if (!Default.isInvalid()) { |
| 751 | // Check only that we have a template template argument. We don't want to |
| 752 | // try to check well-formedness now, because our template template parameter |
| 753 | // might have dependent types in its template parameters, which we wouldn't |
| 754 | // be able to match now. |
| 755 | // |
| 756 | // If none of the template template parameter's template arguments mention |
| 757 | // other template parameters, we could actually perform more checking here. |
| 758 | // However, it isn't worth doing. |
| 759 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 760 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 761 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 762 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 763 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 764 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 765 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 766 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 767 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 768 | DefaultArg.getArgument().getAsTemplate(), |
| 769 | UPPC_DefaultArgument)) |
| 770 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 771 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 772 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 773 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 774 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 775 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 778 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 779 | /// contains the template parameters in Params/NumParams. |
| 780 | Sema::TemplateParamsTy * |
| 781 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 782 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 784 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 785 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 786 | SourceLocation RAngleLoc) { |
| 787 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 788 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 789 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 790 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 791 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 792 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 793 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 794 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 795 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 796 | if (SS.isSet()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 797 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 798 | } |
| 799 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 800 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 801 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 802 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 803 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 804 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 805 | TemplateParameterList *TemplateParams, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 806 | AccessSpecifier AS, |
| 807 | unsigned NumOuterTemplateParamLists, |
| 808 | TemplateParameterList** OuterTemplateParamLists) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 809 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 810 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 811 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 812 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 813 | |
| 814 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 815 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 816 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 817 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 818 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 819 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 820 | |
| 821 | // There is no such thing as an unnamed class template. |
| 822 | if (!Name) { |
| 823 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 824 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 828 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 829 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 830 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 831 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 832 | SemanticContext = computeDeclContext(SS, true); |
| 833 | if (!SemanticContext) { |
| 834 | // FIXME: Produce a reasonable diagnostic here |
| 835 | return true; |
| 836 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 837 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 838 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 839 | return true; |
| 840 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 841 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 842 | } else { |
| 843 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 844 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 845 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 846 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 847 | if (Previous.isAmbiguous()) |
| 848 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 849 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 850 | NamedDecl *PrevDecl = 0; |
| 851 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 852 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 853 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 854 | // If there is a previous declaration with the same name, check |
| 855 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 856 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 857 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 858 | |
| 859 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 860 | // class template partial specialization, or class template specialization. |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 861 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 862 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 863 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 864 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 865 | PrevClassTemplate |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 866 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 867 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 868 | PrevClassTemplate |
| 869 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 870 | ->getSpecializedTemplate(); |
| 871 | } |
| 872 | } |
| 873 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 874 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 875 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 876 | // [...] When looking for a prior declaration of a class or a function |
| 877 | // 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] | 878 | // function is neither a qualified name nor a template-id, scopes outside |
| 879 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 880 | if (!SS.isSet()) { |
| 881 | DeclContext *OutermostContext = CurContext; |
| 882 | while (!OutermostContext->isFileContext()) |
| 883 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 884 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 885 | if (PrevDecl && |
| 886 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 887 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 888 | SemanticContext = PrevDecl->getDeclContext(); |
| 889 | } else { |
| 890 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 891 | // context we computed is the semantic context for our new |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 892 | // declaration. |
| 893 | PrevDecl = PrevClassTemplate = 0; |
| 894 | SemanticContext = OutermostContext; |
| 895 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 896 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 897 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 898 | if (CurContext->isDependentContext()) { |
| 899 | // If this is a dependent context, we don't want to link the friend |
| 900 | // class template to the template in scope, because that would perform |
| 901 | // checking of the template parameter lists that can't be performed |
| 902 | // until the outer context is instantiated. |
| 903 | PrevDecl = PrevClassTemplate = 0; |
| 904 | } |
| 905 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 906 | PrevDecl = PrevClassTemplate = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 907 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 908 | if (PrevClassTemplate) { |
| 909 | // Ensure that the template parameter lists are compatible. |
| 910 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 911 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 912 | /*Complain=*/true, |
| 913 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 914 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 915 | |
| 916 | // C++ [temp.class]p4: |
| 917 | // In a redeclaration, partial specialization, explicit |
| 918 | // specialization or explicit instantiation of a class template, |
| 919 | // the class-key shall agree in kind with the original class |
| 920 | // template declaration (7.1.5.3). |
| 921 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 922 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 923 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 924 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 925 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 926 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 927 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 930 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 931 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 932 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 933 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 934 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 935 | // FIXME: Would it make sense to try to "forget" the previous |
| 936 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 937 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 938 | } |
| 939 | } |
| 940 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 941 | // Maybe we will complain about the shadowed template parameter. |
| 942 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 943 | // Just pretend that we didn't see the previous declaration. |
| 944 | PrevDecl = 0; |
| 945 | } else if (PrevDecl) { |
| 946 | // C++ [temp]p5: |
| 947 | // A class template shall not have the same name as any other |
| 948 | // template, class, function, object, enumeration, enumerator, |
| 949 | // namespace, or type in the same scope (3.3), except as specified |
| 950 | // in (14.5.4). |
| 951 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 952 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 953 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 956 | // Check the template parameter list of this declaration, possibly |
| 957 | // merging in the template parameter list from the previous class |
| 958 | // template declaration. |
| 959 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 960 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 961 | (SS.isSet() && SemanticContext && |
Douglas Gregor | 461bf2e | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 962 | SemanticContext->isRecord() && |
| 963 | SemanticContext->isDependentContext()) |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 964 | ? TPC_ClassTemplateMember |
| 965 | : TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 966 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 968 | if (SS.isSet()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 969 | // 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] | 970 | // template out-of-line. |
| 971 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
| 972 | !(TUK == TUK_Friend && CurContext->isDependentContext())) |
| 973 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 974 | << Name << SemanticContext << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | CXXRecordDecl *NewClass = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 978 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 980 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 981 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 982 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 983 | if (NumOuterTemplateParamLists > 0) |
| 984 | NewClass->setTemplateParameterListsInfo(Context, |
| 985 | NumOuterTemplateParamLists, |
| 986 | OuterTemplateParamLists); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 987 | |
| 988 | ClassTemplateDecl *NewTemplate |
| 989 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 990 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 991 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 992 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 993 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 994 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 995 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 996 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 997 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 998 | (void)T; |
| 999 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1000 | // 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] | 1001 | // class template, make a note of that. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1002 | if (PrevClassTemplate && |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1003 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1004 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1005 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1006 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1007 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1008 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1009 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1010 | // Set the lexical context of these templates |
| 1011 | NewClass->setLexicalDeclContext(CurContext); |
| 1012 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1013 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1014 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1015 | NewClass->startDefinition(); |
| 1016 | |
| 1017 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1018 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1019 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1020 | if (TUK != TUK_Friend) |
| 1021 | PushOnScopeChains(NewTemplate, S); |
| 1022 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1023 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1024 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1025 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1026 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1027 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1028 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1029 | PrevClassTemplate != NULL); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1030 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1031 | // Friend templates are visible in fairly strange ways. |
| 1032 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1033 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1034 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 1035 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1036 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1037 | /* AddToContext = */ false); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1038 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1039 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1040 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1041 | NewClass->getLocation(), |
| 1042 | NewTemplate, |
| 1043 | /*FIXME:*/NewClass->getLocation()); |
| 1044 | Friend->setAccess(AS_public); |
| 1045 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1046 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1047 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1048 | if (Invalid) { |
| 1049 | NewTemplate->setInvalidDecl(); |
| 1050 | NewClass->setInvalidDecl(); |
| 1051 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1052 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1055 | /// \brief Diagnose the presence of a default template argument on a |
| 1056 | /// template parameter, which is ill-formed in certain contexts. |
| 1057 | /// |
| 1058 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1059 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1060 | Sema::TemplateParamListContext TPC, |
| 1061 | SourceLocation ParamLoc, |
| 1062 | SourceRange DefArgRange) { |
| 1063 | switch (TPC) { |
| 1064 | case Sema::TPC_ClassTemplate: |
| 1065 | return false; |
| 1066 | |
| 1067 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1068 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1069 | // C++ [temp.param]p9: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1070 | // A default template-argument shall not be specified in a |
| 1071 | // function template declaration or a function template |
| 1072 | // definition [...] |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1073 | // If a friend function template declaration specifies a default |
| 1074 | // template-argument, that declaration shall be a definition and shall be |
| 1075 | // the only declaration of the function template in the translation unit. |
| 1076 | // (C++98/03 doesn't have this wording; see DR226). |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1077 | if (!S.getLangOptions().CPlusPlus0x) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1078 | S.Diag(ParamLoc, |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1079 | diag::ext_template_parameter_default_in_function_template) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1080 | << DefArgRange; |
| 1081 | return false; |
| 1082 | |
| 1083 | case Sema::TPC_ClassTemplateMember: |
| 1084 | // C++0x [temp.param]p9: |
| 1085 | // A default template-argument shall not be specified in the |
| 1086 | // template-parameter-lists of the definition of a member of a |
| 1087 | // class template that appears outside of the member's class. |
| 1088 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1089 | << DefArgRange; |
| 1090 | return true; |
| 1091 | |
| 1092 | case Sema::TPC_FriendFunctionTemplate: |
| 1093 | // C++ [temp.param]p9: |
| 1094 | // A default template-argument shall not be specified in a |
| 1095 | // friend template declaration. |
| 1096 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1097 | << DefArgRange; |
| 1098 | return true; |
| 1099 | |
| 1100 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1101 | // for friend function templates if there is only a single |
| 1102 | // declaration (and it is a definition). Strange! |
| 1103 | } |
| 1104 | |
| 1105 | return false; |
| 1106 | } |
| 1107 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1108 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1109 | /// of a template template parameter, recursively. |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1110 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1111 | TemplateTemplateParmDecl *TTP) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1112 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1113 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1114 | NamedDecl *P = Params->getParam(I); |
| 1115 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1116 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1117 | NTTP->getTypeSourceInfo(), |
| 1118 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1119 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1120 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1121 | continue; |
| 1122 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1123 | |
| 1124 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1125 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1126 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1127 | return true; |
| 1128 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1129 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1130 | return false; |
| 1131 | } |
| 1132 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1133 | /// \brief Checks the validity of a template parameter list, possibly |
| 1134 | /// considering the template parameter list from a previous |
| 1135 | /// declaration. |
| 1136 | /// |
| 1137 | /// If an "old" template parameter list is provided, it must be |
| 1138 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1139 | /// template parameter list. |
| 1140 | /// |
| 1141 | /// \param NewParams Template parameter list for a new template |
| 1142 | /// declaration. This template parameter list will be updated with any |
| 1143 | /// default arguments that are carried through from the previous |
| 1144 | /// template parameter list. |
| 1145 | /// |
| 1146 | /// \param OldParams If provided, template parameter list from a |
| 1147 | /// previous declaration of the same template. Default template |
| 1148 | /// arguments will be merged from the old template parameter list to |
| 1149 | /// the new template parameter list. |
| 1150 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1151 | /// \param TPC Describes the context in which we are checking the given |
| 1152 | /// template parameter list. |
| 1153 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1154 | /// \returns true if an error occurred, false otherwise. |
| 1155 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1156 | TemplateParameterList *OldParams, |
| 1157 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1158 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1160 | // C++ [temp.param]p10: |
| 1161 | // The set of default template-arguments available for use with a |
| 1162 | // template declaration or definition is obtained by merging the |
| 1163 | // default arguments from the definition (if in scope) and all |
| 1164 | // declarations in scope in the same way default function |
| 1165 | // arguments are (8.3.6). |
| 1166 | bool SawDefaultArgument = false; |
| 1167 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1168 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1169 | bool SawParameterPack = false; |
| 1170 | SourceLocation ParameterPackLoc; |
| 1171 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1172 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1173 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1174 | if (OldParams) |
| 1175 | OldParam = OldParams->begin(); |
| 1176 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1177 | bool RemoveDefaultArguments = false; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1178 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1179 | NewParamEnd = NewParams->end(); |
| 1180 | NewParam != NewParamEnd; ++NewParam) { |
| 1181 | // Variables used to diagnose redundant default arguments |
| 1182 | bool RedundantDefaultArg = false; |
| 1183 | SourceLocation OldDefaultLoc; |
| 1184 | SourceLocation NewDefaultLoc; |
| 1185 | |
| 1186 | // Variables used to diagnose missing default arguments |
| 1187 | bool MissingDefaultArg = false; |
| 1188 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1189 | // C++0x [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1190 | // If a template parameter of a primary class template is a template |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1191 | // parameter pack, it shall be the last template parameter. |
| 1192 | if (SawParameterPack && TPC == TPC_ClassTemplate) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1194 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1195 | Invalid = true; |
| 1196 | } |
| 1197 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1198 | if (TemplateTypeParmDecl *NewTypeParm |
| 1199 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1200 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1201 | if (NewTypeParm->hasDefaultArgument() && |
| 1202 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1203 | NewTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1204 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1205 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1206 | NewTypeParm->removeDefaultArgument(); |
| 1207 | |
| 1208 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1210 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1211 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1212 | if (NewTypeParm->isParameterPack()) { |
| 1213 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1214 | "Parameter packs can't have a default argument!"); |
| 1215 | SawParameterPack = true; |
| 1216 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1217 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1218 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1219 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1220 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1221 | SawDefaultArgument = true; |
| 1222 | RedundantDefaultArg = true; |
| 1223 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1224 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1225 | // Merge the default argument from the old declaration to the |
| 1226 | // new declaration. |
| 1227 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1228 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1229 | true); |
| 1230 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1231 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1232 | SawDefaultArgument = true; |
| 1233 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1234 | } else if (SawDefaultArgument) |
| 1235 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1236 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1237 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1238 | // Check for unexpanded parameter packs. |
| 1239 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1240 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1241 | UPPC_NonTypeTemplateParameterType)) { |
| 1242 | Invalid = true; |
| 1243 | continue; |
| 1244 | } |
| 1245 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1246 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1247 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1248 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1249 | NewNonTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1250 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1251 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1254 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1255 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1256 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1257 | if (NewNonTypeParm->isParameterPack()) { |
| 1258 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1259 | "Parameter packs can't have a default argument!"); |
| 1260 | SawParameterPack = true; |
| 1261 | ParameterPackLoc = NewNonTypeParm->getLocation(); |
| 1262 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1263 | NewNonTypeParm->hasDefaultArgument()) { |
| 1264 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1265 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1266 | SawDefaultArgument = true; |
| 1267 | RedundantDefaultArg = true; |
| 1268 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1269 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1270 | // Merge the default argument from the old declaration to the |
| 1271 | // new declaration. |
| 1272 | SawDefaultArgument = true; |
| 1273 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1274 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1275 | // parameter. |
| 1276 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1277 | OldNonTypeParm->getDefaultArgument(), |
| 1278 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1279 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1280 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1281 | SawDefaultArgument = true; |
| 1282 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1283 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1284 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1285 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1286 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1287 | TemplateTemplateParmDecl *NewTemplateParm |
| 1288 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1289 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1290 | // Check for unexpanded parameter packs, recursively. |
| 1291 | if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
| 1292 | Invalid = true; |
| 1293 | continue; |
| 1294 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1295 | |
| 1296 | if (NewTemplateParm->hasDefaultArgument() && |
| 1297 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1298 | NewTemplateParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1299 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1300 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1301 | |
| 1302 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1303 | TemplateTemplateParmDecl *OldTemplateParm |
| 1304 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1305 | if (NewTemplateParm->isParameterPack()) { |
| 1306 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1307 | "Parameter packs can't have a default argument!"); |
| 1308 | SawParameterPack = true; |
| 1309 | ParameterPackLoc = NewTemplateParm->getLocation(); |
| 1310 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1311 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1312 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1313 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1314 | SawDefaultArgument = true; |
| 1315 | RedundantDefaultArg = true; |
| 1316 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1317 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1318 | // Merge the default argument from the old declaration to the |
| 1319 | // new declaration. |
| 1320 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1321 | // FIXME: We need to create a new kind of "default argument" expression |
| 1322 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1323 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1324 | OldTemplateParm->getDefaultArgument(), |
| 1325 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1326 | PreviousDefaultArgLoc |
| 1327 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1328 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1329 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1330 | PreviousDefaultArgLoc |
| 1331 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1332 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1333 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | if (RedundantDefaultArg) { |
| 1337 | // C++ [temp.param]p12: |
| 1338 | // A template-parameter shall not be given default arguments |
| 1339 | // by two different declarations in the same scope. |
| 1340 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1341 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1342 | Invalid = true; |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1343 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1344 | // C++ [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1345 | // If a template-parameter of a class template has a default |
| 1346 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1347 | // have a default template-argument supplied or be a template parameter |
| 1348 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1349 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1350 | diag::err_template_param_default_arg_missing); |
| 1351 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1352 | Invalid = true; |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1353 | RemoveDefaultArguments = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | // If we have an old template parameter list that we're merging |
| 1357 | // in, move on to the next parameter. |
| 1358 | if (OldParams) |
| 1359 | ++OldParam; |
| 1360 | } |
| 1361 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1362 | // We were missing some default arguments at the end of the list, so remove |
| 1363 | // all of the default arguments. |
| 1364 | if (RemoveDefaultArguments) { |
| 1365 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1366 | NewParamEnd = NewParams->end(); |
| 1367 | NewParam != NewParamEnd; ++NewParam) { |
| 1368 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1369 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1370 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1371 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1372 | NTTP->removeDefaultArgument(); |
| 1373 | else |
| 1374 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1375 | } |
| 1376 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1377 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1378 | return Invalid; |
| 1379 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1380 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1381 | namespace { |
| 1382 | |
| 1383 | /// A class which looks for a use of a certain level of template |
| 1384 | /// parameter. |
| 1385 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1386 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1387 | |
| 1388 | unsigned Depth; |
| 1389 | bool Match; |
| 1390 | |
| 1391 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1392 | NamedDecl *ND = Params->getParam(0); |
| 1393 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1394 | Depth = PD->getDepth(); |
| 1395 | } else if (NonTypeTemplateParmDecl *PD = |
| 1396 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1397 | Depth = PD->getDepth(); |
| 1398 | } else { |
| 1399 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | bool Matches(unsigned ParmDepth) { |
| 1404 | if (ParmDepth >= Depth) { |
| 1405 | Match = true; |
| 1406 | return true; |
| 1407 | } |
| 1408 | return false; |
| 1409 | } |
| 1410 | |
| 1411 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1412 | return !Matches(T->getDepth()); |
| 1413 | } |
| 1414 | |
| 1415 | bool TraverseTemplateName(TemplateName N) { |
| 1416 | if (TemplateTemplateParmDecl *PD = |
| 1417 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1418 | if (Matches(PD->getDepth())) return false; |
| 1419 | return super::TraverseTemplateName(N); |
| 1420 | } |
| 1421 | |
| 1422 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1423 | if (NonTypeTemplateParmDecl *PD = |
| 1424 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1425 | if (PD->getDepth() == Depth) { |
| 1426 | Match = true; |
| 1427 | return false; |
| 1428 | } |
| 1429 | } |
| 1430 | return super::VisitDeclRefExpr(E); |
| 1431 | } |
| 1432 | }; |
| 1433 | } |
| 1434 | |
| 1435 | /// Determines whether a template-id depends on the given parameter |
| 1436 | /// list. |
| 1437 | static bool |
| 1438 | DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId, |
| 1439 | TemplateParameterList *Params) { |
| 1440 | DependencyChecker Checker(Params); |
| 1441 | Checker.TraverseType(QualType(TemplateId, 0)); |
| 1442 | return Checker.Match; |
| 1443 | } |
| 1444 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1445 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1446 | /// specifier, returning the template parameter list that applies to the |
| 1447 | /// name. |
| 1448 | /// |
| 1449 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1450 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1451 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1452 | /// \param SS the scope specifier that will be matched to the given template |
| 1453 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1454 | /// being declared. |
| 1455 | /// |
| 1456 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1457 | /// innermost template parameter lists. |
| 1458 | /// |
| 1459 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1460 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1461 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1462 | /// matching template parameters to scope specifiers in friend |
| 1463 | /// declarations. |
| 1464 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1465 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1466 | /// declared is an explicit specialization, false otherwise. |
| 1467 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1469 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1470 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1472 | /// 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] | 1473 | /// itself a template). |
| 1474 | TemplateParameterList * |
| 1475 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
| 1476 | const CXXScopeSpec &SS, |
| 1477 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1478 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1479 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1480 | bool &IsExplicitSpecialization, |
| 1481 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1482 | IsExplicitSpecialization = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1483 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1484 | // Find the template-ids that occur within the nested-name-specifier. These |
| 1485 | // template-ids will match up with the template parameter lists. |
| 1486 | llvm::SmallVector<const TemplateSpecializationType *, 4> |
| 1487 | TemplateIdsInSpecifier; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1488 | llvm::SmallVector<ClassTemplateSpecializationDecl *, 4> |
| 1489 | ExplicitSpecializationsInSpecifier; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1490 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 1491 | NNS; NNS = NNS->getPrefix()) { |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1492 | const Type *T = NNS->getAsType(); |
| 1493 | if (!T) break; |
| 1494 | |
| 1495 | // C++0x [temp.expl.spec]p17: |
| 1496 | // A member or a member template may be nested within many |
| 1497 | // enclosing class templates. In an explicit specialization for |
| 1498 | // such a member, the member declaration shall be preceded by a |
| 1499 | // template<> for each enclosing class template that is |
| 1500 | // explicitly specialized. |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1501 | // |
| 1502 | // Following the existing practice of GNU and EDG, we allow a typedef of a |
| 1503 | // template specialization type. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1504 | while (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
| 1505 | T = TT->getDecl()->getUnderlyingType().getTypePtr(); |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1506 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1507 | if (const TemplateSpecializationType *SpecType |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1508 | = dyn_cast<TemplateSpecializationType>(T)) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1509 | TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl(); |
| 1510 | if (!Template) |
| 1511 | continue; // FIXME: should this be an error? probably... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1512 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1513 | if (const RecordType *Record = SpecType->getAs<RecordType>()) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1514 | ClassTemplateSpecializationDecl *SpecDecl |
| 1515 | = cast<ClassTemplateSpecializationDecl>(Record->getDecl()); |
| 1516 | // If the nested name specifier refers to an explicit specialization, |
| 1517 | // we don't need a template<> header. |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1518 | if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1519 | ExplicitSpecializationsInSpecifier.push_back(SpecDecl); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1520 | continue; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1521 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1522 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1523 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1524 | TemplateIdsInSpecifier.push_back(SpecType); |
| 1525 | } |
| 1526 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1527 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1528 | // Reverse the list of template-ids in the scope specifier, so that we can |
| 1529 | // more easily match up the template-ids and the template parameter lists. |
| 1530 | std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1531 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1532 | SourceLocation FirstTemplateLoc = DeclStartLoc; |
| 1533 | if (NumParamLists) |
| 1534 | FirstTemplateLoc = ParamLists[0]->getTemplateLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1536 | // Match the template-ids found in the specifier to the template parameter |
| 1537 | // lists. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1538 | unsigned ParamIdx = 0, TemplateIdx = 0; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1539 | for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size(); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1540 | TemplateIdx != NumTemplateIds; ++TemplateIdx) { |
| 1541 | const TemplateSpecializationType *TemplateId |
| 1542 | = TemplateIdsInSpecifier[TemplateIdx]; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1543 | bool DependentTemplateId = TemplateId->isDependentType(); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1544 | |
| 1545 | // In friend declarations we can have template-ids which don't |
| 1546 | // depend on the corresponding template parameter lists. But |
| 1547 | // assume that empty parameter lists are supposed to match this |
| 1548 | // template-id. |
| 1549 | if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) { |
| 1550 | if (!DependentTemplateId || |
| 1551 | !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx])) |
| 1552 | continue; |
| 1553 | } |
| 1554 | |
| 1555 | if (ParamIdx >= NumParamLists) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1556 | // We have a template-id without a corresponding template parameter |
| 1557 | // list. |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1558 | |
| 1559 | // ...which is fine if this is a friend declaration. |
| 1560 | if (IsFriend) { |
| 1561 | IsExplicitSpecialization = true; |
| 1562 | break; |
| 1563 | } |
| 1564 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1565 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1566 | // FIXME: the location information here isn't great. |
| 1567 | Diag(SS.getRange().getBegin(), |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1568 | diag::err_template_spec_needs_template_parameters) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1569 | << QualType(TemplateId, 0) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1570 | << SS.getRange(); |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1571 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1572 | } else { |
| 1573 | Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header) |
| 1574 | << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1575 | << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1576 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1577 | } |
| 1578 | return 0; |
| 1579 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1580 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1581 | // Check the template parameter list against its corresponding template-id. |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1582 | if (DependentTemplateId) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1583 | TemplateParameterList *ExpectedTemplateParams = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1584 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1585 | // Are there cases in (e.g.) friends where this won't match? |
| 1586 | if (const InjectedClassNameType *Injected |
| 1587 | = TemplateId->getAs<InjectedClassNameType>()) { |
| 1588 | CXXRecordDecl *Record = Injected->getDecl(); |
| 1589 | if (ClassTemplatePartialSpecializationDecl *Partial = |
| 1590 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 1591 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1592 | else |
| 1593 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
| 1594 | ->getTemplateParameters(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1595 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1596 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1597 | if (ExpectedTemplateParams) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1598 | TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1599 | ExpectedTemplateParams, |
| 1600 | true, TPL_TemplateMatch); |
| 1601 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1602 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1603 | TPC_ClassTemplateMember); |
| 1604 | } else if (ParamLists[ParamIdx]->size() > 0) |
| 1605 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1606 | diag::err_template_param_list_matches_nontemplate) |
| 1607 | << TemplateId |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1608 | << ParamLists[ParamIdx]->getSourceRange(); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1609 | else |
| 1610 | IsExplicitSpecialization = true; |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1611 | |
| 1612 | ++ParamIdx; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1613 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1614 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1615 | // If there were at least as many template-ids as there were template |
| 1616 | // parameter lists, then there are no template parameter lists remaining for |
| 1617 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1618 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1619 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1620 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1621 | // If there were too many template parameter lists, complain about that now. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1622 | if (ParamIdx != NumParamLists - 1) { |
| 1623 | while (ParamIdx < NumParamLists - 1) { |
| 1624 | bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0; |
| 1625 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1626 | isExplicitSpecHeader? diag::warn_template_spec_extra_headers |
| 1627 | : diag::err_template_spec_extra_headers) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1628 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1629 | ParamLists[ParamIdx]->getRAngleLoc()); |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1630 | |
| 1631 | if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) { |
| 1632 | Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(), |
| 1633 | diag::note_explicit_template_spec_does_not_need_header) |
| 1634 | << ExplicitSpecializationsInSpecifier.back(); |
| 1635 | ExplicitSpecializationsInSpecifier.pop_back(); |
| 1636 | } |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1637 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1638 | // We have a template parameter list with no corresponding scope, which |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1639 | // means that the resulting template declaration can't be instantiated |
| 1640 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1641 | if (!isExplicitSpecHeader) |
| 1642 | Invalid = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1643 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1644 | ++ParamIdx; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1645 | } |
| 1646 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1647 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1648 | // Return the last template parameter list, which corresponds to the |
| 1649 | // entity being declared. |
| 1650 | return ParamLists[NumParamLists - 1]; |
| 1651 | } |
| 1652 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1653 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1654 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1655 | Diag(Template->getLocation(), diag::note_template_declared_here) |
| 1656 | << (isa<FunctionTemplateDecl>(Template)? 0 |
| 1657 | : isa<ClassTemplateDecl>(Template)? 1 |
| 1658 | : 2) |
| 1659 | << Template->getDeclName(); |
| 1660 | return; |
| 1661 | } |
| 1662 | |
| 1663 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1664 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1665 | IEnd = OST->end(); |
| 1666 | I != IEnd; ++I) |
| 1667 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1668 | << 0 << (*I)->getDeclName(); |
| 1669 | |
| 1670 | return; |
| 1671 | } |
| 1672 | } |
| 1673 | |
| 1674 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1675 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1676 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1677 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1678 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1679 | if (!Template || isa<FunctionTemplateDecl>(Template)) { |
| 1680 | // We might have a substituted template template parameter pack. If so, |
| 1681 | // build a template specialization type for it. |
| 1682 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 1683 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
| 1684 | |
| 1685 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 1686 | << Name; |
| 1687 | NoteAllFoundTemplates(Name); |
| 1688 | return QualType(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1689 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1690 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1691 | // Check that the template argument list is well-formed for this |
| 1692 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1693 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1694 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1695 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1696 | return QualType(); |
| 1697 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1698 | assert((Converted.size() == Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1699 | "Converted template argument list is too short!"); |
| 1700 | |
| 1701 | QualType CanonType; |
| 1702 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 1703 | if (Name.isDependent() || |
| 1704 | TemplateSpecializationType::anyDependentTemplateArguments( |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1705 | TemplateArgs)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1706 | // This class template specialization is a dependent |
| 1707 | // type. Therefore, its canonical type is another class template |
| 1708 | // specialization type that contains all of the converted |
| 1709 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1710 | // A<T, T> have identical types when A is declared as: |
| 1711 | // |
| 1712 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1713 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1714 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1715 | Converted.data(), |
| 1716 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1717 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1718 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1719 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1720 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1721 | // build the canonical type and return that to us. |
| 1722 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1723 | |
| 1724 | // This might work out to be a current instantiation, in which |
| 1725 | // case the canonical type needs to be the InjectedClassNameType. |
| 1726 | // |
| 1727 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1728 | // changes to CurContext don't change the set of current |
| 1729 | // instantiations. |
| 1730 | if (isa<ClassTemplateDecl>(Template)) { |
| 1731 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1732 | // If we get out to a namespace, we're done. |
| 1733 | if (Ctx->isFileContext()) break; |
| 1734 | |
| 1735 | // If this isn't a record, keep looking. |
| 1736 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1737 | if (!Record) continue; |
| 1738 | |
| 1739 | // Look for one of the two cases with InjectedClassNameTypes |
| 1740 | // and check whether it's the same template. |
| 1741 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1742 | !Record->getDescribedClassTemplate()) |
| 1743 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1744 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1745 | // Fetch the injected class name type and check whether its |
| 1746 | // injected type is equal to the type we just built. |
| 1747 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1748 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 1749 | ->getInjectedSpecializationType(); |
| 1750 | |
| 1751 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 1752 | continue; |
| 1753 | |
| 1754 | // If so, the canonical type of this TST is the injected |
| 1755 | // class name type of the record we just found. |
| 1756 | assert(ICNT.isCanonical()); |
| 1757 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1758 | break; |
| 1759 | } |
| 1760 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1761 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1762 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1763 | // Find the class template specialization declaration that |
| 1764 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1765 | void *InsertPos = 0; |
| 1766 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1767 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1768 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1769 | if (!Decl) { |
| 1770 | // This is the first time we have referenced this class template |
| 1771 | // specialization. Create the canonical declaration and add it to |
| 1772 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1773 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1774 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 1775 | ClassTemplate->getDeclContext(), |
| 1776 | ClassTemplate->getLocation(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1777 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1778 | ClassTemplate, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1779 | Converted.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1780 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 1781 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1782 | Decl->setLexicalDeclContext(CurContext); |
| 1783 | } |
| 1784 | |
| 1785 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1786 | assert(isa<RecordType>(CanonType) && |
| 1787 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1788 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1789 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1790 | // Build the fully-sugared type for this class template |
| 1791 | // specialization, which refers back to the class template |
| 1792 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1793 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1796 | TypeResult |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1797 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, |
| 1798 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1800 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1801 | SourceLocation RAngleLoc) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1802 | if (SS.isInvalid()) |
| 1803 | return true; |
| 1804 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1805 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1806 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1807 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1808 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1809 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1810 | |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 1811 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 1812 | QualType T = Context.getDependentTemplateSpecializationType(ETK_None, |
| 1813 | DTN->getQualifier(), |
| 1814 | DTN->getIdentifier(), |
| 1815 | TemplateArgs); |
| 1816 | |
| 1817 | // Build type-source information. |
| 1818 | TypeLocBuilder TLB; |
| 1819 | DependentTemplateSpecializationTypeLoc SpecTL |
| 1820 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1821 | SpecTL.setKeywordLoc(SourceLocation()); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 1822 | SpecTL.setNameLoc(TemplateLoc); |
| 1823 | SpecTL.setLAngleLoc(LAngleLoc); |
| 1824 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 1825 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 1826 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 1827 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 1828 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 1829 | } |
| 1830 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1831 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1832 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1833 | |
| 1834 | if (Result.isNull()) |
| 1835 | return true; |
| 1836 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1837 | // Build type-source information. |
| 1838 | TypeLocBuilder TLB; |
| 1839 | TemplateSpecializationTypeLoc SpecTL |
| 1840 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 1841 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 1842 | SpecTL.setLAngleLoc(LAngleLoc); |
| 1843 | SpecTL.setRAngleLoc(RAngleLoc); |
| 1844 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 1845 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1846 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1847 | if (SS.isNotEmpty()) { |
| 1848 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 1849 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 1850 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 1851 | ElabTL.setKeywordLoc(SourceLocation()); |
| 1852 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 1853 | } |
| 1854 | |
| 1855 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1856 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1857 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1858 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1859 | TypeSpecifierType TagSpec, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1860 | SourceLocation TagLoc, |
| 1861 | CXXScopeSpec &SS, |
| 1862 | TemplateTy TemplateD, |
| 1863 | SourceLocation TemplateLoc, |
| 1864 | SourceLocation LAngleLoc, |
| 1865 | ASTTemplateArgsPtr TemplateArgsIn, |
| 1866 | SourceLocation RAngleLoc) { |
| 1867 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
| 1868 | |
| 1869 | // Translate the parser's template argument list in our AST format. |
| 1870 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 1871 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 1872 | |
| 1873 | // Determine the tag kind |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1874 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1875 | ElaboratedTypeKeyword Keyword |
| 1876 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1877 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1878 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 1879 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 1880 | DTN->getQualifier(), |
| 1881 | DTN->getIdentifier(), |
| 1882 | TemplateArgs); |
| 1883 | |
| 1884 | // Build type-source information. |
| 1885 | TypeLocBuilder TLB; |
| 1886 | DependentTemplateSpecializationTypeLoc SpecTL |
| 1887 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 1888 | SpecTL.setKeywordLoc(TagLoc); |
| 1889 | SpecTL.setNameLoc(TemplateLoc); |
| 1890 | SpecTL.setLAngleLoc(LAngleLoc); |
| 1891 | SpecTL.setRAngleLoc(RAngleLoc); |
| 1892 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 1893 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 1894 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 1895 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 1896 | } |
| 1897 | |
| 1898 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 1899 | if (Result.isNull()) |
| 1900 | return TypeResult(); |
| 1901 | |
| 1902 | // Check the tag kind |
| 1903 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1904 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1905 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1906 | IdentifierInfo *Id = D->getIdentifier(); |
| 1907 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1908 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1909 | if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { |
| 1910 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1911 | << Result |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1912 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1913 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1914 | } |
| 1915 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1916 | |
| 1917 | // Provide source-location information for the template specialization. |
| 1918 | TypeLocBuilder TLB; |
| 1919 | TemplateSpecializationTypeLoc SpecTL |
| 1920 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 1921 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 1922 | SpecTL.setLAngleLoc(LAngleLoc); |
| 1923 | SpecTL.setRAngleLoc(RAngleLoc); |
| 1924 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 1925 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1926 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1927 | // Construct an elaborated type containing the nested-name-specifier (if any) |
| 1928 | // and keyword. |
| 1929 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 1930 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 1931 | ElabTL.setKeywordLoc(TagLoc); |
| 1932 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 1933 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1936 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1937 | LookupResult &R, |
| 1938 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1939 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1940 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 1941 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | // 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] | 1943 | // though. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1944 | // foo<int> could identify a single function unambiguously |
| 1945 | // This approach does NOT work, since f<int>(1); |
| 1946 | // gets resolved prior to resorting to overload resolution |
| 1947 | // i.e., template<class T> void f(double); |
| 1948 | // vs template<class T, class U> void f(U); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1949 | |
| 1950 | // These should be filtered out by our callers. |
| 1951 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 1952 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 1953 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1954 | // We don't want lookup warnings at this point. |
| 1955 | R.suppressDiagnostics(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1956 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1957 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1958 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1959 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1960 | R.getLookupNameInfo(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1961 | RequiresADL, TemplateArgs, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 1962 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1963 | |
| 1964 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1967 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1968 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1969 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1970 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1971 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1972 | DeclContext *DC; |
| 1973 | if (!(DC = computeDeclContext(SS, false)) || |
| 1974 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1975 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1976 | return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1977 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1978 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1979 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1980 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 1981 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1982 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1983 | if (R.isAmbiguous()) |
| 1984 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1985 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1986 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1987 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 1988 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1989 | return ExprError(); |
| 1990 | } |
| 1991 | |
| 1992 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1993 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 1994 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 1995 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1996 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 1997 | return ExprError(); |
| 1998 | } |
| 1999 | |
| 2000 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2003 | /// \brief Form a dependent template name. |
| 2004 | /// |
| 2005 | /// This action forms a dependent template name given the template |
| 2006 | /// name and its (presumably dependent) scope specifier. For |
| 2007 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2008 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2009 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2010 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2011 | SourceLocation TemplateKWLoc, |
| 2012 | CXXScopeSpec &SS, |
| 2013 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2014 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2015 | bool EnteringContext, |
| 2016 | TemplateTy &Result) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 2017 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() && |
| 2018 | !getLangOptions().CPlusPlus0x) |
| 2019 | Diag(TemplateKWLoc, diag::ext_template_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2020 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2021 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2022 | DeclContext *LookupCtx = 0; |
| 2023 | if (SS.isSet()) |
| 2024 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2025 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2026 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2027 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2028 | // C++0x [temp.names]p5: |
| 2029 | // If a name prefixed by the keyword template is not the name of |
| 2030 | // a template, the program is ill-formed. [Note: the keyword |
| 2031 | // template may not be applied to non-template members of class |
| 2032 | // templates. -end note ] [ Note: as is the case with the |
| 2033 | // typename prefix, the template prefix is allowed in cases |
| 2034 | // where it is not strictly necessary; i.e., when the |
| 2035 | // nested-name-specifier or the expression on the left of the -> |
| 2036 | // or . is not dependent on a template-parameter, or the use |
| 2037 | // does not appear in the scope of a template. -end note] |
| 2038 | // |
| 2039 | // Note: C++03 was more strict here, because it banned the use of |
| 2040 | // the "template" keyword prior to a template-name that was not a |
| 2041 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2042 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2043 | // 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] | 2044 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2045 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 2046 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2047 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2048 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2049 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | d078bd2 | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2050 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2051 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2052 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2053 | } else if (TNK == TNK_Non_template) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2054 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2055 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2056 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2057 | << Name.getSourceRange() |
| 2058 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2059 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2060 | } else { |
| 2061 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2062 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2063 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2066 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2067 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2068 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2069 | switch (Name.getKind()) { |
| 2070 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2071 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2072 | Name.Identifier)); |
| 2073 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2074 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2075 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2076 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2077 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2078 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2079 | |
| 2080 | case UnqualifiedId::IK_LiteralOperatorId: |
| 2081 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 2082 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2083 | default: |
| 2084 | break; |
| 2085 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2086 | |
| 2087 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2088 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2089 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2090 | << Name.getSourceRange() |
| 2091 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2092 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2095 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2096 | const TemplateArgumentLoc &AL, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2097 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2098 | const TemplateArgument &Arg = AL.getArgument(); |
| 2099 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2100 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2101 | switch(Arg.getKind()) { |
| 2102 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2103 | // C++ [temp.arg.type]p1: |
| 2104 | // A template-argument for a template-parameter which is a |
| 2105 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2106 | break; |
| 2107 | case TemplateArgument::Template: { |
| 2108 | // We have a template type parameter but the template argument |
| 2109 | // is a template without any arguments. |
| 2110 | SourceRange SR = AL.getSourceRange(); |
| 2111 | TemplateName Name = Arg.getAsTemplate(); |
| 2112 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 2113 | << Name << SR; |
| 2114 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 2115 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2116 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2117 | return true; |
| 2118 | } |
| 2119 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2120 | // We have a template type parameter but the template argument |
| 2121 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 2122 | SourceRange SR = AL.getSourceRange(); |
| 2123 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2124 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2125 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2126 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2127 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2128 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2129 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2130 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2131 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2132 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2133 | // Add the converted template type argument. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2134 | Converted.push_back( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2135 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2136 | return false; |
| 2137 | } |
| 2138 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2139 | /// \brief Substitute template arguments into the default template argument for |
| 2140 | /// the given template type parameter. |
| 2141 | /// |
| 2142 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2143 | /// the substitution. |
| 2144 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2145 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2146 | /// for. |
| 2147 | /// |
| 2148 | /// \param TemplateLoc the location of the template name that started the |
| 2149 | /// template-id we are checking. |
| 2150 | /// |
| 2151 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2152 | /// terminates the template-id. |
| 2153 | /// |
| 2154 | /// \param Param the template template parameter whose default we are |
| 2155 | /// substituting into. |
| 2156 | /// |
| 2157 | /// \param Converted the list of template arguments provided for template |
| 2158 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2159 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2160 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2161 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2162 | TemplateDecl *Template, |
| 2163 | SourceLocation TemplateLoc, |
| 2164 | SourceLocation RAngleLoc, |
| 2165 | TemplateTypeParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2166 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2167 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2168 | |
| 2169 | // If the argument type is dependent, instantiate it now based |
| 2170 | // on the previously-computed template arguments. |
| 2171 | if (ArgType->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2172 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2173 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2174 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2175 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2176 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2177 | |
| 2178 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2179 | Template, Converted.data(), |
| 2180 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2181 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2182 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2183 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2184 | Param->getDefaultArgumentLoc(), |
| 2185 | Param->getDeclName()); |
| 2186 | } |
| 2187 | |
| 2188 | return ArgType; |
| 2189 | } |
| 2190 | |
| 2191 | /// \brief Substitute template arguments into the default template argument for |
| 2192 | /// the given non-type template parameter. |
| 2193 | /// |
| 2194 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2195 | /// the substitution. |
| 2196 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2197 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2198 | /// for. |
| 2199 | /// |
| 2200 | /// \param TemplateLoc the location of the template name that started the |
| 2201 | /// template-id we are checking. |
| 2202 | /// |
| 2203 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2204 | /// terminates the template-id. |
| 2205 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2206 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2207 | /// substituting into. |
| 2208 | /// |
| 2209 | /// \param Converted the list of template arguments provided for template |
| 2210 | /// parameters that precede \p Param in the template parameter list. |
| 2211 | /// |
| 2212 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2213 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2214 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2215 | TemplateDecl *Template, |
| 2216 | SourceLocation TemplateLoc, |
| 2217 | SourceLocation RAngleLoc, |
| 2218 | NonTypeTemplateParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2219 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2220 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2221 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2222 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2223 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2224 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2225 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2226 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2227 | Template, Converted.data(), |
| 2228 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2229 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2230 | |
| 2231 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2232 | } |
| 2233 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2234 | /// \brief Substitute template arguments into the default template argument for |
| 2235 | /// the given template template parameter. |
| 2236 | /// |
| 2237 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2238 | /// the substitution. |
| 2239 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2240 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2241 | /// for. |
| 2242 | /// |
| 2243 | /// \param TemplateLoc the location of the template name that started the |
| 2244 | /// template-id we are checking. |
| 2245 | /// |
| 2246 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2247 | /// terminates the template-id. |
| 2248 | /// |
| 2249 | /// \param Param the template template parameter whose default we are |
| 2250 | /// substituting into. |
| 2251 | /// |
| 2252 | /// \param Converted the list of template arguments provided for template |
| 2253 | /// parameters that precede \p Param in the template parameter list. |
| 2254 | /// |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2255 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 2256 | /// source-location information) that precedes the template name. |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2257 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2258 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2259 | static TemplateName |
| 2260 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2261 | TemplateDecl *Template, |
| 2262 | SourceLocation TemplateLoc, |
| 2263 | SourceLocation RAngleLoc, |
| 2264 | TemplateTemplateParmDecl *Param, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2265 | llvm::SmallVectorImpl<TemplateArgument> &Converted, |
| 2266 | NestedNameSpecifierLoc &QualifierLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2267 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2268 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2269 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2270 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2271 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2272 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2273 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2274 | Template, Converted.data(), |
| 2275 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2276 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2277 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2278 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2279 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2280 | if (QualifierLoc) { |
| 2281 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2282 | AllTemplateArgs); |
| 2283 | if (!QualifierLoc) |
| 2284 | return TemplateName(); |
| 2285 | } |
| 2286 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2287 | return SemaRef.SubstTemplateName(QualifierLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2288 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2289 | Param->getDefaultArgument().getTemplateNameLoc(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2290 | AllTemplateArgs); |
| 2291 | } |
| 2292 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2293 | /// \brief If the given template parameter has a default template |
| 2294 | /// argument, substitute into that default template argument and |
| 2295 | /// return the corresponding template argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2296 | TemplateArgumentLoc |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2297 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2298 | SourceLocation TemplateLoc, |
| 2299 | SourceLocation RAngleLoc, |
| 2300 | Decl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2301 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 2302 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2303 | if (!TypeParm->hasDefaultArgument()) |
| 2304 | return TemplateArgumentLoc(); |
| 2305 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2306 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2307 | TemplateLoc, |
| 2308 | RAngleLoc, |
| 2309 | TypeParm, |
| 2310 | Converted); |
| 2311 | if (DI) |
| 2312 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2313 | |
| 2314 | return TemplateArgumentLoc(); |
| 2315 | } |
| 2316 | |
| 2317 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2318 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2319 | if (!NonTypeParm->hasDefaultArgument()) |
| 2320 | return TemplateArgumentLoc(); |
| 2321 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2322 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2323 | TemplateLoc, |
| 2324 | RAngleLoc, |
| 2325 | NonTypeParm, |
| 2326 | Converted); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2327 | if (Arg.isInvalid()) |
| 2328 | return TemplateArgumentLoc(); |
| 2329 | |
| 2330 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2331 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2332 | } |
| 2333 | |
| 2334 | TemplateTemplateParmDecl *TempTempParm |
| 2335 | = cast<TemplateTemplateParmDecl>(Param); |
| 2336 | if (!TempTempParm->hasDefaultArgument()) |
| 2337 | return TemplateArgumentLoc(); |
| 2338 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2339 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2340 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2341 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2342 | TemplateLoc, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2343 | RAngleLoc, |
| 2344 | TempTempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2345 | Converted, |
| 2346 | QualifierLoc); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2347 | if (TName.isNull()) |
| 2348 | return TemplateArgumentLoc(); |
| 2349 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2350 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2351 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2352 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2353 | } |
| 2354 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2355 | /// \brief Check that the given template argument corresponds to the given |
| 2356 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2357 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2358 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2359 | /// checked. |
| 2360 | /// |
| 2361 | /// \param Arg The template argument. |
| 2362 | /// |
| 2363 | /// \param Template The template in which the template argument resides. |
| 2364 | /// |
| 2365 | /// \param TemplateLoc The location of the template name for the template |
| 2366 | /// whose argument list we're matching. |
| 2367 | /// |
| 2368 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2369 | /// the template argument list. |
| 2370 | /// |
| 2371 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2372 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2373 | /// |
| 2374 | /// \param Converted The checked, converted argument will be added to the |
| 2375 | /// end of this small vector. |
| 2376 | /// |
| 2377 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2378 | /// explicitly written, deduced, etc. |
| 2379 | /// |
| 2380 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2381 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2382 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2383 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2384 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2385 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2386 | unsigned ArgumentPackIndex, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2387 | llvm::SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2388 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2389 | // Check template type parameters. |
| 2390 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2391 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2392 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2393 | // Check non-type template parameters. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2394 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2395 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2396 | // with the template arguments we've seen thus far. But if the |
| 2397 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2398 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2399 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2400 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2401 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2402 | if (NTTPType->isDependentType() && |
| 2403 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2404 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2405 | // Do substitution on the type of the non-type template parameter. |
| 2406 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2407 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2408 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2409 | |
| 2410 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2411 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2412 | NTTPType = SubstType(NTTPType, |
| 2413 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2414 | NTTP->getLocation(), |
| 2415 | NTTP->getDeclName()); |
| 2416 | // If that worked, check the non-type template parameter type |
| 2417 | // for validity. |
| 2418 | if (!NTTPType.isNull()) |
| 2419 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2420 | NTTP->getLocation()); |
| 2421 | if (NTTPType.isNull()) |
| 2422 | return true; |
| 2423 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2424 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2425 | switch (Arg.getArgument().getKind()) { |
| 2426 | case TemplateArgument::Null: |
| 2427 | assert(false && "Should never see a NULL template argument here"); |
| 2428 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2429 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2430 | case TemplateArgument::Expression: { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2431 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2432 | ExprResult Res = |
| 2433 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 2434 | Result, CTAK); |
| 2435 | if (Res.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2436 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2437 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2438 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2439 | break; |
| 2440 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2441 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2442 | case TemplateArgument::Declaration: |
| 2443 | case TemplateArgument::Integral: |
| 2444 | // We've already checked this template argument, so just copy |
| 2445 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2446 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2447 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2448 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2449 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2450 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2451 | // We were given a template template argument. It may not be ill-formed; |
| 2452 | // see below. |
| 2453 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2454 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2455 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2456 | // We have a template argument such as \c T::template X, which we |
| 2457 | // parsed as a template template argument. However, since we now |
| 2458 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2459 | // template name into an expression. |
| 2460 | |
| 2461 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2462 | Arg.getTemplateNameLoc()); |
| 2463 | |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2464 | CXXScopeSpec SS; |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2465 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2466 | ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2467 | SS.getWithLocInContext(Context), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2468 | NameInfo)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2469 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2470 | // If we parsed the template argument as a pack expansion, create a |
| 2471 | // pack expansion expression. |
| 2472 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2473 | E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc()); |
| 2474 | if (E.isInvalid()) |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2475 | return true; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2476 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2477 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2478 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2479 | E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result); |
| 2480 | if (E.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2481 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2482 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2483 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2484 | break; |
| 2485 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2486 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2487 | // We have a template argument that actually does refer to a class |
| 2488 | // template, template alias, or template template parameter, and |
| 2489 | // therefore cannot be a non-type template argument. |
| 2490 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2491 | << Arg.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2492 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2493 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2494 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2495 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2496 | case TemplateArgument::Type: { |
| 2497 | // We have a non-type template parameter but the template |
| 2498 | // argument is a type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2499 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2500 | // C++ [temp.arg]p2: |
| 2501 | // In a template-argument, an ambiguity between a type-id and |
| 2502 | // an expression is resolved to a type-id, regardless of the |
| 2503 | // form of the corresponding template-parameter. |
| 2504 | // |
| 2505 | // We warn specifically about this case, since it can be rather |
| 2506 | // confusing for users. |
| 2507 | QualType T = Arg.getArgument().getAsType(); |
| 2508 | SourceRange SR = Arg.getSourceRange(); |
| 2509 | if (T->isFunctionType()) |
| 2510 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2511 | else |
| 2512 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2513 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2514 | return true; |
| 2515 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2516 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2517 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2518 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2519 | break; |
| 2520 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2521 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2522 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2526 | // Check template template parameters. |
| 2527 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2528 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2529 | // Substitute into the template parameter list of the template |
| 2530 | // template parameter, since previously-supplied template arguments |
| 2531 | // may appear within the template template parameter. |
| 2532 | { |
| 2533 | // Set up a template instantiation context. |
| 2534 | LocalInstantiationScope Scope(*this); |
| 2535 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2536 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2537 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2538 | |
| 2539 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2540 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2541 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2542 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2543 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2544 | if (!TempParm) |
| 2545 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2546 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2547 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2548 | switch (Arg.getArgument().getKind()) { |
| 2549 | case TemplateArgument::Null: |
| 2550 | assert(false && "Should never see a NULL template argument here"); |
| 2551 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2552 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2553 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2554 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2555 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2556 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2557 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2558 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2559 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2560 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2561 | case TemplateArgument::Expression: |
| 2562 | case TemplateArgument::Type: |
| 2563 | // We have a template template parameter but the template |
| 2564 | // argument does not refer to a template. |
| 2565 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 2566 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2567 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2568 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2569 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2570 | "Declaration argument with template template parameter"); |
| 2571 | break; |
| 2572 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2573 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2574 | "Integral argument with template template parameter"); |
| 2575 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2576 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2577 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2578 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2579 | break; |
| 2580 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2581 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2582 | return false; |
| 2583 | } |
| 2584 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2585 | /// \brief Check that the given template argument list is well-formed |
| 2586 | /// for specializing the given template. |
| 2587 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2588 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2589 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2590 | bool PartialTemplateArgs, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2591 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2592 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2593 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2594 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2595 | bool Invalid = false; |
| 2596 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2597 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2598 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2599 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2600 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2601 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2602 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2603 | (NumArgs < Params->getMinRequiredArguments() && |
| 2604 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2605 | // FIXME: point at either the first arg beyond what we can handle, |
| 2606 | // or the '>', depending on whether we have too many or too few |
| 2607 | // arguments. |
| 2608 | SourceRange Range; |
| 2609 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2610 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2611 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2612 | << (NumArgs > NumParams) |
| 2613 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2614 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2615 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2616 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2617 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2618 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2619 | Invalid = true; |
| 2620 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2621 | |
| 2622 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2623 | // [...] The type and form of each template-argument specified in |
| 2624 | // a template-id shall match the type and form specified for the |
| 2625 | // corresponding parameter declared by the template in its |
| 2626 | // template-parameter-list. |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2627 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2628 | llvm::SmallVector<TemplateArgument, 2> ArgumentPack; |
| 2629 | TemplateParameterList::iterator Param = Params->begin(), |
| 2630 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2631 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 2632 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2633 | while (Param != ParamEnd) { |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2634 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 2635 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2636 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2637 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2638 | // If we have an expanded parameter pack, make sure we don't have too |
| 2639 | // many arguments. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2640 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2641 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2642 | if (NTTP->isExpandedParameterPack() && |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2643 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 2644 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2645 | << true |
| 2646 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2647 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2648 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2649 | << Template; |
| 2650 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2651 | << Params->getSourceRange(); |
| 2652 | return true; |
| 2653 | } |
| 2654 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2655 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2656 | // Check the template argument we were given. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2657 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2658 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2659 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2660 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2661 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2662 | if ((*Param)->isTemplateParameterPack()) { |
| 2663 | // The template parameter was a template parameter pack, so take the |
| 2664 | // deduced argument and place it on the argument pack. Note that we |
| 2665 | // stay on the same template parameter so that we can deduce more |
| 2666 | // arguments. |
| 2667 | ArgumentPack.push_back(Converted.back()); |
| 2668 | Converted.pop_back(); |
| 2669 | } else { |
| 2670 | // Move to the next template parameter. |
| 2671 | ++Param; |
| 2672 | } |
| 2673 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2674 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2675 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2676 | |
| 2677 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2678 | // arguments, just break out now and we'll fill in the argument pack below. |
| 2679 | if ((*Param)->isTemplateParameterPack()) |
| 2680 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2681 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2682 | // We have a default template argument that we will use. |
| 2683 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2684 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2685 | // Retrieve the default template argument from the template |
| 2686 | // parameter. For each kind of template parameter, we substitute the |
| 2687 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2688 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2689 | // the default argument. |
| 2690 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2691 | if (!TTP->hasDefaultArgument()) { |
| 2692 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2693 | break; |
| 2694 | } |
| 2695 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2696 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2697 | Template, |
| 2698 | TemplateLoc, |
| 2699 | RAngleLoc, |
| 2700 | TTP, |
| 2701 | Converted); |
| 2702 | if (!ArgType) |
| 2703 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2704 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2705 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2706 | ArgType); |
| 2707 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2708 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2709 | if (!NTTP->hasDefaultArgument()) { |
| 2710 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2711 | break; |
| 2712 | } |
| 2713 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2714 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2715 | TemplateLoc, |
| 2716 | RAngleLoc, |
| 2717 | NTTP, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2718 | Converted); |
| 2719 | if (E.isInvalid()) |
| 2720 | return true; |
| 2721 | |
| 2722 | Expr *Ex = E.takeAs<Expr>(); |
| 2723 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2724 | } else { |
| 2725 | TemplateTemplateParmDecl *TempParm |
| 2726 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2727 | |
| 2728 | if (!TempParm->hasDefaultArgument()) { |
| 2729 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2730 | break; |
| 2731 | } |
| 2732 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2733 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2734 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2735 | TemplateLoc, |
| 2736 | RAngleLoc, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2737 | TempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2738 | Converted, |
| 2739 | QualifierLoc); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2740 | if (Name.isNull()) |
| 2741 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2742 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2743 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 2744 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2745 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2746 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2747 | // Introduce an instantiation record that describes where we are using |
| 2748 | // the default template argument. |
| 2749 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2750 | Converted.data(), Converted.size(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2751 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2752 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2753 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2754 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2755 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2756 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2757 | |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2758 | // Core issue 150 (assumed resolution): if this is a template template |
| 2759 | // parameter, keep track of the default template arguments from the |
| 2760 | // template definition. |
| 2761 | if (isTemplateTemplateParameter) |
| 2762 | TemplateArgs.addArgument(Arg); |
| 2763 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2764 | // Move to the next template parameter and argument. |
| 2765 | ++Param; |
| 2766 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2767 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2768 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2769 | // Form argument packs for each of the parameter packs remaining. |
| 2770 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2771 | // If we're checking a partial list of template arguments, don't fill |
| 2772 | // in arguments for non-template parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2773 | |
| 2774 | if ((*Param)->isTemplateParameterPack()) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2775 | if (PartialTemplateArgs && ArgumentPack.empty()) { |
| 2776 | Converted.push_back(TemplateArgument()); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2777 | } else if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2778 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2779 | else { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2780 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 2781 | ArgumentPack.data(), |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2782 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2783 | ArgumentPack.clear(); |
| 2784 | } |
| 2785 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2786 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2787 | ++Param; |
| 2788 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2789 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2790 | return Invalid; |
| 2791 | } |
| 2792 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2793 | namespace { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2794 | class UnnamedLocalNoLinkageFinder |
| 2795 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2796 | { |
| 2797 | Sema &S; |
| 2798 | SourceRange SR; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2799 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2800 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2801 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2802 | public: |
| 2803 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 2804 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2805 | bool Visit(QualType T) { |
| 2806 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2807 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2808 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2809 | #define TYPE(Class, Parent) \ |
| 2810 | bool Visit##Class##Type(const Class##Type *); |
| 2811 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 2812 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2813 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 2814 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2815 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2816 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2817 | bool VisitTagDecl(const TagDecl *Tag); |
| 2818 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 2819 | }; |
| 2820 | } |
| 2821 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2822 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2823 | return false; |
| 2824 | } |
| 2825 | |
| 2826 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 2827 | return Visit(T->getElementType()); |
| 2828 | } |
| 2829 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2830 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2831 | return Visit(T->getPointeeType()); |
| 2832 | } |
| 2833 | |
| 2834 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2835 | const BlockPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2836 | return Visit(T->getPointeeType()); |
| 2837 | } |
| 2838 | |
| 2839 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2840 | const LValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2841 | return Visit(T->getPointeeType()); |
| 2842 | } |
| 2843 | |
| 2844 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2845 | const RValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2846 | return Visit(T->getPointeeType()); |
| 2847 | } |
| 2848 | |
| 2849 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2850 | const MemberPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2851 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 2852 | } |
| 2853 | |
| 2854 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2855 | const ConstantArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2856 | return Visit(T->getElementType()); |
| 2857 | } |
| 2858 | |
| 2859 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2860 | const IncompleteArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2861 | return Visit(T->getElementType()); |
| 2862 | } |
| 2863 | |
| 2864 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2865 | const VariableArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2866 | return Visit(T->getElementType()); |
| 2867 | } |
| 2868 | |
| 2869 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2870 | const DependentSizedArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2871 | return Visit(T->getElementType()); |
| 2872 | } |
| 2873 | |
| 2874 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2875 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2876 | return Visit(T->getElementType()); |
| 2877 | } |
| 2878 | |
| 2879 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 2880 | return Visit(T->getElementType()); |
| 2881 | } |
| 2882 | |
| 2883 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 2884 | return Visit(T->getElementType()); |
| 2885 | } |
| 2886 | |
| 2887 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 2888 | const FunctionProtoType* T) { |
| 2889 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2890 | AEnd = T->arg_type_end(); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2891 | A != AEnd; ++A) { |
| 2892 | if (Visit(*A)) |
| 2893 | return true; |
| 2894 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2895 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2896 | return Visit(T->getResultType()); |
| 2897 | } |
| 2898 | |
| 2899 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 2900 | const FunctionNoProtoType* T) { |
| 2901 | return Visit(T->getResultType()); |
| 2902 | } |
| 2903 | |
| 2904 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 2905 | const UnresolvedUsingType*) { |
| 2906 | return false; |
| 2907 | } |
| 2908 | |
| 2909 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 2910 | return false; |
| 2911 | } |
| 2912 | |
| 2913 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 2914 | return Visit(T->getUnderlyingType()); |
| 2915 | } |
| 2916 | |
| 2917 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 2918 | return false; |
| 2919 | } |
| 2920 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2921 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 2922 | return Visit(T->getDeducedType()); |
| 2923 | } |
| 2924 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2925 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 2926 | return VisitTagDecl(T->getDecl()); |
| 2927 | } |
| 2928 | |
| 2929 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 2930 | return VisitTagDecl(T->getDecl()); |
| 2931 | } |
| 2932 | |
| 2933 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 2934 | const TemplateTypeParmType*) { |
| 2935 | return false; |
| 2936 | } |
| 2937 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2938 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 2939 | const SubstTemplateTypeParmPackType *) { |
| 2940 | return false; |
| 2941 | } |
| 2942 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2943 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 2944 | const TemplateSpecializationType*) { |
| 2945 | return false; |
| 2946 | } |
| 2947 | |
| 2948 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 2949 | const InjectedClassNameType* T) { |
| 2950 | return VisitTagDecl(T->getDecl()); |
| 2951 | } |
| 2952 | |
| 2953 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 2954 | const DependentNameType* T) { |
| 2955 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 2956 | } |
| 2957 | |
| 2958 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 2959 | const DependentTemplateSpecializationType* T) { |
| 2960 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 2961 | } |
| 2962 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2963 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 2964 | const PackExpansionType* T) { |
| 2965 | return Visit(T->getPattern()); |
| 2966 | } |
| 2967 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2968 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 2969 | return false; |
| 2970 | } |
| 2971 | |
| 2972 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 2973 | const ObjCInterfaceType *) { |
| 2974 | return false; |
| 2975 | } |
| 2976 | |
| 2977 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 2978 | const ObjCObjectPointerType *) { |
| 2979 | return false; |
| 2980 | } |
| 2981 | |
| 2982 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 2983 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
| 2984 | S.Diag(SR.getBegin(), diag::ext_template_arg_local_type) |
| 2985 | << S.Context.getTypeDeclType(Tag) << SR; |
| 2986 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2989 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2990 | S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR; |
| 2991 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 2992 | return true; |
| 2993 | } |
| 2994 | |
| 2995 | return false; |
| 2996 | } |
| 2997 | |
| 2998 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 2999 | NestedNameSpecifier *NNS) { |
| 3000 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 3001 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3002 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3003 | switch (NNS->getKind()) { |
| 3004 | case NestedNameSpecifier::Identifier: |
| 3005 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3006 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3007 | case NestedNameSpecifier::Global: |
| 3008 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3009 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3010 | case NestedNameSpecifier::TypeSpec: |
| 3011 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3012 | return Visit(QualType(NNS->getAsType(), 0)); |
| 3013 | } |
Fariborz Jahanian | 7b1ec6c | 2010-10-13 16:19:16 +0000 | [diff] [blame] | 3014 | return false; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3018 | /// \brief Check a template argument against its corresponding |
| 3019 | /// template type parameter. |
| 3020 | /// |
| 3021 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 3022 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3024 | TypeSourceInfo *ArgInfo) { |
| 3025 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3026 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 3027 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 3028 | |
| 3029 | if (Arg->isVariablyModifiedType()) { |
| 3030 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3031 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3032 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3033 | } |
| 3034 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3035 | // C++03 [temp.arg.type]p2: |
| 3036 | // A local type, a type with no linkage, an unnamed type or a type |
| 3037 | // compounded from any of these types shall not be used as a |
| 3038 | // template-argument for a template type-parameter. |
| 3039 | // |
| 3040 | // C++0x allows these, and even in C++03 we allow them as an extension with |
| 3041 | // a warning. |
Douglas Gregor | db4d4bb | 2010-10-13 18:05:20 +0000 | [diff] [blame] | 3042 | if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3043 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 3044 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 3045 | } |
| 3046 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3047 | return false; |
| 3048 | } |
| 3049 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3050 | /// \brief Checks whether the given template argument is the address |
| 3051 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3052 | static bool |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3053 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 3054 | NonTypeTemplateParmDecl *Param, |
| 3055 | QualType ParamType, |
| 3056 | Expr *ArgIn, |
| 3057 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3058 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3059 | Expr *Arg = ArgIn; |
| 3060 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3061 | |
| 3062 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3063 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3064 | Arg = Cast->getSubExpr(); |
| 3065 | |
| 3066 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3067 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3068 | // A template-argument for a non-type, non-template |
| 3069 | // template-parameter shall be one of: [...] |
| 3070 | // |
| 3071 | // -- the address of an object or function with external |
| 3072 | // linkage, including function templates and function |
| 3073 | // template-ids but excluding non-static class members, |
| 3074 | // expressed as & id-expression where the & is optional if |
| 3075 | // the name refers to a function or array, or if the |
| 3076 | // corresponding template-parameter is a reference; or |
| 3077 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3078 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3079 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3080 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3081 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3082 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3083 | if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3084 | S.Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3085 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3086 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3087 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3088 | } |
| 3089 | |
| 3090 | Arg = Parens->getSubExpr(); |
| 3091 | } |
| 3092 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3093 | bool AddressTaken = false; |
| 3094 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3095 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3096 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3097 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3098 | AddressTaken = true; |
| 3099 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 3100 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3101 | } else |
| 3102 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 3103 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3104 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3105 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 3106 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3107 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3108 | return true; |
| 3109 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3110 | |
| 3111 | // Stop checking the precise nature of the argument if it is value dependent, |
| 3112 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3113 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3114 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3115 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3116 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3117 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3118 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 3119 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3120 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3121 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3122 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3123 | return true; |
| 3124 | } |
| 3125 | |
| 3126 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3127 | |
| 3128 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3129 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 3130 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3131 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3132 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3133 | return true; |
| 3134 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3135 | |
| 3136 | // Cannot refer to non-static member functions |
| 3137 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3138 | if (!Method->isStatic()) { |
| 3139 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3140 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3141 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3142 | return true; |
| 3143 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3144 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3145 | // Functions must have external linkage. |
| 3146 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3147 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3148 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3149 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3150 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3151 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3152 | << true; |
| 3153 | return true; |
| 3154 | } |
| 3155 | |
| 3156 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3157 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3158 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3159 | // If the template parameter has pointer type, the function decays. |
| 3160 | if (ParamType->isPointerType() && !AddressTaken) |
| 3161 | ArgType = S.Context.getPointerType(Func->getType()); |
| 3162 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 3163 | // If we originally had an address-of operator, but the |
| 3164 | // parameter has reference type, complain and (if things look |
| 3165 | // like they will work) drop the address-of operator. |
| 3166 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 3167 | ParamType.getNonReferenceType())) { |
| 3168 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3169 | << ParamType; |
| 3170 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3171 | return true; |
| 3172 | } |
| 3173 | |
| 3174 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3175 | << ParamType |
| 3176 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3177 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3178 | |
| 3179 | ArgType = Func->getType(); |
| 3180 | } |
| 3181 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3182 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3183 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3184 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3185 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3186 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3187 | << true; |
| 3188 | return true; |
| 3189 | } |
| 3190 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3191 | // A value of reference type is not an object. |
| 3192 | if (Var->getType()->isReferenceType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3193 | S.Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3194 | diag::err_template_arg_reference_var) |
| 3195 | << Var->getType() << Arg->getSourceRange(); |
| 3196 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3197 | return true; |
| 3198 | } |
| 3199 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3200 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3201 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3202 | |
| 3203 | // If the template parameter has pointer type, we must have taken |
| 3204 | // the address of this object. |
| 3205 | if (ParamType->isReferenceType()) { |
| 3206 | if (AddressTaken) { |
| 3207 | // If we originally had an address-of operator, but the |
| 3208 | // parameter has reference type, complain and (if things look |
| 3209 | // like they will work) drop the address-of operator. |
| 3210 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3211 | ParamType.getNonReferenceType())) { |
| 3212 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3213 | << ParamType; |
| 3214 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3215 | return true; |
| 3216 | } |
| 3217 | |
| 3218 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3219 | << ParamType |
| 3220 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3221 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3222 | |
| 3223 | ArgType = Var->getType(); |
| 3224 | } |
| 3225 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3226 | if (Var->getType()->isArrayType()) { |
| 3227 | // Array-to-pointer decay. |
| 3228 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3229 | } else { |
| 3230 | // If the template parameter has pointer type but the address of |
| 3231 | // this object was not taken, complain and (possibly) recover by |
| 3232 | // taking the address of the entity. |
| 3233 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3234 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3235 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3236 | << ParamType; |
| 3237 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3238 | return true; |
| 3239 | } |
| 3240 | |
| 3241 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3242 | << ParamType |
| 3243 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3244 | |
| 3245 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3246 | } |
| 3247 | } |
| 3248 | } else { |
| 3249 | // We found something else, but we don't know specifically what it is. |
| 3250 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3251 | diag::err_template_arg_not_object_or_func) |
| 3252 | << Arg->getSourceRange(); |
| 3253 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3254 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3255 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3256 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3257 | if (ParamType->isPointerType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3258 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3259 | S.IsQualificationConversion(ArgType, ParamType, false)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3260 | // For pointer-to-object types, qualification conversions are |
| 3261 | // permitted. |
| 3262 | } else { |
| 3263 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3264 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3265 | // C++ [temp.arg.nontype]p5b3: |
| 3266 | // For a non-type template-parameter of type reference to |
| 3267 | // object, no conversions apply. The type referred to by the |
| 3268 | // reference may be more cv-qualified than the (otherwise |
| 3269 | // identical) type of the template- argument. The |
| 3270 | // template-parameter is bound directly to the |
| 3271 | // template-argument, which shall be an lvalue. |
| 3272 | |
| 3273 | // FIXME: Other qualifiers? |
| 3274 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3275 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3276 | |
| 3277 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 3278 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3279 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3280 | << ParamType << Arg->getType() |
| 3281 | << Arg->getSourceRange(); |
| 3282 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3283 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3284 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3285 | } |
| 3286 | } |
| 3287 | |
| 3288 | // At this point, the template argument refers to an object or |
| 3289 | // function with external linkage. We now need to check whether the |
| 3290 | // argument and parameter types are compatible. |
| 3291 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3292 | ParamType.getNonReferenceType())) { |
| 3293 | // We can't perform this conversion or binding. |
| 3294 | if (ParamType->isReferenceType()) |
| 3295 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 3296 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 3297 | else |
| 3298 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 3299 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 3300 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3301 | return true; |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | // Create the template argument. |
| 3306 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 3307 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3308 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
| 3311 | /// \brief Checks whether the given template argument is a pointer to |
| 3312 | /// member constant according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3313 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3314 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3315 | bool Invalid = false; |
| 3316 | |
| 3317 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3318 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3319 | Arg = Cast->getSubExpr(); |
| 3320 | |
| 3321 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3322 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3323 | // A template-argument for a non-type, non-template |
| 3324 | // template-parameter shall be one of: [...] |
| 3325 | // |
| 3326 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3327 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3328 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3329 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3330 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3331 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3332 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3333 | if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3334 | Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3335 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3336 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3337 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
| 3340 | Arg = Parens->getSubExpr(); |
| 3341 | } |
| 3342 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3343 | // A pointer-to-member constant written &Class::member. |
| 3344 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3345 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3346 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3347 | if (DRE && !DRE->getQualifier()) |
| 3348 | DRE = 0; |
| 3349 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3350 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3351 | // A constant of pointer-to-member type. |
| 3352 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3353 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3354 | if (VD->getType()->isMemberPointerType()) { |
| 3355 | if (isa<NonTypeTemplateParmDecl>(VD) || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3356 | (isa<VarDecl>(VD) && |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3357 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3358 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3359 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3360 | else |
| 3361 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3362 | return Invalid; |
| 3363 | } |
| 3364 | } |
| 3365 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3366 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3367 | DRE = 0; |
| 3368 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3369 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3370 | if (!DRE) |
| 3371 | return Diag(Arg->getSourceRange().getBegin(), |
| 3372 | diag::err_template_arg_not_pointer_to_member_form) |
| 3373 | << Arg->getSourceRange(); |
| 3374 | |
| 3375 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3376 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3377 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3378 | "Only non-static member pointers can make it here"); |
| 3379 | |
| 3380 | // Okay: this is the address of a non-static member, and therefore |
| 3381 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3382 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3383 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3384 | else |
| 3385 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3386 | return Invalid; |
| 3387 | } |
| 3388 | |
| 3389 | // 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] | 3390 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3391 | diag::err_template_arg_not_pointer_to_member_form) |
| 3392 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3393 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3394 | diag::note_template_arg_refers_here); |
| 3395 | return true; |
| 3396 | } |
| 3397 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3398 | /// \brief Check a template argument against its corresponding |
| 3399 | /// non-type template parameter. |
| 3400 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3401 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3402 | /// If an error occurred, it returns ExprError(); otherwise, it |
| 3403 | /// returns the converted template argument. \p |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3404 | /// InstantiatedParamType is the type of the non-type template |
| 3405 | /// parameter after it has been instantiated. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3406 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 3407 | QualType InstantiatedParamType, Expr *Arg, |
| 3408 | TemplateArgument &Converted, |
| 3409 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3410 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3411 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3412 | // If either the parameter has a dependent type or the argument is |
| 3413 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3414 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3415 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3416 | Converted = TemplateArgument(Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3417 | return Owned(Arg); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3418 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3419 | |
| 3420 | // C++ [temp.arg.nontype]p5: |
| 3421 | // The following conversions are performed on each expression used |
| 3422 | // as a non-type template-argument. If a non-type |
| 3423 | // template-argument cannot be converted to the type of the |
| 3424 | // corresponding template-parameter then the program is |
| 3425 | // ill-formed. |
| 3426 | // |
| 3427 | // -- for a non-type template-parameter of integral or |
| 3428 | // enumeration type, integral promotions (4.5) and integral |
| 3429 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3430 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3431 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3432 | if (ParamType->isIntegralOrEnumerationType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3433 | // C++ [temp.arg.nontype]p1: |
| 3434 | // A template-argument for a non-type, non-template |
| 3435 | // template-parameter shall be one of: |
| 3436 | // |
| 3437 | // -- an integral constant-expression of integral or enumeration |
| 3438 | // type; or |
| 3439 | // -- the name of a non-type template-parameter; or |
| 3440 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3441 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3442 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3443 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3444 | diag::err_template_arg_not_integral_or_enumeral) |
| 3445 | << ArgType << Arg->getSourceRange(); |
| 3446 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3447 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3448 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3449 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3450 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 3451 | << ArgType << Arg->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3452 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3455 | // From here on out, all we care about are the unqualified forms |
| 3456 | // of the parameter and argument types. |
| 3457 | ParamType = ParamType.getUnqualifiedType(); |
| 3458 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3459 | |
| 3460 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3461 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3462 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3463 | } else if (CTAK == CTAK_Deduced) { |
| 3464 | // C++ [temp.deduct.type]p17: |
| 3465 | // If, in the declaration of a function template with a non-type |
| 3466 | // template-parameter, the non-type template- parameter is used |
| 3467 | // in an expression in the function parameter-list and, if the |
| 3468 | // corresponding template-argument is deduced, the |
| 3469 | // template-argument type shall match the type of the |
| 3470 | // template-parameter exactly, except that a template-argument |
| 3471 | // deduced from an array bound may be of any integral type. |
| 3472 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3473 | << ArgType << ParamType; |
| 3474 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3475 | return ExprError(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3476 | } else if (ParamType->isBooleanType()) { |
| 3477 | // This is an integral-to-boolean conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3478 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3479 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3480 | !ParamType->isEnumeralType()) { |
| 3481 | // This is an integral promotion or conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3482 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3483 | } else { |
| 3484 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3485 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3486 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3487 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3488 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3489 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3492 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3493 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3494 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3495 | |
| 3496 | if (!Arg->isValueDependent()) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3497 | llvm::APSInt OldValue = Value; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3498 | |
| 3499 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3500 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3501 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3502 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3503 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3504 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3505 | |
| 3506 | // Complain if an unsigned parameter received a negative value. |
| 3507 | if (IntegerType->isUnsignedIntegerType() |
| 3508 | && (OldValue.isSigned() && OldValue.isNegative())) { |
| 3509 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3510 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3511 | << Arg->getSourceRange(); |
| 3512 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3513 | } |
| 3514 | |
| 3515 | // Complain if we overflowed the template parameter's type. |
| 3516 | unsigned RequiredBits; |
| 3517 | if (IntegerType->isUnsignedIntegerType()) |
| 3518 | RequiredBits = OldValue.getActiveBits(); |
| 3519 | else if (OldValue.isUnsigned()) |
| 3520 | RequiredBits = OldValue.getActiveBits() + 1; |
| 3521 | else |
| 3522 | RequiredBits = OldValue.getMinSignedBits(); |
| 3523 | if (RequiredBits > AllowedBits) { |
| 3524 | Diag(Arg->getSourceRange().getBegin(), |
| 3525 | diag::warn_template_arg_too_large) |
| 3526 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3527 | << Arg->getSourceRange(); |
| 3528 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3529 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3530 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3531 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3532 | // Add the value of this argument to the list of converted |
| 3533 | // arguments. We use the bitwidth and signedness of the template |
| 3534 | // parameter. |
| 3535 | if (Arg->isValueDependent()) { |
| 3536 | // The argument is value-dependent. Create a new |
| 3537 | // TemplateArgument with the converted expression. |
| 3538 | Converted = TemplateArgument(Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3539 | return Owned(Arg); |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3542 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3543 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3544 | : IntegerType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3545 | return Owned(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3546 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3547 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3548 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 3549 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3550 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 3551 | // from a template argument of type std::nullptr_t to a non-type |
| 3552 | // template parameter of type pointer to object, pointer to |
| 3553 | // function, or pointer-to-member, respectively. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3554 | if (ArgType->isNullPtrType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3555 | (ParamType->isPointerType() || ParamType->isMemberPointerType())) { |
| 3556 | Converted = TemplateArgument((NamedDecl *)0); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3557 | return Owned(Arg); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3560 | // Handle pointer-to-function, reference-to-function, and |
| 3561 | // pointer-to-member-function all in (roughly) the same way. |
| 3562 | if (// -- For a non-type template-parameter of type pointer to |
| 3563 | // function, only the function-to-pointer conversion (4.3) is |
| 3564 | // applied. If the template-argument represents a set of |
| 3565 | // overloaded functions (or a pointer to such), the matching |
| 3566 | // function is selected from the set (13.4). |
| 3567 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3568 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3569 | // -- For a non-type template-parameter of type reference to |
| 3570 | // function, no conversions apply. If the template-argument |
| 3571 | // represents a set of overloaded functions, the matching |
| 3572 | // function is selected from the set (13.4). |
| 3573 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3574 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3575 | // -- For a non-type template-parameter of type pointer to |
| 3576 | // member function, no conversions apply. If the |
| 3577 | // template-argument represents a set of overloaded member |
| 3578 | // functions, the matching member function is selected from |
| 3579 | // the set (13.4). |
| 3580 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3581 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3582 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3583 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3584 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3585 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3586 | true, |
| 3587 | FoundResult)) { |
| 3588 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3589 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3590 | |
| 3591 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3592 | ArgType = Arg->getType(); |
| 3593 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3594 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3595 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3596 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3597 | if (!ParamType->isMemberPointerType()) { |
| 3598 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3599 | ParamType, |
| 3600 | Arg, Converted)) |
| 3601 | return ExprError(); |
| 3602 | return Owned(Arg); |
| 3603 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3604 | |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3605 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(), |
| 3606 | false)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3607 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3608 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 3609 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3610 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3611 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3612 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3613 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3614 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3615 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3616 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3617 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3618 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 3619 | return ExprError(); |
| 3620 | return Owned(Arg); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 3623 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3624 | // -- for a non-type template-parameter of type pointer to |
| 3625 | // object, qualification conversions (4.4) and the |
| 3626 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3627 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3628 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3629 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3630 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3631 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3632 | ParamType, |
| 3633 | Arg, Converted)) |
| 3634 | return ExprError(); |
| 3635 | return Owned(Arg); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3636 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3637 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3638 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3639 | // -- For a non-type template-parameter of type reference to |
| 3640 | // object, no conversions apply. The type referred to by the |
| 3641 | // reference may be more cv-qualified than the (otherwise |
| 3642 | // identical) type of the template-argument. The |
| 3643 | // template-parameter is bound directly to the |
| 3644 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3645 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3646 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3647 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3648 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3649 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 3650 | ParamRefType->getPointeeType(), |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3651 | true, |
| 3652 | FoundResult)) { |
| 3653 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3654 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3655 | |
| 3656 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3657 | ArgType = Arg->getType(); |
| 3658 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3659 | return ExprError(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3660 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3661 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3662 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3663 | ParamType, |
| 3664 | Arg, Converted)) |
| 3665 | return ExprError(); |
| 3666 | return Owned(Arg); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3667 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3668 | |
| 3669 | // -- For a non-type template-parameter of type pointer to data |
| 3670 | // member, qualification conversions (4.4) are applied. |
| 3671 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 3672 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 3673 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3674 | // Types match exactly: nothing more to do here. |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3675 | } else if (IsQualificationConversion(ArgType, ParamType, false)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3676 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3677 | } else { |
| 3678 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3679 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3680 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3681 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3682 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3683 | return ExprError(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3684 | } |
| 3685 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3686 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 3687 | return ExprError(); |
| 3688 | return Owned(Arg); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3689 | } |
| 3690 | |
| 3691 | /// \brief Check a template argument against its corresponding |
| 3692 | /// template template parameter. |
| 3693 | /// |
| 3694 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 3695 | /// It returns true if an error occurred, and false otherwise. |
| 3696 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3697 | const TemplateArgumentLoc &Arg) { |
| 3698 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 3699 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 3700 | if (!Template) { |
| 3701 | // Any dependent template name is fine. |
| 3702 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 3703 | return false; |
| 3704 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3705 | |
| 3706 | // C++ [temp.arg.template]p1: |
| 3707 | // A template-argument for a template template-parameter shall be |
| 3708 | // the name of a class template, expressed as id-expression. Only |
| 3709 | // primary class templates are considered when matching the |
| 3710 | // template template argument with the corresponding parameter; |
| 3711 | // partial specializations are not considered even if their |
| 3712 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3713 | // |
| 3714 | // Note that we also allow template template parameters here, which |
| 3715 | // will happen when we are dealing with, e.g., class template |
| 3716 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3717 | if (!isa<ClassTemplateDecl>(Template) && |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3718 | !isa<TemplateTemplateParmDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3719 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3720 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3721 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3722 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3723 | << Template; |
| 3724 | } |
| 3725 | |
| 3726 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 3727 | Param->getTemplateParameters(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3728 | true, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3729 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3730 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3731 | } |
| 3732 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3733 | /// \brief Given a non-type template argument that refers to a |
| 3734 | /// declaration and the type of its corresponding non-type template |
| 3735 | /// parameter, produce an expression that properly refers to that |
| 3736 | /// declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3737 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3738 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 3739 | QualType ParamType, |
| 3740 | SourceLocation Loc) { |
| 3741 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 3742 | "Only declaration template arguments permitted here"); |
| 3743 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 3744 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3745 | if (VD->getDeclContext()->isRecord() && |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3746 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 3747 | // If the value is a class member, we might have a pointer-to-member. |
| 3748 | // Determine whether the non-type template template parameter is of |
| 3749 | // pointer-to-member type. If so, we need to build an appropriate |
| 3750 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 3751 | // would refer to the member itself. |
| 3752 | if (ParamType->isMemberPointerType()) { |
| 3753 | QualType ClassType |
| 3754 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 3755 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3756 | = NestedNameSpecifier::Create(Context, 0, false, |
| 3757 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3758 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 3759 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3760 | |
| 3761 | // The actual value-ness of this is unimportant, but for |
| 3762 | // internal consistency's sake, references to instance methods |
| 3763 | // are r-values. |
| 3764 | ExprValueKind VK = VK_LValue; |
| 3765 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 3766 | VK = VK_RValue; |
| 3767 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3768 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3769 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3770 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3771 | Loc, |
| 3772 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3773 | if (RefExpr.isInvalid()) |
| 3774 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3775 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3776 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3777 | |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3778 | // We might need to perform a trailing qualification conversion, since |
| 3779 | // the element type on the parameter could be more qualified than the |
| 3780 | // element type in the expression we constructed. |
| 3781 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3782 | ParamType.getUnqualifiedType(), false)) |
| 3783 | RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3784 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3785 | assert(!RefExpr.isInvalid() && |
| 3786 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3787 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3788 | return move(RefExpr); |
| 3789 | } |
| 3790 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3791 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3792 | QualType T = VD->getType().getNonReferenceType(); |
| 3793 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3794 | // When the non-type template parameter is a pointer, take the |
| 3795 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3796 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3797 | if (RefExpr.isInvalid()) |
| 3798 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3799 | |
| 3800 | if (T->isFunctionType() || T->isArrayType()) { |
| 3801 | // Decay functions and arrays. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3802 | RefExpr = DefaultFunctionArrayConversion(RefExpr.take()); |
| 3803 | if (RefExpr.isInvalid()) |
| 3804 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3805 | |
| 3806 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3807 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3808 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3809 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3810 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3811 | } |
| 3812 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3813 | ExprValueKind VK = VK_RValue; |
| 3814 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3815 | // If the non-type template parameter has reference type, qualify the |
| 3816 | // resulting declaration reference with the extra qualifiers on the |
| 3817 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3818 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 3819 | VK = VK_LValue; |
| 3820 | T = Context.getQualifiedType(T, |
| 3821 | TargetRef->getPointeeType().getQualifiers()); |
| 3822 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3823 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3824 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
| 3827 | /// \brief Construct a new expression that refers to the given |
| 3828 | /// integral template argument with the given source-location |
| 3829 | /// information. |
| 3830 | /// |
| 3831 | /// This routine takes care of the mapping from an integral template |
| 3832 | /// argument (which may have any integral type) to the appropriate |
| 3833 | /// literal value. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3834 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3835 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 3836 | SourceLocation Loc) { |
| 3837 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3838 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3839 | QualType T = Arg.getIntegralType(); |
| 3840 | if (T->isCharType() || T->isWideCharType()) |
| 3841 | return Owned(new (Context) CharacterLiteral( |
| 3842 | Arg.getAsIntegral()->getZExtValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame^] | 3843 | T->isWideCharType(), T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3844 | if (T->isBooleanType()) |
| 3845 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 3846 | Arg.getAsIntegral()->getBoolValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame^] | 3847 | T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3848 | |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame^] | 3849 | // If this is an enum type that we're instantiating, we need to use an integer |
| 3850 | // type the same size as the enumerator. We don't want to build an |
| 3851 | // IntegerLiteral with enum type. |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 3852 | QualType BT; |
| 3853 | if (const EnumType *ET = T->getAs<EnumType>()) |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame^] | 3854 | BT = ET->getDecl()->getIntegerType(); |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 3855 | else |
| 3856 | BT = T; |
| 3857 | |
| 3858 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
Douglas Gregor | 8f5667d | 2011-02-18 02:12:44 +0000 | [diff] [blame] | 3859 | if (T->isEnumeralType()) { |
| 3860 | // FIXME: This is a hack. We need a better way to handle substituted |
| 3861 | // non-type template parameters. |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame^] | 3862 | E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0, |
| 3863 | Context.getTrivialTypeSourceInfo(T, Loc), |
| 3864 | Loc, Loc); |
Douglas Gregor | 8f5667d | 2011-02-18 02:12:44 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 3867 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3870 | /// \brief Match two template parameters within template parameter lists. |
| 3871 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 3872 | bool Complain, |
| 3873 | Sema::TemplateParameterListEqualKind Kind, |
| 3874 | SourceLocation TemplateArgLoc) { |
| 3875 | // Check the actual kind (type, non-type, template). |
| 3876 | if (Old->getKind() != New->getKind()) { |
| 3877 | if (Complain) { |
| 3878 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 3879 | if (TemplateArgLoc.isValid()) { |
| 3880 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3881 | NextDiag = diag::note_template_param_different_kind; |
| 3882 | } |
| 3883 | S.Diag(New->getLocation(), NextDiag) |
| 3884 | << (Kind != Sema::TPL_TemplateMatch); |
| 3885 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 3886 | << (Kind != Sema::TPL_TemplateMatch); |
| 3887 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3888 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3889 | return false; |
| 3890 | } |
| 3891 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3892 | // Check that both are parameter packs are neither are parameter packs. |
| 3893 | // However, if we are matching a template template argument to a |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3894 | // template template parameter, the template template parameter can have |
| 3895 | // a parameter pack where the template template argument does not. |
| 3896 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 3897 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 3898 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3899 | if (Complain) { |
| 3900 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 3901 | if (TemplateArgLoc.isValid()) { |
| 3902 | S.Diag(TemplateArgLoc, |
| 3903 | diag::err_template_arg_template_params_mismatch); |
| 3904 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 3905 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3906 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3907 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 3908 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 3909 | : 2; |
| 3910 | S.Diag(New->getLocation(), NextDiag) |
| 3911 | << ParamKind << New->isParameterPack(); |
| 3912 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 3913 | << ParamKind << Old->isParameterPack(); |
| 3914 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3915 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3916 | return false; |
| 3917 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3918 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3919 | // For non-type template parameters, check the type of the parameter. |
| 3920 | if (NonTypeTemplateParmDecl *OldNTTP |
| 3921 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 3922 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3923 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3924 | // If we are matching a template template argument to a template |
| 3925 | // template parameter and one of the non-type template parameter types |
| 3926 | // is dependent, then we must wait until template instantiation time |
| 3927 | // to actually compare the arguments. |
| 3928 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 3929 | (OldNTTP->getType()->isDependentType() || |
| 3930 | NewNTTP->getType()->isDependentType())) |
| 3931 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3932 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3933 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 3934 | if (Complain) { |
| 3935 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 3936 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3937 | S.Diag(TemplateArgLoc, |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3938 | diag::err_template_arg_template_params_mismatch); |
| 3939 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 3940 | } |
| 3941 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 3942 | << NewNTTP->getType() |
| 3943 | << (Kind != Sema::TPL_TemplateMatch); |
| 3944 | S.Diag(OldNTTP->getLocation(), |
| 3945 | diag::note_template_nontype_parm_prev_declaration) |
| 3946 | << OldNTTP->getType(); |
| 3947 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3948 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3949 | return false; |
| 3950 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3951 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3952 | return true; |
| 3953 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3954 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3955 | // For template template parameters, check the template parameter types. |
| 3956 | // The template parameter lists of template template |
| 3957 | // parameters must agree. |
| 3958 | if (TemplateTemplateParmDecl *OldTTP |
| 3959 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3960 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3961 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 3962 | OldTTP->getTemplateParameters(), |
| 3963 | Complain, |
| 3964 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3965 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3966 | : Kind), |
| 3967 | TemplateArgLoc); |
| 3968 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3969 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3970 | return true; |
| 3971 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3972 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3973 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 3974 | /// lists. |
| 3975 | static |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3976 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3977 | TemplateParameterList *New, |
| 3978 | TemplateParameterList *Old, |
| 3979 | Sema::TemplateParameterListEqualKind Kind, |
| 3980 | SourceLocation TemplateArgLoc) { |
| 3981 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 3982 | if (TemplateArgLoc.isValid()) { |
| 3983 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3984 | NextDiag = diag::note_template_param_list_different_arity; |
| 3985 | } |
| 3986 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 3987 | << (New->size() > Old->size()) |
| 3988 | << (Kind != Sema::TPL_TemplateMatch) |
| 3989 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 3990 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 3991 | << (Kind != Sema::TPL_TemplateMatch) |
| 3992 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 3993 | } |
| 3994 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3995 | /// \brief Determine whether the given template parameter lists are |
| 3996 | /// equivalent. |
| 3997 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3998 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3999 | /// source code as part of a new template declaration. |
| 4000 | /// |
| 4001 | /// \param Old The old template parameter list, typically found via |
| 4002 | /// name lookup of the template declared with this template parameter |
| 4003 | /// list. |
| 4004 | /// |
| 4005 | /// \param Complain If true, this routine will produce a diagnostic if |
| 4006 | /// the template parameter lists are not equivalent. |
| 4007 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4008 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4009 | /// |
| 4010 | /// \param TemplateArgLoc If this source location is valid, then we |
| 4011 | /// are actually checking the template parameter list of a template |
| 4012 | /// argument (New) against the template parameter list of its |
| 4013 | /// corresponding template template parameter (Old). We produce |
| 4014 | /// slightly different diagnostics in this scenario. |
| 4015 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4016 | /// \returns True if the template parameter lists are equal, false |
| 4017 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4018 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4019 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 4020 | TemplateParameterList *Old, |
| 4021 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4022 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4023 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4024 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 4025 | if (Complain) |
| 4026 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4027 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4028 | |
| 4029 | return false; |
| 4030 | } |
| 4031 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4032 | // C++0x [temp.arg.template]p3: |
| 4033 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4034 | // when each of the template parameters in the template-parameter-list of |
| 4035 | // the template-argument's corresponding class template or template alias |
| 4036 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4037 | // template-parameter-list of P. [...] |
| 4038 | TemplateParameterList::iterator NewParm = New->begin(); |
| 4039 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4040 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4041 | OldParmEnd = Old->end(); |
| 4042 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 4043 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 4044 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4045 | if (NewParm == NewParmEnd) { |
| 4046 | if (Complain) |
| 4047 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4048 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4049 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4050 | return false; |
| 4051 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4052 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4053 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4054 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4055 | return false; |
| 4056 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4057 | ++NewParm; |
| 4058 | continue; |
| 4059 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4060 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4061 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4062 | // [...] When P's template- parameter-list contains a template parameter |
| 4063 | // pack (14.5.3), the template parameter pack will match zero or more |
| 4064 | // template parameters or template parameter packs in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4065 | // template-parameter-list of A with the same type and form as the |
| 4066 | // template parameter pack in P (ignoring whether those template |
| 4067 | // parameters are template parameter packs). |
| 4068 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 4069 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4070 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4071 | return false; |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4072 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4073 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4074 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4075 | // Make sure we exhausted all of the arguments. |
| 4076 | if (NewParm != NewParmEnd) { |
| 4077 | if (Complain) |
| 4078 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4079 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4080 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4081 | return false; |
| 4082 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4083 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4084 | return true; |
| 4085 | } |
| 4086 | |
| 4087 | /// \brief Check whether a template can be declared within this scope. |
| 4088 | /// |
| 4089 | /// If the template declaration is valid in this scope, returns |
| 4090 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4091 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4092 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4093 | // Find the nearest enclosing declaration scope. |
| 4094 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4095 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4096 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4097 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4098 | // C++ [temp]p2: |
| 4099 | // A template-declaration can appear only as a namespace scope or |
| 4100 | // class scope declaration. |
| 4101 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4102 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 4103 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4104 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4105 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4106 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4107 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4108 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4109 | |
| 4110 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 4111 | return false; |
| 4112 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4113 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4114 | diag::err_template_outside_namespace_or_class_scope) |
| 4115 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4116 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4117 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4118 | /// \brief Determine what kind of template specialization the given declaration |
| 4119 | /// is. |
| 4120 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 4121 | if (!D) |
| 4122 | return TSK_Undeclared; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4123 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4124 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 4125 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4126 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 4127 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4128 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 4129 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4130 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4131 | return TSK_Undeclared; |
| 4132 | } |
| 4133 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4134 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4135 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4136 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4137 | /// This routine determines whether a template specialization can be declared |
| 4138 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4139 | /// |
| 4140 | /// \param S the semantic analysis object for which this check is being |
| 4141 | /// performed. |
| 4142 | /// |
| 4143 | /// \param Specialized the entity being specialized or instantiated, which |
| 4144 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4145 | /// a member of a class template (member function, static data member, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4146 | /// member class). |
| 4147 | /// |
| 4148 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 4149 | /// |
| 4150 | /// \param Loc the location of the explicit specialization or instantiation of |
| 4151 | /// this entity. |
| 4152 | /// |
| 4153 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 4154 | /// a class template. |
| 4155 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4156 | /// \returns true if there was an error that we cannot recover from, false |
| 4157 | /// otherwise. |
| 4158 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 4159 | NamedDecl *Specialized, |
| 4160 | NamedDecl *PrevDecl, |
| 4161 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4162 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4163 | // Keep these "kind" numbers in sync with the %select statements in the |
| 4164 | // various diagnostics emitted by this routine. |
| 4165 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4166 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4167 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4168 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4169 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4170 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4171 | EntityKind = 3; |
| 4172 | else if (isa<VarDecl>(Specialized)) |
| 4173 | EntityKind = 4; |
| 4174 | else if (isa<RecordDecl>(Specialized)) |
| 4175 | EntityKind = 5; |
| 4176 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4177 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 4178 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4179 | return true; |
| 4180 | } |
| 4181 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4182 | // C++ [temp.expl.spec]p2: |
| 4183 | // An explicit specialization shall be declared in the namespace |
| 4184 | // of which the template is a member, or, for member templates, in |
| 4185 | // the namespace of which the enclosing class or enclosing class |
| 4186 | // template is a member. An explicit specialization of a member |
| 4187 | // function, member class or static data member of a class |
| 4188 | // template shall be declared in the namespace of which the class |
| 4189 | // template is a member. Such a declaration may also be a |
| 4190 | // definition. If the declaration is not a definition, the |
| 4191 | // specialization may be defined later in the name- space in which |
| 4192 | // the explicit specialization was declared, or in a namespace |
| 4193 | // that encloses the one in which the explicit specialization was |
| 4194 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4195 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4196 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4197 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4198 | return true; |
| 4199 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4200 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4201 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 4202 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4203 | << Specialized; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4204 | return true; |
| 4205 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4206 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4207 | // C++ [temp.class.spec]p6: |
| 4208 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4209 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4210 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4211 | bool ComplainedAboutScope = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4212 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4213 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4214 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4215 | if ((!PrevDecl || |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4216 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4217 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4218 | // C++ [temp.exp.spec]p2: |
| 4219 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4220 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4221 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4222 | // An explicit specialization of a member function, member class or |
| 4223 | // static data member of a class template shall be declared in the |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4224 | // namespace of which the class template is a member. |
| 4225 | // |
| 4226 | // C++0x [temp.expl.spec]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4227 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4228 | // the specialized template. |
| 4229 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) && |
| 4230 | !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) { |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4231 | bool IsCPlusPlus0xExtension |
| 4232 | = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4233 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4234 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4235 | ? diag::ext_template_spec_decl_out_of_scope_global |
| 4236 | : diag::err_template_spec_decl_out_of_scope_global) |
| 4237 | << EntityKind << Specialized; |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4238 | else if (isa<NamespaceDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4239 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4240 | ? diag::ext_template_spec_decl_out_of_scope |
| 4241 | : diag::err_template_spec_decl_out_of_scope) |
| 4242 | << EntityKind << Specialized |
| 4243 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4244 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4245 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 4246 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4247 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4248 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4249 | |
| 4250 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4251 | // namespace. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4252 | // Note that HandleDeclarator() performs this check for explicit |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4253 | // specializations of function templates, static data members, and member |
| 4254 | // functions, so we skip the check here for those kinds of entities. |
| 4255 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4256 | // Should we refactor that check, so that it occurs later? |
| 4257 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4258 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 4259 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4260 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 4261 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 4262 | << EntityKind << Specialized; |
| 4263 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 4264 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 4265 | << EntityKind << Specialized |
| 4266 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4267 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4268 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4269 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4270 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4271 | // FIXME: check for specialization-after-instantiation errors and such. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4272 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4273 | return false; |
| 4274 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4275 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4276 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 4277 | /// that checks non-type template partial specialization arguments. |
| 4278 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 4279 | NonTypeTemplateParmDecl *Param, |
| 4280 | const TemplateArgument *Args, |
| 4281 | unsigned NumArgs) { |
| 4282 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4283 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 4284 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4285 | Args[I].pack_begin(), |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4286 | Args[I].pack_size())) |
| 4287 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4288 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4289 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4290 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4291 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4292 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4293 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4294 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4295 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4296 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4297 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4298 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4299 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4300 | |
| 4301 | // Strip off any implicit casts we added as part of type checking. |
| 4302 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4303 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4304 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4305 | // C++ [temp.class.spec]p8: |
| 4306 | // A non-type argument is non-specialized if it is the name of a |
| 4307 | // non-type parameter. All other non-type arguments are |
| 4308 | // specialized. |
| 4309 | // |
| 4310 | // Below, we check the two conditions that only apply to |
| 4311 | // specialized non-type arguments, so skip any non-specialized |
| 4312 | // arguments. |
| 4313 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4314 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4315 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4316 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4317 | // C++ [temp.class.spec]p9: |
| 4318 | // Within the argument list of a class template partial |
| 4319 | // specialization, the following restrictions apply: |
| 4320 | // -- A partially specialized non-type argument expression |
| 4321 | // shall not involve a template parameter of the partial |
| 4322 | // specialization except when the argument expression is a |
| 4323 | // simple identifier. |
| 4324 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4325 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4326 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4327 | << ArgExpr->getSourceRange(); |
| 4328 | return true; |
| 4329 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4330 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4331 | // -- The type of a template parameter corresponding to a |
| 4332 | // specialized non-type argument shall not be dependent on a |
| 4333 | // parameter of the specialization. |
| 4334 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4335 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4336 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 4337 | << Param->getType() |
| 4338 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4339 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4340 | return true; |
| 4341 | } |
| 4342 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4343 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4344 | return false; |
| 4345 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4346 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4347 | /// \brief Check the non-type template arguments of a class template |
| 4348 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 4349 | /// |
| 4350 | /// \param TemplateParams the template parameters of the primary class |
| 4351 | /// template. |
| 4352 | /// |
| 4353 | /// \param TemplateArg the template arguments of the class template |
| 4354 | /// partial specialization. |
| 4355 | /// |
| 4356 | /// \returns true if there was an error, false otherwise. |
| 4357 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 4358 | TemplateParameterList *TemplateParams, |
| 4359 | llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
| 4360 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 4361 | |
| 4362 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4363 | NonTypeTemplateParmDecl *Param |
| 4364 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 4365 | if (!Param) |
| 4366 | continue; |
| 4367 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4368 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4369 | &ArgList[I], 1)) |
| 4370 | return true; |
| 4371 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4372 | |
| 4373 | return false; |
| 4374 | } |
| 4375 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4376 | /// \brief Retrieve the previous declaration of the given declaration. |
| 4377 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 4378 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4379 | return VD->getPreviousDeclaration(); |
| 4380 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 4381 | return FD->getPreviousDeclaration(); |
| 4382 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 4383 | return TD->getPreviousDeclaration(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4384 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4385 | return TD->getPreviousDeclaration(); |
| 4386 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 4387 | return FTD->getPreviousDeclaration(); |
| 4388 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 4389 | return CTD->getPreviousDeclaration(); |
| 4390 | return 0; |
| 4391 | } |
| 4392 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4393 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4394 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4395 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4396 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4397 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4398 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4399 | SourceLocation TemplateNameLoc, |
| 4400 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4401 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4402 | SourceLocation RAngleLoc, |
| 4403 | AttributeList *Attr, |
| 4404 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4405 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4406 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4407 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 4408 | // store the location of the outermost template keyword in the declaration. |
| 4409 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
| 4410 | ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation(); |
| 4411 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4412 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4413 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4414 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4415 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4416 | |
| 4417 | if (!ClassTemplate) { |
| 4418 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4419 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4420 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4421 | return true; |
| 4422 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4423 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4424 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4425 | bool isPartialSpecialization = false; |
| 4426 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4427 | // Check the validity of the template headers that introduce this |
| 4428 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4429 | // FIXME: We probably shouldn't complain about these headers for |
| 4430 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4431 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4432 | TemplateParameterList *TemplateParams |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4433 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, |
| 4434 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4435 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4436 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4437 | isExplicitSpecialization, |
| 4438 | Invalid); |
| 4439 | if (Invalid) |
| 4440 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4441 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4442 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4443 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4444 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4445 | if (TUK == TUK_Friend) { |
| 4446 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4447 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4448 | return true; |
| 4449 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4450 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4451 | // C++ [temp.class.spec]p10: |
| 4452 | // The template parameter list of a specialization shall not |
| 4453 | // contain default template argument values. |
| 4454 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4455 | Decl *Param = TemplateParams->getParam(I); |
| 4456 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4457 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4458 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4459 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4460 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4461 | } |
| 4462 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4463 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4464 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4465 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4466 | diag::err_default_arg_in_partial_spec) |
| 4467 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4468 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4469 | } |
| 4470 | } else { |
| 4471 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4472 | if (TTP->hasDefaultArgument()) { |
| 4473 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4474 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4475 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4476 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4477 | } |
| 4478 | } |
| 4479 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4480 | } else if (TemplateParams) { |
| 4481 | if (TUK == TUK_Friend) |
| 4482 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4483 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4484 | SourceRange(TemplateParams->getTemplateLoc(), |
| 4485 | TemplateParams->getRAngleLoc())) |
| 4486 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4487 | else |
| 4488 | isExplicitSpecialization = true; |
| 4489 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4490 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4491 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4492 | isExplicitSpecialization = true; |
| 4493 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4494 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4495 | // Check that the specialization uses the same tag kind as the |
| 4496 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4497 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4498 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4499 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4500 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4501 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4502 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4503 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4504 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4505 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4506 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4507 | diag::note_previous_use); |
| 4508 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4509 | } |
| 4510 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4511 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4512 | TemplateArgumentListInfo TemplateArgs; |
| 4513 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 4514 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4515 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4516 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4517 | // Check for unexpanded parameter packs in any of the template arguments. |
| 4518 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4519 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4520 | UPPC_PartialSpecialization)) |
| 4521 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4522 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4523 | // Check that the template argument list is well-formed for this |
| 4524 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4525 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4526 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4527 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4528 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4529 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4530 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4531 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4532 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4533 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4534 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4535 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4536 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4537 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4538 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4539 | return true; |
| 4540 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4541 | if (!Name.isDependent() && |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4542 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4543 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4544 | TemplateArgs.size())) { |
| 4545 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 4546 | << ClassTemplate->getDeclName(); |
| 4547 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4548 | } |
| 4549 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4550 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4551 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4552 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 4553 | |
| 4554 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4555 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4556 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4557 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 4558 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4559 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4560 | else |
| 4561 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4562 | = ClassTemplate->findSpecialization(Converted.data(), |
| 4563 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4564 | |
| 4565 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4566 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4567 | // Check whether we can declare a class template specialization in |
| 4568 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4569 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4570 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 4571 | TemplateNameLoc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4572 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4573 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4574 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4575 | // The canonical type |
| 4576 | QualType CanonType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4577 | if (PrevDecl && |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4578 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4579 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4580 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4581 | // arguments was referenced but not declared, or we're only |
| 4582 | // referencing this specialization as a friend, reuse that |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4583 | // declaration node as our own, updating its source location and |
| 4584 | // the list of outer template parameters to reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4585 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4586 | Specialization->setLocation(TemplateNameLoc); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4587 | if (TemplateParameterLists.size() > 0) { |
| 4588 | Specialization->setTemplateParameterListsInfo(Context, |
| 4589 | TemplateParameterLists.size(), |
| 4590 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4591 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4592 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4593 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4594 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4595 | // Build the canonical type that describes the converted template |
| 4596 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4597 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4598 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4599 | Converted.data(), |
| 4600 | Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4601 | |
| 4602 | if (Context.hasSameType(CanonType, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4603 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 4604 | // C++ [temp.class.spec]p9b3: |
| 4605 | // |
| 4606 | // -- The argument list of the specialization shall not be identical |
| 4607 | // to the implicit argument list of the primary template. |
| 4608 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 4609 | << (TUK == TUK_Definition) |
| 4610 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 4611 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 4612 | ClassTemplate->getIdentifier(), |
| 4613 | TemplateNameLoc, |
| 4614 | Attr, |
| 4615 | TemplateParams, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 4616 | AS_none, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4617 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 4618 | (TemplateParameterList**) TemplateParameterLists.release()); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4619 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4620 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4621 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4622 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 4623 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4624 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4625 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4626 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4627 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4628 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4629 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 4630 | TemplateParams, |
| 4631 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4632 | Converted.data(), |
| 4633 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4634 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4635 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4636 | PrevPartial, |
| 4637 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4638 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4639 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4640 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4641 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4642 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4643 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4644 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4645 | if (!PrevPartial) |
| 4646 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4647 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4648 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4649 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4650 | // template specialization, make a note of that. |
| 4651 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 4652 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4653 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4654 | // Check that all of the template parameters of the class template |
| 4655 | // partial specialization are deducible from the template |
| 4656 | // arguments. If not, this class template partial specialization |
| 4657 | // will never be used. |
| 4658 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 4659 | DeducibleParams.resize(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4660 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4661 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4662 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4663 | unsigned NumNonDeducible = 0; |
| 4664 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 4665 | if (!DeducibleParams[I]) |
| 4666 | ++NumNonDeducible; |
| 4667 | |
| 4668 | if (NumNonDeducible) { |
| 4669 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 4670 | << (NumNonDeducible > 1) |
| 4671 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 4672 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 4673 | if (!DeducibleParams[I]) { |
| 4674 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 4675 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4676 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4677 | diag::note_partial_spec_unused_parameter) |
| 4678 | << Param->getDeclName(); |
| 4679 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4680 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4681 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 4682 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4683 | } |
| 4684 | } |
| 4685 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4686 | } else { |
| 4687 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4688 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4689 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4690 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4691 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4692 | KWLoc, TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4693 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4694 | Converted.data(), |
| 4695 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4696 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4697 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4698 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4699 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4700 | TemplateParameterLists.size(), |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4701 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4702 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4703 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4704 | if (!PrevDecl) |
| 4705 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4706 | |
| 4707 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4710 | // C++ [temp.expl.spec]p6: |
| 4711 | // 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] | 4712 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4713 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4714 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4715 | // use occurs; no diagnostic is required. |
| 4716 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4717 | bool Okay = false; |
| 4718 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4719 | // Is there any previous explicit specialization declaration? |
| 4720 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 4721 | Okay = true; |
| 4722 | break; |
| 4723 | } |
| 4724 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4725 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4726 | if (!Okay) { |
| 4727 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 4728 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 4729 | << Context.getTypeDeclType(Specialization) << Range; |
| 4730 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4731 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4732 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4733 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4734 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4735 | return true; |
| 4736 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4737 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4738 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4739 | // If this is not a friend, note that this is an explicit specialization. |
| 4740 | if (TUK != TUK_Friend) |
| 4741 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4742 | |
| 4743 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4744 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4745 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4746 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4747 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4748 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4749 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 4750 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4751 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4752 | } |
| 4753 | } |
| 4754 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 4755 | if (Attr) |
| 4756 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 4757 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 4758 | // Build the fully-sugared type for this class template |
| 4759 | // specialization as the user wrote in the specialization |
| 4760 | // itself. This means that we'll pretty-print the type retrieved |
| 4761 | // from the specialization's declaration the way that the user |
| 4762 | // actually wrote the specialization, rather than formatting the |
| 4763 | // name based on the "canonical" representation used to store the |
| 4764 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4765 | TypeSourceInfo *WrittenTy |
| 4766 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 4767 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4768 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4769 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4770 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4771 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4772 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4773 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4774 | // C++ [temp.expl.spec]p9: |
| 4775 | // A template explicit specialization is in the scope of the |
| 4776 | // namespace in which the template was defined. |
| 4777 | // |
| 4778 | // We actually implement this paragraph where we set the semantic |
| 4779 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 4780 | // but we also maintain the lexical context where the actual |
| 4781 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4782 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4783 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4784 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4785 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4786 | Specialization->startDefinition(); |
| 4787 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4788 | if (TUK == TUK_Friend) { |
| 4789 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 4790 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 4791 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4792 | /*FIXME:*/KWLoc); |
| 4793 | Friend->setAccess(AS_public); |
| 4794 | CurContext->addDecl(Friend); |
| 4795 | } else { |
| 4796 | // Add the specialization into its lexical context, so that it can |
| 4797 | // be seen when iterating through the list of declarations in that |
| 4798 | // context. However, specializations are not found by name lookup. |
| 4799 | CurContext->addDecl(Specialization); |
| 4800 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4801 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4802 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4803 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4804 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4805 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4806 | Declarator &D) { |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4807 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 4808 | } |
| 4809 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4810 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4811 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4812 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4813 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4814 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4815 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4816 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4817 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4818 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4819 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4820 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4821 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4822 | Decl *DP = HandleDeclarator(ParentScope, D, |
| 4823 | move(TemplateParameterLists), |
| 4824 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4825 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4826 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4827 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4828 | FunctionTemplate->getTemplatedDecl()); |
| 4829 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 4830 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 4831 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4832 | } |
| 4833 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4834 | /// \brief Strips various properties off an implicit instantiation |
| 4835 | /// that has just been explicitly specialized. |
| 4836 | static void StripImplicitInstantiation(NamedDecl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4837 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4838 | |
| 4839 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4840 | FD->setInlineSpecified(false); |
| 4841 | } |
| 4842 | } |
| 4843 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4844 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4845 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4846 | /// for those cases where they are required and determining whether the |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4847 | /// new specialization/instantiation will have any effect. |
| 4848 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4849 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4850 | /// instantiation. |
| 4851 | /// |
| 4852 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 4853 | /// |
| 4854 | /// \param PrevDecl the previous declaration of the entity. |
| 4855 | /// |
| 4856 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 4857 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4858 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4859 | /// declaration was instantiated (either implicitly or explicitly). |
| 4860 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4861 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4862 | /// specialization or instantiation has no effect and should be ignored. |
| 4863 | /// |
| 4864 | /// \returns true if there was an error that should prevent the introduction of |
| 4865 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4866 | bool |
| 4867 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 4868 | TemplateSpecializationKind NewTSK, |
| 4869 | NamedDecl *PrevDecl, |
| 4870 | TemplateSpecializationKind PrevTSK, |
| 4871 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4872 | bool &HasNoEffect) { |
| 4873 | HasNoEffect = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4874 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4875 | switch (NewTSK) { |
| 4876 | case TSK_Undeclared: |
| 4877 | case TSK_ImplicitInstantiation: |
| 4878 | assert(false && "Don't check implicit instantiations here"); |
| 4879 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4880 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4881 | case TSK_ExplicitSpecialization: |
| 4882 | switch (PrevTSK) { |
| 4883 | case TSK_Undeclared: |
| 4884 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4885 | // Okay, we're just specializing something that is either already |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4886 | // explicitly specialized or has merely been mentioned without any |
| 4887 | // instantiation. |
| 4888 | return false; |
| 4889 | |
| 4890 | case TSK_ImplicitInstantiation: |
| 4891 | if (PrevPointOfInstantiation.isInvalid()) { |
| 4892 | // The declaration itself has not actually been instantiated, so it is |
| 4893 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4894 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4895 | return false; |
| 4896 | } |
| 4897 | // Fall through |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4898 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4899 | case TSK_ExplicitInstantiationDeclaration: |
| 4900 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4901 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 4902 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4903 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4904 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4905 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4906 | // 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] | 4907 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4908 | // before the first use of that specialization that would cause an |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4909 | // implicit instantiation to take place, in every translation unit in |
| 4910 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4911 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4912 | // Is there any previous explicit specialization declaration? |
| 4913 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 4914 | return false; |
| 4915 | } |
| 4916 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4917 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4918 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4919 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4920 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4921 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4922 | return true; |
| 4923 | } |
| 4924 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4925 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4926 | case TSK_ExplicitInstantiationDeclaration: |
| 4927 | switch (PrevTSK) { |
| 4928 | case TSK_ExplicitInstantiationDeclaration: |
| 4929 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4930 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4931 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4932 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4933 | case TSK_Undeclared: |
| 4934 | case TSK_ImplicitInstantiation: |
| 4935 | // We're explicitly instantiating something that may have already been |
| 4936 | // implicitly instantiated; that's fine. |
| 4937 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4938 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4939 | case TSK_ExplicitSpecialization: |
| 4940 | // C++0x [temp.explicit]p4: |
| 4941 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4942 | // of a template appears after a declaration of an explicit |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4943 | // specialization for that template, the explicit instantiation has no |
| 4944 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4945 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4946 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4947 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4948 | case TSK_ExplicitInstantiationDefinition: |
| 4949 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4950 | // If an entity is the subject of both an explicit instantiation |
| 4951 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4952 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4953 | Diag(NewLoc, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4954 | diag::err_explicit_instantiation_declaration_after_definition); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4955 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4956 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4957 | assert(PrevPointOfInstantiation.isValid() && |
| 4958 | "Explicit instantiation without point of instantiation?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4959 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4960 | return false; |
| 4961 | } |
| 4962 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4963 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4964 | case TSK_ExplicitInstantiationDefinition: |
| 4965 | switch (PrevTSK) { |
| 4966 | case TSK_Undeclared: |
| 4967 | case TSK_ImplicitInstantiation: |
| 4968 | // We're explicitly instantiating something that may have already been |
| 4969 | // implicitly instantiated; that's fine. |
| 4970 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4971 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4972 | case TSK_ExplicitSpecialization: |
| 4973 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 4974 | // For a given set of template parameters, if an explicit |
| 4975 | // instantiation of a template appears after a declaration of |
| 4976 | // an explicit specialization for that template, the explicit |
| 4977 | // instantiation has no effect. |
| 4978 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4979 | // 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] | 4980 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4981 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4982 | if (!getLangOptions().CPlusPlus0x) { |
| 4983 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4984 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4985 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4986 | diag::note_previous_template_specialization); |
| 4987 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4988 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4989 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4990 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4991 | case TSK_ExplicitInstantiationDeclaration: |
| 4992 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4993 | // were previously asked to suppress instantiations. That's fine. |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4994 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4995 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4996 | case TSK_ExplicitInstantiationDefinition: |
| 4997 | // C++0x [temp.spec]p5: |
| 4998 | // For a given template and a given set of template-arguments, |
| 4999 | // - an explicit instantiation definition shall appear at most once |
| 5000 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5001 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5002 | << PrevDecl; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5003 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5004 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5005 | HasNoEffect = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5006 | return false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5007 | } |
| 5008 | break; |
| 5009 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5010 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5011 | assert(false && "Missing specialization/instantiation case?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5012 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5013 | return false; |
| 5014 | } |
| 5015 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5016 | /// \brief Perform semantic analysis for the given dependent function |
| 5017 | /// template specialization. The only possible way to get a dependent |
| 5018 | /// function template specialization is with a friend declaration, |
| 5019 | /// like so: |
| 5020 | /// |
| 5021 | /// template <class T> void foo(T); |
| 5022 | /// template <class T> class A { |
| 5023 | /// friend void foo<>(T); |
| 5024 | /// }; |
| 5025 | /// |
| 5026 | /// There really isn't any useful analysis we can do here, so we |
| 5027 | /// just store the information. |
| 5028 | bool |
| 5029 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 5030 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 5031 | LookupResult &Previous) { |
| 5032 | // Remove anything from Previous that isn't a function template in |
| 5033 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5034 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5035 | LookupResult::Filter F = Previous.makeFilter(); |
| 5036 | while (F.hasNext()) { |
| 5037 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 5038 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5039 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 5040 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5041 | F.erase(); |
| 5042 | } |
| 5043 | F.done(); |
| 5044 | |
| 5045 | // Should this be diagnosed here? |
| 5046 | if (Previous.empty()) return true; |
| 5047 | |
| 5048 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 5049 | ExplicitTemplateArgs); |
| 5050 | return false; |
| 5051 | } |
| 5052 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5053 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5054 | /// specialization. |
| 5055 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5056 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5057 | /// explicit function template specialization. On successful completion, |
| 5058 | /// the function declaration \p FD will become a function template |
| 5059 | /// specialization. |
| 5060 | /// |
| 5061 | /// \param FD the function declaration, which will be updated to become a |
| 5062 | /// function template specialization. |
| 5063 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5064 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 5065 | /// if any. Note that this may be valid info even when 0 arguments are |
| 5066 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 5067 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5068 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5069 | /// \param PrevDecl the set of declarations that may be specialized by |
| 5070 | /// this function specialization. |
| 5071 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5072 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5073 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5074 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5075 | // The set of function template specializations that could match this |
| 5076 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5077 | UnresolvedSet<8> Candidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5078 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5079 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5080 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5081 | I != E; ++I) { |
| 5082 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 5083 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5084 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5085 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5086 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 5087 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5088 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5089 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5090 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5091 | // A trailing template-argument can be left unspecified in the |
| 5092 | // template-id naming an explicit function template specialization |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5093 | // provided it can be deduced from the function argument type. |
| 5094 | // Perform template argument deduction to determine whether we may be |
| 5095 | // specializing this template. |
| 5096 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5097 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5098 | FunctionDecl *Specialization = 0; |
| 5099 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5100 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5101 | FD->getType(), |
| 5102 | Specialization, |
| 5103 | Info)) { |
| 5104 | // FIXME: Template argument deduction failed; record why it failed, so |
| 5105 | // that we can provide nifty diagnostics. |
| 5106 | (void)TDK; |
| 5107 | continue; |
| 5108 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5109 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5110 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5111 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5112 | } |
| 5113 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5114 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5115 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5116 | UnresolvedSetIterator Result |
| 5117 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5118 | TPOC_Other, 0, FD->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5119 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5120 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5121 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5122 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5123 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5124 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5125 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5126 | |
| 5127 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 5128 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | abfb405 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 5129 | |
| 5130 | FunctionTemplateSpecializationInfo *SpecInfo |
| 5131 | = Specialization->getTemplateSpecializationInfo(); |
| 5132 | assert(SpecInfo && "Function template specialization info missing?"); |
| 5133 | { |
| 5134 | // Note: do not overwrite location info if previous template |
| 5135 | // specialization kind was explicit. |
| 5136 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
| 5137 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) |
| 5138 | Specialization->setLocation(FD->getLocation()); |
| 5139 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5140 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5141 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5142 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5143 | |
| 5144 | // If this is a friend declaration, then we're not really declaring |
| 5145 | // an explicit specialization. |
| 5146 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5147 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5148 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5149 | if (!isFriend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5150 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5151 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5152 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5153 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5154 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5155 | |
| 5156 | // C++ [temp.expl.spec]p6: |
| 5157 | // 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] | 5158 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5159 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5160 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5161 | // use occurs; no diagnostic is required. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5162 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5163 | if (!isFriend && |
| 5164 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5165 | TSK_ExplicitSpecialization, |
| 5166 | Specialization, |
| 5167 | SpecInfo->getTemplateSpecializationKind(), |
| 5168 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5169 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5170 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5171 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5172 | // Mark the prior declaration as an explicit specialization, so that later |
| 5173 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5174 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5175 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5176 | MarkUnusedFileScopedDecl(Specialization); |
| 5177 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5178 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5179 | // Turn the given function declaration into a function template |
| 5180 | // specialization, with the template arguments from the previous |
| 5181 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5182 | // Take copies of (semantic and syntactic) template argument lists. |
| 5183 | const TemplateArgumentList* TemplArgs = new (Context) |
| 5184 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
| 5185 | const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs |
| 5186 | ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5187 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5188 | TemplArgs, /*InsertPos=*/0, |
| 5189 | SpecInfo->getTemplateSpecializationKind(), |
| 5190 | TemplArgsAsWritten); |
| 5191 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5192 | // The "previous declaration" for this function template specialization is |
| 5193 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5194 | Previous.clear(); |
| 5195 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5196 | return false; |
| 5197 | } |
| 5198 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5199 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5200 | /// specialization. |
| 5201 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5202 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5203 | /// explicit member function specialization. On successful completion, |
| 5204 | /// the function declaration \p FD will become a member function |
| 5205 | /// specialization. |
| 5206 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5207 | /// \param Member the member declaration, which will be updated to become a |
| 5208 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5209 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5210 | /// \param Previous the set of declarations, one of which may be specialized |
| 5211 | /// by this function specialization; the set will be modified to contain the |
| 5212 | /// redeclared member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5213 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5214 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5215 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5216 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5217 | // Try to find the member we are instantiating. |
| 5218 | NamedDecl *Instantiation = 0; |
| 5219 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5220 | MemberSpecializationInfo *MSInfo = 0; |
| 5221 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5222 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5223 | // Nowhere to look anyway. |
| 5224 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5225 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5226 | I != E; ++I) { |
| 5227 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5228 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5229 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 5230 | Instantiation = Method; |
| 5231 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5232 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5233 | break; |
| 5234 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5235 | } |
| 5236 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5237 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5238 | VarDecl *PrevVar; |
| 5239 | if (Previous.isSingleResult() && |
| 5240 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5241 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5242 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5243 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5244 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5245 | } |
| 5246 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5247 | CXXRecordDecl *PrevRecord; |
| 5248 | if (Previous.isSingleResult() && |
| 5249 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 5250 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5251 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5252 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5253 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5254 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5255 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5256 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5257 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5258 | // specializations are always out-of-line, the caller will complain about |
| 5259 | // this mismatch later. |
| 5260 | return false; |
| 5261 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5262 | |
| 5263 | // If this is a friend, just bail out here before we start turning |
| 5264 | // things into explicit specializations. |
| 5265 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 5266 | // Preserve instantiation information. |
| 5267 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 5268 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 5269 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5270 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5271 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 5272 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 5273 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5274 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5275 | } |
| 5276 | |
| 5277 | Previous.clear(); |
| 5278 | Previous.addDecl(Instantiation); |
| 5279 | return false; |
| 5280 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5281 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5282 | // Make sure that this is a specialization of a member. |
| 5283 | if (!InstantiatedFrom) { |
| 5284 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 5285 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5286 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 5287 | return true; |
| 5288 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5289 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5290 | // C++ [temp.expl.spec]p6: |
| 5291 | // 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] | 5292 | // explicitly specialized then that spe- cialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5293 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5294 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5295 | // use occurs; no diagnostic is required. |
| 5296 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5297 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5298 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5299 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 5300 | TSK_ExplicitSpecialization, |
| 5301 | Instantiation, |
| 5302 | MSInfo->getTemplateSpecializationKind(), |
| 5303 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5304 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5305 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5306 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5307 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5308 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5309 | InstantiatedFrom, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5310 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5311 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5312 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 5313 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5314 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5315 | // the original declaration to note that it is an explicit specialization |
| 5316 | // (if it was previously an implicit instantiation). This latter step |
| 5317 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5318 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5319 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 5320 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 5321 | TSK_ImplicitInstantiation) { |
| 5322 | InstantiationFunction->setTemplateSpecializationKind( |
| 5323 | TSK_ExplicitSpecialization); |
| 5324 | InstantiationFunction->setLocation(Member->getLocation()); |
| 5325 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5326 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5327 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 5328 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5329 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5330 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5331 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5332 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 5333 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 5334 | TSK_ImplicitInstantiation) { |
| 5335 | InstantiationVar->setTemplateSpecializationKind( |
| 5336 | TSK_ExplicitSpecialization); |
| 5337 | InstantiationVar->setLocation(Member->getLocation()); |
| 5338 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5339 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5340 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 5341 | cast<VarDecl>(InstantiatedFrom), |
| 5342 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5343 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5344 | } else { |
| 5345 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5346 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 5347 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 5348 | TSK_ImplicitInstantiation) { |
| 5349 | InstantiationClass->setTemplateSpecializationKind( |
| 5350 | TSK_ExplicitSpecialization); |
| 5351 | InstantiationClass->setLocation(Member->getLocation()); |
| 5352 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5353 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5354 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5355 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5356 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5357 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5358 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5359 | // Save the caller the trouble of having to figure out which declaration |
| 5360 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5361 | Previous.clear(); |
| 5362 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5363 | return false; |
| 5364 | } |
| 5365 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5366 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5367 | /// |
| 5368 | /// \returns true if a serious error occurs, false otherwise. |
| 5369 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5370 | SourceLocation InstLoc, |
| 5371 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5372 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5373 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5374 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5375 | if (CurContext->isRecord()) { |
| 5376 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5377 | << D; |
| 5378 | return true; |
| 5379 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5380 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5381 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5382 | // An explicit instantiation shall appear in an enclosing namespace of its |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5383 | // template. |
| 5384 | // |
| 5385 | // This is DR275, which we do not retroactively apply to C++98/03. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5386 | if (S.getLangOptions().CPlusPlus0x && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5387 | !CurContext->Encloses(OrigContext)) { |
| 5388 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5389 | S.Diag(InstLoc, |
| 5390 | S.getLangOptions().CPlusPlus0x? |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5391 | diag::err_explicit_instantiation_out_of_scope |
| 5392 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5393 | << D << NS; |
| 5394 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5395 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5396 | S.getLangOptions().CPlusPlus0x? |
| 5397 | diag::err_explicit_instantiation_must_be_global |
| 5398 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5399 | << D; |
| 5400 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5401 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5402 | } |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5403 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5404 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5405 | // If the name declared in the explicit instantiation is an unqualified |
| 5406 | // name, the explicit instantiation shall appear in the namespace where |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5407 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 5408 | // namespace from its enclosing namespace set. |
| 5409 | if (WasQualifiedName) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5410 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5411 | |
| 5412 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5413 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5414 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5415 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5416 | S.getLangOptions().CPlusPlus0x? |
| 5417 | diag::err_explicit_instantiation_unqualified_wrong_namespace |
| 5418 | : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5419 | << D << OrigContext; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5420 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5421 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5422 | } |
| 5423 | |
| 5424 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5425 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5426 | if (!SS.isSet()) |
| 5427 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5428 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5429 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5430 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5431 | // or a static data member of a class template specialization, the name of |
| 5432 | // the class template specialization in the qualified-id for the member |
| 5433 | // name shall be a simple-template-id. |
| 5434 | // |
| 5435 | // C++98 has the same restriction, just worded differently. |
| 5436 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5437 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5438 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5439 | if (isa<TemplateSpecializationType>(T)) |
| 5440 | return true; |
| 5441 | |
| 5442 | return false; |
| 5443 | } |
| 5444 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5445 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5446 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5447 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5448 | SourceLocation ExternLoc, |
| 5449 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5450 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5451 | SourceLocation KWLoc, |
| 5452 | const CXXScopeSpec &SS, |
| 5453 | TemplateTy TemplateD, |
| 5454 | SourceLocation TemplateNameLoc, |
| 5455 | SourceLocation LAngleLoc, |
| 5456 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5457 | SourceLocation RAngleLoc, |
| 5458 | AttributeList *Attr) { |
| 5459 | // Find the class template we're specializing |
| 5460 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5461 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5462 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5463 | |
| 5464 | // Check that the specialization uses the same tag kind as the |
| 5465 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5466 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5467 | assert(Kind != TTK_Enum && |
| 5468 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5469 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5470 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5471 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5472 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5473 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5474 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5475 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5476 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5477 | diag::note_previous_use); |
| 5478 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5479 | } |
| 5480 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5481 | // C++0x [temp.explicit]p2: |
| 5482 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5483 | // definition and an explicit instantiation declaration. An explicit |
| 5484 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5485 | TemplateSpecializationKind TSK |
| 5486 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5487 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5488 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5489 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5490 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5491 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5492 | |
| 5493 | // Check that the template argument list is well-formed for this |
| 5494 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5495 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5496 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5497 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5498 | return true; |
| 5499 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5500 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5501 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5502 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5503 | // Find the class template specialization declaration that |
| 5504 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5505 | void *InsertPos = 0; |
| 5506 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5507 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5508 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5509 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5510 | TemplateSpecializationKind PrevDecl_TSK |
| 5511 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 5512 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5513 | // C++0x [temp.explicit]p2: |
| 5514 | // [...] An explicit instantiation shall appear in an enclosing |
| 5515 | // namespace of its template. [...] |
| 5516 | // |
| 5517 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5518 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 5519 | SS.isSet())) |
| 5520 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5521 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5522 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5523 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5524 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5525 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5526 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5527 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5528 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5529 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5530 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5531 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5532 | // Even though HasNoEffect == true means that this explicit instantiation |
| 5533 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 5534 | |
| 5535 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 5536 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5537 | // Since the only prior class template specialization with these |
| 5538 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5539 | // declaration node as our own, updating the source location |
| 5540 | // for the template name to reflect our new declaration. |
| 5541 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5542 | Specialization = PrevDecl; |
| 5543 | Specialization->setLocation(TemplateNameLoc); |
| 5544 | PrevDecl = 0; |
| 5545 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5546 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5547 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5548 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5549 | // Create a new class template specialization declaration node for |
| 5550 | // this explicit specialization. |
| 5551 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5552 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5553 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5554 | KWLoc, TemplateNameLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5555 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5556 | Converted.data(), |
| 5557 | Converted.size(), |
| 5558 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5559 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5560 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5561 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5562 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5563 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5564 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5565 | } |
| 5566 | |
| 5567 | // Build the fully-sugared type for this explicit instantiation as |
| 5568 | // the user wrote in the explicit instantiation itself. This means |
| 5569 | // that we'll pretty-print the type retrieved from the |
| 5570 | // specialization's declaration the way that the user actually wrote |
| 5571 | // the explicit instantiation, rather than formatting the name based |
| 5572 | // on the "canonical" representation used to store the template |
| 5573 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5574 | TypeSourceInfo *WrittenTy |
| 5575 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5576 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5577 | Context.getTypeDeclType(Specialization)); |
| 5578 | Specialization->setTypeAsWritten(WrittenTy); |
| 5579 | TemplateArgsIn.release(); |
| 5580 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5581 | // Set source locations for keywords. |
| 5582 | Specialization->setExternLoc(ExternLoc); |
| 5583 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 5584 | |
| 5585 | // Add the explicit instantiation into its lexical context. However, |
| 5586 | // since explicit instantiations are never found by name lookup, we |
| 5587 | // just put it into the declaration context directly. |
| 5588 | Specialization->setLexicalDeclContext(CurContext); |
| 5589 | CurContext->addDecl(Specialization); |
| 5590 | |
| 5591 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 5592 | if (HasNoEffect) { |
| 5593 | // Set the template specialization kind. |
| 5594 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5595 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5596 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5597 | |
| 5598 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5599 | // A definition of a class template or class member template |
| 5600 | // shall be in scope at the point of the explicit instantiation of |
| 5601 | // the class template or class member template. |
| 5602 | // |
| 5603 | // This check comes when we actually try to perform the |
| 5604 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5605 | ClassTemplateSpecializationDecl *Def |
| 5606 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5607 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5608 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5609 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5610 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5611 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5612 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 5613 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5614 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5615 | // Instantiate the members of this class template specialization. |
| 5616 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5617 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5618 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 5619 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 5620 | |
| 5621 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 5622 | // TSK_ExplicitInstantiationDefinition |
| 5623 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 5624 | TSK == TSK_ExplicitInstantiationDefinition) |
| 5625 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5626 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5627 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5628 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5629 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5630 | // Set the template specialization kind. |
| 5631 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5632 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5633 | } |
| 5634 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5635 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5636 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5637 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5638 | SourceLocation ExternLoc, |
| 5639 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5640 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5641 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5642 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5643 | IdentifierInfo *Name, |
| 5644 | SourceLocation NameLoc, |
| 5645 | AttributeList *Attr) { |
| 5646 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 5647 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5648 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5649 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5650 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
| 5651 | MultiTemplateParamsArg(*this, 0, 0), |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 5652 | Owned, IsDependent, false, false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5653 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5654 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 5655 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5656 | if (!TagD) |
| 5657 | return true; |
| 5658 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5659 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5660 | if (Tag->isEnum()) { |
| 5661 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 5662 | << Context.getTypeDeclType(Tag); |
| 5663 | return true; |
| 5664 | } |
| 5665 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 5666 | if (Tag->isInvalidDecl()) |
| 5667 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5668 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5669 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 5670 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 5671 | if (!Pattern) { |
| 5672 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 5673 | << Context.getTypeDeclType(Record); |
| 5674 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 5675 | return true; |
| 5676 | } |
| 5677 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5678 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5679 | // If the explicit instantiation is for a class or member class, the |
| 5680 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5681 | // simple-template-id. |
| 5682 | // |
| 5683 | // C++98 has the same restriction, just worded differently. |
| 5684 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5685 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5686 | << Record << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5687 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5688 | // C++0x [temp.explicit]p2: |
| 5689 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5690 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5691 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 5692 | TemplateSpecializationKind TSK |
| 5693 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5694 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5695 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5696 | // C++0x [temp.explicit]p2: |
| 5697 | // [...] An explicit instantiation shall appear in an enclosing |
| 5698 | // namespace of its template. [...] |
| 5699 | // |
| 5700 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5701 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5702 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5703 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5704 | CXXRecordDecl *PrevDecl |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5705 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5706 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5707 | PrevDecl = Record; |
| 5708 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5709 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5710 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5711 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5712 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5713 | PrevDecl, |
| 5714 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5715 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5716 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5717 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5718 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5719 | return TagD; |
| 5720 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5721 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5722 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5723 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5724 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5725 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5726 | // 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] | 5727 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5728 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5729 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5730 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 5731 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 5732 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5733 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 5734 | << Pattern; |
| 5735 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5736 | } else { |
| 5737 | if (InstantiateClass(NameLoc, Record, Def, |
| 5738 | getTemplateInstantiationArgs(Record), |
| 5739 | TSK)) |
| 5740 | return true; |
| 5741 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5742 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5743 | if (!RecordDef) |
| 5744 | return true; |
| 5745 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5746 | } |
| 5747 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5748 | // Instantiate all of the members of the class. |
| 5749 | InstantiateClassMembers(NameLoc, RecordDef, |
| 5750 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5751 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5752 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 5753 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 5754 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5755 | // FIXME: We don't have any representation for explicit instantiations of |
| 5756 | // member classes. Such a representation is not needed for compilation, but it |
| 5757 | // should be available for clients that want to see all of the declarations in |
| 5758 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5759 | return TagD; |
| 5760 | } |
| 5761 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5762 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 5763 | SourceLocation ExternLoc, |
| 5764 | SourceLocation TemplateLoc, |
| 5765 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5766 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5767 | // TODO: check if/when DNInfo should replace Name. |
| 5768 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 5769 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5770 | if (!Name) { |
| 5771 | if (!D.isInvalidType()) |
| 5772 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 5773 | diag::err_explicit_instantiation_requires_name) |
| 5774 | << D.getDeclSpec().getSourceRange() |
| 5775 | << D.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5776 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5777 | return true; |
| 5778 | } |
| 5779 | |
| 5780 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 5781 | // we find one that is. |
| 5782 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5783 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5784 | S = S->getParent(); |
| 5785 | |
| 5786 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 5787 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 5788 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5789 | if (R.isNull()) |
| 5790 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5791 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5792 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 5793 | // Cannot explicitly instantiate a typedef. |
| 5794 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 5795 | << Name; |
| 5796 | return true; |
| 5797 | } |
| 5798 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5799 | // C++0x [temp.explicit]p1: |
| 5800 | // [...] An explicit instantiation of a function template shall not use the |
| 5801 | // inline or constexpr specifiers. |
| 5802 | // Presumably, this also applies to member functions of class templates as |
| 5803 | // well. |
| 5804 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5805 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5806 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5807 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5808 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5809 | // FIXME: check for constexpr specifier. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5810 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5811 | // C++0x [temp.explicit]p2: |
| 5812 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5813 | // definition and an explicit instantiation declaration. An explicit |
| 5814 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5815 | TemplateSpecializationKind TSK |
| 5816 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5817 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5818 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5819 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5820 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5821 | |
| 5822 | if (!R->isFunctionType()) { |
| 5823 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5824 | // A [...] static data member of a class template can be explicitly |
| 5825 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5826 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5827 | if (Previous.isAmbiguous()) |
| 5828 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5829 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 5830 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5831 | if (!Prev || !Prev->isStaticDataMember()) { |
| 5832 | // We expect to see a data data member here. |
| 5833 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 5834 | << Name; |
| 5835 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5836 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5837 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5838 | return true; |
| 5839 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5840 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5841 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 5842 | // FIXME: Check for explicit specialization? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5843 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5844 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 5845 | << Prev; |
| 5846 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 5847 | // FIXME: Can we provide a note showing where this was declared? |
| 5848 | return true; |
| 5849 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5850 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5851 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5852 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5853 | // or a static data member of a class template specialization, the name of |
| 5854 | // the class template specialization in the qualified-id for the member |
| 5855 | // name shall be a simple-template-id. |
| 5856 | // |
| 5857 | // C++98 has the same restriction, just worded differently. |
| 5858 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5859 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5860 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5861 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5862 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5863 | // Check the scope of this explicit instantiation. |
| 5864 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5865 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5866 | // Verify that it is okay to explicitly instantiate here. |
| 5867 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 5868 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5869 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5870 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5871 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5872 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5873 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5874 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5875 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5876 | return (Decl*) 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5877 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5878 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5879 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5880 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5881 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5882 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5883 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5884 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5885 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5886 | |
| 5887 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5888 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5889 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5890 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5891 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 5892 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5893 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 5894 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5895 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 5896 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5897 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5898 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5899 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 5900 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5901 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5902 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5903 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5904 | // A [...] function [...] can be explicitly instantiated from its template. |
| 5905 | // A member function [...] of a class template can be explicitly |
| 5906 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5907 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5908 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5909 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5910 | P != PEnd; ++P) { |
| 5911 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5912 | if (!HasExplicitTemplateArgs) { |
| 5913 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 5914 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 5915 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5916 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5917 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5918 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 5919 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5920 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5921 | } |
| 5922 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5923 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5924 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 5925 | if (!FunTmpl) |
| 5926 | continue; |
| 5927 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5928 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5929 | FunctionDecl *Specialization = 0; |
| 5930 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5931 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5932 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5933 | R, Specialization, Info)) { |
| 5934 | // FIXME: Keep track of almost-matches? |
| 5935 | (void)TDK; |
| 5936 | continue; |
| 5937 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5938 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5939 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5940 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5941 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5942 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5943 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5944 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5945 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5946 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 5947 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 5948 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5949 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5950 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5951 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5952 | |
| 5953 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 5954 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5955 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5956 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5957 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5958 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 5959 | << Specialization |
| 5960 | << (Specialization->getTemplateSpecializationKind() == |
| 5961 | TSK_ExplicitSpecialization); |
| 5962 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 5963 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5964 | } |
| 5965 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5966 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5967 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 5968 | PrevDecl = Specialization; |
| 5969 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5970 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5971 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5972 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5973 | PrevDecl, |
| 5974 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5975 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5976 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5977 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5978 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5979 | // FIXME: We may still want to build some representation of this |
| 5980 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5981 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5982 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5983 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 5984 | |
| 5985 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5986 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5987 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5988 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5989 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5990 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5991 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5992 | // or a static data member of a class template specialization, the name of |
| 5993 | // the class template specialization in the qualified-id for the member |
| 5994 | // name shall be a simple-template-id. |
| 5995 | // |
| 5996 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5997 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5998 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5999 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6000 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6001 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6002 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6003 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6004 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6005 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6006 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6007 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6008 | D.getIdentifierLoc(), |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6009 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6010 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6011 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6012 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6013 | } |
| 6014 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6015 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6016 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 6017 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 6018 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 6019 | // This has to hold, because SS is expected to be defined. |
| 6020 | assert(Name && "Expected a name in a dependent tag"); |
| 6021 | |
| 6022 | NestedNameSpecifier *NNS |
| 6023 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6024 | if (!NNS) |
| 6025 | return true; |
| 6026 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6027 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 6028 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6029 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 6030 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6031 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6032 | return true; |
| 6033 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6034 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6035 | // Create the resulting type. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6036 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6037 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 6038 | |
| 6039 | // Create type-source location information for this type. |
| 6040 | TypeLocBuilder TLB; |
| 6041 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
| 6042 | TL.setKeywordLoc(TagLoc); |
| 6043 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6044 | TL.setNameLoc(NameLoc); |
| 6045 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6046 | } |
| 6047 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6048 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6049 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6050 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6051 | SourceLocation IdLoc) { |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6052 | if (SS.isInvalid()) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6053 | return true; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6054 | |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6055 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 6056 | !getLangOptions().CPlusPlus0x) |
| 6057 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6058 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6059 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6060 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6061 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 6062 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 6063 | if (T.isNull()) |
| 6064 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6065 | |
| 6066 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6067 | if (isa<DependentNameType>(T)) { |
| 6068 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6069 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6070 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6071 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6072 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6073 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6074 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6075 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6076 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6077 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6078 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6079 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6080 | } |
| 6081 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6082 | TypeResult |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6083 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6084 | const CXXScopeSpec &SS, |
| 6085 | SourceLocation TemplateLoc, |
| 6086 | TemplateTy TemplateIn, |
| 6087 | SourceLocation TemplateNameLoc, |
| 6088 | SourceLocation LAngleLoc, |
| 6089 | ASTTemplateArgsPtr TemplateArgsIn, |
| 6090 | SourceLocation RAngleLoc) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6091 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 6092 | !getLangOptions().CPlusPlus0x) |
| 6093 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6094 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6095 | |
| 6096 | // Translate the parser's template argument list in our AST format. |
| 6097 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 6098 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 6099 | |
| 6100 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6101 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 6102 | // Construct a dependent template specialization type. |
| 6103 | assert(DTN && "dependent template has non-dependent name?"); |
| 6104 | assert(DTN->getQualifier() |
| 6105 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 6106 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 6107 | DTN->getQualifier(), |
| 6108 | DTN->getIdentifier(), |
| 6109 | TemplateArgs); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6110 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6111 | // Create source-location information for this type. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6112 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6113 | DependentTemplateSpecializationTypeLoc SpecTL |
| 6114 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6115 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6116 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6117 | SpecTL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 6118 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6119 | SpecTL.setNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6120 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6121 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6122 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 6123 | } |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6124 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6125 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 6126 | if (T.isNull()) |
| 6127 | return true; |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6128 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6129 | // Provide source-location information for the template specialization |
| 6130 | // type. |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6131 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6132 | TemplateSpecializationTypeLoc SpecTL |
| 6133 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
| 6134 | |
| 6135 | // FIXME: No place to set the location of the 'template' keyword! |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6136 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6137 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6138 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6139 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6140 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 6141 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6142 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 6143 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 6144 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6145 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6146 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6147 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 6148 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 6149 | } |
| 6150 | |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6151 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6152 | /// \brief Build the type that describes a C++ typename specifier, |
| 6153 | /// e.g., "typename T::type". |
| 6154 | QualType |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6155 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 6156 | SourceLocation KeywordLoc, |
| 6157 | NestedNameSpecifierLoc QualifierLoc, |
| 6158 | const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6159 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6160 | CXXScopeSpec SS; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6161 | SS.Adopt(QualifierLoc); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6162 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6163 | DeclContext *Ctx = computeDeclContext(SS); |
| 6164 | if (!Ctx) { |
| 6165 | // If the nested-name-specifier is dependent and couldn't be |
| 6166 | // resolved to a type, build a typename type. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6167 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 6168 | return Context.getDependentNameType(Keyword, |
| 6169 | QualifierLoc.getNestedNameSpecifier(), |
| 6170 | &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 6171 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6172 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6173 | // If the nested-name-specifier refers to the current instantiation, |
| 6174 | // the "typename" keyword itself is superfluous. In C++03, the |
| 6175 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 6176 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 6177 | // 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] | 6178 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6179 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 6180 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6181 | |
| 6182 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6183 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6184 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6185 | unsigned DiagID = 0; |
| 6186 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6187 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6188 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6189 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6190 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6191 | |
| 6192 | case LookupResult::FoundUnresolvedValue: { |
| 6193 | // We found a using declaration that is a value. Most likely, the using |
| 6194 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6195 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6196 | IILoc); |
| 6197 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 6198 | << Name << Ctx << FullRange; |
| 6199 | if (UnresolvedUsingValueDecl *Using |
| 6200 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6201 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6202 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 6203 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 6204 | } |
| 6205 | } |
| 6206 | // Fall through to create a dependent typename type, from which we can recover |
| 6207 | // better. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6208 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 6209 | case LookupResult::NotFoundInCurrentInstantiation: |
| 6210 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6211 | return Context.getDependentNameType(Keyword, |
| 6212 | QualifierLoc.getNestedNameSpecifier(), |
| 6213 | &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6214 | |
| 6215 | case LookupResult::Found: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6216 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6217 | // We found a type. Build an ElaboratedType, since the |
| 6218 | // typename-specifier was just sugar. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6219 | return Context.getElaboratedType(ETK_Typename, |
| 6220 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6221 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6222 | } |
| 6223 | |
| 6224 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6225 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6226 | break; |
| 6227 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6228 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6229 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6230 | return QualType(); |
| 6231 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6232 | case LookupResult::FoundOverloaded: |
| 6233 | DiagID = diag::err_typename_nested_not_type; |
| 6234 | Referenced = *Result.begin(); |
| 6235 | break; |
| 6236 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 6237 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6238 | return QualType(); |
| 6239 | } |
| 6240 | |
| 6241 | // If we get here, it's because name lookup did not find a |
| 6242 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6243 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6244 | IILoc); |
| 6245 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6246 | if (Referenced) |
| 6247 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 6248 | << Name; |
| 6249 | return QualType(); |
| 6250 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6251 | |
| 6252 | namespace { |
| 6253 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 6254 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6255 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6256 | SourceLocation Loc; |
| 6257 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6258 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6259 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 6260 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6261 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6262 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6263 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6264 | DeclarationName Entity) |
| 6265 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6266 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6267 | |
| 6268 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6269 | /// transformed. |
| 6270 | /// |
| 6271 | /// For the purposes of type reconstruction, a type has already been |
| 6272 | /// transformed if it is NULL or if it is not dependent. |
| 6273 | bool AlreadyTransformed(QualType T) { |
| 6274 | return T.isNull() || !T->isDependentType(); |
| 6275 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6276 | |
| 6277 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6278 | /// rebuilt. |
| 6279 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6280 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6281 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 6282 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6283 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6284 | /// \brief Sets the "base" location and entity when that |
| 6285 | /// information is known based on another transformation. |
| 6286 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 6287 | this->Loc = Loc; |
| 6288 | this->Entity = Entity; |
| 6289 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6290 | }; |
| 6291 | } |
| 6292 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6293 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 6294 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6295 | /// 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] | 6296 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6297 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6298 | /// partial specialization thereof). This routine will rebuild that type now |
| 6299 | /// that we have entered the declarator's scope, which may produce different |
| 6300 | /// canonical types, e.g., |
| 6301 | /// |
| 6302 | /// \code |
| 6303 | /// template<typename T> |
| 6304 | /// struct X { |
| 6305 | /// typedef T* pointer; |
| 6306 | /// pointer data(); |
| 6307 | /// }; |
| 6308 | /// |
| 6309 | /// template<typename T> |
| 6310 | /// typename X<T>::pointer X<T>::data() { ... } |
| 6311 | /// \endcode |
| 6312 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 6313 | /// 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] | 6314 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 6315 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6316 | /// 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] | 6317 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 6318 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6319 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 6320 | SourceLocation Loc, |
| 6321 | DeclarationName Name) { |
| 6322 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6323 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6324 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6325 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 6326 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 6327 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6328 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6329 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6330 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 6331 | DeclarationName()); |
| 6332 | return Rebuilder.TransformExpr(E); |
| 6333 | } |
| 6334 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6335 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6336 | if (SS.isInvalid()) |
| 6337 | return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6338 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6339 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6340 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 6341 | DeclarationName()); |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6342 | NestedNameSpecifierLoc Rebuilt |
| 6343 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 6344 | if (!Rebuilt) |
| 6345 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6346 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6347 | SS.Adopt(Rebuilt); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6348 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6349 | } |
| 6350 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6351 | /// \brief Produces a formatted string that describes the binding of |
| 6352 | /// template parameters to template arguments. |
| 6353 | std::string |
| 6354 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6355 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6356 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6357 | } |
| 6358 | |
| 6359 | std::string |
| 6360 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6361 | const TemplateArgument *Args, |
| 6362 | unsigned NumArgs) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6363 | llvm::SmallString<128> Str; |
| 6364 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6365 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6366 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6367 | return std::string(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6368 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6369 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6370 | if (I >= NumArgs) |
| 6371 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6372 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6373 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6374 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6375 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6376 | Out << ", "; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6377 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6378 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6379 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6380 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6381 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6382 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6383 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6384 | Out << " = "; |
| 6385 | Args[I].print(Context.PrintingPolicy, Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6386 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6387 | |
| 6388 | Out << ']'; |
| 6389 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6390 | } |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 6391 | |
| 6392 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) { |
| 6393 | if (!FD) |
| 6394 | return; |
| 6395 | FD->setLateTemplateParsed(Flag); |
| 6396 | } |
| 6397 | |
| 6398 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 6399 | DeclContext *DC = CurContext; |
| 6400 | |
| 6401 | while (DC) { |
| 6402 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 6403 | const FunctionDecl *FD = RD->isLocalClass(); |
| 6404 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 6405 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 6406 | return false; |
| 6407 | |
| 6408 | DC = DC->getParent(); |
| 6409 | } |
| 6410 | return false; |
| 6411 | } |