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 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 79 | static void FilterAcceptableTemplateNames(ASTContext &C, 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(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 85 | NamedDecl *Repl = isAcceptableTemplateName(C, 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 | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 117 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 118 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 119 | bool hasTemplateKeyword, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 120 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 121 | ParsedType ObjectTypePtr, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 122 | bool EnteringContext, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 123 | TemplateTy &TemplateResult, |
| 124 | bool &MemberOfUnknownSpecialization) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 125 | assert(getLangOptions().CPlusPlus && "No template names in C!"); |
| 126 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 127 | DeclarationName TName; |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 128 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 129 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 130 | switch (Name.getKind()) { |
| 131 | case UnqualifiedId::IK_Identifier: |
| 132 | TName = DeclarationName(Name.Identifier); |
| 133 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 134 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 135 | case UnqualifiedId::IK_OperatorFunctionId: |
| 136 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 137 | Name.OperatorFunctionId.Operator); |
| 138 | break; |
| 139 | |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 140 | case UnqualifiedId::IK_LiteralOperatorId: |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 141 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 142 | break; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 143 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 144 | default: |
| 145 | return TNK_Non_template; |
| 146 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 148 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 150 | LookupResult R(*this, TName, Name.getSourceRange().getBegin(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 151 | LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 152 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 153 | MemberOfUnknownSpecialization); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 154 | if (R.empty()) return TNK_Non_template; |
| 155 | if (R.isAmbiguous()) { |
| 156 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 157 | R.suppressDiagnostics(); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 158 | |
| 159 | // FIXME: we might have ambiguous templates, in which case we |
| 160 | // should at least parse them properly! |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 161 | return TNK_Non_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 162 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 163 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 164 | TemplateName Template; |
| 165 | TemplateNameKind TemplateKind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 167 | unsigned ResultCount = R.end() - R.begin(); |
| 168 | if (ResultCount > 1) { |
| 169 | // We assume that we'll preserve the qualifier from a function |
| 170 | // template name in other ways. |
| 171 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 172 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 173 | |
| 174 | // We'll do this lookup again later. |
| 175 | R.suppressDiagnostics(); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 176 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 177 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 178 | |
| 179 | if (SS.isSet() && !SS.isInvalid()) { |
| 180 | NestedNameSpecifier *Qualifier |
| 181 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 182 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 183 | hasTemplateKeyword, TD); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 184 | } else { |
| 185 | Template = TemplateName(TD); |
| 186 | } |
| 187 | |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 188 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 189 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 190 | |
| 191 | // We'll do this lookup again later. |
| 192 | R.suppressDiagnostics(); |
| 193 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 194 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD)); |
| 195 | TemplateKind = TNK_Type_template; |
| 196 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 197 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 199 | TemplateResult = TemplateTy::make(Template); |
| 200 | return TemplateKind; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 201 | } |
| 202 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 203 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 204 | SourceLocation IILoc, |
| 205 | Scope *S, |
| 206 | const CXXScopeSpec *SS, |
| 207 | TemplateTy &SuggestedTemplate, |
| 208 | TemplateNameKind &SuggestedKind) { |
| 209 | // We can't recover unless there's a dependent scope specifier preceding the |
| 210 | // template name. |
Douglas Gregor | d5ab9b0 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 211 | // FIXME: Typo correction? |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 212 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 213 | computeDeclContext(*SS)) |
| 214 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 215 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 216 | // The code is missing a 'template' keyword prior to the dependent template |
| 217 | // name. |
| 218 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 219 | Diag(IILoc, diag::err_template_kw_missing) |
| 220 | << Qualifier << II.getName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 221 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 222 | SuggestedTemplate |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 223 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 224 | SuggestedKind = TNK_Dependent_template_name; |
| 225 | return true; |
| 226 | } |
| 227 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 228 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 229 | Scope *S, CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 230 | QualType ObjectType, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 231 | bool EnteringContext, |
| 232 | bool &MemberOfUnknownSpecialization) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 233 | // Determine where to perform name lookup |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 234 | MemberOfUnknownSpecialization = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 235 | DeclContext *LookupCtx = 0; |
| 236 | bool isDependent = false; |
| 237 | if (!ObjectType.isNull()) { |
| 238 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 239 | // x->B::f, and we are looking into the type of the object. |
| 240 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 241 | LookupCtx = computeDeclContext(ObjectType); |
| 242 | isDependent = ObjectType->isDependentType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 243 | assert((isDependent || !ObjectType->isIncompleteType()) && |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 244 | "Caller should have completed object type"); |
| 245 | } else if (SS.isSet()) { |
| 246 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 247 | // so long into the context associated with the prior nested-name-specifier. |
| 248 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 249 | isDependent = isDependentScopeSpecifier(SS); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 250 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 251 | // The declaration context must be complete. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 252 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 253 | return; |
| 254 | } |
| 255 | |
| 256 | bool ObjectTypeSearchedInScope = false; |
| 257 | if (LookupCtx) { |
| 258 | // Perform "qualified" name lookup into the declaration context we |
| 259 | // computed, which is either the type of the base of a member access |
| 260 | // expression or the declaration context associated with a prior |
| 261 | // nested-name-specifier. |
| 262 | LookupQualifiedName(Found, LookupCtx); |
| 263 | |
| 264 | if (!ObjectType.isNull() && Found.empty()) { |
| 265 | // C++ [basic.lookup.classref]p1: |
| 266 | // In a class member access expression (5.2.5), if the . or -> token is |
| 267 | // immediately followed by an identifier followed by a <, the |
| 268 | // identifier must be looked up to determine whether the < is the |
| 269 | // beginning of a template argument list (14.2) or a less-than operator. |
| 270 | // The identifier is first looked up in the class of the object |
| 271 | // expression. If the identifier is not found, it is then looked up in |
| 272 | // the context of the entire postfix-expression and shall name a class |
| 273 | // or function template. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 274 | if (S) LookupName(Found, S); |
| 275 | ObjectTypeSearchedInScope = true; |
| 276 | } |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 277 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 278 | // We cannot look into a dependent object type or nested nme |
| 279 | // specifier. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 280 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 281 | return; |
| 282 | } else { |
| 283 | // Perform unqualified name lookup in the current scope. |
| 284 | LookupName(Found, S); |
| 285 | } |
| 286 | |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 287 | if (Found.empty() && !isDependent) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 288 | // If we did not find any names, attempt to correct any typos. |
| 289 | DeclarationName Name = Found.getLookupName(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 290 | if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx, |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 291 | false, CTC_CXXCasts)) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 292 | FilterAcceptableTemplateNames(Context, Found); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 293 | if (!Found.empty()) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 294 | if (LookupCtx) |
| 295 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) |
| 296 | << Name << LookupCtx << Found.getLookupName() << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 297 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 298 | Found.getLookupName().getAsString()); |
| 299 | else |
| 300 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) |
| 301 | << Name << Found.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 302 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 303 | Found.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 304 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) |
| 305 | Diag(Template->getLocation(), diag::note_previous_decl) |
| 306 | << Template->getDeclName(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 307 | } |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 308 | } else { |
| 309 | Found.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 310 | Found.setLookupName(Name); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 314 | FilterAcceptableTemplateNames(Context, Found); |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 315 | if (Found.empty()) { |
| 316 | if (isDependent) |
| 317 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 318 | return; |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 319 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 320 | |
| 321 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
| 322 | // C++ [basic.lookup.classref]p1: |
| 323 | // [...] If the lookup in the class of the object expression finds a |
| 324 | // template, the name is also looked up in the context of the entire |
| 325 | // postfix-expression and [...] |
| 326 | // |
| 327 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 328 | LookupOrdinaryName); |
| 329 | LookupName(FoundOuter, S); |
| 330 | FilterAcceptableTemplateNames(Context, FoundOuter); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 331 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 332 | if (FoundOuter.empty()) { |
| 333 | // - if the name is not found, the name found in the class of the |
| 334 | // object expression is used, otherwise |
| 335 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) { |
| 336 | // - if the name is found in the context of the entire |
| 337 | // postfix-expression and does not name a class template, the name |
| 338 | // found in the class of the object expression is used, otherwise |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 339 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 340 | // - if the name found is a class template, it must refer to the same |
| 341 | // entity as the one found in the class of the object expression, |
| 342 | // otherwise the program is ill-formed. |
| 343 | if (!Found.isSingleResult() || |
| 344 | Found.getFoundDecl()->getCanonicalDecl() |
| 345 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 346 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 21d07e4 | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 347 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 348 | << Found.getLookupName() |
| 349 | << ObjectType; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 350 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 351 | diag::note_ambig_member_ref_object_type) |
| 352 | << ObjectType; |
| 353 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 354 | diag::note_ambig_member_ref_scope); |
| 355 | |
| 356 | // Recover by taking the template that we found in the object |
| 357 | // expression's type. |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 363 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 364 | /// was just parsed. This is only possible with an explicit scope |
| 365 | /// specifier naming a dependent type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 366 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 367 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 368 | const DeclarationNameInfo &NameInfo, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 369 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 370 | const TemplateArgumentListInfo *TemplateArgs) { |
| 371 | NestedNameSpecifier *Qualifier |
| 372 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 373 | |
| 374 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 375 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 376 | if (!isAddressOfOperand && |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 377 | isa<CXXMethodDecl>(DC) && |
| 378 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 379 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 380 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 381 | // Since the 'this' expression is synthesized, we don't need to |
| 382 | // perform the double-lookup check. |
| 383 | NamedDecl *FirstQualifierInScope = 0; |
| 384 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 385 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 386 | /*This*/ 0, ThisType, |
| 387 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 388 | /*Op*/ SourceLocation(), |
| 389 | Qualifier, SS.getRange(), |
| 390 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 391 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 392 | TemplateArgs)); |
| 393 | } |
| 394 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 395 | return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 396 | } |
| 397 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 398 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 399 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 400 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 401 | const TemplateArgumentListInfo *TemplateArgs) { |
| 402 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
| 403 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 404 | SS.getRange(), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 405 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 406 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 409 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 410 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 411 | /// declaration at location Loc. Returns true to indicate that this is |
| 412 | /// an error, and false otherwise. |
| 413 | bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 414 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 415 | |
| 416 | // Microsoft Visual C++ permits template parameters to be shadowed. |
| 417 | if (getLangOptions().Microsoft) |
| 418 | return false; |
| 419 | |
| 420 | // C++ [temp.local]p4: |
| 421 | // A template-parameter shall not be redeclared within its |
| 422 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 424 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 425 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
| 426 | return true; |
| 427 | } |
| 428 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 429 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 430 | /// the parameter D to reference the templated declaration and return a pointer |
| 431 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 432 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 433 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 434 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 435 | return Temp; |
| 436 | } |
| 437 | return 0; |
| 438 | } |
| 439 | |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 440 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 441 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 442 | assert(Kind == Template && |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 443 | "Only template template arguments can be pack expansions here"); |
| 444 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 445 | "Template template argument pack expansion without packs"); |
| 446 | ParsedTemplateArgument Result(*this); |
| 447 | Result.EllipsisLoc = EllipsisLoc; |
| 448 | return Result; |
| 449 | } |
| 450 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 451 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 452 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 453 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 454 | switch (Arg.getKind()) { |
| 455 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 456 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 457 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 458 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 459 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 460 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 461 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 462 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 463 | case ParsedTemplateArgument::NonType: { |
| 464 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 465 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 466 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 467 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 468 | case ParsedTemplateArgument::Template: { |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 469 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 470 | TemplateArgument TArg; |
| 471 | if (Arg.getEllipsisLoc().isValid()) |
| 472 | TArg = TemplateArgument(Template, llvm::Optional<unsigned int>()); |
| 473 | else |
| 474 | TArg = Template; |
| 475 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 476 | Arg.getScopeSpec().getRange(), |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 477 | Arg.getLocation(), |
| 478 | Arg.getEllipsisLoc()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 481 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 482 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 483 | return TemplateArgumentLoc(); |
| 484 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 485 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 486 | /// \brief Translates template arguments as provided by the parser |
| 487 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 488 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 489 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 490 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 491 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 492 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 493 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 494 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 495 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 496 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 497 | /// the keyword "typename" was used to declare the type parameter |
| 498 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 499 | /// "class" or "typename" keyword. ParamName is the name of the |
| 500 | /// parameter (NULL indicates an unnamed template parameter) and |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 501 | /// ParamName is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 502 | /// If the type parameter has a default argument, it will be added |
| 503 | /// later via ActOnTypeParameterDefault. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 504 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
| 505 | SourceLocation EllipsisLoc, |
| 506 | SourceLocation KeyLoc, |
| 507 | IdentifierInfo *ParamName, |
| 508 | SourceLocation ParamNameLoc, |
| 509 | unsigned Depth, unsigned Position, |
| 510 | SourceLocation EqualLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 511 | ParsedType DefaultArg) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | assert(S->isTemplateParamScope() && |
| 513 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 514 | bool Invalid = false; |
| 515 | |
| 516 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 517 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 518 | LookupOrdinaryName, |
| 519 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 520 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 521 | Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 525 | SourceLocation Loc = ParamNameLoc; |
| 526 | if (!ParamName) |
| 527 | Loc = KeyLoc; |
| 528 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 529 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 530 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 531 | Loc, Depth, Position, ParamName, Typename, |
Anders Carlsson | 6d845ae | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 532 | Ellipsis); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 533 | if (Invalid) |
| 534 | Param->setInvalidDecl(); |
| 535 | |
| 536 | if (ParamName) { |
| 537 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 538 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 539 | IdResolver.AddDecl(Param); |
| 540 | } |
| 541 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 542 | // C++0x [temp.param]p9: |
| 543 | // A default template-argument may be specified for any kind of |
| 544 | // template-parameter that is not a template parameter pack. |
| 545 | if (DefaultArg && Ellipsis) { |
| 546 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 547 | DefaultArg = ParsedType(); |
| 548 | } |
| 549 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 550 | // Handle the default argument, if provided. |
| 551 | if (DefaultArg) { |
| 552 | TypeSourceInfo *DefaultTInfo; |
| 553 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 554 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 555 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 556 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 557 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 558 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 559 | UPPC_DefaultArgument)) |
| 560 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 561 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 562 | // Check the template argument itself. |
| 563 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 564 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 565 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 566 | } |
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 | Param->setDefaultArgument(DefaultTInfo, false); |
| 569 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 570 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 571 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 574 | /// \brief Check that the type of a non-type template parameter is |
| 575 | /// well-formed. |
| 576 | /// |
| 577 | /// \returns the (possibly-promoted) parameter type if valid; |
| 578 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 580 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 581 | // We don't allow variably-modified types as the type of non-type template |
| 582 | // parameters. |
| 583 | if (T->isVariablyModifiedType()) { |
| 584 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 585 | << T; |
| 586 | return QualType(); |
| 587 | } |
| 588 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 589 | // C++ [temp.param]p4: |
| 590 | // |
| 591 | // A non-type template-parameter shall have one of the following |
| 592 | // (optionally cv-qualified) types: |
| 593 | // |
| 594 | // -- integral or enumeration type, |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 595 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | // -- pointer to object or pointer to function, |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 597 | T->isPointerType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 598 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 599 | T->isReferenceType() || |
| 600 | // -- pointer to member. |
| 601 | T->isMemberPointerType() || |
| 602 | // If T is a dependent type, we can't do the check now, so we |
| 603 | // assume that it is well-formed. |
| 604 | T->isDependentType()) |
| 605 | return T; |
| 606 | // C++ [temp.param]p8: |
| 607 | // |
| 608 | // A non-type template-parameter of type "array of T" or |
| 609 | // "function returning T" is adjusted to be of type "pointer to |
| 610 | // T" or "pointer to function returning T", respectively. |
| 611 | else if (T->isArrayType()) |
| 612 | // FIXME: Keep the type prior to promotion? |
| 613 | return Context.getArrayDecayedType(T); |
| 614 | else if (T->isFunctionType()) |
| 615 | // FIXME: Keep the type prior to promotion? |
| 616 | return Context.getPointerType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 617 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 618 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 619 | << T; |
| 620 | |
| 621 | return QualType(); |
| 622 | } |
| 623 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 624 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 625 | unsigned Depth, |
| 626 | unsigned Position, |
| 627 | SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 628 | Expr *Default) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 629 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 630 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 631 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 632 | assert(S->isTemplateParamScope() && |
| 633 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 634 | bool Invalid = false; |
| 635 | |
| 636 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 637 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 638 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 639 | LookupOrdinaryName, |
| 640 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 641 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 642 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 643 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 646 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 647 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 648 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 649 | Invalid = true; |
| 650 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 651 | |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 652 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 653 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 654 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 655 | D.getIdentifierLoc(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 656 | Depth, Position, ParamName, T, |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 657 | IsParameterPack, TInfo); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 658 | if (Invalid) |
| 659 | Param->setInvalidDecl(); |
| 660 | |
| 661 | if (D.getIdentifier()) { |
| 662 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 663 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 664 | IdResolver.AddDecl(Param); |
| 665 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 666 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 667 | // C++0x [temp.param]p9: |
| 668 | // A default template-argument may be specified for any kind of |
| 669 | // template-parameter that is not a template parameter pack. |
| 670 | if (Default && IsParameterPack) { |
| 671 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 672 | Default = 0; |
| 673 | } |
| 674 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 675 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 676 | if (Default) { |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 677 | // Check for unexpanded parameter packs. |
| 678 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 679 | return Param; |
| 680 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 681 | TemplateArgument Converted; |
| 682 | if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) { |
| 683 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 684 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 685 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 686 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 687 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 688 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 689 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 690 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 691 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 692 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 693 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 694 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 695 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 696 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 697 | SourceLocation TmpLoc, |
| 698 | TemplateParamsTy *Params, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 699 | SourceLocation EllipsisLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 700 | IdentifierInfo *Name, |
| 701 | SourceLocation NameLoc, |
| 702 | unsigned Depth, |
| 703 | unsigned Position, |
| 704 | SourceLocation EqualLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 705 | ParsedTemplateArgument Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 706 | assert(S->isTemplateParamScope() && |
| 707 | "Template template parameter not in template parameter scope!"); |
| 708 | |
| 709 | // Construct the parameter object. |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 710 | bool IsParameterPack = EllipsisLoc.isValid(); |
| 711 | // FIXME: Pack-ness is dropped |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 712 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 713 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 714 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 715 | Depth, Position, IsParameterPack, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 716 | Name, Params); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 717 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 718 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 719 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 720 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 721 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 722 | IdResolver.AddDecl(Param); |
| 723 | } |
| 724 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 725 | if (Params->size() == 0) { |
| 726 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 727 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 728 | Param->setInvalidDecl(); |
| 729 | } |
| 730 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 731 | // C++0x [temp.param]p9: |
| 732 | // A default template-argument may be specified for any kind of |
| 733 | // template-parameter that is not a template parameter pack. |
| 734 | if (IsParameterPack && !Default.isInvalid()) { |
| 735 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 736 | Default = ParsedTemplateArgument(); |
| 737 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 738 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 739 | if (!Default.isInvalid()) { |
| 740 | // Check only that we have a template template argument. We don't want to |
| 741 | // try to check well-formedness now, because our template template parameter |
| 742 | // might have dependent types in its template parameters, which we wouldn't |
| 743 | // be able to match now. |
| 744 | // |
| 745 | // If none of the template template parameter's template arguments mention |
| 746 | // other template parameters, we could actually perform more checking here. |
| 747 | // However, it isn't worth doing. |
| 748 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 749 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 750 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 751 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 752 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 753 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 754 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 755 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 756 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 757 | DefaultArg.getArgument().getAsTemplate(), |
| 758 | UPPC_DefaultArgument)) |
| 759 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 760 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 761 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 762 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 763 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 764 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 767 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 768 | /// contains the template parameters in Params/NumParams. |
| 769 | Sema::TemplateParamsTy * |
| 770 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 771 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 772 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 773 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 774 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 775 | SourceLocation RAngleLoc) { |
| 776 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 777 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 778 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 779 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 780 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 781 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 782 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 783 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 784 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 785 | if (SS.isSet()) |
| 786 | T->setQualifierInfo(static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 787 | SS.getRange()); |
| 788 | } |
| 789 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 790 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 791 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 792 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 793 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 794 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 795 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 796 | AccessSpecifier AS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 797 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 798 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 799 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 800 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 801 | |
| 802 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 803 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 804 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 805 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 806 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 807 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 808 | |
| 809 | // There is no such thing as an unnamed class template. |
| 810 | if (!Name) { |
| 811 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 812 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 816 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 817 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 818 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 819 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 820 | SemanticContext = computeDeclContext(SS, true); |
| 821 | if (!SemanticContext) { |
| 822 | // FIXME: Produce a reasonable diagnostic here |
| 823 | return true; |
| 824 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 826 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 827 | return true; |
| 828 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 829 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 830 | } else { |
| 831 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 832 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 833 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 835 | if (Previous.isAmbiguous()) |
| 836 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 837 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 838 | NamedDecl *PrevDecl = 0; |
| 839 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 840 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 841 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 842 | // If there is a previous declaration with the same name, check |
| 843 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 844 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 845 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 846 | |
| 847 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 848 | // class template partial specialization, or class template specialization. |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 849 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 850 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 851 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 852 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 853 | PrevClassTemplate |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 854 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 855 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 856 | PrevClassTemplate |
| 857 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 858 | ->getSpecializedTemplate(); |
| 859 | } |
| 860 | } |
| 861 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 862 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 863 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 864 | // [...] When looking for a prior declaration of a class or a function |
| 865 | // 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] | 866 | // function is neither a qualified name nor a template-id, scopes outside |
| 867 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 868 | if (!SS.isSet()) { |
| 869 | DeclContext *OutermostContext = CurContext; |
| 870 | while (!OutermostContext->isFileContext()) |
| 871 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 872 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 873 | if (PrevDecl && |
| 874 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 875 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 876 | SemanticContext = PrevDecl->getDeclContext(); |
| 877 | } else { |
| 878 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 879 | // context we computed is the semantic context for our new |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 880 | // declaration. |
| 881 | PrevDecl = PrevClassTemplate = 0; |
| 882 | SemanticContext = OutermostContext; |
| 883 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 884 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 885 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 886 | if (CurContext->isDependentContext()) { |
| 887 | // If this is a dependent context, we don't want to link the friend |
| 888 | // class template to the template in scope, because that would perform |
| 889 | // checking of the template parameter lists that can't be performed |
| 890 | // until the outer context is instantiated. |
| 891 | PrevDecl = PrevClassTemplate = 0; |
| 892 | } |
| 893 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 894 | PrevDecl = PrevClassTemplate = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 895 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 896 | if (PrevClassTemplate) { |
| 897 | // Ensure that the template parameter lists are compatible. |
| 898 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 899 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 900 | /*Complain=*/true, |
| 901 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 902 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 903 | |
| 904 | // C++ [temp.class]p4: |
| 905 | // In a redeclaration, partial specialization, explicit |
| 906 | // specialization or explicit instantiation of a class template, |
| 907 | // the class-key shall agree in kind with the original class |
| 908 | // template declaration (7.1.5.3). |
| 909 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 910 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 911 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 912 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 913 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 914 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 915 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 918 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 919 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 920 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 921 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 922 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 923 | // FIXME: Would it make sense to try to "forget" the previous |
| 924 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 925 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 929 | // Maybe we will complain about the shadowed template parameter. |
| 930 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 931 | // Just pretend that we didn't see the previous declaration. |
| 932 | PrevDecl = 0; |
| 933 | } else if (PrevDecl) { |
| 934 | // C++ [temp]p5: |
| 935 | // A class template shall not have the same name as any other |
| 936 | // template, class, function, object, enumeration, enumerator, |
| 937 | // namespace, or type in the same scope (3.3), except as specified |
| 938 | // in (14.5.4). |
| 939 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 940 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 941 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 944 | // Check the template parameter list of this declaration, possibly |
| 945 | // merging in the template parameter list from the previous class |
| 946 | // template declaration. |
| 947 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 948 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
| 949 | TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 950 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 952 | if (SS.isSet()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 953 | // 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] | 954 | // template out-of-line. |
| 955 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
| 956 | !(TUK == TUK_Friend && CurContext->isDependentContext())) |
| 957 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 958 | << Name << SemanticContext << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | CXXRecordDecl *NewClass = |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 962 | CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 963 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 964 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 965 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 966 | SetNestedNameSpecifier(NewClass, SS); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 967 | |
| 968 | ClassTemplateDecl *NewTemplate |
| 969 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 970 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 971 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 972 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 973 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 974 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 975 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 976 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 977 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 978 | (void)T; |
| 979 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 980 | // 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] | 981 | // class template, make a note of that. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 982 | if (PrevClassTemplate && |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 983 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 984 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 985 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 986 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 987 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 988 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 990 | // Set the lexical context of these templates |
| 991 | NewClass->setLexicalDeclContext(CurContext); |
| 992 | NewTemplate->setLexicalDeclContext(CurContext); |
| 993 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 994 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 995 | NewClass->startDefinition(); |
| 996 | |
| 997 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 998 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 999 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1000 | if (TUK != TUK_Friend) |
| 1001 | PushOnScopeChains(NewTemplate, S); |
| 1002 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1003 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1004 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1005 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1006 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1007 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1008 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1009 | PrevClassTemplate != NULL); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1010 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1011 | // Friend templates are visible in fairly strange ways. |
| 1012 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1013 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1014 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 1015 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1016 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1017 | /* AddToContext = */ false); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1018 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1019 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1020 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1021 | NewClass->getLocation(), |
| 1022 | NewTemplate, |
| 1023 | /*FIXME:*/NewClass->getLocation()); |
| 1024 | Friend->setAccess(AS_public); |
| 1025 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1026 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1027 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1028 | if (Invalid) { |
| 1029 | NewTemplate->setInvalidDecl(); |
| 1030 | NewClass->setInvalidDecl(); |
| 1031 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1032 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1035 | /// \brief Diagnose the presence of a default template argument on a |
| 1036 | /// template parameter, which is ill-formed in certain contexts. |
| 1037 | /// |
| 1038 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1039 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1040 | Sema::TemplateParamListContext TPC, |
| 1041 | SourceLocation ParamLoc, |
| 1042 | SourceRange DefArgRange) { |
| 1043 | switch (TPC) { |
| 1044 | case Sema::TPC_ClassTemplate: |
| 1045 | return false; |
| 1046 | |
| 1047 | case Sema::TPC_FunctionTemplate: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1048 | // C++ [temp.param]p9: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1049 | // A default template-argument shall not be specified in a |
| 1050 | // function template declaration or a function template |
| 1051 | // definition [...] |
| 1052 | // (This sentence is not in C++0x, per DR226). |
| 1053 | if (!S.getLangOptions().CPlusPlus0x) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1054 | S.Diag(ParamLoc, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1055 | diag::err_template_parameter_default_in_function_template) |
| 1056 | << DefArgRange; |
| 1057 | return false; |
| 1058 | |
| 1059 | case Sema::TPC_ClassTemplateMember: |
| 1060 | // C++0x [temp.param]p9: |
| 1061 | // A default template-argument shall not be specified in the |
| 1062 | // template-parameter-lists of the definition of a member of a |
| 1063 | // class template that appears outside of the member's class. |
| 1064 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1065 | << DefArgRange; |
| 1066 | return true; |
| 1067 | |
| 1068 | case Sema::TPC_FriendFunctionTemplate: |
| 1069 | // C++ [temp.param]p9: |
| 1070 | // A default template-argument shall not be specified in a |
| 1071 | // friend template declaration. |
| 1072 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1073 | << DefArgRange; |
| 1074 | return true; |
| 1075 | |
| 1076 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1077 | // for friend function templates if there is only a single |
| 1078 | // declaration (and it is a definition). Strange! |
| 1079 | } |
| 1080 | |
| 1081 | return false; |
| 1082 | } |
| 1083 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1084 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1085 | /// of a template template parameter, recursively. |
| 1086 | bool DiagnoseUnexpandedParameterPacks(Sema &S, TemplateTemplateParmDecl *TTP){ |
| 1087 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1088 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1089 | NamedDecl *P = Params->getParam(I); |
| 1090 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1091 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1092 | NTTP->getTypeSourceInfo(), |
| 1093 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1094 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1095 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1096 | continue; |
| 1097 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1098 | |
| 1099 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1100 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1101 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1102 | return true; |
| 1103 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1104 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1105 | return false; |
| 1106 | } |
| 1107 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1108 | /// \brief Checks the validity of a template parameter list, possibly |
| 1109 | /// considering the template parameter list from a previous |
| 1110 | /// declaration. |
| 1111 | /// |
| 1112 | /// If an "old" template parameter list is provided, it must be |
| 1113 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1114 | /// template parameter list. |
| 1115 | /// |
| 1116 | /// \param NewParams Template parameter list for a new template |
| 1117 | /// declaration. This template parameter list will be updated with any |
| 1118 | /// default arguments that are carried through from the previous |
| 1119 | /// template parameter list. |
| 1120 | /// |
| 1121 | /// \param OldParams If provided, template parameter list from a |
| 1122 | /// previous declaration of the same template. Default template |
| 1123 | /// arguments will be merged from the old template parameter list to |
| 1124 | /// the new template parameter list. |
| 1125 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1126 | /// \param TPC Describes the context in which we are checking the given |
| 1127 | /// template parameter list. |
| 1128 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1129 | /// \returns true if an error occurred, false otherwise. |
| 1130 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1131 | TemplateParameterList *OldParams, |
| 1132 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1133 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1134 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1135 | // C++ [temp.param]p10: |
| 1136 | // The set of default template-arguments available for use with a |
| 1137 | // template declaration or definition is obtained by merging the |
| 1138 | // default arguments from the definition (if in scope) and all |
| 1139 | // declarations in scope in the same way default function |
| 1140 | // arguments are (8.3.6). |
| 1141 | bool SawDefaultArgument = false; |
| 1142 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1143 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1144 | bool SawParameterPack = false; |
| 1145 | SourceLocation ParameterPackLoc; |
| 1146 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1147 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1148 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1149 | if (OldParams) |
| 1150 | OldParam = OldParams->begin(); |
| 1151 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1152 | bool RemoveDefaultArguments = false; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1153 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1154 | NewParamEnd = NewParams->end(); |
| 1155 | NewParam != NewParamEnd; ++NewParam) { |
| 1156 | // Variables used to diagnose redundant default arguments |
| 1157 | bool RedundantDefaultArg = false; |
| 1158 | SourceLocation OldDefaultLoc; |
| 1159 | SourceLocation NewDefaultLoc; |
| 1160 | |
| 1161 | // Variables used to diagnose missing default arguments |
| 1162 | bool MissingDefaultArg = false; |
| 1163 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1164 | // C++0x [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1165 | // If a template parameter of a primary class template is a template |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1166 | // parameter pack, it shall be the last template parameter. |
| 1167 | if (SawParameterPack && TPC == TPC_ClassTemplate) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1169 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1170 | Invalid = true; |
| 1171 | } |
| 1172 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1173 | if (TemplateTypeParmDecl *NewTypeParm |
| 1174 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1175 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1176 | if (NewTypeParm->hasDefaultArgument() && |
| 1177 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1178 | NewTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1179 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1180 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1181 | NewTypeParm->removeDefaultArgument(); |
| 1182 | |
| 1183 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1184 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1185 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1186 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1187 | if (NewTypeParm->isParameterPack()) { |
| 1188 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1189 | "Parameter packs can't have a default argument!"); |
| 1190 | SawParameterPack = true; |
| 1191 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1192 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1193 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1194 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1195 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1196 | SawDefaultArgument = true; |
| 1197 | RedundantDefaultArg = true; |
| 1198 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1199 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1200 | // Merge the default argument from the old declaration to the |
| 1201 | // new declaration. |
| 1202 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1203 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1204 | true); |
| 1205 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1206 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1207 | SawDefaultArgument = true; |
| 1208 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1209 | } else if (SawDefaultArgument) |
| 1210 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1211 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1212 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1213 | // Check for unexpanded parameter packs. |
| 1214 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1215 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1216 | UPPC_NonTypeTemplateParameterType)) { |
| 1217 | Invalid = true; |
| 1218 | continue; |
| 1219 | } |
| 1220 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1221 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1222 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1223 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1224 | NewNonTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1225 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1226 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1229 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1230 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1231 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1232 | if (NewNonTypeParm->isParameterPack()) { |
| 1233 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1234 | "Parameter packs can't have a default argument!"); |
| 1235 | SawParameterPack = true; |
| 1236 | ParameterPackLoc = NewNonTypeParm->getLocation(); |
| 1237 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1238 | NewNonTypeParm->hasDefaultArgument()) { |
| 1239 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1240 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1241 | SawDefaultArgument = true; |
| 1242 | RedundantDefaultArg = true; |
| 1243 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1244 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1245 | // Merge the default argument from the old declaration to the |
| 1246 | // new declaration. |
| 1247 | SawDefaultArgument = true; |
| 1248 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1249 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1250 | // parameter. |
| 1251 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1252 | OldNonTypeParm->getDefaultArgument(), |
| 1253 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1254 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1255 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1256 | SawDefaultArgument = true; |
| 1257 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1258 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1260 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1261 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1262 | TemplateTemplateParmDecl *NewTemplateParm |
| 1263 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1264 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1265 | // Check for unexpanded parameter packs, recursively. |
| 1266 | if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
| 1267 | Invalid = true; |
| 1268 | continue; |
| 1269 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1270 | |
| 1271 | if (NewTemplateParm->hasDefaultArgument() && |
| 1272 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1273 | NewTemplateParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1274 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1275 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1276 | |
| 1277 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1278 | TemplateTemplateParmDecl *OldTemplateParm |
| 1279 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1280 | if (NewTemplateParm->isParameterPack()) { |
| 1281 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1282 | "Parameter packs can't have a default argument!"); |
| 1283 | SawParameterPack = true; |
| 1284 | ParameterPackLoc = NewTemplateParm->getLocation(); |
| 1285 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1286 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1287 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1288 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1289 | SawDefaultArgument = true; |
| 1290 | RedundantDefaultArg = true; |
| 1291 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1292 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1293 | // Merge the default argument from the old declaration to the |
| 1294 | // new declaration. |
| 1295 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1296 | // FIXME: We need to create a new kind of "default argument" expression |
| 1297 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1298 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1299 | OldTemplateParm->getDefaultArgument(), |
| 1300 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1301 | PreviousDefaultArgLoc |
| 1302 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1303 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1304 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1305 | PreviousDefaultArgLoc |
| 1306 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1307 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | if (RedundantDefaultArg) { |
| 1312 | // C++ [temp.param]p12: |
| 1313 | // A template-parameter shall not be given default arguments |
| 1314 | // by two different declarations in the same scope. |
| 1315 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1316 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1317 | Invalid = true; |
| 1318 | } else if (MissingDefaultArg) { |
| 1319 | // C++ [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1320 | // If a template-parameter of a class template has a default |
| 1321 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1322 | // have a default template-argument supplied or be a template parameter |
| 1323 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1324 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1325 | diag::err_template_param_default_arg_missing); |
| 1326 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1327 | Invalid = true; |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1328 | RemoveDefaultArguments = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | // If we have an old template parameter list that we're merging |
| 1332 | // in, move on to the next parameter. |
| 1333 | if (OldParams) |
| 1334 | ++OldParam; |
| 1335 | } |
| 1336 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1337 | // We were missing some default arguments at the end of the list, so remove |
| 1338 | // all of the default arguments. |
| 1339 | if (RemoveDefaultArguments) { |
| 1340 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1341 | NewParamEnd = NewParams->end(); |
| 1342 | NewParam != NewParamEnd; ++NewParam) { |
| 1343 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1344 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1345 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1346 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1347 | NTTP->removeDefaultArgument(); |
| 1348 | else |
| 1349 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1350 | } |
| 1351 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1352 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1353 | return Invalid; |
| 1354 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1355 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1356 | namespace { |
| 1357 | |
| 1358 | /// A class which looks for a use of a certain level of template |
| 1359 | /// parameter. |
| 1360 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1361 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1362 | |
| 1363 | unsigned Depth; |
| 1364 | bool Match; |
| 1365 | |
| 1366 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1367 | NamedDecl *ND = Params->getParam(0); |
| 1368 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1369 | Depth = PD->getDepth(); |
| 1370 | } else if (NonTypeTemplateParmDecl *PD = |
| 1371 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1372 | Depth = PD->getDepth(); |
| 1373 | } else { |
| 1374 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | bool Matches(unsigned ParmDepth) { |
| 1379 | if (ParmDepth >= Depth) { |
| 1380 | Match = true; |
| 1381 | return true; |
| 1382 | } |
| 1383 | return false; |
| 1384 | } |
| 1385 | |
| 1386 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1387 | return !Matches(T->getDepth()); |
| 1388 | } |
| 1389 | |
| 1390 | bool TraverseTemplateName(TemplateName N) { |
| 1391 | if (TemplateTemplateParmDecl *PD = |
| 1392 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1393 | if (Matches(PD->getDepth())) return false; |
| 1394 | return super::TraverseTemplateName(N); |
| 1395 | } |
| 1396 | |
| 1397 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1398 | if (NonTypeTemplateParmDecl *PD = |
| 1399 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1400 | if (PD->getDepth() == Depth) { |
| 1401 | Match = true; |
| 1402 | return false; |
| 1403 | } |
| 1404 | } |
| 1405 | return super::VisitDeclRefExpr(E); |
| 1406 | } |
| 1407 | }; |
| 1408 | } |
| 1409 | |
| 1410 | /// Determines whether a template-id depends on the given parameter |
| 1411 | /// list. |
| 1412 | static bool |
| 1413 | DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId, |
| 1414 | TemplateParameterList *Params) { |
| 1415 | DependencyChecker Checker(Params); |
| 1416 | Checker.TraverseType(QualType(TemplateId, 0)); |
| 1417 | return Checker.Match; |
| 1418 | } |
| 1419 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1420 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1421 | /// specifier, returning the template parameter list that applies to the |
| 1422 | /// name. |
| 1423 | /// |
| 1424 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1425 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1426 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1427 | /// \param SS the scope specifier that will be matched to the given template |
| 1428 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1429 | /// being declared. |
| 1430 | /// |
| 1431 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1432 | /// innermost template parameter lists. |
| 1433 | /// |
| 1434 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1435 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1436 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1437 | /// matching template parameters to scope specifiers in friend |
| 1438 | /// declarations. |
| 1439 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1440 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1441 | /// declared is an explicit specialization, false otherwise. |
| 1442 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1444 | /// name that is preceded by the scope specifier @p SS. This template |
| 1445 | /// parameter list may be have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1446 | /// template) or may have no template parameters (if we're declaring a |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1447 | /// template specialization), or may be NULL (if we were's declaring isn't |
| 1448 | /// itself a template). |
| 1449 | TemplateParameterList * |
| 1450 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
| 1451 | const CXXScopeSpec &SS, |
| 1452 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1453 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1454 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1455 | bool &IsExplicitSpecialization, |
| 1456 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1457 | IsExplicitSpecialization = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1458 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1459 | // Find the template-ids that occur within the nested-name-specifier. These |
| 1460 | // template-ids will match up with the template parameter lists. |
| 1461 | llvm::SmallVector<const TemplateSpecializationType *, 4> |
| 1462 | TemplateIdsInSpecifier; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1463 | llvm::SmallVector<ClassTemplateSpecializationDecl *, 4> |
| 1464 | ExplicitSpecializationsInSpecifier; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1465 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 1466 | NNS; NNS = NNS->getPrefix()) { |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1467 | const Type *T = NNS->getAsType(); |
| 1468 | if (!T) break; |
| 1469 | |
| 1470 | // C++0x [temp.expl.spec]p17: |
| 1471 | // A member or a member template may be nested within many |
| 1472 | // enclosing class templates. In an explicit specialization for |
| 1473 | // such a member, the member declaration shall be preceded by a |
| 1474 | // template<> for each enclosing class template that is |
| 1475 | // explicitly specialized. |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1476 | // |
| 1477 | // Following the existing practice of GNU and EDG, we allow a typedef of a |
| 1478 | // template specialization type. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1479 | while (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
| 1480 | T = TT->getDecl()->getUnderlyingType().getTypePtr(); |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1481 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | if (const TemplateSpecializationType *SpecType |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1483 | = dyn_cast<TemplateSpecializationType>(T)) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1484 | TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl(); |
| 1485 | if (!Template) |
| 1486 | continue; // FIXME: should this be an error? probably... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1488 | if (const RecordType *Record = SpecType->getAs<RecordType>()) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1489 | ClassTemplateSpecializationDecl *SpecDecl |
| 1490 | = cast<ClassTemplateSpecializationDecl>(Record->getDecl()); |
| 1491 | // If the nested name specifier refers to an explicit specialization, |
| 1492 | // we don't need a template<> header. |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1493 | if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1494 | ExplicitSpecializationsInSpecifier.push_back(SpecDecl); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1495 | continue; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1496 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1497 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1498 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1499 | TemplateIdsInSpecifier.push_back(SpecType); |
| 1500 | } |
| 1501 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1502 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1503 | // Reverse the list of template-ids in the scope specifier, so that we can |
| 1504 | // more easily match up the template-ids and the template parameter lists. |
| 1505 | std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1507 | SourceLocation FirstTemplateLoc = DeclStartLoc; |
| 1508 | if (NumParamLists) |
| 1509 | FirstTemplateLoc = ParamLists[0]->getTemplateLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1510 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1511 | // Match the template-ids found in the specifier to the template parameter |
| 1512 | // lists. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1513 | unsigned ParamIdx = 0, TemplateIdx = 0; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1514 | for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size(); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1515 | TemplateIdx != NumTemplateIds; ++TemplateIdx) { |
| 1516 | const TemplateSpecializationType *TemplateId |
| 1517 | = TemplateIdsInSpecifier[TemplateIdx]; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1518 | bool DependentTemplateId = TemplateId->isDependentType(); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1519 | |
| 1520 | // In friend declarations we can have template-ids which don't |
| 1521 | // depend on the corresponding template parameter lists. But |
| 1522 | // assume that empty parameter lists are supposed to match this |
| 1523 | // template-id. |
| 1524 | if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) { |
| 1525 | if (!DependentTemplateId || |
| 1526 | !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx])) |
| 1527 | continue; |
| 1528 | } |
| 1529 | |
| 1530 | if (ParamIdx >= NumParamLists) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1531 | // We have a template-id without a corresponding template parameter |
| 1532 | // list. |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1533 | |
| 1534 | // ...which is fine if this is a friend declaration. |
| 1535 | if (IsFriend) { |
| 1536 | IsExplicitSpecialization = true; |
| 1537 | break; |
| 1538 | } |
| 1539 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1540 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1541 | // FIXME: the location information here isn't great. |
| 1542 | Diag(SS.getRange().getBegin(), |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1543 | diag::err_template_spec_needs_template_parameters) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1544 | << QualType(TemplateId, 0) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1545 | << SS.getRange(); |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1546 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1547 | } else { |
| 1548 | Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header) |
| 1549 | << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1550 | << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1551 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1552 | } |
| 1553 | return 0; |
| 1554 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1556 | // Check the template parameter list against its corresponding template-id. |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1557 | if (DependentTemplateId) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1558 | TemplateParameterList *ExpectedTemplateParams = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1559 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1560 | // Are there cases in (e.g.) friends where this won't match? |
| 1561 | if (const InjectedClassNameType *Injected |
| 1562 | = TemplateId->getAs<InjectedClassNameType>()) { |
| 1563 | CXXRecordDecl *Record = Injected->getDecl(); |
| 1564 | if (ClassTemplatePartialSpecializationDecl *Partial = |
| 1565 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 1566 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1567 | else |
| 1568 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
| 1569 | ->getTemplateParameters(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1571 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1572 | if (ExpectedTemplateParams) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1573 | TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1574 | ExpectedTemplateParams, |
| 1575 | true, TPL_TemplateMatch); |
| 1576 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1577 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1578 | TPC_ClassTemplateMember); |
| 1579 | } else if (ParamLists[ParamIdx]->size() > 0) |
| 1580 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1581 | diag::err_template_param_list_matches_nontemplate) |
| 1582 | << TemplateId |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1583 | << ParamLists[ParamIdx]->getSourceRange(); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1584 | else |
| 1585 | IsExplicitSpecialization = true; |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1586 | |
| 1587 | ++ParamIdx; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1588 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1589 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1590 | // If there were at least as many template-ids as there were template |
| 1591 | // parameter lists, then there are no template parameter lists remaining for |
| 1592 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1593 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1594 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1595 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1596 | // If there were too many template parameter lists, complain about that now. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1597 | if (ParamIdx != NumParamLists - 1) { |
| 1598 | while (ParamIdx < NumParamLists - 1) { |
| 1599 | bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0; |
| 1600 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1601 | isExplicitSpecHeader? diag::warn_template_spec_extra_headers |
| 1602 | : diag::err_template_spec_extra_headers) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1603 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1604 | ParamLists[ParamIdx]->getRAngleLoc()); |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1605 | |
| 1606 | if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) { |
| 1607 | Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(), |
| 1608 | diag::note_explicit_template_spec_does_not_need_header) |
| 1609 | << ExplicitSpecializationsInSpecifier.back(); |
| 1610 | ExplicitSpecializationsInSpecifier.pop_back(); |
| 1611 | } |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1612 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1613 | // We have a template parameter list with no corresponding scope, which |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1614 | // means that the resulting template declaration can't be instantiated |
| 1615 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1616 | if (!isExplicitSpecHeader) |
| 1617 | Invalid = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1618 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1619 | ++ParamIdx; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1620 | } |
| 1621 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1622 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1623 | // Return the last template parameter list, which corresponds to the |
| 1624 | // entity being declared. |
| 1625 | return ParamLists[NumParamLists - 1]; |
| 1626 | } |
| 1627 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1628 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1629 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1630 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1631 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1632 | if (!Template) { |
| 1633 | // The template name does not resolve to a template, so we just |
| 1634 | // build a dependent template-id type. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1635 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1636 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1637 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1638 | // Check that the template argument list is well-formed for this |
| 1639 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1640 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1641 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1642 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1643 | return QualType(); |
| 1644 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1645 | assert((Converted.size() == Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1646 | "Converted template argument list is too short!"); |
| 1647 | |
| 1648 | QualType CanonType; |
| 1649 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 1650 | if (Name.isDependent() || |
| 1651 | TemplateSpecializationType::anyDependentTemplateArguments( |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1652 | TemplateArgs)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1653 | // This class template specialization is a dependent |
| 1654 | // type. Therefore, its canonical type is another class template |
| 1655 | // specialization type that contains all of the converted |
| 1656 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1657 | // A<T, T> have identical types when A is declared as: |
| 1658 | // |
| 1659 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1660 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1661 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1662 | Converted.data(), |
| 1663 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1664 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1665 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1666 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1667 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1668 | // build the canonical type and return that to us. |
| 1669 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1670 | |
| 1671 | // This might work out to be a current instantiation, in which |
| 1672 | // case the canonical type needs to be the InjectedClassNameType. |
| 1673 | // |
| 1674 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1675 | // changes to CurContext don't change the set of current |
| 1676 | // instantiations. |
| 1677 | if (isa<ClassTemplateDecl>(Template)) { |
| 1678 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1679 | // If we get out to a namespace, we're done. |
| 1680 | if (Ctx->isFileContext()) break; |
| 1681 | |
| 1682 | // If this isn't a record, keep looking. |
| 1683 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1684 | if (!Record) continue; |
| 1685 | |
| 1686 | // Look for one of the two cases with InjectedClassNameTypes |
| 1687 | // and check whether it's the same template. |
| 1688 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1689 | !Record->getDescribedClassTemplate()) |
| 1690 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1691 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1692 | // Fetch the injected class name type and check whether its |
| 1693 | // injected type is equal to the type we just built. |
| 1694 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1695 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 1696 | ->getInjectedSpecializationType(); |
| 1697 | |
| 1698 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 1699 | continue; |
| 1700 | |
| 1701 | // If so, the canonical type of this TST is the injected |
| 1702 | // class name type of the record we just found. |
| 1703 | assert(ICNT.isCanonical()); |
| 1704 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1705 | break; |
| 1706 | } |
| 1707 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1708 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1709 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1710 | // Find the class template specialization declaration that |
| 1711 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1712 | void *InsertPos = 0; |
| 1713 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1714 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1715 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1716 | if (!Decl) { |
| 1717 | // This is the first time we have referenced this class template |
| 1718 | // specialization. Create the canonical declaration and add it to |
| 1719 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1720 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1721 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 1722 | ClassTemplate->getDeclContext(), |
| 1723 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1724 | ClassTemplate, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1725 | Converted.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1726 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 1727 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1728 | Decl->setLexicalDeclContext(CurContext); |
| 1729 | } |
| 1730 | |
| 1731 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1732 | assert(isa<RecordType>(CanonType) && |
| 1733 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1734 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1736 | // Build the fully-sugared type for this class template |
| 1737 | // specialization, which refers back to the class template |
| 1738 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1739 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1742 | TypeResult |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1743 | Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1744 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1745 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1746 | SourceLocation RAngleLoc) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1747 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1748 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1749 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1750 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1751 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1752 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1753 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1754 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1755 | |
| 1756 | if (Result.isNull()) |
| 1757 | return true; |
| 1758 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1759 | TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1760 | TemplateSpecializationTypeLoc TL |
| 1761 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1762 | TL.setTemplateNameLoc(TemplateLoc); |
| 1763 | TL.setLAngleLoc(LAngleLoc); |
| 1764 | TL.setRAngleLoc(RAngleLoc); |
| 1765 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1766 | TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
| 1767 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1768 | return CreateParsedType(Result, DI); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1769 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1770 | |
Craig Silverstein | 45ab4b5 | 2010-11-18 08:32:02 +0000 | [diff] [blame] | 1771 | TypeResult Sema::ActOnTagTemplateIdType(CXXScopeSpec &SS, |
| 1772 | TypeResult TypeResult, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1773 | TagUseKind TUK, |
| 1774 | TypeSpecifierType TagSpec, |
| 1775 | SourceLocation TagLoc) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1776 | if (TypeResult.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1777 | return ::TypeResult(); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1778 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1779 | TypeSourceInfo *DI; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1780 | QualType Type = GetTypeFromParser(TypeResult.get(), &DI); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1781 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1782 | // Verify the tag specifier. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1783 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1785 | if (const RecordType *RT = Type->getAs<RecordType>()) { |
| 1786 | RecordDecl *D = RT->getDecl(); |
| 1787 | |
| 1788 | IdentifierInfo *Id = D->getIdentifier(); |
| 1789 | assert(Id && "templated class must have an identifier"); |
| 1790 | |
| 1791 | if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { |
| 1792 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1793 | << Type |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1794 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1795 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1796 | } |
| 1797 | } |
| 1798 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1799 | ElaboratedTypeKeyword Keyword |
| 1800 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
| 1801 | QualType ElabType = Context.getElaboratedType(Keyword, /*NNS=*/0, Type); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1802 | |
Craig Silverstein | 45ab4b5 | 2010-11-18 08:32:02 +0000 | [diff] [blame] | 1803 | TypeSourceInfo *ElabDI = Context.CreateTypeSourceInfo(ElabType); |
| 1804 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(ElabDI->getTypeLoc()); |
| 1805 | TL.setKeywordLoc(TagLoc); |
| 1806 | TL.setQualifierRange(SS.getRange()); |
| 1807 | TL.getNamedTypeLoc().initializeFullCopy(DI->getTypeLoc()); |
| 1808 | return CreateParsedType(ElabType, ElabDI); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1811 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1812 | LookupResult &R, |
| 1813 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1814 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1815 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 1816 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1817 | // 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] | 1818 | // though. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1819 | |
| 1820 | // These should be filtered out by our callers. |
| 1821 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 1822 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 1823 | |
| 1824 | NestedNameSpecifier *Qualifier = 0; |
| 1825 | SourceRange QualifierRange; |
| 1826 | if (SS.isSet()) { |
| 1827 | Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 1828 | QualifierRange = SS.getRange(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1829 | } |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1830 | |
| 1831 | // We don't want lookup warnings at this point. |
| 1832 | R.suppressDiagnostics(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1833 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1834 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1835 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1836 | Qualifier, QualifierRange, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1837 | R.getLookupNameInfo(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1838 | RequiresADL, TemplateArgs, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 1839 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1840 | |
| 1841 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1844 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1845 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1846 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1847 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1848 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1849 | DeclContext *DC; |
| 1850 | if (!(DC = computeDeclContext(SS, false)) || |
| 1851 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1852 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1853 | return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1855 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1856 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1857 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 1858 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1860 | if (R.isAmbiguous()) |
| 1861 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1862 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1863 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1864 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 1865 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1866 | return ExprError(); |
| 1867 | } |
| 1868 | |
| 1869 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1870 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 1871 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 1872 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1873 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 1874 | return ExprError(); |
| 1875 | } |
| 1876 | |
| 1877 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1880 | /// \brief Form a dependent template name. |
| 1881 | /// |
| 1882 | /// This action forms a dependent template name given the template |
| 1883 | /// name and its (presumably dependent) scope specifier. For |
| 1884 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 1885 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 1886 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1887 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1888 | SourceLocation TemplateKWLoc, |
| 1889 | CXXScopeSpec &SS, |
| 1890 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1891 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1892 | bool EnteringContext, |
| 1893 | TemplateTy &Result) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 1894 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() && |
| 1895 | !getLangOptions().CPlusPlus0x) |
| 1896 | Diag(TemplateKWLoc, diag::ext_template_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1897 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 1898 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1899 | DeclContext *LookupCtx = 0; |
| 1900 | if (SS.isSet()) |
| 1901 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 1902 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1903 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1904 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1905 | // C++0x [temp.names]p5: |
| 1906 | // If a name prefixed by the keyword template is not the name of |
| 1907 | // a template, the program is ill-formed. [Note: the keyword |
| 1908 | // template may not be applied to non-template members of class |
| 1909 | // templates. -end note ] [ Note: as is the case with the |
| 1910 | // typename prefix, the template prefix is allowed in cases |
| 1911 | // where it is not strictly necessary; i.e., when the |
| 1912 | // nested-name-specifier or the expression on the left of the -> |
| 1913 | // or . is not dependent on a template-parameter, or the use |
| 1914 | // does not appear in the scope of a template. -end note] |
| 1915 | // |
| 1916 | // Note: C++03 was more strict here, because it banned the use of |
| 1917 | // the "template" keyword prior to a template-name that was not a |
| 1918 | // dependent name. C++ DR468 relaxed this requirement (the |
| 1919 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 1920 | // 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] | 1921 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 1922 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 1923 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1924 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1925 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 1926 | isa<CXXRecordDecl>(LookupCtx) && |
| 1927 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1928 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1929 | } else if (TNK == TNK_Non_template) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1930 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1931 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1932 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1933 | << Name.getSourceRange() |
| 1934 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1935 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1936 | } else { |
| 1937 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1938 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1939 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1943 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1944 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1945 | switch (Name.getKind()) { |
| 1946 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1947 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1948 | Name.Identifier)); |
| 1949 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1950 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1951 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1952 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1953 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1954 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 1955 | |
| 1956 | case UnqualifiedId::IK_LiteralOperatorId: |
| 1957 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 1958 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1959 | default: |
| 1960 | break; |
| 1961 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1962 | |
| 1963 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1964 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1965 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1966 | << Name.getSourceRange() |
| 1967 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1968 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1971 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1972 | const TemplateArgumentLoc &AL, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1973 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1974 | const TemplateArgument &Arg = AL.getArgument(); |
| 1975 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1976 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1977 | switch(Arg.getKind()) { |
| 1978 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1979 | // C++ [temp.arg.type]p1: |
| 1980 | // A template-argument for a template-parameter which is a |
| 1981 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1982 | break; |
| 1983 | case TemplateArgument::Template: { |
| 1984 | // We have a template type parameter but the template argument |
| 1985 | // is a template without any arguments. |
| 1986 | SourceRange SR = AL.getSourceRange(); |
| 1987 | TemplateName Name = Arg.getAsTemplate(); |
| 1988 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 1989 | << Name << SR; |
| 1990 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 1991 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1992 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1993 | return true; |
| 1994 | } |
| 1995 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1996 | // We have a template type parameter but the template argument |
| 1997 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 1998 | SourceRange SR = AL.getSourceRange(); |
| 1999 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2000 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2001 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2002 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2003 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2004 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2005 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2006 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2007 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2008 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2009 | // Add the converted template type argument. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2010 | Converted.push_back( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2011 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2012 | return false; |
| 2013 | } |
| 2014 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2015 | /// \brief Substitute template arguments into the default template argument for |
| 2016 | /// the given template type parameter. |
| 2017 | /// |
| 2018 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2019 | /// the substitution. |
| 2020 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2021 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2022 | /// for. |
| 2023 | /// |
| 2024 | /// \param TemplateLoc the location of the template name that started the |
| 2025 | /// template-id we are checking. |
| 2026 | /// |
| 2027 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2028 | /// terminates the template-id. |
| 2029 | /// |
| 2030 | /// \param Param the template template parameter whose default we are |
| 2031 | /// substituting into. |
| 2032 | /// |
| 2033 | /// \param Converted the list of template arguments provided for template |
| 2034 | /// parameters that precede \p Param in the template parameter list. |
| 2035 | /// |
| 2036 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2037 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2038 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2039 | TemplateDecl *Template, |
| 2040 | SourceLocation TemplateLoc, |
| 2041 | SourceLocation RAngleLoc, |
| 2042 | TemplateTypeParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2043 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2044 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2045 | |
| 2046 | // If the argument type is dependent, instantiate it now based |
| 2047 | // on the previously-computed template arguments. |
| 2048 | if (ArgType->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2049 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2050 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2051 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2052 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2053 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2054 | |
| 2055 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2056 | Template, Converted.data(), |
| 2057 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2058 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2059 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2060 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2061 | Param->getDefaultArgumentLoc(), |
| 2062 | Param->getDeclName()); |
| 2063 | } |
| 2064 | |
| 2065 | return ArgType; |
| 2066 | } |
| 2067 | |
| 2068 | /// \brief Substitute template arguments into the default template argument for |
| 2069 | /// the given non-type template parameter. |
| 2070 | /// |
| 2071 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2072 | /// the substitution. |
| 2073 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2074 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2075 | /// for. |
| 2076 | /// |
| 2077 | /// \param TemplateLoc the location of the template name that started the |
| 2078 | /// template-id we are checking. |
| 2079 | /// |
| 2080 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2081 | /// terminates the template-id. |
| 2082 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2083 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2084 | /// substituting into. |
| 2085 | /// |
| 2086 | /// \param Converted the list of template arguments provided for template |
| 2087 | /// parameters that precede \p Param in the template parameter list. |
| 2088 | /// |
| 2089 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2090 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2091 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2092 | TemplateDecl *Template, |
| 2093 | SourceLocation TemplateLoc, |
| 2094 | SourceLocation RAngleLoc, |
| 2095 | NonTypeTemplateParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2096 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2097 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2098 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2099 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2100 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2101 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2102 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2103 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2104 | Template, Converted.data(), |
| 2105 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2106 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2107 | |
| 2108 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2109 | } |
| 2110 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2111 | /// \brief Substitute template arguments into the default template argument for |
| 2112 | /// the given template template parameter. |
| 2113 | /// |
| 2114 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2115 | /// the substitution. |
| 2116 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2117 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2118 | /// for. |
| 2119 | /// |
| 2120 | /// \param TemplateLoc the location of the template name that started the |
| 2121 | /// template-id we are checking. |
| 2122 | /// |
| 2123 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2124 | /// terminates the template-id. |
| 2125 | /// |
| 2126 | /// \param Param the template template parameter whose default we are |
| 2127 | /// substituting into. |
| 2128 | /// |
| 2129 | /// \param Converted the list of template arguments provided for template |
| 2130 | /// parameters that precede \p Param in the template parameter list. |
| 2131 | /// |
| 2132 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2133 | static TemplateName |
| 2134 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2135 | TemplateDecl *Template, |
| 2136 | SourceLocation TemplateLoc, |
| 2137 | SourceLocation RAngleLoc, |
| 2138 | TemplateTemplateParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2139 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2140 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2141 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2142 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2143 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2144 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2145 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2146 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2147 | Template, Converted.data(), |
| 2148 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2149 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2150 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2151 | return SemaRef.SubstTemplateName( |
| 2152 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2153 | Param->getDefaultArgument().getTemplateNameLoc(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2154 | AllTemplateArgs); |
| 2155 | } |
| 2156 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2157 | /// \brief If the given template parameter has a default template |
| 2158 | /// argument, substitute into that default template argument and |
| 2159 | /// return the corresponding template argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2160 | TemplateArgumentLoc |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2161 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2162 | SourceLocation TemplateLoc, |
| 2163 | SourceLocation RAngleLoc, |
| 2164 | Decl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2165 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 2166 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2167 | if (!TypeParm->hasDefaultArgument()) |
| 2168 | return TemplateArgumentLoc(); |
| 2169 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2170 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2171 | TemplateLoc, |
| 2172 | RAngleLoc, |
| 2173 | TypeParm, |
| 2174 | Converted); |
| 2175 | if (DI) |
| 2176 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2177 | |
| 2178 | return TemplateArgumentLoc(); |
| 2179 | } |
| 2180 | |
| 2181 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2182 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2183 | if (!NonTypeParm->hasDefaultArgument()) |
| 2184 | return TemplateArgumentLoc(); |
| 2185 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2186 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2187 | TemplateLoc, |
| 2188 | RAngleLoc, |
| 2189 | NonTypeParm, |
| 2190 | Converted); |
| 2191 | if (Arg.isInvalid()) |
| 2192 | return TemplateArgumentLoc(); |
| 2193 | |
| 2194 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2195 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2196 | } |
| 2197 | |
| 2198 | TemplateTemplateParmDecl *TempTempParm |
| 2199 | = cast<TemplateTemplateParmDecl>(Param); |
| 2200 | if (!TempTempParm->hasDefaultArgument()) |
| 2201 | return TemplateArgumentLoc(); |
| 2202 | |
| 2203 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2204 | TemplateLoc, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2205 | RAngleLoc, |
| 2206 | TempTempParm, |
| 2207 | Converted); |
| 2208 | if (TName.isNull()) |
| 2209 | return TemplateArgumentLoc(); |
| 2210 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2211 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2212 | TempTempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2213 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2214 | } |
| 2215 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2216 | /// \brief Check that the given template argument corresponds to the given |
| 2217 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2218 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2219 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2220 | /// checked. |
| 2221 | /// |
| 2222 | /// \param Arg The template argument. |
| 2223 | /// |
| 2224 | /// \param Template The template in which the template argument resides. |
| 2225 | /// |
| 2226 | /// \param TemplateLoc The location of the template name for the template |
| 2227 | /// whose argument list we're matching. |
| 2228 | /// |
| 2229 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2230 | /// the template argument list. |
| 2231 | /// |
| 2232 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2233 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2234 | /// |
| 2235 | /// \param Converted The checked, converted argument will be added to the |
| 2236 | /// end of this small vector. |
| 2237 | /// |
| 2238 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2239 | /// explicitly written, deduced, etc. |
| 2240 | /// |
| 2241 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2242 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2243 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2244 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2245 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2246 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2247 | unsigned ArgumentPackIndex, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2248 | llvm::SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2249 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2250 | // Check template type parameters. |
| 2251 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2252 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2253 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2254 | // Check non-type template parameters. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2255 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2256 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2257 | // with the template arguments we've seen thus far. But if the |
| 2258 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2259 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2260 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2261 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2262 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2263 | if (NTTPType->isDependentType() && |
| 2264 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2265 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2266 | // Do substitution on the type of the non-type template parameter. |
| 2267 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2268 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2269 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2270 | |
| 2271 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2272 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2273 | NTTPType = SubstType(NTTPType, |
| 2274 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2275 | NTTP->getLocation(), |
| 2276 | NTTP->getDeclName()); |
| 2277 | // If that worked, check the non-type template parameter type |
| 2278 | // for validity. |
| 2279 | if (!NTTPType.isNull()) |
| 2280 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2281 | NTTP->getLocation()); |
| 2282 | if (NTTPType.isNull()) |
| 2283 | return true; |
| 2284 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2285 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2286 | switch (Arg.getArgument().getKind()) { |
| 2287 | case TemplateArgument::Null: |
| 2288 | assert(false && "Should never see a NULL template argument here"); |
| 2289 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2290 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2291 | case TemplateArgument::Expression: { |
| 2292 | Expr *E = Arg.getArgument().getAsExpr(); |
| 2293 | TemplateArgument Result; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2294 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2295 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2296 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2297 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2298 | break; |
| 2299 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2300 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2301 | case TemplateArgument::Declaration: |
| 2302 | case TemplateArgument::Integral: |
| 2303 | // We've already checked this template argument, so just copy |
| 2304 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2305 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2306 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2307 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2308 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2309 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2310 | // We were given a template template argument. It may not be ill-formed; |
| 2311 | // see below. |
| 2312 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2313 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2314 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2315 | // We have a template argument such as \c T::template X, which we |
| 2316 | // parsed as a template template argument. However, since we now |
| 2317 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2318 | // template name into an expression. |
| 2319 | |
| 2320 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2321 | Arg.getTemplateNameLoc()); |
| 2322 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2323 | Expr *E = DependentScopeDeclRefExpr::Create(Context, |
| 2324 | DTN->getQualifier(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2325 | Arg.getTemplateQualifierRange(), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2326 | NameInfo); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2327 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2328 | // If we parsed the template argument as a pack expansion, create a |
| 2329 | // pack expansion expression. |
| 2330 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2331 | ExprResult Expansion = ActOnPackExpansion(E, |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2332 | Arg.getTemplateEllipsisLoc()); |
| 2333 | if (Expansion.isInvalid()) |
| 2334 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2335 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2336 | E = Expansion.get(); |
| 2337 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2338 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2339 | TemplateArgument Result; |
| 2340 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
| 2341 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2342 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2343 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2344 | break; |
| 2345 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2346 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2347 | // We have a template argument that actually does refer to a class |
| 2348 | // template, template alias, or template template parameter, and |
| 2349 | // therefore cannot be a non-type template argument. |
| 2350 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2351 | << Arg.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2352 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2353 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2354 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2355 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2356 | case TemplateArgument::Type: { |
| 2357 | // We have a non-type template parameter but the template |
| 2358 | // argument is a type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2359 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2360 | // C++ [temp.arg]p2: |
| 2361 | // In a template-argument, an ambiguity between a type-id and |
| 2362 | // an expression is resolved to a type-id, regardless of the |
| 2363 | // form of the corresponding template-parameter. |
| 2364 | // |
| 2365 | // We warn specifically about this case, since it can be rather |
| 2366 | // confusing for users. |
| 2367 | QualType T = Arg.getArgument().getAsType(); |
| 2368 | SourceRange SR = Arg.getSourceRange(); |
| 2369 | if (T->isFunctionType()) |
| 2370 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2371 | else |
| 2372 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2373 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2374 | return true; |
| 2375 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2376 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2377 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2378 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2379 | break; |
| 2380 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2381 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2382 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2386 | // Check template template parameters. |
| 2387 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2388 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2389 | // Substitute into the template parameter list of the template |
| 2390 | // template parameter, since previously-supplied template arguments |
| 2391 | // may appear within the template template parameter. |
| 2392 | { |
| 2393 | // Set up a template instantiation context. |
| 2394 | LocalInstantiationScope Scope(*this); |
| 2395 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2396 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2397 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2398 | |
| 2399 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2400 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2401 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2402 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2403 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2404 | if (!TempParm) |
| 2405 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2406 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2407 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2408 | switch (Arg.getArgument().getKind()) { |
| 2409 | case TemplateArgument::Null: |
| 2410 | assert(false && "Should never see a NULL template argument here"); |
| 2411 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2412 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2413 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2414 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2415 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2416 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2417 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2418 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2419 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2420 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2421 | case TemplateArgument::Expression: |
| 2422 | case TemplateArgument::Type: |
| 2423 | // We have a template template parameter but the template |
| 2424 | // argument does not refer to a template. |
| 2425 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 2426 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2427 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2428 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2429 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2430 | "Declaration argument with template template parameter"); |
| 2431 | break; |
| 2432 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2433 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2434 | "Integral argument with template template parameter"); |
| 2435 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2436 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2437 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2438 | llvm_unreachable("Caller must expand template argument packs"); |
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 | return false; |
| 2443 | } |
| 2444 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2445 | /// \brief Check that the given template argument list is well-formed |
| 2446 | /// for specializing the given template. |
| 2447 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2448 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2449 | const TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2450 | bool PartialTemplateArgs, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2451 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2452 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2453 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2454 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2455 | bool Invalid = false; |
| 2456 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2457 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2458 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2459 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2460 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2461 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2462 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2463 | (NumArgs < Params->getMinRequiredArguments() && |
| 2464 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2465 | // FIXME: point at either the first arg beyond what we can handle, |
| 2466 | // or the '>', depending on whether we have too many or too few |
| 2467 | // arguments. |
| 2468 | SourceRange Range; |
| 2469 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2470 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2471 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2472 | << (NumArgs > NumParams) |
| 2473 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2474 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2475 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2476 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2477 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2478 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2479 | Invalid = true; |
| 2480 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2481 | |
| 2482 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2483 | // [...] The type and form of each template-argument specified in |
| 2484 | // a template-id shall match the type and form specified for the |
| 2485 | // corresponding parameter declared by the template in its |
| 2486 | // template-parameter-list. |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2487 | llvm::SmallVector<TemplateArgument, 2> ArgumentPack; |
| 2488 | TemplateParameterList::iterator Param = Params->begin(), |
| 2489 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2490 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 2491 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2492 | while (Param != ParamEnd) { |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2493 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 2494 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2495 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2496 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2497 | // If we have an expanded parameter pack, make sure we don't have too |
| 2498 | // many arguments. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2499 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2500 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2501 | if (NTTP->isExpandedParameterPack() && |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2502 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 2503 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2504 | << true |
| 2505 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2506 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2507 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2508 | << Template; |
| 2509 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2510 | << Params->getSourceRange(); |
| 2511 | return true; |
| 2512 | } |
| 2513 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2514 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2515 | // Check the template argument we were given. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2516 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2517 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2518 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2519 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2520 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2521 | if ((*Param)->isTemplateParameterPack()) { |
| 2522 | // The template parameter was a template parameter pack, so take the |
| 2523 | // deduced argument and place it on the argument pack. Note that we |
| 2524 | // stay on the same template parameter so that we can deduce more |
| 2525 | // arguments. |
| 2526 | ArgumentPack.push_back(Converted.back()); |
| 2527 | Converted.pop_back(); |
| 2528 | } else { |
| 2529 | // Move to the next template parameter. |
| 2530 | ++Param; |
| 2531 | } |
| 2532 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2533 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2534 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2535 | |
| 2536 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2537 | // arguments, just break out now and we'll fill in the argument pack below. |
| 2538 | if ((*Param)->isTemplateParameterPack()) |
| 2539 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2540 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2541 | // We have a default template argument that we will use. |
| 2542 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2543 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2544 | // Retrieve the default template argument from the template |
| 2545 | // parameter. For each kind of template parameter, we substitute the |
| 2546 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2547 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2548 | // the default argument. |
| 2549 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2550 | if (!TTP->hasDefaultArgument()) { |
| 2551 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2552 | break; |
| 2553 | } |
| 2554 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2555 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2556 | Template, |
| 2557 | TemplateLoc, |
| 2558 | RAngleLoc, |
| 2559 | TTP, |
| 2560 | Converted); |
| 2561 | if (!ArgType) |
| 2562 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2563 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2564 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2565 | ArgType); |
| 2566 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2567 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2568 | if (!NTTP->hasDefaultArgument()) { |
| 2569 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2570 | break; |
| 2571 | } |
| 2572 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2573 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2574 | TemplateLoc, |
| 2575 | RAngleLoc, |
| 2576 | NTTP, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2577 | Converted); |
| 2578 | if (E.isInvalid()) |
| 2579 | return true; |
| 2580 | |
| 2581 | Expr *Ex = E.takeAs<Expr>(); |
| 2582 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2583 | } else { |
| 2584 | TemplateTemplateParmDecl *TempParm |
| 2585 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2586 | |
| 2587 | if (!TempParm->hasDefaultArgument()) { |
| 2588 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2589 | break; |
| 2590 | } |
| 2591 | |
| 2592 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2593 | TemplateLoc, |
| 2594 | RAngleLoc, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2595 | TempParm, |
| 2596 | Converted); |
| 2597 | if (Name.isNull()) |
| 2598 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2599 | |
| 2600 | Arg = TemplateArgumentLoc(TemplateArgument(Name), |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2601 | TempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2602 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2603 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2604 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2605 | // Introduce an instantiation record that describes where we are using |
| 2606 | // the default template argument. |
| 2607 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2608 | Converted.data(), Converted.size(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2609 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2610 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2611 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2612 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2613 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2614 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2615 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2616 | // Move to the next template parameter and argument. |
| 2617 | ++Param; |
| 2618 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2619 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2620 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2621 | // Form argument packs for each of the parameter packs remaining. |
| 2622 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2623 | // If we're checking a partial list of template arguments, don't fill |
| 2624 | // in arguments for non-template parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2625 | |
| 2626 | if ((*Param)->isTemplateParameterPack()) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2627 | if (PartialTemplateArgs && ArgumentPack.empty()) { |
| 2628 | Converted.push_back(TemplateArgument()); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2629 | } else if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2630 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2631 | else { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2632 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 2633 | ArgumentPack.data(), |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2634 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2635 | ArgumentPack.clear(); |
| 2636 | } |
| 2637 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2638 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2639 | ++Param; |
| 2640 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2641 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2642 | return Invalid; |
| 2643 | } |
| 2644 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2645 | namespace { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2646 | class UnnamedLocalNoLinkageFinder |
| 2647 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2648 | { |
| 2649 | Sema &S; |
| 2650 | SourceRange SR; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2651 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2652 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2653 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2654 | public: |
| 2655 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 2656 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2657 | bool Visit(QualType T) { |
| 2658 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2659 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2660 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2661 | #define TYPE(Class, Parent) \ |
| 2662 | bool Visit##Class##Type(const Class##Type *); |
| 2663 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 2664 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2665 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 2666 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2667 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2668 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2669 | bool VisitTagDecl(const TagDecl *Tag); |
| 2670 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 2671 | }; |
| 2672 | } |
| 2673 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2674 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2675 | return false; |
| 2676 | } |
| 2677 | |
| 2678 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 2679 | return Visit(T->getElementType()); |
| 2680 | } |
| 2681 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2682 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2683 | return Visit(T->getPointeeType()); |
| 2684 | } |
| 2685 | |
| 2686 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2687 | const BlockPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2688 | return Visit(T->getPointeeType()); |
| 2689 | } |
| 2690 | |
| 2691 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2692 | const LValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2693 | return Visit(T->getPointeeType()); |
| 2694 | } |
| 2695 | |
| 2696 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2697 | const RValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2698 | return Visit(T->getPointeeType()); |
| 2699 | } |
| 2700 | |
| 2701 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2702 | const MemberPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2703 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 2704 | } |
| 2705 | |
| 2706 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2707 | const ConstantArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2708 | return Visit(T->getElementType()); |
| 2709 | } |
| 2710 | |
| 2711 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2712 | const IncompleteArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2713 | return Visit(T->getElementType()); |
| 2714 | } |
| 2715 | |
| 2716 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2717 | const VariableArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2718 | return Visit(T->getElementType()); |
| 2719 | } |
| 2720 | |
| 2721 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2722 | const DependentSizedArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2723 | return Visit(T->getElementType()); |
| 2724 | } |
| 2725 | |
| 2726 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2727 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2728 | return Visit(T->getElementType()); |
| 2729 | } |
| 2730 | |
| 2731 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 2732 | return Visit(T->getElementType()); |
| 2733 | } |
| 2734 | |
| 2735 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 2736 | return Visit(T->getElementType()); |
| 2737 | } |
| 2738 | |
| 2739 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 2740 | const FunctionProtoType* T) { |
| 2741 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2742 | AEnd = T->arg_type_end(); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2743 | A != AEnd; ++A) { |
| 2744 | if (Visit(*A)) |
| 2745 | return true; |
| 2746 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2747 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2748 | return Visit(T->getResultType()); |
| 2749 | } |
| 2750 | |
| 2751 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 2752 | const FunctionNoProtoType* T) { |
| 2753 | return Visit(T->getResultType()); |
| 2754 | } |
| 2755 | |
| 2756 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 2757 | const UnresolvedUsingType*) { |
| 2758 | return false; |
| 2759 | } |
| 2760 | |
| 2761 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 2762 | return false; |
| 2763 | } |
| 2764 | |
| 2765 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 2766 | return Visit(T->getUnderlyingType()); |
| 2767 | } |
| 2768 | |
| 2769 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 2770 | return false; |
| 2771 | } |
| 2772 | |
| 2773 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 2774 | return VisitTagDecl(T->getDecl()); |
| 2775 | } |
| 2776 | |
| 2777 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 2778 | return VisitTagDecl(T->getDecl()); |
| 2779 | } |
| 2780 | |
| 2781 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 2782 | const TemplateTypeParmType*) { |
| 2783 | return false; |
| 2784 | } |
| 2785 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2786 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 2787 | const SubstTemplateTypeParmPackType *) { |
| 2788 | return false; |
| 2789 | } |
| 2790 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2791 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 2792 | const TemplateSpecializationType*) { |
| 2793 | return false; |
| 2794 | } |
| 2795 | |
| 2796 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 2797 | const InjectedClassNameType* T) { |
| 2798 | return VisitTagDecl(T->getDecl()); |
| 2799 | } |
| 2800 | |
| 2801 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 2802 | const DependentNameType* T) { |
| 2803 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 2804 | } |
| 2805 | |
| 2806 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 2807 | const DependentTemplateSpecializationType* T) { |
| 2808 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 2809 | } |
| 2810 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2811 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 2812 | const PackExpansionType* T) { |
| 2813 | return Visit(T->getPattern()); |
| 2814 | } |
| 2815 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2816 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 2817 | return false; |
| 2818 | } |
| 2819 | |
| 2820 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 2821 | const ObjCInterfaceType *) { |
| 2822 | return false; |
| 2823 | } |
| 2824 | |
| 2825 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 2826 | const ObjCObjectPointerType *) { |
| 2827 | return false; |
| 2828 | } |
| 2829 | |
| 2830 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 2831 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
| 2832 | S.Diag(SR.getBegin(), diag::ext_template_arg_local_type) |
| 2833 | << S.Context.getTypeDeclType(Tag) << SR; |
| 2834 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2837 | if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) { |
| 2838 | S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR; |
| 2839 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 2840 | return true; |
| 2841 | } |
| 2842 | |
| 2843 | return false; |
| 2844 | } |
| 2845 | |
| 2846 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 2847 | NestedNameSpecifier *NNS) { |
| 2848 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 2849 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2850 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2851 | switch (NNS->getKind()) { |
| 2852 | case NestedNameSpecifier::Identifier: |
| 2853 | case NestedNameSpecifier::Namespace: |
| 2854 | case NestedNameSpecifier::Global: |
| 2855 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2856 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2857 | case NestedNameSpecifier::TypeSpec: |
| 2858 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 2859 | return Visit(QualType(NNS->getAsType(), 0)); |
| 2860 | } |
Fariborz Jahanian | 7b1ec6c | 2010-10-13 16:19:16 +0000 | [diff] [blame] | 2861 | return false; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
| 2864 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2865 | /// \brief Check a template argument against its corresponding |
| 2866 | /// template type parameter. |
| 2867 | /// |
| 2868 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 2869 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2870 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2871 | TypeSourceInfo *ArgInfo) { |
| 2872 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2873 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 2874 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 2875 | |
| 2876 | if (Arg->isVariablyModifiedType()) { |
| 2877 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2878 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2879 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2882 | // C++03 [temp.arg.type]p2: |
| 2883 | // A local type, a type with no linkage, an unnamed type or a type |
| 2884 | // compounded from any of these types shall not be used as a |
| 2885 | // template-argument for a template type-parameter. |
| 2886 | // |
| 2887 | // C++0x allows these, and even in C++03 we allow them as an extension with |
| 2888 | // a warning. |
Douglas Gregor | db4d4bb | 2010-10-13 18:05:20 +0000 | [diff] [blame] | 2889 | if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2890 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 2891 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 2892 | } |
| 2893 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2894 | return false; |
| 2895 | } |
| 2896 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2897 | /// \brief Checks whether the given template argument is the address |
| 2898 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2899 | static bool |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2900 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 2901 | NonTypeTemplateParmDecl *Param, |
| 2902 | QualType ParamType, |
| 2903 | Expr *ArgIn, |
| 2904 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2905 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2906 | Expr *Arg = ArgIn; |
| 2907 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2908 | |
| 2909 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2910 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2911 | Arg = Cast->getSubExpr(); |
| 2912 | |
| 2913 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2914 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2915 | // A template-argument for a non-type, non-template |
| 2916 | // template-parameter shall be one of: [...] |
| 2917 | // |
| 2918 | // -- the address of an object or function with external |
| 2919 | // linkage, including function templates and function |
| 2920 | // template-ids but excluding non-static class members, |
| 2921 | // expressed as & id-expression where the & is optional if |
| 2922 | // the name refers to a function or array, or if the |
| 2923 | // corresponding template-parameter is a reference; or |
| 2924 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2925 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2926 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 2927 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 2928 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2929 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2930 | if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2931 | S.Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2932 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2933 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2934 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | Arg = Parens->getSubExpr(); |
| 2938 | } |
| 2939 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2940 | bool AddressTaken = false; |
| 2941 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2942 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2943 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2944 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2945 | AddressTaken = true; |
| 2946 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 2947 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2948 | } else |
| 2949 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 2950 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2951 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2952 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 2953 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2954 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2955 | return true; |
| 2956 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2957 | |
| 2958 | // Stop checking the precise nature of the argument if it is value dependent, |
| 2959 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2960 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2961 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2962 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2963 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2964 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2965 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 2966 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2967 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2968 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2969 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2970 | return true; |
| 2971 | } |
| 2972 | |
| 2973 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2974 | |
| 2975 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2976 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 2977 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2978 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2979 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2980 | return true; |
| 2981 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2982 | |
| 2983 | // Cannot refer to non-static member functions |
| 2984 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2985 | if (!Method->isStatic()) { |
| 2986 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2987 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2988 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2989 | return true; |
| 2990 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2991 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2992 | // Functions must have external linkage. |
| 2993 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2994 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2995 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2996 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2997 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2998 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2999 | << true; |
| 3000 | return true; |
| 3001 | } |
| 3002 | |
| 3003 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3004 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3005 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3006 | // If the template parameter has pointer type, the function decays. |
| 3007 | if (ParamType->isPointerType() && !AddressTaken) |
| 3008 | ArgType = S.Context.getPointerType(Func->getType()); |
| 3009 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 3010 | // If we originally had an address-of operator, but the |
| 3011 | // parameter has reference type, complain and (if things look |
| 3012 | // like they will work) drop the address-of operator. |
| 3013 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 3014 | ParamType.getNonReferenceType())) { |
| 3015 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3016 | << ParamType; |
| 3017 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3018 | return true; |
| 3019 | } |
| 3020 | |
| 3021 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3022 | << ParamType |
| 3023 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3024 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3025 | |
| 3026 | ArgType = Func->getType(); |
| 3027 | } |
| 3028 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3029 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3030 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3031 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3032 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3033 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3034 | << true; |
| 3035 | return true; |
| 3036 | } |
| 3037 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3038 | // A value of reference type is not an object. |
| 3039 | if (Var->getType()->isReferenceType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3040 | S.Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3041 | diag::err_template_arg_reference_var) |
| 3042 | << Var->getType() << Arg->getSourceRange(); |
| 3043 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3044 | return true; |
| 3045 | } |
| 3046 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3047 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3048 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3049 | |
| 3050 | // If the template parameter has pointer type, we must have taken |
| 3051 | // the address of this object. |
| 3052 | if (ParamType->isReferenceType()) { |
| 3053 | if (AddressTaken) { |
| 3054 | // If we originally had an address-of operator, but the |
| 3055 | // parameter has reference type, complain and (if things look |
| 3056 | // like they will work) drop the address-of operator. |
| 3057 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3058 | ParamType.getNonReferenceType())) { |
| 3059 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3060 | << ParamType; |
| 3061 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3062 | return true; |
| 3063 | } |
| 3064 | |
| 3065 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3066 | << ParamType |
| 3067 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3068 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3069 | |
| 3070 | ArgType = Var->getType(); |
| 3071 | } |
| 3072 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3073 | if (Var->getType()->isArrayType()) { |
| 3074 | // Array-to-pointer decay. |
| 3075 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3076 | } else { |
| 3077 | // If the template parameter has pointer type but the address of |
| 3078 | // this object was not taken, complain and (possibly) recover by |
| 3079 | // taking the address of the entity. |
| 3080 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3081 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3082 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3083 | << ParamType; |
| 3084 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3085 | return true; |
| 3086 | } |
| 3087 | |
| 3088 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3089 | << ParamType |
| 3090 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3091 | |
| 3092 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3093 | } |
| 3094 | } |
| 3095 | } else { |
| 3096 | // We found something else, but we don't know specifically what it is. |
| 3097 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3098 | diag::err_template_arg_not_object_or_func) |
| 3099 | << Arg->getSourceRange(); |
| 3100 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3101 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3102 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3103 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3104 | if (ParamType->isPointerType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3105 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3106 | S.IsQualificationConversion(ArgType, ParamType, false)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3107 | // For pointer-to-object types, qualification conversions are |
| 3108 | // permitted. |
| 3109 | } else { |
| 3110 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3111 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3112 | // C++ [temp.arg.nontype]p5b3: |
| 3113 | // For a non-type template-parameter of type reference to |
| 3114 | // object, no conversions apply. The type referred to by the |
| 3115 | // reference may be more cv-qualified than the (otherwise |
| 3116 | // identical) type of the template- argument. The |
| 3117 | // template-parameter is bound directly to the |
| 3118 | // template-argument, which shall be an lvalue. |
| 3119 | |
| 3120 | // FIXME: Other qualifiers? |
| 3121 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3122 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3123 | |
| 3124 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 3125 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3126 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3127 | << ParamType << Arg->getType() |
| 3128 | << Arg->getSourceRange(); |
| 3129 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3130 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3131 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3132 | } |
| 3133 | } |
| 3134 | |
| 3135 | // At this point, the template argument refers to an object or |
| 3136 | // function with external linkage. We now need to check whether the |
| 3137 | // argument and parameter types are compatible. |
| 3138 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3139 | ParamType.getNonReferenceType())) { |
| 3140 | // We can't perform this conversion or binding. |
| 3141 | if (ParamType->isReferenceType()) |
| 3142 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 3143 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 3144 | else |
| 3145 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 3146 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 3147 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3148 | return true; |
| 3149 | } |
| 3150 | } |
| 3151 | |
| 3152 | // Create the template argument. |
| 3153 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 3154 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3155 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
| 3158 | /// \brief Checks whether the given template argument is a pointer to |
| 3159 | /// member constant according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3160 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3161 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3162 | bool Invalid = false; |
| 3163 | |
| 3164 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3165 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3166 | Arg = Cast->getSubExpr(); |
| 3167 | |
| 3168 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3169 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3170 | // A template-argument for a non-type, non-template |
| 3171 | // template-parameter shall be one of: [...] |
| 3172 | // |
| 3173 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3174 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3175 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3176 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3177 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3178 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3179 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3180 | if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3181 | Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3182 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3183 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3184 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | Arg = Parens->getSubExpr(); |
| 3188 | } |
| 3189 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3190 | // A pointer-to-member constant written &Class::member. |
| 3191 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3192 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3193 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3194 | if (DRE && !DRE->getQualifier()) |
| 3195 | DRE = 0; |
| 3196 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3197 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3198 | // A constant of pointer-to-member type. |
| 3199 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3200 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3201 | if (VD->getType()->isMemberPointerType()) { |
| 3202 | if (isa<NonTypeTemplateParmDecl>(VD) || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3203 | (isa<VarDecl>(VD) && |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3204 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3205 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3206 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3207 | else |
| 3208 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3209 | return Invalid; |
| 3210 | } |
| 3211 | } |
| 3212 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3213 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3214 | DRE = 0; |
| 3215 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3216 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3217 | if (!DRE) |
| 3218 | return Diag(Arg->getSourceRange().getBegin(), |
| 3219 | diag::err_template_arg_not_pointer_to_member_form) |
| 3220 | << Arg->getSourceRange(); |
| 3221 | |
| 3222 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3223 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3224 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3225 | "Only non-static member pointers can make it here"); |
| 3226 | |
| 3227 | // Okay: this is the address of a non-static member, and therefore |
| 3228 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3229 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3230 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3231 | else |
| 3232 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3233 | return Invalid; |
| 3234 | } |
| 3235 | |
| 3236 | // 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] | 3237 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3238 | diag::err_template_arg_not_pointer_to_member_form) |
| 3239 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3240 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3241 | diag::note_template_arg_refers_here); |
| 3242 | return true; |
| 3243 | } |
| 3244 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3245 | /// \brief Check a template argument against its corresponding |
| 3246 | /// non-type template parameter. |
| 3247 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3248 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
| 3249 | /// It returns true if an error occurred, and false otherwise. \p |
| 3250 | /// InstantiatedParamType is the type of the non-type template |
| 3251 | /// parameter after it has been instantiated. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3252 | /// |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3253 | /// If no error was detected, Converted receives the converted template argument. |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3254 | bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3255 | QualType InstantiatedParamType, Expr *&Arg, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3256 | TemplateArgument &Converted, |
| 3257 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3258 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3259 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3260 | // If either the parameter has a dependent type or the argument is |
| 3261 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3262 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3263 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3264 | Converted = TemplateArgument(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3265 | return false; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3266 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3267 | |
| 3268 | // C++ [temp.arg.nontype]p5: |
| 3269 | // The following conversions are performed on each expression used |
| 3270 | // as a non-type template-argument. If a non-type |
| 3271 | // template-argument cannot be converted to the type of the |
| 3272 | // corresponding template-parameter then the program is |
| 3273 | // ill-formed. |
| 3274 | // |
| 3275 | // -- for a non-type template-parameter of integral or |
| 3276 | // enumeration type, integral promotions (4.5) and integral |
| 3277 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3278 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3279 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3280 | if (ParamType->isIntegralOrEnumerationType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3281 | // C++ [temp.arg.nontype]p1: |
| 3282 | // A template-argument for a non-type, non-template |
| 3283 | // template-parameter shall be one of: |
| 3284 | // |
| 3285 | // -- an integral constant-expression of integral or enumeration |
| 3286 | // type; or |
| 3287 | // -- the name of a non-type template-parameter; or |
| 3288 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3289 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3290 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3291 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3292 | diag::err_template_arg_not_integral_or_enumeral) |
| 3293 | << ArgType << Arg->getSourceRange(); |
| 3294 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3295 | return true; |
| 3296 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3297 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3298 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 3299 | << ArgType << Arg->getSourceRange(); |
| 3300 | return true; |
| 3301 | } |
| 3302 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3303 | // From here on out, all we care about are the unqualified forms |
| 3304 | // of the parameter and argument types. |
| 3305 | ParamType = ParamType.getUnqualifiedType(); |
| 3306 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3307 | |
| 3308 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3309 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3310 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3311 | } else if (CTAK == CTAK_Deduced) { |
| 3312 | // C++ [temp.deduct.type]p17: |
| 3313 | // If, in the declaration of a function template with a non-type |
| 3314 | // template-parameter, the non-type template- parameter is used |
| 3315 | // in an expression in the function parameter-list and, if the |
| 3316 | // corresponding template-argument is deduced, the |
| 3317 | // template-argument type shall match the type of the |
| 3318 | // template-parameter exactly, except that a template-argument |
| 3319 | // deduced from an array bound may be of any integral type. |
| 3320 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3321 | << ArgType << ParamType; |
| 3322 | Diag(Param->getLocation(), diag::note_template_param_here); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3323 | return true; |
| 3324 | } else if (ParamType->isBooleanType()) { |
| 3325 | // This is an integral-to-boolean conversion. |
| 3326 | ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3327 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3328 | !ParamType->isEnumeralType()) { |
| 3329 | // This is an integral promotion or conversion. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3330 | ImpCastExprToType(Arg, ParamType, CK_IntegralCast); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3331 | } else { |
| 3332 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3333 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3334 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3335 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3336 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3337 | return true; |
| 3338 | } |
| 3339 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3340 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3341 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3342 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3343 | |
| 3344 | if (!Arg->isValueDependent()) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3345 | llvm::APSInt OldValue = Value; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3346 | |
| 3347 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3348 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3349 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3350 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3351 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3352 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3353 | |
| 3354 | // Complain if an unsigned parameter received a negative value. |
| 3355 | if (IntegerType->isUnsignedIntegerType() |
| 3356 | && (OldValue.isSigned() && OldValue.isNegative())) { |
| 3357 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3358 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3359 | << Arg->getSourceRange(); |
| 3360 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3361 | } |
| 3362 | |
| 3363 | // Complain if we overflowed the template parameter's type. |
| 3364 | unsigned RequiredBits; |
| 3365 | if (IntegerType->isUnsignedIntegerType()) |
| 3366 | RequiredBits = OldValue.getActiveBits(); |
| 3367 | else if (OldValue.isUnsigned()) |
| 3368 | RequiredBits = OldValue.getActiveBits() + 1; |
| 3369 | else |
| 3370 | RequiredBits = OldValue.getMinSignedBits(); |
| 3371 | if (RequiredBits > AllowedBits) { |
| 3372 | Diag(Arg->getSourceRange().getBegin(), |
| 3373 | diag::warn_template_arg_too_large) |
| 3374 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3375 | << Arg->getSourceRange(); |
| 3376 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3377 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3378 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3379 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3380 | // Add the value of this argument to the list of converted |
| 3381 | // arguments. We use the bitwidth and signedness of the template |
| 3382 | // parameter. |
| 3383 | if (Arg->isValueDependent()) { |
| 3384 | // The argument is value-dependent. Create a new |
| 3385 | // TemplateArgument with the converted expression. |
| 3386 | Converted = TemplateArgument(Arg); |
| 3387 | return false; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3390 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3391 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3392 | : IntegerType); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3393 | return false; |
| 3394 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3395 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3396 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 3397 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3398 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 3399 | // from a template argument of type std::nullptr_t to a non-type |
| 3400 | // template parameter of type pointer to object, pointer to |
| 3401 | // function, or pointer-to-member, respectively. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3402 | if (ArgType->isNullPtrType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3403 | (ParamType->isPointerType() || ParamType->isMemberPointerType())) { |
| 3404 | Converted = TemplateArgument((NamedDecl *)0); |
| 3405 | return false; |
| 3406 | } |
| 3407 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3408 | // Handle pointer-to-function, reference-to-function, and |
| 3409 | // pointer-to-member-function all in (roughly) the same way. |
| 3410 | if (// -- For a non-type template-parameter of type pointer to |
| 3411 | // function, only the function-to-pointer conversion (4.3) is |
| 3412 | // applied. If the template-argument represents a set of |
| 3413 | // overloaded functions (or a pointer to such), the matching |
| 3414 | // function is selected from the set (13.4). |
| 3415 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3416 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3417 | // -- For a non-type template-parameter of type reference to |
| 3418 | // function, no conversions apply. If the template-argument |
| 3419 | // represents a set of overloaded functions, the matching |
| 3420 | // function is selected from the set (13.4). |
| 3421 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3422 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3423 | // -- For a non-type template-parameter of type pointer to |
| 3424 | // member function, no conversions apply. If the |
| 3425 | // template-argument represents a set of overloaded member |
| 3426 | // functions, the matching member function is selected from |
| 3427 | // the set (13.4). |
| 3428 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3429 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3430 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3431 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3432 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3433 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3434 | true, |
| 3435 | FoundResult)) { |
| 3436 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 3437 | return true; |
| 3438 | |
| 3439 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3440 | ArgType = Arg->getType(); |
| 3441 | } else |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3442 | return true; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3443 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3444 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3445 | if (!ParamType->isMemberPointerType()) |
| 3446 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3447 | ParamType, |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3448 | Arg, Converted); |
| 3449 | |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3450 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(), |
| 3451 | false)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3452 | ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3453 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 3454 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3455 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3456 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3457 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3458 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3459 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3460 | return true; |
| 3461 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3462 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3463 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3464 | } |
| 3465 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 3466 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3467 | // -- for a non-type template-parameter of type pointer to |
| 3468 | // object, qualification conversions (4.4) and the |
| 3469 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3470 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3471 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3472 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3473 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3474 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3475 | ParamType, |
| 3476 | Arg, Converted); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3477 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3478 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3479 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3480 | // -- For a non-type template-parameter of type reference to |
| 3481 | // object, no conversions apply. The type referred to by the |
| 3482 | // reference may be more cv-qualified than the (otherwise |
| 3483 | // identical) type of the template-argument. The |
| 3484 | // template-parameter is bound directly to the |
| 3485 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3486 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3487 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3488 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3489 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3490 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 3491 | ParamRefType->getPointeeType(), |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3492 | true, |
| 3493 | FoundResult)) { |
| 3494 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 3495 | return true; |
| 3496 | |
| 3497 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3498 | ArgType = Arg->getType(); |
| 3499 | } else |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3500 | return true; |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3501 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3502 | |
| 3503 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3504 | ParamType, |
| 3505 | Arg, Converted); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3506 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3507 | |
| 3508 | // -- For a non-type template-parameter of type pointer to data |
| 3509 | // member, qualification conversions (4.4) are applied. |
| 3510 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 3511 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 3512 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3513 | // Types match exactly: nothing more to do here. |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3514 | } else if (IsQualificationConversion(ArgType, ParamType, false)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3515 | ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3516 | } else { |
| 3517 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3518 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3519 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3520 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3521 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3522 | return true; |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3523 | } |
| 3524 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3525 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | /// \brief Check a template argument against its corresponding |
| 3529 | /// template template parameter. |
| 3530 | /// |
| 3531 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 3532 | /// It returns true if an error occurred, and false otherwise. |
| 3533 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3534 | const TemplateArgumentLoc &Arg) { |
| 3535 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 3536 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 3537 | if (!Template) { |
| 3538 | // Any dependent template name is fine. |
| 3539 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 3540 | return false; |
| 3541 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3542 | |
| 3543 | // C++ [temp.arg.template]p1: |
| 3544 | // A template-argument for a template template-parameter shall be |
| 3545 | // the name of a class template, expressed as id-expression. Only |
| 3546 | // primary class templates are considered when matching the |
| 3547 | // template template argument with the corresponding parameter; |
| 3548 | // partial specializations are not considered even if their |
| 3549 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3550 | // |
| 3551 | // Note that we also allow template template parameters here, which |
| 3552 | // will happen when we are dealing with, e.g., class template |
| 3553 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3554 | if (!isa<ClassTemplateDecl>(Template) && |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3555 | !isa<TemplateTemplateParmDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3556 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3557 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3558 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3559 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3560 | << Template; |
| 3561 | } |
| 3562 | |
| 3563 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 3564 | Param->getTemplateParameters(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3565 | true, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3566 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3567 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3570 | /// \brief Given a non-type template argument that refers to a |
| 3571 | /// declaration and the type of its corresponding non-type template |
| 3572 | /// parameter, produce an expression that properly refers to that |
| 3573 | /// declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3574 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3575 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 3576 | QualType ParamType, |
| 3577 | SourceLocation Loc) { |
| 3578 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 3579 | "Only declaration template arguments permitted here"); |
| 3580 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 3581 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3582 | if (VD->getDeclContext()->isRecord() && |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3583 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 3584 | // If the value is a class member, we might have a pointer-to-member. |
| 3585 | // Determine whether the non-type template template parameter is of |
| 3586 | // pointer-to-member type. If so, we need to build an appropriate |
| 3587 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 3588 | // would refer to the member itself. |
| 3589 | if (ParamType->isMemberPointerType()) { |
| 3590 | QualType ClassType |
| 3591 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 3592 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3593 | = NestedNameSpecifier::Create(Context, 0, false, |
| 3594 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3595 | CXXScopeSpec SS; |
| 3596 | SS.setScopeRep(Qualifier); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3597 | |
| 3598 | // The actual value-ness of this is unimportant, but for |
| 3599 | // internal consistency's sake, references to instance methods |
| 3600 | // are r-values. |
| 3601 | ExprValueKind VK = VK_LValue; |
| 3602 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 3603 | VK = VK_RValue; |
| 3604 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3605 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3606 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3607 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3608 | Loc, |
| 3609 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3610 | if (RefExpr.isInvalid()) |
| 3611 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3612 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3613 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3614 | |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3615 | // We might need to perform a trailing qualification conversion, since |
| 3616 | // the element type on the parameter could be more qualified than the |
| 3617 | // element type in the expression we constructed. |
| 3618 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3619 | ParamType.getUnqualifiedType(), false)) { |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3620 | Expr *RefE = RefExpr.takeAs<Expr>(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3621 | ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp); |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3622 | RefExpr = Owned(RefE); |
| 3623 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3624 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3625 | assert(!RefExpr.isInvalid() && |
| 3626 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3627 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3628 | return move(RefExpr); |
| 3629 | } |
| 3630 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3631 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3632 | QualType T = VD->getType().getNonReferenceType(); |
| 3633 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3634 | // When the non-type template parameter is a pointer, take the |
| 3635 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3636 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3637 | if (RefExpr.isInvalid()) |
| 3638 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3639 | |
| 3640 | if (T->isFunctionType() || T->isArrayType()) { |
| 3641 | // Decay functions and arrays. |
| 3642 | Expr *RefE = (Expr *)RefExpr.get(); |
| 3643 | DefaultFunctionArrayConversion(RefE); |
| 3644 | if (RefE != RefExpr.get()) { |
| 3645 | RefExpr.release(); |
| 3646 | RefExpr = Owned(RefE); |
| 3647 | } |
| 3648 | |
| 3649 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3650 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3651 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3652 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3653 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3654 | } |
| 3655 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3656 | ExprValueKind VK = VK_RValue; |
| 3657 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3658 | // If the non-type template parameter has reference type, qualify the |
| 3659 | // resulting declaration reference with the extra qualifiers on the |
| 3660 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3661 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 3662 | VK = VK_LValue; |
| 3663 | T = Context.getQualifiedType(T, |
| 3664 | TargetRef->getPointeeType().getQualifiers()); |
| 3665 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3666 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3667 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | /// \brief Construct a new expression that refers to the given |
| 3671 | /// integral template argument with the given source-location |
| 3672 | /// information. |
| 3673 | /// |
| 3674 | /// This routine takes care of the mapping from an integral template |
| 3675 | /// argument (which may have any integral type) to the appropriate |
| 3676 | /// literal value. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3677 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3678 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 3679 | SourceLocation Loc) { |
| 3680 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3681 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3682 | QualType T = Arg.getIntegralType(); |
| 3683 | if (T->isCharType() || T->isWideCharType()) |
| 3684 | return Owned(new (Context) CharacterLiteral( |
| 3685 | Arg.getAsIntegral()->getZExtValue(), |
| 3686 | T->isWideCharType(), |
| 3687 | T, |
| 3688 | Loc)); |
| 3689 | if (T->isBooleanType()) |
| 3690 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 3691 | Arg.getAsIntegral()->getBoolValue(), |
| 3692 | T, |
| 3693 | Loc)); |
| 3694 | |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 3695 | QualType BT; |
| 3696 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 3697 | BT = ET->getDecl()->getPromotionType(); |
| 3698 | else |
| 3699 | BT = T; |
| 3700 | |
| 3701 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
| 3702 | ImpCastExprToType(E, T, CK_IntegralCast); |
| 3703 | |
| 3704 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3707 | /// \brief Match two template parameters within template parameter lists. |
| 3708 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 3709 | bool Complain, |
| 3710 | Sema::TemplateParameterListEqualKind Kind, |
| 3711 | SourceLocation TemplateArgLoc) { |
| 3712 | // Check the actual kind (type, non-type, template). |
| 3713 | if (Old->getKind() != New->getKind()) { |
| 3714 | if (Complain) { |
| 3715 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 3716 | if (TemplateArgLoc.isValid()) { |
| 3717 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3718 | NextDiag = diag::note_template_param_different_kind; |
| 3719 | } |
| 3720 | S.Diag(New->getLocation(), NextDiag) |
| 3721 | << (Kind != Sema::TPL_TemplateMatch); |
| 3722 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 3723 | << (Kind != Sema::TPL_TemplateMatch); |
| 3724 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3725 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3726 | return false; |
| 3727 | } |
| 3728 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3729 | // Check that both are parameter packs are neither are parameter packs. |
| 3730 | // However, if we are matching a template template argument to a |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3731 | // template template parameter, the template template parameter can have |
| 3732 | // a parameter pack where the template template argument does not. |
| 3733 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 3734 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 3735 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3736 | if (Complain) { |
| 3737 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 3738 | if (TemplateArgLoc.isValid()) { |
| 3739 | S.Diag(TemplateArgLoc, |
| 3740 | diag::err_template_arg_template_params_mismatch); |
| 3741 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 3742 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3743 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3744 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 3745 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 3746 | : 2; |
| 3747 | S.Diag(New->getLocation(), NextDiag) |
| 3748 | << ParamKind << New->isParameterPack(); |
| 3749 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 3750 | << ParamKind << Old->isParameterPack(); |
| 3751 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3752 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3753 | return false; |
| 3754 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3755 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3756 | // For non-type template parameters, check the type of the parameter. |
| 3757 | if (NonTypeTemplateParmDecl *OldNTTP |
| 3758 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 3759 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3760 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3761 | // If we are matching a template template argument to a template |
| 3762 | // template parameter and one of the non-type template parameter types |
| 3763 | // is dependent, then we must wait until template instantiation time |
| 3764 | // to actually compare the arguments. |
| 3765 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 3766 | (OldNTTP->getType()->isDependentType() || |
| 3767 | NewNTTP->getType()->isDependentType())) |
| 3768 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3769 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3770 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 3771 | if (Complain) { |
| 3772 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 3773 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3774 | S.Diag(TemplateArgLoc, |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3775 | diag::err_template_arg_template_params_mismatch); |
| 3776 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 3777 | } |
| 3778 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 3779 | << NewNTTP->getType() |
| 3780 | << (Kind != Sema::TPL_TemplateMatch); |
| 3781 | S.Diag(OldNTTP->getLocation(), |
| 3782 | diag::note_template_nontype_parm_prev_declaration) |
| 3783 | << OldNTTP->getType(); |
| 3784 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3785 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3786 | return false; |
| 3787 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3788 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3789 | return true; |
| 3790 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3791 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3792 | // For template template parameters, check the template parameter types. |
| 3793 | // The template parameter lists of template template |
| 3794 | // parameters must agree. |
| 3795 | if (TemplateTemplateParmDecl *OldTTP |
| 3796 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3797 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3798 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 3799 | OldTTP->getTemplateParameters(), |
| 3800 | Complain, |
| 3801 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3802 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3803 | : Kind), |
| 3804 | TemplateArgLoc); |
| 3805 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3806 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3807 | return true; |
| 3808 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3809 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3810 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 3811 | /// lists. |
| 3812 | static |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3813 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3814 | TemplateParameterList *New, |
| 3815 | TemplateParameterList *Old, |
| 3816 | Sema::TemplateParameterListEqualKind Kind, |
| 3817 | SourceLocation TemplateArgLoc) { |
| 3818 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 3819 | if (TemplateArgLoc.isValid()) { |
| 3820 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3821 | NextDiag = diag::note_template_param_list_different_arity; |
| 3822 | } |
| 3823 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 3824 | << (New->size() > Old->size()) |
| 3825 | << (Kind != Sema::TPL_TemplateMatch) |
| 3826 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 3827 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 3828 | << (Kind != Sema::TPL_TemplateMatch) |
| 3829 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 3830 | } |
| 3831 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3832 | /// \brief Determine whether the given template parameter lists are |
| 3833 | /// equivalent. |
| 3834 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3835 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3836 | /// source code as part of a new template declaration. |
| 3837 | /// |
| 3838 | /// \param Old The old template parameter list, typically found via |
| 3839 | /// name lookup of the template declared with this template parameter |
| 3840 | /// list. |
| 3841 | /// |
| 3842 | /// \param Complain If true, this routine will produce a diagnostic if |
| 3843 | /// the template parameter lists are not equivalent. |
| 3844 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3845 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3846 | /// |
| 3847 | /// \param TemplateArgLoc If this source location is valid, then we |
| 3848 | /// are actually checking the template parameter list of a template |
| 3849 | /// argument (New) against the template parameter list of its |
| 3850 | /// corresponding template template parameter (Old). We produce |
| 3851 | /// slightly different diagnostics in this scenario. |
| 3852 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3853 | /// \returns True if the template parameter lists are equal, false |
| 3854 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3855 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3856 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 3857 | TemplateParameterList *Old, |
| 3858 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3859 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3860 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3861 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 3862 | if (Complain) |
| 3863 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 3864 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3865 | |
| 3866 | return false; |
| 3867 | } |
| 3868 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3869 | // C++0x [temp.arg.template]p3: |
| 3870 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3871 | // when each of the template parameters in the template-parameter-list of |
| 3872 | // the template-argument's corresponding class template or template alias |
| 3873 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3874 | // template-parameter-list of P. [...] |
| 3875 | TemplateParameterList::iterator NewParm = New->begin(); |
| 3876 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3877 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3878 | OldParmEnd = Old->end(); |
| 3879 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 3880 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 3881 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3882 | if (NewParm == NewParmEnd) { |
| 3883 | if (Complain) |
| 3884 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 3885 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3886 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3887 | return false; |
| 3888 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3889 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3890 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 3891 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3892 | return false; |
| 3893 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3894 | ++NewParm; |
| 3895 | continue; |
| 3896 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3897 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3898 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3899 | // [...] When P's template- parameter-list contains a template parameter |
| 3900 | // pack (14.5.3), the template parameter pack will match zero or more |
| 3901 | // template parameters or template parameter packs in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3902 | // template-parameter-list of A with the same type and form as the |
| 3903 | // template parameter pack in P (ignoring whether those template |
| 3904 | // parameters are template parameter packs). |
| 3905 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 3906 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 3907 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3908 | return false; |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3909 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3910 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3911 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3912 | // Make sure we exhausted all of the arguments. |
| 3913 | if (NewParm != NewParmEnd) { |
| 3914 | if (Complain) |
| 3915 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 3916 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3917 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3918 | return false; |
| 3919 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3920 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3921 | return true; |
| 3922 | } |
| 3923 | |
| 3924 | /// \brief Check whether a template can be declared within this scope. |
| 3925 | /// |
| 3926 | /// If the template declaration is valid in this scope, returns |
| 3927 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3928 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3929 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3930 | // Find the nearest enclosing declaration scope. |
| 3931 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 3932 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 3933 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3934 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3935 | // C++ [temp]p2: |
| 3936 | // A template-declaration can appear only as a namespace scope or |
| 3937 | // class scope declaration. |
| 3938 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3939 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 3940 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3941 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3942 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3943 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3944 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3945 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3946 | |
| 3947 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 3948 | return false; |
| 3949 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3950 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3951 | diag::err_template_outside_namespace_or_class_scope) |
| 3952 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3953 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3954 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3955 | /// \brief Determine what kind of template specialization the given declaration |
| 3956 | /// is. |
| 3957 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 3958 | if (!D) |
| 3959 | return TSK_Undeclared; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3960 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3961 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 3962 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3963 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 3964 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3965 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 3966 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3967 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3968 | return TSK_Undeclared; |
| 3969 | } |
| 3970 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3971 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3972 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3973 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3974 | /// This routine determines whether a template specialization can be declared |
| 3975 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3976 | /// |
| 3977 | /// \param S the semantic analysis object for which this check is being |
| 3978 | /// performed. |
| 3979 | /// |
| 3980 | /// \param Specialized the entity being specialized or instantiated, which |
| 3981 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3982 | /// a member of a class template (member function, static data member, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3983 | /// member class). |
| 3984 | /// |
| 3985 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 3986 | /// |
| 3987 | /// \param Loc the location of the explicit specialization or instantiation of |
| 3988 | /// this entity. |
| 3989 | /// |
| 3990 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 3991 | /// a class template. |
| 3992 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3993 | /// \returns true if there was an error that we cannot recover from, false |
| 3994 | /// otherwise. |
| 3995 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 3996 | NamedDecl *Specialized, |
| 3997 | NamedDecl *PrevDecl, |
| 3998 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3999 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4000 | // Keep these "kind" numbers in sync with the %select statements in the |
| 4001 | // various diagnostics emitted by this routine. |
| 4002 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4003 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4004 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4005 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4006 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4007 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4008 | EntityKind = 3; |
| 4009 | else if (isa<VarDecl>(Specialized)) |
| 4010 | EntityKind = 4; |
| 4011 | else if (isa<RecordDecl>(Specialized)) |
| 4012 | EntityKind = 5; |
| 4013 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4014 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 4015 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4016 | return true; |
| 4017 | } |
| 4018 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4019 | // C++ [temp.expl.spec]p2: |
| 4020 | // An explicit specialization shall be declared in the namespace |
| 4021 | // of which the template is a member, or, for member templates, in |
| 4022 | // the namespace of which the enclosing class or enclosing class |
| 4023 | // template is a member. An explicit specialization of a member |
| 4024 | // function, member class or static data member of a class |
| 4025 | // template shall be declared in the namespace of which the class |
| 4026 | // template is a member. Such a declaration may also be a |
| 4027 | // definition. If the declaration is not a definition, the |
| 4028 | // specialization may be defined later in the name- space in which |
| 4029 | // the explicit specialization was declared, or in a namespace |
| 4030 | // that encloses the one in which the explicit specialization was |
| 4031 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4032 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4033 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4034 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4035 | return true; |
| 4036 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4037 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4038 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 4039 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4040 | << Specialized; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4041 | return true; |
| 4042 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4043 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4044 | // C++ [temp.class.spec]p6: |
| 4045 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4046 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4047 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4048 | bool ComplainedAboutScope = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4049 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4050 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4051 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4052 | if ((!PrevDecl || |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4053 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4054 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4055 | // C++ [temp.exp.spec]p2: |
| 4056 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4057 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4058 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4059 | // An explicit specialization of a member function, member class or |
| 4060 | // static data member of a class template shall be declared in the |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4061 | // namespace of which the class template is a member. |
| 4062 | // |
| 4063 | // C++0x [temp.expl.spec]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4064 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4065 | // the specialized template. |
| 4066 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) && |
| 4067 | !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) { |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4068 | bool IsCPlusPlus0xExtension |
| 4069 | = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4070 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4071 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4072 | ? diag::ext_template_spec_decl_out_of_scope_global |
| 4073 | : diag::err_template_spec_decl_out_of_scope_global) |
| 4074 | << EntityKind << Specialized; |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4075 | else if (isa<NamespaceDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4076 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4077 | ? diag::ext_template_spec_decl_out_of_scope |
| 4078 | : diag::err_template_spec_decl_out_of_scope) |
| 4079 | << EntityKind << Specialized |
| 4080 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4081 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4082 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 4083 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4084 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4085 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4086 | |
| 4087 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4088 | // namespace. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4089 | // Note that HandleDeclarator() performs this check for explicit |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4090 | // specializations of function templates, static data members, and member |
| 4091 | // functions, so we skip the check here for those kinds of entities. |
| 4092 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4093 | // Should we refactor that check, so that it occurs later? |
| 4094 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4095 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 4096 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4097 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 4098 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 4099 | << EntityKind << Specialized; |
| 4100 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 4101 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 4102 | << EntityKind << Specialized |
| 4103 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4104 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4105 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4106 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4107 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4108 | // FIXME: check for specialization-after-instantiation errors and such. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4109 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4110 | return false; |
| 4111 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4112 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4113 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 4114 | /// that checks non-type template partial specialization arguments. |
| 4115 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 4116 | NonTypeTemplateParmDecl *Param, |
| 4117 | const TemplateArgument *Args, |
| 4118 | unsigned NumArgs) { |
| 4119 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4120 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 4121 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4122 | Args[I].pack_begin(), |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4123 | Args[I].pack_size())) |
| 4124 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4125 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4126 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4127 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4128 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4129 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4130 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4131 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4132 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4133 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4134 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4135 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4136 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4137 | |
| 4138 | // Strip off any implicit casts we added as part of type checking. |
| 4139 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4140 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4141 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4142 | // C++ [temp.class.spec]p8: |
| 4143 | // A non-type argument is non-specialized if it is the name of a |
| 4144 | // non-type parameter. All other non-type arguments are |
| 4145 | // specialized. |
| 4146 | // |
| 4147 | // Below, we check the two conditions that only apply to |
| 4148 | // specialized non-type arguments, so skip any non-specialized |
| 4149 | // arguments. |
| 4150 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4151 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4152 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4153 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4154 | // C++ [temp.class.spec]p9: |
| 4155 | // Within the argument list of a class template partial |
| 4156 | // specialization, the following restrictions apply: |
| 4157 | // -- A partially specialized non-type argument expression |
| 4158 | // shall not involve a template parameter of the partial |
| 4159 | // specialization except when the argument expression is a |
| 4160 | // simple identifier. |
| 4161 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4162 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4163 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4164 | << ArgExpr->getSourceRange(); |
| 4165 | return true; |
| 4166 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4167 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4168 | // -- The type of a template parameter corresponding to a |
| 4169 | // specialized non-type argument shall not be dependent on a |
| 4170 | // parameter of the specialization. |
| 4171 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4172 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4173 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 4174 | << Param->getType() |
| 4175 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4176 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4177 | return true; |
| 4178 | } |
| 4179 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4180 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4181 | return false; |
| 4182 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4183 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4184 | /// \brief Check the non-type template arguments of a class template |
| 4185 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 4186 | /// |
| 4187 | /// \param TemplateParams the template parameters of the primary class |
| 4188 | /// template. |
| 4189 | /// |
| 4190 | /// \param TemplateArg the template arguments of the class template |
| 4191 | /// partial specialization. |
| 4192 | /// |
| 4193 | /// \returns true if there was an error, false otherwise. |
| 4194 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 4195 | TemplateParameterList *TemplateParams, |
| 4196 | llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
| 4197 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 4198 | |
| 4199 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4200 | NonTypeTemplateParmDecl *Param |
| 4201 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 4202 | if (!Param) |
| 4203 | continue; |
| 4204 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4205 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4206 | &ArgList[I], 1)) |
| 4207 | return true; |
| 4208 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4209 | |
| 4210 | return false; |
| 4211 | } |
| 4212 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4213 | /// \brief Retrieve the previous declaration of the given declaration. |
| 4214 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 4215 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4216 | return VD->getPreviousDeclaration(); |
| 4217 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 4218 | return FD->getPreviousDeclaration(); |
| 4219 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 4220 | return TD->getPreviousDeclaration(); |
| 4221 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) |
| 4222 | return TD->getPreviousDeclaration(); |
| 4223 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 4224 | return FTD->getPreviousDeclaration(); |
| 4225 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 4226 | return CTD->getPreviousDeclaration(); |
| 4227 | return 0; |
| 4228 | } |
| 4229 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4230 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4231 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4232 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4233 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4234 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4235 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4236 | SourceLocation TemplateNameLoc, |
| 4237 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4238 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4239 | SourceLocation RAngleLoc, |
| 4240 | AttributeList *Attr, |
| 4241 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4242 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4243 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4244 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4245 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4246 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4247 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4248 | |
| 4249 | if (!ClassTemplate) { |
| 4250 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4251 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4252 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4253 | return true; |
| 4254 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4255 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4256 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4257 | bool isPartialSpecialization = false; |
| 4258 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4259 | // Check the validity of the template headers that introduce this |
| 4260 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4261 | // FIXME: We probably shouldn't complain about these headers for |
| 4262 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4263 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4264 | TemplateParameterList *TemplateParams |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4265 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, |
| 4266 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4267 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4268 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4269 | isExplicitSpecialization, |
| 4270 | Invalid); |
| 4271 | if (Invalid) |
| 4272 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4273 | |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4274 | unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size(); |
| 4275 | if (TemplateParams) |
| 4276 | --NumMatchedTemplateParamLists; |
| 4277 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4278 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4279 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4280 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4281 | if (TUK == TUK_Friend) { |
| 4282 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4283 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4284 | return true; |
| 4285 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4286 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4287 | // C++ [temp.class.spec]p10: |
| 4288 | // The template parameter list of a specialization shall not |
| 4289 | // contain default template argument values. |
| 4290 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4291 | Decl *Param = TemplateParams->getParam(I); |
| 4292 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4293 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4294 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4295 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4296 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4297 | } |
| 4298 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4299 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4300 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4301 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4302 | diag::err_default_arg_in_partial_spec) |
| 4303 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4304 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4305 | } |
| 4306 | } else { |
| 4307 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4308 | if (TTP->hasDefaultArgument()) { |
| 4309 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4310 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4311 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4312 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4313 | } |
| 4314 | } |
| 4315 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4316 | } else if (TemplateParams) { |
| 4317 | if (TUK == TUK_Friend) |
| 4318 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4319 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4320 | SourceRange(TemplateParams->getTemplateLoc(), |
| 4321 | TemplateParams->getRAngleLoc())) |
| 4322 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4323 | else |
| 4324 | isExplicitSpecialization = true; |
| 4325 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4326 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4327 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4328 | isExplicitSpecialization = true; |
| 4329 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4330 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4331 | // Check that the specialization uses the same tag kind as the |
| 4332 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4333 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4334 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4335 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4336 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4337 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4338 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4339 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4340 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4341 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4342 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4343 | diag::note_previous_use); |
| 4344 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4345 | } |
| 4346 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4347 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4348 | TemplateArgumentListInfo TemplateArgs; |
| 4349 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 4350 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4351 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4352 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4353 | // Check for unexpanded parameter packs in any of the template arguments. |
| 4354 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4355 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4356 | UPPC_PartialSpecialization)) |
| 4357 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4358 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4359 | // Check that the template argument list is well-formed for this |
| 4360 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4361 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4362 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4363 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4364 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4365 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4366 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4367 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4368 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4369 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4370 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4371 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4372 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4373 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4374 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4375 | return true; |
| 4376 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4377 | if (!Name.isDependent() && |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4378 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4379 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4380 | TemplateArgs.size())) { |
| 4381 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 4382 | << ClassTemplate->getDeclName(); |
| 4383 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4384 | } |
| 4385 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4386 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4387 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4388 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 4389 | |
| 4390 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4391 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4392 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4393 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 4394 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4395 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4396 | else |
| 4397 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4398 | = ClassTemplate->findSpecialization(Converted.data(), |
| 4399 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4400 | |
| 4401 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4402 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4403 | // Check whether we can declare a class template specialization in |
| 4404 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4405 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4406 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 4407 | TemplateNameLoc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4408 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4409 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4410 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4411 | // The canonical type |
| 4412 | QualType CanonType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4413 | if (PrevDecl && |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4414 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4415 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4416 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4417 | // arguments was referenced but not declared, or we're only |
| 4418 | // referencing this specialization as a friend, reuse that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4419 | // declaration node as our own, updating its source location to |
| 4420 | // reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4421 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4422 | Specialization->setLocation(TemplateNameLoc); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4423 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4424 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4425 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4426 | // Build the canonical type that describes the converted template |
| 4427 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4428 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4429 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4430 | Converted.data(), |
| 4431 | Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4432 | |
| 4433 | if (Context.hasSameType(CanonType, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4434 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 4435 | // C++ [temp.class.spec]p9b3: |
| 4436 | // |
| 4437 | // -- The argument list of the specialization shall not be identical |
| 4438 | // to the implicit argument list of the primary template. |
| 4439 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 4440 | << (TUK == TUK_Definition) |
| 4441 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 4442 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 4443 | ClassTemplate->getIdentifier(), |
| 4444 | TemplateNameLoc, |
| 4445 | Attr, |
| 4446 | TemplateParams, |
| 4447 | AS_none); |
| 4448 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4449 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4450 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4451 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 4452 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4453 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4454 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4455 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4456 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4457 | ClassTemplate->getDeclContext(), |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 4458 | TemplateNameLoc, |
| 4459 | TemplateParams, |
| 4460 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4461 | Converted.data(), |
| 4462 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4463 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4464 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4465 | PrevPartial, |
| 4466 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4467 | SetNestedNameSpecifier(Partial, SS); |
Douglas Gregor | 98c2e62 | 2010-07-28 23:59:57 +0000 | [diff] [blame] | 4468 | if (NumMatchedTemplateParamLists > 0 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4469 | Partial->setTemplateParameterListsInfo(Context, |
| 4470 | NumMatchedTemplateParamLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4471 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4472 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4473 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4474 | if (!PrevPartial) |
| 4475 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4476 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4477 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4478 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4479 | // template specialization, make a note of that. |
| 4480 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 4481 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4482 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4483 | // Check that all of the template parameters of the class template |
| 4484 | // partial specialization are deducible from the template |
| 4485 | // arguments. If not, this class template partial specialization |
| 4486 | // will never be used. |
| 4487 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 4488 | DeducibleParams.resize(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4489 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4490 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4491 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4492 | unsigned NumNonDeducible = 0; |
| 4493 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 4494 | if (!DeducibleParams[I]) |
| 4495 | ++NumNonDeducible; |
| 4496 | |
| 4497 | if (NumNonDeducible) { |
| 4498 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 4499 | << (NumNonDeducible > 1) |
| 4500 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 4501 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 4502 | if (!DeducibleParams[I]) { |
| 4503 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 4504 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4505 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4506 | diag::note_partial_spec_unused_parameter) |
| 4507 | << Param->getDeclName(); |
| 4508 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4509 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4510 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 4511 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4512 | } |
| 4513 | } |
| 4514 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4515 | } else { |
| 4516 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4517 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4518 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4519 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4520 | ClassTemplate->getDeclContext(), |
| 4521 | TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4522 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4523 | Converted.data(), |
| 4524 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4525 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4526 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 98c2e62 | 2010-07-28 23:59:57 +0000 | [diff] [blame] | 4527 | if (NumMatchedTemplateParamLists > 0 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4528 | Specialization->setTemplateParameterListsInfo(Context, |
| 4529 | NumMatchedTemplateParamLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4530 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4531 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4532 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4533 | if (!PrevDecl) |
| 4534 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4535 | |
| 4536 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4537 | } |
| 4538 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4539 | // C++ [temp.expl.spec]p6: |
| 4540 | // 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] | 4541 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4542 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4543 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4544 | // use occurs; no diagnostic is required. |
| 4545 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4546 | bool Okay = false; |
| 4547 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4548 | // Is there any previous explicit specialization declaration? |
| 4549 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 4550 | Okay = true; |
| 4551 | break; |
| 4552 | } |
| 4553 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4554 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4555 | if (!Okay) { |
| 4556 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 4557 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 4558 | << Context.getTypeDeclType(Specialization) << Range; |
| 4559 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4560 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4561 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4562 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4563 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4564 | return true; |
| 4565 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4566 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4567 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4568 | // If this is not a friend, note that this is an explicit specialization. |
| 4569 | if (TUK != TUK_Friend) |
| 4570 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4571 | |
| 4572 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4573 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4574 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4575 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4576 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4577 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4578 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 4579 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4580 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4581 | } |
| 4582 | } |
| 4583 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 4584 | if (Attr) |
| 4585 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 4586 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 4587 | // Build the fully-sugared type for this class template |
| 4588 | // specialization as the user wrote in the specialization |
| 4589 | // itself. This means that we'll pretty-print the type retrieved |
| 4590 | // from the specialization's declaration the way that the user |
| 4591 | // actually wrote the specialization, rather than formatting the |
| 4592 | // name based on the "canonical" representation used to store the |
| 4593 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4594 | TypeSourceInfo *WrittenTy |
| 4595 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 4596 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4597 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4598 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | 7e9b57b | 2010-07-06 18:33:12 +0000 | [diff] [blame] | 4599 | if (TemplateParams) |
| 4600 | Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc()); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4601 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4602 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4603 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4604 | // C++ [temp.expl.spec]p9: |
| 4605 | // A template explicit specialization is in the scope of the |
| 4606 | // namespace in which the template was defined. |
| 4607 | // |
| 4608 | // We actually implement this paragraph where we set the semantic |
| 4609 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 4610 | // but we also maintain the lexical context where the actual |
| 4611 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4612 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4613 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4614 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4615 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4616 | Specialization->startDefinition(); |
| 4617 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4618 | if (TUK == TUK_Friend) { |
| 4619 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 4620 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 4621 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4622 | /*FIXME:*/KWLoc); |
| 4623 | Friend->setAccess(AS_public); |
| 4624 | CurContext->addDecl(Friend); |
| 4625 | } else { |
| 4626 | // Add the specialization into its lexical context, so that it can |
| 4627 | // be seen when iterating through the list of declarations in that |
| 4628 | // context. However, specializations are not found by name lookup. |
| 4629 | CurContext->addDecl(Specialization); |
| 4630 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4631 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4632 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4633 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4634 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4635 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4636 | Declarator &D) { |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4637 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 4638 | } |
| 4639 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4640 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4641 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4642 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4643 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4644 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4645 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4646 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4647 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4648 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4649 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4650 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4651 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4652 | Decl *DP = HandleDeclarator(ParentScope, D, |
| 4653 | move(TemplateParameterLists), |
| 4654 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4655 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4656 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4657 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4658 | FunctionTemplate->getTemplatedDecl()); |
| 4659 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 4660 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 4661 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4662 | } |
| 4663 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4664 | /// \brief Strips various properties off an implicit instantiation |
| 4665 | /// that has just been explicitly specialized. |
| 4666 | static void StripImplicitInstantiation(NamedDecl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4667 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4668 | |
| 4669 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4670 | FD->setInlineSpecified(false); |
| 4671 | } |
| 4672 | } |
| 4673 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4674 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4675 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4676 | /// for those cases where they are required and determining whether the |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4677 | /// new specialization/instantiation will have any effect. |
| 4678 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4679 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4680 | /// instantiation. |
| 4681 | /// |
| 4682 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 4683 | /// |
| 4684 | /// \param PrevDecl the previous declaration of the entity. |
| 4685 | /// |
| 4686 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 4687 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4688 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4689 | /// declaration was instantiated (either implicitly or explicitly). |
| 4690 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4691 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4692 | /// specialization or instantiation has no effect and should be ignored. |
| 4693 | /// |
| 4694 | /// \returns true if there was an error that should prevent the introduction of |
| 4695 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4696 | bool |
| 4697 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 4698 | TemplateSpecializationKind NewTSK, |
| 4699 | NamedDecl *PrevDecl, |
| 4700 | TemplateSpecializationKind PrevTSK, |
| 4701 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4702 | bool &HasNoEffect) { |
| 4703 | HasNoEffect = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4704 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4705 | switch (NewTSK) { |
| 4706 | case TSK_Undeclared: |
| 4707 | case TSK_ImplicitInstantiation: |
| 4708 | assert(false && "Don't check implicit instantiations here"); |
| 4709 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4710 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4711 | case TSK_ExplicitSpecialization: |
| 4712 | switch (PrevTSK) { |
| 4713 | case TSK_Undeclared: |
| 4714 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4715 | // Okay, we're just specializing something that is either already |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4716 | // explicitly specialized or has merely been mentioned without any |
| 4717 | // instantiation. |
| 4718 | return false; |
| 4719 | |
| 4720 | case TSK_ImplicitInstantiation: |
| 4721 | if (PrevPointOfInstantiation.isInvalid()) { |
| 4722 | // The declaration itself has not actually been instantiated, so it is |
| 4723 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4724 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4725 | return false; |
| 4726 | } |
| 4727 | // Fall through |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4728 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4729 | case TSK_ExplicitInstantiationDeclaration: |
| 4730 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4731 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 4732 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4733 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4734 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4735 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4736 | // 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] | 4737 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4738 | // before the first use of that specialization that would cause an |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4739 | // implicit instantiation to take place, in every translation unit in |
| 4740 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4741 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4742 | // Is there any previous explicit specialization declaration? |
| 4743 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 4744 | return false; |
| 4745 | } |
| 4746 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4747 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4748 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4749 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4750 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4751 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4752 | return true; |
| 4753 | } |
| 4754 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4755 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4756 | case TSK_ExplicitInstantiationDeclaration: |
| 4757 | switch (PrevTSK) { |
| 4758 | case TSK_ExplicitInstantiationDeclaration: |
| 4759 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4760 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4761 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4762 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4763 | case TSK_Undeclared: |
| 4764 | case TSK_ImplicitInstantiation: |
| 4765 | // We're explicitly instantiating something that may have already been |
| 4766 | // implicitly instantiated; that's fine. |
| 4767 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4768 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4769 | case TSK_ExplicitSpecialization: |
| 4770 | // C++0x [temp.explicit]p4: |
| 4771 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4772 | // of a template appears after a declaration of an explicit |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4773 | // specialization for that template, the explicit instantiation has no |
| 4774 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4775 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4776 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4777 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4778 | case TSK_ExplicitInstantiationDefinition: |
| 4779 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4780 | // If an entity is the subject of both an explicit instantiation |
| 4781 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4782 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4783 | Diag(NewLoc, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4784 | diag::err_explicit_instantiation_declaration_after_definition); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4785 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4786 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4787 | assert(PrevPointOfInstantiation.isValid() && |
| 4788 | "Explicit instantiation without point of instantiation?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4789 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4790 | return false; |
| 4791 | } |
| 4792 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4793 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4794 | case TSK_ExplicitInstantiationDefinition: |
| 4795 | switch (PrevTSK) { |
| 4796 | case TSK_Undeclared: |
| 4797 | case TSK_ImplicitInstantiation: |
| 4798 | // We're explicitly instantiating something that may have already been |
| 4799 | // implicitly instantiated; that's fine. |
| 4800 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4801 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4802 | case TSK_ExplicitSpecialization: |
| 4803 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 4804 | // For a given set of template parameters, if an explicit |
| 4805 | // instantiation of a template appears after a declaration of |
| 4806 | // an explicit specialization for that template, the explicit |
| 4807 | // instantiation has no effect. |
| 4808 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4809 | // 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] | 4810 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4811 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4812 | if (!getLangOptions().CPlusPlus0x) { |
| 4813 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4814 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4815 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4816 | diag::note_previous_template_specialization); |
| 4817 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4818 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4819 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4820 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4821 | case TSK_ExplicitInstantiationDeclaration: |
| 4822 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4823 | // were previously asked to suppress instantiations. That's fine. |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4824 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4825 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4826 | case TSK_ExplicitInstantiationDefinition: |
| 4827 | // C++0x [temp.spec]p5: |
| 4828 | // For a given template and a given set of template-arguments, |
| 4829 | // - an explicit instantiation definition shall appear at most once |
| 4830 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4831 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4832 | << PrevDecl; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4833 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4834 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4835 | HasNoEffect = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4836 | return false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4837 | } |
| 4838 | break; |
| 4839 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4840 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4841 | assert(false && "Missing specialization/instantiation case?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4842 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4843 | return false; |
| 4844 | } |
| 4845 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4846 | /// \brief Perform semantic analysis for the given dependent function |
| 4847 | /// template specialization. The only possible way to get a dependent |
| 4848 | /// function template specialization is with a friend declaration, |
| 4849 | /// like so: |
| 4850 | /// |
| 4851 | /// template <class T> void foo(T); |
| 4852 | /// template <class T> class A { |
| 4853 | /// friend void foo<>(T); |
| 4854 | /// }; |
| 4855 | /// |
| 4856 | /// There really isn't any useful analysis we can do here, so we |
| 4857 | /// just store the information. |
| 4858 | bool |
| 4859 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 4860 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 4861 | LookupResult &Previous) { |
| 4862 | // Remove anything from Previous that isn't a function template in |
| 4863 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4864 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4865 | LookupResult::Filter F = Previous.makeFilter(); |
| 4866 | while (F.hasNext()) { |
| 4867 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 4868 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4869 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 4870 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4871 | F.erase(); |
| 4872 | } |
| 4873 | F.done(); |
| 4874 | |
| 4875 | // Should this be diagnosed here? |
| 4876 | if (Previous.empty()) return true; |
| 4877 | |
| 4878 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 4879 | ExplicitTemplateArgs); |
| 4880 | return false; |
| 4881 | } |
| 4882 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4883 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4884 | /// specialization. |
| 4885 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4886 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4887 | /// explicit function template specialization. On successful completion, |
| 4888 | /// the function declaration \p FD will become a function template |
| 4889 | /// specialization. |
| 4890 | /// |
| 4891 | /// \param FD the function declaration, which will be updated to become a |
| 4892 | /// function template specialization. |
| 4893 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4894 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 4895 | /// if any. Note that this may be valid info even when 0 arguments are |
| 4896 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 4897 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4898 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4899 | /// \param PrevDecl the set of declarations that may be specialized by |
| 4900 | /// this function specialization. |
| 4901 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4902 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4903 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4904 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4905 | // The set of function template specializations that could match this |
| 4906 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4907 | UnresolvedSet<8> Candidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4908 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4909 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4910 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4911 | I != E; ++I) { |
| 4912 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 4913 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4914 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4915 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4916 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 4917 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4918 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4919 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4920 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4921 | // A trailing template-argument can be left unspecified in the |
| 4922 | // template-id naming an explicit function template specialization |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4923 | // provided it can be deduced from the function argument type. |
| 4924 | // Perform template argument deduction to determine whether we may be |
| 4925 | // specializing this template. |
| 4926 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4927 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4928 | FunctionDecl *Specialization = 0; |
| 4929 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4930 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4931 | FD->getType(), |
| 4932 | Specialization, |
| 4933 | Info)) { |
| 4934 | // FIXME: Template argument deduction failed; record why it failed, so |
| 4935 | // that we can provide nifty diagnostics. |
| 4936 | (void)TDK; |
| 4937 | continue; |
| 4938 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4939 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4940 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4941 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4942 | } |
| 4943 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4944 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4945 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4946 | UnresolvedSetIterator Result |
| 4947 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4948 | TPOC_Other, 0, FD->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4949 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4950 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4951 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4952 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4953 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4954 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4955 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4956 | |
| 4957 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 4958 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 4959 | Specialization->setLocation(FD->getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4960 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4961 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4962 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4963 | |
| 4964 | // If this is a friend declaration, then we're not really declaring |
| 4965 | // an explicit specialization. |
| 4966 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4967 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4968 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4969 | if (!isFriend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4970 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4971 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4972 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4973 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4974 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4975 | |
| 4976 | // C++ [temp.expl.spec]p6: |
| 4977 | // 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] | 4978 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4979 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4980 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4981 | // use occurs; no diagnostic is required. |
| 4982 | FunctionTemplateSpecializationInfo *SpecInfo |
| 4983 | = Specialization->getTemplateSpecializationInfo(); |
| 4984 | assert(SpecInfo && "Function template specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4985 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4986 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4987 | if (!isFriend && |
| 4988 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4989 | TSK_ExplicitSpecialization, |
| 4990 | Specialization, |
| 4991 | SpecInfo->getTemplateSpecializationKind(), |
| 4992 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4993 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4994 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4995 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4996 | // Mark the prior declaration as an explicit specialization, so that later |
| 4997 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 4998 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4999 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5000 | MarkUnusedFileScopedDecl(Specialization); |
| 5001 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5002 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5003 | // Turn the given function declaration into a function template |
| 5004 | // specialization, with the template arguments from the previous |
| 5005 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5006 | // Take copies of (semantic and syntactic) template argument lists. |
| 5007 | const TemplateArgumentList* TemplArgs = new (Context) |
| 5008 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
| 5009 | const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs |
| 5010 | ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5011 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5012 | TemplArgs, /*InsertPos=*/0, |
| 5013 | SpecInfo->getTemplateSpecializationKind(), |
| 5014 | TemplArgsAsWritten); |
| 5015 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5016 | // The "previous declaration" for this function template specialization is |
| 5017 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5018 | Previous.clear(); |
| 5019 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5020 | return false; |
| 5021 | } |
| 5022 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5023 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5024 | /// specialization. |
| 5025 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5026 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5027 | /// explicit member function specialization. On successful completion, |
| 5028 | /// the function declaration \p FD will become a member function |
| 5029 | /// specialization. |
| 5030 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5031 | /// \param Member the member declaration, which will be updated to become a |
| 5032 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5033 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5034 | /// \param Previous the set of declarations, one of which may be specialized |
| 5035 | /// by this function specialization; the set will be modified to contain the |
| 5036 | /// redeclared member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5037 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5038 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5039 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5040 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5041 | // Try to find the member we are instantiating. |
| 5042 | NamedDecl *Instantiation = 0; |
| 5043 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5044 | MemberSpecializationInfo *MSInfo = 0; |
| 5045 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5046 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5047 | // Nowhere to look anyway. |
| 5048 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5049 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5050 | I != E; ++I) { |
| 5051 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5052 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5053 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 5054 | Instantiation = Method; |
| 5055 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5056 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5057 | break; |
| 5058 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5059 | } |
| 5060 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5061 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5062 | VarDecl *PrevVar; |
| 5063 | if (Previous.isSingleResult() && |
| 5064 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5065 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5066 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5067 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5068 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5069 | } |
| 5070 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5071 | CXXRecordDecl *PrevRecord; |
| 5072 | if (Previous.isSingleResult() && |
| 5073 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 5074 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5075 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5076 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5077 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5078 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5079 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5080 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5081 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5082 | // specializations are always out-of-line, the caller will complain about |
| 5083 | // this mismatch later. |
| 5084 | return false; |
| 5085 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5086 | |
| 5087 | // If this is a friend, just bail out here before we start turning |
| 5088 | // things into explicit specializations. |
| 5089 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 5090 | // Preserve instantiation information. |
| 5091 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 5092 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 5093 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5094 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5095 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 5096 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 5097 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5098 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5099 | } |
| 5100 | |
| 5101 | Previous.clear(); |
| 5102 | Previous.addDecl(Instantiation); |
| 5103 | return false; |
| 5104 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5105 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5106 | // Make sure that this is a specialization of a member. |
| 5107 | if (!InstantiatedFrom) { |
| 5108 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 5109 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5110 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 5111 | return true; |
| 5112 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5113 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5114 | // C++ [temp.expl.spec]p6: |
| 5115 | // 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] | 5116 | // explicitly specialized then that spe- cialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5117 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5118 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5119 | // use occurs; no diagnostic is required. |
| 5120 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5121 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5122 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5123 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 5124 | TSK_ExplicitSpecialization, |
| 5125 | Instantiation, |
| 5126 | MSInfo->getTemplateSpecializationKind(), |
| 5127 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5128 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5129 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5130 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5131 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5132 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5133 | InstantiatedFrom, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5134 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5135 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5136 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 5137 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5138 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5139 | // the original declaration to note that it is an explicit specialization |
| 5140 | // (if it was previously an implicit instantiation). This latter step |
| 5141 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5142 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5143 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 5144 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 5145 | TSK_ImplicitInstantiation) { |
| 5146 | InstantiationFunction->setTemplateSpecializationKind( |
| 5147 | TSK_ExplicitSpecialization); |
| 5148 | InstantiationFunction->setLocation(Member->getLocation()); |
| 5149 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5150 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5151 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 5152 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5153 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5154 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5155 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5156 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 5157 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 5158 | TSK_ImplicitInstantiation) { |
| 5159 | InstantiationVar->setTemplateSpecializationKind( |
| 5160 | TSK_ExplicitSpecialization); |
| 5161 | InstantiationVar->setLocation(Member->getLocation()); |
| 5162 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5163 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5164 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 5165 | cast<VarDecl>(InstantiatedFrom), |
| 5166 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5167 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5168 | } else { |
| 5169 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5170 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 5171 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 5172 | TSK_ImplicitInstantiation) { |
| 5173 | InstantiationClass->setTemplateSpecializationKind( |
| 5174 | TSK_ExplicitSpecialization); |
| 5175 | InstantiationClass->setLocation(Member->getLocation()); |
| 5176 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5177 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5178 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5179 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5180 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5181 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5182 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5183 | // Save the caller the trouble of having to figure out which declaration |
| 5184 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5185 | Previous.clear(); |
| 5186 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5187 | return false; |
| 5188 | } |
| 5189 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5190 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5191 | /// |
| 5192 | /// \returns true if a serious error occurs, false otherwise. |
| 5193 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5194 | SourceLocation InstLoc, |
| 5195 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5196 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5197 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5198 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5199 | if (CurContext->isRecord()) { |
| 5200 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5201 | << D; |
| 5202 | return true; |
| 5203 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5204 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5205 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5206 | // An explicit instantiation shall appear in an enclosing namespace of its |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5207 | // template. |
| 5208 | // |
| 5209 | // 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] | 5210 | if (S.getLangOptions().CPlusPlus0x && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5211 | !CurContext->Encloses(OrigContext)) { |
| 5212 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5213 | S.Diag(InstLoc, |
| 5214 | S.getLangOptions().CPlusPlus0x? |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5215 | diag::err_explicit_instantiation_out_of_scope |
| 5216 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5217 | << D << NS; |
| 5218 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5219 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5220 | S.getLangOptions().CPlusPlus0x? |
| 5221 | diag::err_explicit_instantiation_must_be_global |
| 5222 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5223 | << D; |
| 5224 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5225 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5226 | } |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5227 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5228 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5229 | // If the name declared in the explicit instantiation is an unqualified |
| 5230 | // name, the explicit instantiation shall appear in the namespace where |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5231 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 5232 | // namespace from its enclosing namespace set. |
| 5233 | if (WasQualifiedName) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5234 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5235 | |
| 5236 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5237 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5238 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5239 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5240 | S.getLangOptions().CPlusPlus0x? |
| 5241 | diag::err_explicit_instantiation_unqualified_wrong_namespace |
| 5242 | : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5243 | << D << OrigContext; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5244 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5245 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5246 | } |
| 5247 | |
| 5248 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5249 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5250 | if (!SS.isSet()) |
| 5251 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5252 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5253 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5254 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5255 | // or a static data member of a class template specialization, the name of |
| 5256 | // the class template specialization in the qualified-id for the member |
| 5257 | // name shall be a simple-template-id. |
| 5258 | // |
| 5259 | // C++98 has the same restriction, just worded differently. |
| 5260 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5261 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5262 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5263 | if (isa<TemplateSpecializationType>(T)) |
| 5264 | return true; |
| 5265 | |
| 5266 | return false; |
| 5267 | } |
| 5268 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5269 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5270 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5271 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5272 | SourceLocation ExternLoc, |
| 5273 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5274 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5275 | SourceLocation KWLoc, |
| 5276 | const CXXScopeSpec &SS, |
| 5277 | TemplateTy TemplateD, |
| 5278 | SourceLocation TemplateNameLoc, |
| 5279 | SourceLocation LAngleLoc, |
| 5280 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5281 | SourceLocation RAngleLoc, |
| 5282 | AttributeList *Attr) { |
| 5283 | // Find the class template we're specializing |
| 5284 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5285 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5286 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5287 | |
| 5288 | // Check that the specialization uses the same tag kind as the |
| 5289 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5290 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5291 | assert(Kind != TTK_Enum && |
| 5292 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5293 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5294 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5295 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5296 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5297 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5298 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5299 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5300 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5301 | diag::note_previous_use); |
| 5302 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5303 | } |
| 5304 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5305 | // C++0x [temp.explicit]p2: |
| 5306 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5307 | // definition and an explicit instantiation declaration. An explicit |
| 5308 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5309 | TemplateSpecializationKind TSK |
| 5310 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5311 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5312 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5313 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5314 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5315 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5316 | |
| 5317 | // Check that the template argument list is well-formed for this |
| 5318 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5319 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5320 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5321 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5322 | return true; |
| 5323 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5324 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5325 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5326 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5327 | // Find the class template specialization declaration that |
| 5328 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5329 | void *InsertPos = 0; |
| 5330 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5331 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5332 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5333 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5334 | TemplateSpecializationKind PrevDecl_TSK |
| 5335 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 5336 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5337 | // C++0x [temp.explicit]p2: |
| 5338 | // [...] An explicit instantiation shall appear in an enclosing |
| 5339 | // namespace of its template. [...] |
| 5340 | // |
| 5341 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5342 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 5343 | SS.isSet())) |
| 5344 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5345 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5346 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5347 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5348 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5349 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5350 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5351 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5352 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5353 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5354 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5355 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5356 | // Even though HasNoEffect == true means that this explicit instantiation |
| 5357 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 5358 | |
| 5359 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 5360 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5361 | // Since the only prior class template specialization with these |
| 5362 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5363 | // declaration node as our own, updating the source location |
| 5364 | // for the template name to reflect our new declaration. |
| 5365 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5366 | Specialization = PrevDecl; |
| 5367 | Specialization->setLocation(TemplateNameLoc); |
| 5368 | PrevDecl = 0; |
| 5369 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5370 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5371 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5372 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5373 | // Create a new class template specialization declaration node for |
| 5374 | // this explicit specialization. |
| 5375 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5376 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5377 | ClassTemplate->getDeclContext(), |
| 5378 | TemplateNameLoc, |
| 5379 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5380 | Converted.data(), |
| 5381 | Converted.size(), |
| 5382 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5383 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5384 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5385 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5386 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5387 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5388 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5389 | } |
| 5390 | |
| 5391 | // Build the fully-sugared type for this explicit instantiation as |
| 5392 | // the user wrote in the explicit instantiation itself. This means |
| 5393 | // that we'll pretty-print the type retrieved from the |
| 5394 | // specialization's declaration the way that the user actually wrote |
| 5395 | // the explicit instantiation, rather than formatting the name based |
| 5396 | // on the "canonical" representation used to store the template |
| 5397 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5398 | TypeSourceInfo *WrittenTy |
| 5399 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5400 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5401 | Context.getTypeDeclType(Specialization)); |
| 5402 | Specialization->setTypeAsWritten(WrittenTy); |
| 5403 | TemplateArgsIn.release(); |
| 5404 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5405 | // Set source locations for keywords. |
| 5406 | Specialization->setExternLoc(ExternLoc); |
| 5407 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 5408 | |
| 5409 | // Add the explicit instantiation into its lexical context. However, |
| 5410 | // since explicit instantiations are never found by name lookup, we |
| 5411 | // just put it into the declaration context directly. |
| 5412 | Specialization->setLexicalDeclContext(CurContext); |
| 5413 | CurContext->addDecl(Specialization); |
| 5414 | |
| 5415 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 5416 | if (HasNoEffect) { |
| 5417 | // Set the template specialization kind. |
| 5418 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5419 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5420 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5421 | |
| 5422 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5423 | // A definition of a class template or class member template |
| 5424 | // shall be in scope at the point of the explicit instantiation of |
| 5425 | // the class template or class member template. |
| 5426 | // |
| 5427 | // This check comes when we actually try to perform the |
| 5428 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5429 | ClassTemplateSpecializationDecl *Def |
| 5430 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5431 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5432 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5433 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5434 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5435 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5436 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 5437 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5438 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5439 | // Instantiate the members of this class template specialization. |
| 5440 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5441 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5442 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 5443 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 5444 | |
| 5445 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 5446 | // TSK_ExplicitInstantiationDefinition |
| 5447 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 5448 | TSK == TSK_ExplicitInstantiationDefinition) |
| 5449 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5450 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5451 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5452 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5453 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5454 | // Set the template specialization kind. |
| 5455 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5456 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5457 | } |
| 5458 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5459 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5460 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5461 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5462 | SourceLocation ExternLoc, |
| 5463 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5464 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5465 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5466 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5467 | IdentifierInfo *Name, |
| 5468 | SourceLocation NameLoc, |
| 5469 | AttributeList *Attr) { |
| 5470 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 5471 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5472 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5473 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5474 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
| 5475 | MultiTemplateParamsArg(*this, 0, 0), |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 5476 | Owned, IsDependent, false, false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5477 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5478 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 5479 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5480 | if (!TagD) |
| 5481 | return true; |
| 5482 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5483 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5484 | if (Tag->isEnum()) { |
| 5485 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 5486 | << Context.getTypeDeclType(Tag); |
| 5487 | return true; |
| 5488 | } |
| 5489 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 5490 | if (Tag->isInvalidDecl()) |
| 5491 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5492 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5493 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 5494 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 5495 | if (!Pattern) { |
| 5496 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 5497 | << Context.getTypeDeclType(Record); |
| 5498 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 5499 | return true; |
| 5500 | } |
| 5501 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5502 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5503 | // If the explicit instantiation is for a class or member class, the |
| 5504 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5505 | // simple-template-id. |
| 5506 | // |
| 5507 | // C++98 has the same restriction, just worded differently. |
| 5508 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5509 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5510 | << Record << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5511 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5512 | // C++0x [temp.explicit]p2: |
| 5513 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5514 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5515 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 5516 | TemplateSpecializationKind TSK |
| 5517 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5518 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5519 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5520 | // C++0x [temp.explicit]p2: |
| 5521 | // [...] An explicit instantiation shall appear in an enclosing |
| 5522 | // namespace of its template. [...] |
| 5523 | // |
| 5524 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5525 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5526 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5527 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5528 | CXXRecordDecl *PrevDecl |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5529 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5530 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5531 | PrevDecl = Record; |
| 5532 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5533 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5534 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5535 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5536 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5537 | PrevDecl, |
| 5538 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5539 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5540 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5541 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5542 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5543 | return TagD; |
| 5544 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5545 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5546 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5547 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5548 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5549 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5550 | // 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] | 5551 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5552 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5553 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5554 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 5555 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 5556 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5557 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 5558 | << Pattern; |
| 5559 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5560 | } else { |
| 5561 | if (InstantiateClass(NameLoc, Record, Def, |
| 5562 | getTemplateInstantiationArgs(Record), |
| 5563 | TSK)) |
| 5564 | return true; |
| 5565 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5566 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5567 | if (!RecordDef) |
| 5568 | return true; |
| 5569 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5572 | // Instantiate all of the members of the class. |
| 5573 | InstantiateClassMembers(NameLoc, RecordDef, |
| 5574 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5575 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5576 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 5577 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 5578 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5579 | // FIXME: We don't have any representation for explicit instantiations of |
| 5580 | // member classes. Such a representation is not needed for compilation, but it |
| 5581 | // should be available for clients that want to see all of the declarations in |
| 5582 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5583 | return TagD; |
| 5584 | } |
| 5585 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5586 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 5587 | SourceLocation ExternLoc, |
| 5588 | SourceLocation TemplateLoc, |
| 5589 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5590 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5591 | // TODO: check if/when DNInfo should replace Name. |
| 5592 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 5593 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5594 | if (!Name) { |
| 5595 | if (!D.isInvalidType()) |
| 5596 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 5597 | diag::err_explicit_instantiation_requires_name) |
| 5598 | << D.getDeclSpec().getSourceRange() |
| 5599 | << D.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5600 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5601 | return true; |
| 5602 | } |
| 5603 | |
| 5604 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 5605 | // we find one that is. |
| 5606 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5607 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5608 | S = S->getParent(); |
| 5609 | |
| 5610 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 5611 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 5612 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5613 | if (R.isNull()) |
| 5614 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5615 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5616 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 5617 | // Cannot explicitly instantiate a typedef. |
| 5618 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 5619 | << Name; |
| 5620 | return true; |
| 5621 | } |
| 5622 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5623 | // C++0x [temp.explicit]p1: |
| 5624 | // [...] An explicit instantiation of a function template shall not use the |
| 5625 | // inline or constexpr specifiers. |
| 5626 | // Presumably, this also applies to member functions of class templates as |
| 5627 | // well. |
| 5628 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5629 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5630 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5631 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5632 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5633 | // FIXME: check for constexpr specifier. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5634 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5635 | // C++0x [temp.explicit]p2: |
| 5636 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5637 | // definition and an explicit instantiation declaration. An explicit |
| 5638 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5639 | TemplateSpecializationKind TSK |
| 5640 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5641 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5642 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5643 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5644 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5645 | |
| 5646 | if (!R->isFunctionType()) { |
| 5647 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5648 | // A [...] static data member of a class template can be explicitly |
| 5649 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5650 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5651 | if (Previous.isAmbiguous()) |
| 5652 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5653 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 5654 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5655 | if (!Prev || !Prev->isStaticDataMember()) { |
| 5656 | // We expect to see a data data member here. |
| 5657 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 5658 | << Name; |
| 5659 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5660 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5661 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5662 | return true; |
| 5663 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5664 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5665 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 5666 | // FIXME: Check for explicit specialization? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5667 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5668 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 5669 | << Prev; |
| 5670 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 5671 | // FIXME: Can we provide a note showing where this was declared? |
| 5672 | return true; |
| 5673 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5674 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5675 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5676 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5677 | // or a static data member of a class template specialization, the name of |
| 5678 | // the class template specialization in the qualified-id for the member |
| 5679 | // name shall be a simple-template-id. |
| 5680 | // |
| 5681 | // C++98 has the same restriction, just worded differently. |
| 5682 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5683 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5684 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5685 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5686 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5687 | // Check the scope of this explicit instantiation. |
| 5688 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5689 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5690 | // Verify that it is okay to explicitly instantiate here. |
| 5691 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 5692 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5693 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5694 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5695 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5696 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5697 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5698 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5699 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5700 | return (Decl*) 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5701 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5702 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5703 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5704 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5705 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5706 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5707 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5708 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5709 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5710 | |
| 5711 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5712 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5713 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5714 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5715 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 5716 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5717 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 5718 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5719 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 5720 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5721 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5722 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5723 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 5724 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5725 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5726 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5727 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5728 | // A [...] function [...] can be explicitly instantiated from its template. |
| 5729 | // A member function [...] of a class template can be explicitly |
| 5730 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5731 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5732 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5733 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5734 | P != PEnd; ++P) { |
| 5735 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5736 | if (!HasExplicitTemplateArgs) { |
| 5737 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 5738 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 5739 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5740 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5741 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5742 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 5743 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5744 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5745 | } |
| 5746 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5747 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5748 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 5749 | if (!FunTmpl) |
| 5750 | continue; |
| 5751 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5752 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5753 | FunctionDecl *Specialization = 0; |
| 5754 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5755 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5756 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5757 | R, Specialization, Info)) { |
| 5758 | // FIXME: Keep track of almost-matches? |
| 5759 | (void)TDK; |
| 5760 | continue; |
| 5761 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5762 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5763 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5764 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5765 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5766 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5767 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5768 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5769 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5770 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 5771 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 5772 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5773 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5774 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5775 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5776 | |
| 5777 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 5778 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5779 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5780 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5781 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5782 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 5783 | << Specialization |
| 5784 | << (Specialization->getTemplateSpecializationKind() == |
| 5785 | TSK_ExplicitSpecialization); |
| 5786 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 5787 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5788 | } |
| 5789 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5790 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5791 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 5792 | PrevDecl = Specialization; |
| 5793 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5794 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5795 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5796 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5797 | PrevDecl, |
| 5798 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5799 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5800 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5801 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5802 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5803 | // FIXME: We may still want to build some representation of this |
| 5804 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5805 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5806 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5807 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 5808 | |
| 5809 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5810 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5811 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5812 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5813 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5814 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5815 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5816 | // or a static data member of a class template specialization, the name of |
| 5817 | // the class template specialization in the qualified-id for the member |
| 5818 | // name shall be a simple-template-id. |
| 5819 | // |
| 5820 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5821 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5822 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5823 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5824 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5825 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5826 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5827 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5828 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5829 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5830 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5831 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5832 | D.getIdentifierLoc(), |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5833 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5834 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5835 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5836 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5837 | } |
| 5838 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5839 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5840 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 5841 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 5842 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 5843 | // This has to hold, because SS is expected to be defined. |
| 5844 | assert(Name && "Expected a name in a dependent tag"); |
| 5845 | |
| 5846 | NestedNameSpecifier *NNS |
| 5847 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5848 | if (!NNS) |
| 5849 | return true; |
| 5850 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5851 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 5852 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 5853 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 5854 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5855 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 5856 | return true; |
| 5857 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5858 | |
| 5859 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5860 | return ParsedType::make(Context.getDependentNameType(Kwd, NNS, Name)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5861 | } |
| 5862 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5863 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5864 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 5865 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5866 | SourceLocation IdLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5867 | NestedNameSpecifier *NNS |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5868 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5869 | if (!NNS) |
| 5870 | return true; |
| 5871 | |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5872 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 5873 | !getLangOptions().CPlusPlus0x) |
| 5874 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5875 | << FixItHint::CreateRemoval(TypenameLoc); |
| 5876 | |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5877 | QualType T = CheckTypenameType(ETK_Typename, NNS, II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5878 | TypenameLoc, SS.getRange(), IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 5879 | if (T.isNull()) |
| 5880 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5881 | |
| 5882 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 5883 | if (isa<DependentNameType>(T)) { |
| 5884 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5885 | TL.setKeywordLoc(TypenameLoc); |
| 5886 | TL.setQualifierRange(SS.getRange()); |
| 5887 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5888 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5889 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5890 | TL.setKeywordLoc(TypenameLoc); |
| 5891 | TL.setQualifierRange(SS.getRange()); |
| 5892 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5893 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5894 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5895 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5896 | } |
| 5897 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5898 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5899 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 5900 | const CXXScopeSpec &SS, SourceLocation TemplateLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5901 | ParsedType Ty) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5902 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 5903 | !getLangOptions().CPlusPlus0x) |
| 5904 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5905 | << FixItHint::CreateRemoval(TypenameLoc); |
| 5906 | |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5907 | TypeSourceInfo *InnerTSI = 0; |
| 5908 | QualType T = GetTypeFromParser(Ty, &InnerTSI); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5909 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5910 | assert(isa<TemplateSpecializationType>(T) && |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5911 | "Expected a template specialization type"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5912 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5913 | if (computeDeclContext(SS, false)) { |
| 5914 | // If we can compute a declaration context, then the "typename" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5915 | // keyword was superfluous. Just build an ElaboratedType to keep |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5916 | // track of the nested-name-specifier. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5917 | |
| 5918 | // Push the inner type, preserving its source locations if possible. |
| 5919 | TypeLocBuilder Builder; |
| 5920 | if (InnerTSI) |
| 5921 | Builder.pushFullCopy(InnerTSI->getTypeLoc()); |
| 5922 | else |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 5923 | Builder.push<TemplateSpecializationTypeLoc>(T).initialize(Context, |
| 5924 | TemplateLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5925 | |
Abramo Bagnara | 22f638a | 2010-08-10 13:46:45 +0000 | [diff] [blame] | 5926 | /* Note: NNS already embedded in template specialization type T. */ |
| 5927 | T = Context.getElaboratedType(ETK_Typename, /*NNS=*/0, T); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5928 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 5929 | TL.setKeywordLoc(TypenameLoc); |
| 5930 | TL.setQualifierRange(SS.getRange()); |
| 5931 | |
| 5932 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5933 | return CreateParsedType(T, TSI); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5934 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5935 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5936 | // TODO: it's really silly that we make a template specialization |
| 5937 | // type earlier only to drop it again here. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5938 | const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5939 | DependentTemplateName *DTN = |
| 5940 | TST->getTemplateName().getAsDependentTemplateName(); |
| 5941 | assert(DTN && "dependent template has non-dependent name?"); |
Abramo Bagnara | 22f638a | 2010-08-10 13:46:45 +0000 | [diff] [blame] | 5942 | assert(DTN->getQualifier() |
| 5943 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 5944 | T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 5945 | DTN->getQualifier(), |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5946 | DTN->getIdentifier(), |
| 5947 | TST->getNumArgs(), |
| 5948 | TST->getArgs()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5949 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5950 | DependentTemplateSpecializationTypeLoc TL = |
| 5951 | cast<DependentTemplateSpecializationTypeLoc>(TSI->getTypeLoc()); |
| 5952 | if (InnerTSI) { |
| 5953 | TemplateSpecializationTypeLoc TSTL = |
| 5954 | cast<TemplateSpecializationTypeLoc>(InnerTSI->getTypeLoc()); |
| 5955 | TL.setLAngleLoc(TSTL.getLAngleLoc()); |
| 5956 | TL.setRAngleLoc(TSTL.getRAngleLoc()); |
| 5957 | for (unsigned I = 0, E = TST->getNumArgs(); I != E; ++I) |
| 5958 | TL.setArgLocInfo(I, TSTL.getArgLocInfo(I)); |
| 5959 | } else { |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 5960 | // FIXME: Poor source-location information here. |
| 5961 | TL.initializeLocal(Context, TemplateLoc); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5962 | } |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5963 | TL.setKeywordLoc(TypenameLoc); |
| 5964 | TL.setQualifierRange(SS.getRange()); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5965 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5966 | } |
| 5967 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5968 | /// \brief Build the type that describes a C++ typename specifier, |
| 5969 | /// e.g., "typename T::type". |
| 5970 | QualType |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5971 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 5972 | NestedNameSpecifier *NNS, const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5973 | SourceLocation KeywordLoc, SourceRange NNSRange, |
| 5974 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5975 | CXXScopeSpec SS; |
| 5976 | SS.setScopeRep(NNS); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5977 | SS.setRange(NNSRange); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5978 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5979 | DeclContext *Ctx = computeDeclContext(SS); |
| 5980 | if (!Ctx) { |
| 5981 | // If the nested-name-specifier is dependent and couldn't be |
| 5982 | // resolved to a type, build a typename type. |
| 5983 | assert(NNS->isDependent()); |
| 5984 | return Context.getDependentNameType(Keyword, NNS, &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5985 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5986 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5987 | // If the nested-name-specifier refers to the current instantiation, |
| 5988 | // the "typename" keyword itself is superfluous. In C++03, the |
| 5989 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 5990 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 5991 | // 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] | 5992 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5993 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 5994 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5995 | |
| 5996 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5997 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5998 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5999 | unsigned DiagID = 0; |
| 6000 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6001 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6002 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6003 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6004 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6005 | |
| 6006 | case LookupResult::FoundUnresolvedValue: { |
| 6007 | // We found a using declaration that is a value. Most likely, the using |
| 6008 | // declaration itself is meant to have the 'typename' keyword. |
| 6009 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(), |
| 6010 | IILoc); |
| 6011 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 6012 | << Name << Ctx << FullRange; |
| 6013 | if (UnresolvedUsingValueDecl *Using |
| 6014 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
| 6015 | SourceLocation Loc = Using->getTargetNestedNameRange().getBegin(); |
| 6016 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 6017 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 6018 | } |
| 6019 | } |
| 6020 | // Fall through to create a dependent typename type, from which we can recover |
| 6021 | // better. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6022 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 6023 | case LookupResult::NotFoundInCurrentInstantiation: |
| 6024 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 6025 | return Context.getDependentNameType(Keyword, NNS, &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6026 | |
| 6027 | case LookupResult::Found: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6028 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6029 | // We found a type. Build an ElaboratedType, since the |
| 6030 | // typename-specifier was just sugar. |
| 6031 | return Context.getElaboratedType(ETK_Typename, NNS, |
| 6032 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6033 | } |
| 6034 | |
| 6035 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6036 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6037 | break; |
| 6038 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6039 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6040 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6041 | return QualType(); |
| 6042 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6043 | case LookupResult::FoundOverloaded: |
| 6044 | DiagID = diag::err_typename_nested_not_type; |
| 6045 | Referenced = *Result.begin(); |
| 6046 | break; |
| 6047 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 6048 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6049 | return QualType(); |
| 6050 | } |
| 6051 | |
| 6052 | // If we get here, it's because name lookup did not find a |
| 6053 | // type. Emit an appropriate diagnostic and return an error. |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6054 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(), |
| 6055 | IILoc); |
| 6056 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6057 | if (Referenced) |
| 6058 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 6059 | << Name; |
| 6060 | return QualType(); |
| 6061 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6062 | |
| 6063 | namespace { |
| 6064 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 6065 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6066 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6067 | SourceLocation Loc; |
| 6068 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6069 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6070 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 6071 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6072 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6073 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6074 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6075 | DeclarationName Entity) |
| 6076 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6077 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6078 | |
| 6079 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6080 | /// transformed. |
| 6081 | /// |
| 6082 | /// For the purposes of type reconstruction, a type has already been |
| 6083 | /// transformed if it is NULL or if it is not dependent. |
| 6084 | bool AlreadyTransformed(QualType T) { |
| 6085 | return T.isNull() || !T->isDependentType(); |
| 6086 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6087 | |
| 6088 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6089 | /// rebuilt. |
| 6090 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6091 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6092 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 6093 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6094 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6095 | /// \brief Sets the "base" location and entity when that |
| 6096 | /// information is known based on another transformation. |
| 6097 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 6098 | this->Loc = Loc; |
| 6099 | this->Entity = Entity; |
| 6100 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6101 | }; |
| 6102 | } |
| 6103 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6104 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 6105 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6106 | /// 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] | 6107 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6108 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6109 | /// partial specialization thereof). This routine will rebuild that type now |
| 6110 | /// that we have entered the declarator's scope, which may produce different |
| 6111 | /// canonical types, e.g., |
| 6112 | /// |
| 6113 | /// \code |
| 6114 | /// template<typename T> |
| 6115 | /// struct X { |
| 6116 | /// typedef T* pointer; |
| 6117 | /// pointer data(); |
| 6118 | /// }; |
| 6119 | /// |
| 6120 | /// template<typename T> |
| 6121 | /// typename X<T>::pointer X<T>::data() { ... } |
| 6122 | /// \endcode |
| 6123 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 6124 | /// 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] | 6125 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 6126 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6127 | /// 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] | 6128 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 6129 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6130 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 6131 | SourceLocation Loc, |
| 6132 | DeclarationName Name) { |
| 6133 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6134 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6135 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6136 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 6137 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 6138 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6139 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6140 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6141 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 6142 | DeclarationName()); |
| 6143 | return Rebuilder.TransformExpr(E); |
| 6144 | } |
| 6145 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6146 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
| 6147 | if (SS.isInvalid()) return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6148 | |
| 6149 | NestedNameSpecifier *NNS = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 6150 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 6151 | DeclarationName()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6152 | NestedNameSpecifier *Rebuilt = |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6153 | Rebuilder.TransformNestedNameSpecifier(NNS, SS.getRange()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6154 | if (!Rebuilt) return true; |
| 6155 | |
| 6156 | SS.setScopeRep(Rebuilt); |
| 6157 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6158 | } |
| 6159 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6160 | /// \brief Produces a formatted string that describes the binding of |
| 6161 | /// template parameters to template arguments. |
| 6162 | std::string |
| 6163 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6164 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6165 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6166 | } |
| 6167 | |
| 6168 | std::string |
| 6169 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6170 | const TemplateArgument *Args, |
| 6171 | unsigned NumArgs) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6172 | llvm::SmallString<128> Str; |
| 6173 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6174 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6175 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6176 | return std::string(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6177 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6178 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6179 | if (I >= NumArgs) |
| 6180 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6181 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6182 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6183 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6184 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6185 | Out << ", "; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6186 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6187 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6188 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6189 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6190 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6191 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6192 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6193 | Out << " = "; |
| 6194 | Args[I].print(Context.PrintingPolicy, Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6195 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6196 | |
| 6197 | Out << ']'; |
| 6198 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6199 | } |