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) { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 371 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 372 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 373 | if (!isAddressOfOperand && |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 374 | isa<CXXMethodDecl>(DC) && |
| 375 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 376 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 377 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 378 | // Since the 'this' expression is synthesized, we don't need to |
| 379 | // perform the double-lookup check. |
| 380 | NamedDecl *FirstQualifierInScope = 0; |
| 381 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 382 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 383 | /*This*/ 0, ThisType, |
| 384 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 385 | /*Op*/ SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 386 | SS.getWithLocInContext(Context), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 387 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 388 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 389 | TemplateArgs)); |
| 390 | } |
| 391 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 392 | return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 393 | } |
| 394 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 395 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 396 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 397 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 398 | const TemplateArgumentListInfo *TemplateArgs) { |
| 399 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 400 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 401 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 402 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 405 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 406 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 407 | /// declaration at location Loc. Returns true to indicate that this is |
| 408 | /// an error, and false otherwise. |
| 409 | bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 410 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 411 | |
| 412 | // Microsoft Visual C++ permits template parameters to be shadowed. |
| 413 | if (getLangOptions().Microsoft) |
| 414 | return false; |
| 415 | |
| 416 | // C++ [temp.local]p4: |
| 417 | // A template-parameter shall not be redeclared within its |
| 418 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 420 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 421 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
| 422 | return true; |
| 423 | } |
| 424 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 425 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 426 | /// the parameter D to reference the templated declaration and return a pointer |
| 427 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 428 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 429 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 430 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 431 | return Temp; |
| 432 | } |
| 433 | return 0; |
| 434 | } |
| 435 | |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 436 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 437 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 438 | assert(Kind == Template && |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 439 | "Only template template arguments can be pack expansions here"); |
| 440 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 441 | "Template template argument pack expansion without packs"); |
| 442 | ParsedTemplateArgument Result(*this); |
| 443 | Result.EllipsisLoc = EllipsisLoc; |
| 444 | return Result; |
| 445 | } |
| 446 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 447 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 448 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 449 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 450 | switch (Arg.getKind()) { |
| 451 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 452 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 453 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 454 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 455 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 456 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 457 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 458 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 459 | case ParsedTemplateArgument::NonType: { |
| 460 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 461 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 462 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 463 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 464 | case ParsedTemplateArgument::Template: { |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 465 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 466 | TemplateArgument TArg; |
| 467 | if (Arg.getEllipsisLoc().isValid()) |
| 468 | TArg = TemplateArgument(Template, llvm::Optional<unsigned int>()); |
| 469 | else |
| 470 | TArg = Template; |
| 471 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 472 | Arg.getScopeSpec().getWithLocInContext( |
| 473 | SemaRef.Context), |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 474 | Arg.getLocation(), |
| 475 | Arg.getEllipsisLoc()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 478 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 479 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 480 | return TemplateArgumentLoc(); |
| 481 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 482 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 483 | /// \brief Translates template arguments as provided by the parser |
| 484 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 485 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 486 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 487 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 488 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 489 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 490 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 491 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 492 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 493 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 494 | /// the keyword "typename" was used to declare the type parameter |
| 495 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 496 | /// "class" or "typename" keyword. ParamName is the name of the |
| 497 | /// parameter (NULL indicates an unnamed template parameter) and |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 498 | /// ParamName is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 499 | /// If the type parameter has a default argument, it will be added |
| 500 | /// later via ActOnTypeParameterDefault. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 501 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
| 502 | SourceLocation EllipsisLoc, |
| 503 | SourceLocation KeyLoc, |
| 504 | IdentifierInfo *ParamName, |
| 505 | SourceLocation ParamNameLoc, |
| 506 | unsigned Depth, unsigned Position, |
| 507 | SourceLocation EqualLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 508 | ParsedType DefaultArg) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | assert(S->isTemplateParamScope() && |
| 510 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 511 | bool Invalid = false; |
| 512 | |
| 513 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 514 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 515 | LookupOrdinaryName, |
| 516 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 517 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 518 | Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 522 | SourceLocation Loc = ParamNameLoc; |
| 523 | if (!ParamName) |
| 524 | Loc = KeyLoc; |
| 525 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 526 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 527 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 528 | KeyLoc, Loc, Depth, Position, ParamName, |
| 529 | Typename, Ellipsis); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 530 | Param->setAccess(AS_public); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 531 | if (Invalid) |
| 532 | Param->setInvalidDecl(); |
| 533 | |
| 534 | if (ParamName) { |
| 535 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 536 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 537 | IdResolver.AddDecl(Param); |
| 538 | } |
| 539 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 540 | // C++0x [temp.param]p9: |
| 541 | // A default template-argument may be specified for any kind of |
| 542 | // template-parameter that is not a template parameter pack. |
| 543 | if (DefaultArg && Ellipsis) { |
| 544 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 545 | DefaultArg = ParsedType(); |
| 546 | } |
| 547 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 548 | // Handle the default argument, if provided. |
| 549 | if (DefaultArg) { |
| 550 | TypeSourceInfo *DefaultTInfo; |
| 551 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 552 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 553 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 554 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 555 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 556 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 557 | UPPC_DefaultArgument)) |
| 558 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 559 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 560 | // Check the template argument itself. |
| 561 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 562 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 563 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 564 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 565 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 566 | Param->setDefaultArgument(DefaultTInfo, false); |
| 567 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 568 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 569 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 572 | /// \brief Check that the type of a non-type template parameter is |
| 573 | /// well-formed. |
| 574 | /// |
| 575 | /// \returns the (possibly-promoted) parameter type if valid; |
| 576 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 578 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 579 | // We don't allow variably-modified types as the type of non-type template |
| 580 | // parameters. |
| 581 | if (T->isVariablyModifiedType()) { |
| 582 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 583 | << T; |
| 584 | return QualType(); |
| 585 | } |
| 586 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 587 | // C++ [temp.param]p4: |
| 588 | // |
| 589 | // A non-type template-parameter shall have one of the following |
| 590 | // (optionally cv-qualified) types: |
| 591 | // |
| 592 | // -- integral or enumeration type, |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 593 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | // -- pointer to object or pointer to function, |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 595 | T->isPointerType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 597 | T->isReferenceType() || |
| 598 | // -- pointer to member. |
| 599 | T->isMemberPointerType() || |
| 600 | // If T is a dependent type, we can't do the check now, so we |
| 601 | // assume that it is well-formed. |
| 602 | T->isDependentType()) |
| 603 | return T; |
| 604 | // C++ [temp.param]p8: |
| 605 | // |
| 606 | // A non-type template-parameter of type "array of T" or |
| 607 | // "function returning T" is adjusted to be of type "pointer to |
| 608 | // T" or "pointer to function returning T", respectively. |
| 609 | else if (T->isArrayType()) |
| 610 | // FIXME: Keep the type prior to promotion? |
| 611 | return Context.getArrayDecayedType(T); |
| 612 | else if (T->isFunctionType()) |
| 613 | // FIXME: Keep the type prior to promotion? |
| 614 | return Context.getPointerType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 615 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 616 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 617 | << T; |
| 618 | |
| 619 | return QualType(); |
| 620 | } |
| 621 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 622 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 623 | unsigned Depth, |
| 624 | unsigned Position, |
| 625 | SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 626 | Expr *Default) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 627 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 628 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 629 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 630 | assert(S->isTemplateParamScope() && |
| 631 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 632 | bool Invalid = false; |
| 633 | |
| 634 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 635 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 636 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 637 | LookupOrdinaryName, |
| 638 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 639 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 640 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 641 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 644 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 645 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 646 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 647 | Invalid = true; |
| 648 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 649 | |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 650 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 651 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 652 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 653 | D.getSourceRange().getBegin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 654 | D.getIdentifierLoc(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 655 | Depth, Position, ParamName, T, |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 656 | IsParameterPack, TInfo); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 657 | Param->setAccess(AS_public); |
| 658 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 659 | if (Invalid) |
| 660 | Param->setInvalidDecl(); |
| 661 | |
| 662 | if (D.getIdentifier()) { |
| 663 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 664 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 665 | IdResolver.AddDecl(Param); |
| 666 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 667 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 668 | // C++0x [temp.param]p9: |
| 669 | // A default template-argument may be specified for any kind of |
| 670 | // template-parameter that is not a template parameter pack. |
| 671 | if (Default && IsParameterPack) { |
| 672 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 673 | Default = 0; |
| 674 | } |
| 675 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 676 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 677 | if (Default) { |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 678 | // Check for unexpanded parameter packs. |
| 679 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 680 | return Param; |
| 681 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 682 | TemplateArgument Converted; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 683 | ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
| 684 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 685 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 686 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 687 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 688 | Default = DefaultRes.take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 689 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 690 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 691 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 692 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 693 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 694 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 695 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 696 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 697 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 698 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 699 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 700 | SourceLocation TmpLoc, |
| 701 | TemplateParamsTy *Params, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 702 | SourceLocation EllipsisLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 703 | IdentifierInfo *Name, |
| 704 | SourceLocation NameLoc, |
| 705 | unsigned Depth, |
| 706 | unsigned Position, |
| 707 | SourceLocation EqualLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 708 | ParsedTemplateArgument Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 709 | assert(S->isTemplateParamScope() && |
| 710 | "Template template parameter not in template parameter scope!"); |
| 711 | |
| 712 | // Construct the parameter object. |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 713 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 714 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 715 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 716 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 717 | Depth, Position, IsParameterPack, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 718 | Name, Params); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 719 | Param->setAccess(AS_public); |
| 720 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 721 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 722 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 723 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 724 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 725 | IdResolver.AddDecl(Param); |
| 726 | } |
| 727 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 728 | if (Params->size() == 0) { |
| 729 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 730 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 731 | Param->setInvalidDecl(); |
| 732 | } |
| 733 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 734 | // C++0x [temp.param]p9: |
| 735 | // A default template-argument may be specified for any kind of |
| 736 | // template-parameter that is not a template parameter pack. |
| 737 | if (IsParameterPack && !Default.isInvalid()) { |
| 738 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 739 | Default = ParsedTemplateArgument(); |
| 740 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 741 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 742 | if (!Default.isInvalid()) { |
| 743 | // Check only that we have a template template argument. We don't want to |
| 744 | // try to check well-formedness now, because our template template parameter |
| 745 | // might have dependent types in its template parameters, which we wouldn't |
| 746 | // be able to match now. |
| 747 | // |
| 748 | // If none of the template template parameter's template arguments mention |
| 749 | // other template parameters, we could actually perform more checking here. |
| 750 | // However, it isn't worth doing. |
| 751 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 752 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 753 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 754 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 755 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 756 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 757 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 758 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 759 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 760 | DefaultArg.getArgument().getAsTemplate(), |
| 761 | UPPC_DefaultArgument)) |
| 762 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 763 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 764 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 765 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 766 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 767 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 770 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 771 | /// contains the template parameters in Params/NumParams. |
| 772 | Sema::TemplateParamsTy * |
| 773 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 774 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 775 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 776 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 777 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 778 | SourceLocation RAngleLoc) { |
| 779 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 780 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 781 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 782 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 783 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 784 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 785 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 786 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 787 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 788 | if (SS.isSet()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 789 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 790 | } |
| 791 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 792 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 793 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 794 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 795 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 796 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 797 | TemplateParameterList *TemplateParams, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 798 | AccessSpecifier AS, |
| 799 | unsigned NumOuterTemplateParamLists, |
| 800 | TemplateParameterList** OuterTemplateParamLists) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 801 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 802 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 803 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 804 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 805 | |
| 806 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 807 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 808 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 809 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 810 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 811 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 812 | |
| 813 | // There is no such thing as an unnamed class template. |
| 814 | if (!Name) { |
| 815 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 816 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 820 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 821 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 822 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 823 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 824 | SemanticContext = computeDeclContext(SS, true); |
| 825 | if (!SemanticContext) { |
| 826 | // FIXME: Produce a reasonable diagnostic here |
| 827 | return true; |
| 828 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 830 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 831 | return true; |
| 832 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 833 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 834 | } else { |
| 835 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 836 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 837 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 839 | if (Previous.isAmbiguous()) |
| 840 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 841 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 842 | NamedDecl *PrevDecl = 0; |
| 843 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 844 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 845 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 846 | // If there is a previous declaration with the same name, check |
| 847 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 848 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 849 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 850 | |
| 851 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 852 | // class template partial specialization, or class template specialization. |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 853 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 854 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 855 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 856 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 857 | PrevClassTemplate |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 858 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 859 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 860 | PrevClassTemplate |
| 861 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 862 | ->getSpecializedTemplate(); |
| 863 | } |
| 864 | } |
| 865 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 866 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 867 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 868 | // [...] When looking for a prior declaration of a class or a function |
| 869 | // 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] | 870 | // function is neither a qualified name nor a template-id, scopes outside |
| 871 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 872 | if (!SS.isSet()) { |
| 873 | DeclContext *OutermostContext = CurContext; |
| 874 | while (!OutermostContext->isFileContext()) |
| 875 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 876 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 877 | if (PrevDecl && |
| 878 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 879 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 880 | SemanticContext = PrevDecl->getDeclContext(); |
| 881 | } else { |
| 882 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 883 | // context we computed is the semantic context for our new |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 884 | // declaration. |
| 885 | PrevDecl = PrevClassTemplate = 0; |
| 886 | SemanticContext = OutermostContext; |
| 887 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 888 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 889 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 890 | if (CurContext->isDependentContext()) { |
| 891 | // If this is a dependent context, we don't want to link the friend |
| 892 | // class template to the template in scope, because that would perform |
| 893 | // checking of the template parameter lists that can't be performed |
| 894 | // until the outer context is instantiated. |
| 895 | PrevDecl = PrevClassTemplate = 0; |
| 896 | } |
| 897 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 898 | PrevDecl = PrevClassTemplate = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 899 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 900 | if (PrevClassTemplate) { |
| 901 | // Ensure that the template parameter lists are compatible. |
| 902 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 903 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 904 | /*Complain=*/true, |
| 905 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 906 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 907 | |
| 908 | // C++ [temp.class]p4: |
| 909 | // In a redeclaration, partial specialization, explicit |
| 910 | // specialization or explicit instantiation of a class template, |
| 911 | // the class-key shall agree in kind with the original class |
| 912 | // template declaration (7.1.5.3). |
| 913 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 914 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 915 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 916 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 917 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 918 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 919 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 922 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 923 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 924 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 925 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 926 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 927 | // FIXME: Would it make sense to try to "forget" the previous |
| 928 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 929 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 933 | // Maybe we will complain about the shadowed template parameter. |
| 934 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 935 | // Just pretend that we didn't see the previous declaration. |
| 936 | PrevDecl = 0; |
| 937 | } else if (PrevDecl) { |
| 938 | // C++ [temp]p5: |
| 939 | // A class template shall not have the same name as any other |
| 940 | // template, class, function, object, enumeration, enumerator, |
| 941 | // namespace, or type in the same scope (3.3), except as specified |
| 942 | // in (14.5.4). |
| 943 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 944 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 945 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 948 | // Check the template parameter list of this declaration, possibly |
| 949 | // merging in the template parameter list from the previous class |
| 950 | // template declaration. |
| 951 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 952 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 953 | (SS.isSet() && SemanticContext && |
Douglas Gregor | 461bf2e | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 954 | SemanticContext->isRecord() && |
| 955 | SemanticContext->isDependentContext()) |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 956 | ? TPC_ClassTemplateMember |
| 957 | : TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 958 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 960 | if (SS.isSet()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 961 | // 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] | 962 | // template out-of-line. |
| 963 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
| 964 | !(TUK == TUK_Friend && CurContext->isDependentContext())) |
| 965 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 966 | << Name << SemanticContext << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | CXXRecordDecl *NewClass = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 970 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 972 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 973 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 974 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 975 | if (NumOuterTemplateParamLists > 0) |
| 976 | NewClass->setTemplateParameterListsInfo(Context, |
| 977 | NumOuterTemplateParamLists, |
| 978 | OuterTemplateParamLists); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 979 | |
| 980 | ClassTemplateDecl *NewTemplate |
| 981 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 982 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 983 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 984 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 985 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 986 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 987 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 988 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 989 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 990 | (void)T; |
| 991 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 992 | // 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] | 993 | // class template, make a note of that. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 994 | if (PrevClassTemplate && |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 995 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 996 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 997 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 998 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 999 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1000 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1002 | // Set the lexical context of these templates |
| 1003 | NewClass->setLexicalDeclContext(CurContext); |
| 1004 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1005 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1006 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1007 | NewClass->startDefinition(); |
| 1008 | |
| 1009 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1010 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1011 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1012 | if (TUK != TUK_Friend) |
| 1013 | PushOnScopeChains(NewTemplate, S); |
| 1014 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1015 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1016 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1017 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1018 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1019 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1020 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1021 | PrevClassTemplate != NULL); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1022 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1023 | // Friend templates are visible in fairly strange ways. |
| 1024 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1025 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1026 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 1027 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1028 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1029 | /* AddToContext = */ false); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1030 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1031 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1032 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1033 | NewClass->getLocation(), |
| 1034 | NewTemplate, |
| 1035 | /*FIXME:*/NewClass->getLocation()); |
| 1036 | Friend->setAccess(AS_public); |
| 1037 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1038 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1039 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1040 | if (Invalid) { |
| 1041 | NewTemplate->setInvalidDecl(); |
| 1042 | NewClass->setInvalidDecl(); |
| 1043 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1044 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1047 | /// \brief Diagnose the presence of a default template argument on a |
| 1048 | /// template parameter, which is ill-formed in certain contexts. |
| 1049 | /// |
| 1050 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1051 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1052 | Sema::TemplateParamListContext TPC, |
| 1053 | SourceLocation ParamLoc, |
| 1054 | SourceRange DefArgRange) { |
| 1055 | switch (TPC) { |
| 1056 | case Sema::TPC_ClassTemplate: |
| 1057 | return false; |
| 1058 | |
| 1059 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1060 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1061 | // C++ [temp.param]p9: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1062 | // A default template-argument shall not be specified in a |
| 1063 | // function template declaration or a function template |
| 1064 | // definition [...] |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1065 | // If a friend function template declaration specifies a default |
| 1066 | // template-argument, that declaration shall be a definition and shall be |
| 1067 | // the only declaration of the function template in the translation unit. |
| 1068 | // (C++98/03 doesn't have this wording; see DR226). |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1069 | if (!S.getLangOptions().CPlusPlus0x) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1070 | S.Diag(ParamLoc, |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1071 | diag::ext_template_parameter_default_in_function_template) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1072 | << DefArgRange; |
| 1073 | return false; |
| 1074 | |
| 1075 | case Sema::TPC_ClassTemplateMember: |
| 1076 | // C++0x [temp.param]p9: |
| 1077 | // A default template-argument shall not be specified in the |
| 1078 | // template-parameter-lists of the definition of a member of a |
| 1079 | // class template that appears outside of the member's class. |
| 1080 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1081 | << DefArgRange; |
| 1082 | return true; |
| 1083 | |
| 1084 | case Sema::TPC_FriendFunctionTemplate: |
| 1085 | // C++ [temp.param]p9: |
| 1086 | // A default template-argument shall not be specified in a |
| 1087 | // friend template declaration. |
| 1088 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1089 | << DefArgRange; |
| 1090 | return true; |
| 1091 | |
| 1092 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1093 | // for friend function templates if there is only a single |
| 1094 | // declaration (and it is a definition). Strange! |
| 1095 | } |
| 1096 | |
| 1097 | return false; |
| 1098 | } |
| 1099 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1100 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1101 | /// of a template template parameter, recursively. |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1102 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1103 | TemplateTemplateParmDecl *TTP) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1104 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1105 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1106 | NamedDecl *P = Params->getParam(I); |
| 1107 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1108 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1109 | NTTP->getTypeSourceInfo(), |
| 1110 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1111 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1112 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1113 | continue; |
| 1114 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1115 | |
| 1116 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1117 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1118 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1119 | return true; |
| 1120 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1121 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1122 | return false; |
| 1123 | } |
| 1124 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1125 | /// \brief Checks the validity of a template parameter list, possibly |
| 1126 | /// considering the template parameter list from a previous |
| 1127 | /// declaration. |
| 1128 | /// |
| 1129 | /// If an "old" template parameter list is provided, it must be |
| 1130 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1131 | /// template parameter list. |
| 1132 | /// |
| 1133 | /// \param NewParams Template parameter list for a new template |
| 1134 | /// declaration. This template parameter list will be updated with any |
| 1135 | /// default arguments that are carried through from the previous |
| 1136 | /// template parameter list. |
| 1137 | /// |
| 1138 | /// \param OldParams If provided, template parameter list from a |
| 1139 | /// previous declaration of the same template. Default template |
| 1140 | /// arguments will be merged from the old template parameter list to |
| 1141 | /// the new template parameter list. |
| 1142 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1143 | /// \param TPC Describes the context in which we are checking the given |
| 1144 | /// template parameter list. |
| 1145 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1146 | /// \returns true if an error occurred, false otherwise. |
| 1147 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1148 | TemplateParameterList *OldParams, |
| 1149 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1150 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1151 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1152 | // C++ [temp.param]p10: |
| 1153 | // The set of default template-arguments available for use with a |
| 1154 | // template declaration or definition is obtained by merging the |
| 1155 | // default arguments from the definition (if in scope) and all |
| 1156 | // declarations in scope in the same way default function |
| 1157 | // arguments are (8.3.6). |
| 1158 | bool SawDefaultArgument = false; |
| 1159 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1160 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1161 | bool SawParameterPack = false; |
| 1162 | SourceLocation ParameterPackLoc; |
| 1163 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1164 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1165 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1166 | if (OldParams) |
| 1167 | OldParam = OldParams->begin(); |
| 1168 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1169 | bool RemoveDefaultArguments = false; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1170 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1171 | NewParamEnd = NewParams->end(); |
| 1172 | NewParam != NewParamEnd; ++NewParam) { |
| 1173 | // Variables used to diagnose redundant default arguments |
| 1174 | bool RedundantDefaultArg = false; |
| 1175 | SourceLocation OldDefaultLoc; |
| 1176 | SourceLocation NewDefaultLoc; |
| 1177 | |
| 1178 | // Variables used to diagnose missing default arguments |
| 1179 | bool MissingDefaultArg = false; |
| 1180 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1181 | // C++0x [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1182 | // If a template parameter of a primary class template is a template |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1183 | // parameter pack, it shall be the last template parameter. |
| 1184 | if (SawParameterPack && TPC == TPC_ClassTemplate) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1185 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1186 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1187 | Invalid = true; |
| 1188 | } |
| 1189 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1190 | if (TemplateTypeParmDecl *NewTypeParm |
| 1191 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1192 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1193 | if (NewTypeParm->hasDefaultArgument() && |
| 1194 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1195 | NewTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1196 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1197 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1198 | NewTypeParm->removeDefaultArgument(); |
| 1199 | |
| 1200 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1201 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1202 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1203 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1204 | if (NewTypeParm->isParameterPack()) { |
| 1205 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1206 | "Parameter packs can't have a default argument!"); |
| 1207 | SawParameterPack = true; |
| 1208 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1210 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1211 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1212 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1213 | SawDefaultArgument = true; |
| 1214 | RedundantDefaultArg = true; |
| 1215 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1216 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1217 | // Merge the default argument from the old declaration to the |
| 1218 | // new declaration. |
| 1219 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1220 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1221 | true); |
| 1222 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1223 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1224 | SawDefaultArgument = true; |
| 1225 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1226 | } else if (SawDefaultArgument) |
| 1227 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1228 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1229 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1230 | // Check for unexpanded parameter packs. |
| 1231 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1232 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1233 | UPPC_NonTypeTemplateParameterType)) { |
| 1234 | Invalid = true; |
| 1235 | continue; |
| 1236 | } |
| 1237 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1238 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1239 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1240 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1241 | NewNonTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1242 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1243 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1246 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1247 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1248 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1249 | if (NewNonTypeParm->isParameterPack()) { |
| 1250 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1251 | "Parameter packs can't have a default argument!"); |
| 1252 | SawParameterPack = true; |
| 1253 | ParameterPackLoc = NewNonTypeParm->getLocation(); |
| 1254 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1255 | NewNonTypeParm->hasDefaultArgument()) { |
| 1256 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1257 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1258 | SawDefaultArgument = true; |
| 1259 | RedundantDefaultArg = true; |
| 1260 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1261 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1262 | // Merge the default argument from the old declaration to the |
| 1263 | // new declaration. |
| 1264 | SawDefaultArgument = true; |
| 1265 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1266 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1267 | // parameter. |
| 1268 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1269 | OldNonTypeParm->getDefaultArgument(), |
| 1270 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1271 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1272 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1273 | SawDefaultArgument = true; |
| 1274 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1275 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1276 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1277 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1278 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1279 | TemplateTemplateParmDecl *NewTemplateParm |
| 1280 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1281 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1282 | // Check for unexpanded parameter packs, recursively. |
| 1283 | if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
| 1284 | Invalid = true; |
| 1285 | continue; |
| 1286 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1287 | |
| 1288 | if (NewTemplateParm->hasDefaultArgument() && |
| 1289 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1290 | NewTemplateParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1291 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1292 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1293 | |
| 1294 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1295 | TemplateTemplateParmDecl *OldTemplateParm |
| 1296 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1297 | if (NewTemplateParm->isParameterPack()) { |
| 1298 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1299 | "Parameter packs can't have a default argument!"); |
| 1300 | SawParameterPack = true; |
| 1301 | ParameterPackLoc = NewTemplateParm->getLocation(); |
| 1302 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1303 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1304 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1305 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1306 | SawDefaultArgument = true; |
| 1307 | RedundantDefaultArg = true; |
| 1308 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1309 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1310 | // Merge the default argument from the old declaration to the |
| 1311 | // new declaration. |
| 1312 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1313 | // FIXME: We need to create a new kind of "default argument" expression |
| 1314 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1315 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1316 | OldTemplateParm->getDefaultArgument(), |
| 1317 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1318 | PreviousDefaultArgLoc |
| 1319 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1320 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1321 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1322 | PreviousDefaultArgLoc |
| 1323 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1324 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1325 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | if (RedundantDefaultArg) { |
| 1329 | // C++ [temp.param]p12: |
| 1330 | // A template-parameter shall not be given default arguments |
| 1331 | // by two different declarations in the same scope. |
| 1332 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1333 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1334 | Invalid = true; |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1335 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1336 | // C++ [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1337 | // If a template-parameter of a class template has a default |
| 1338 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1339 | // have a default template-argument supplied or be a template parameter |
| 1340 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1342 | diag::err_template_param_default_arg_missing); |
| 1343 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1344 | Invalid = true; |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1345 | RemoveDefaultArguments = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | // If we have an old template parameter list that we're merging |
| 1349 | // in, move on to the next parameter. |
| 1350 | if (OldParams) |
| 1351 | ++OldParam; |
| 1352 | } |
| 1353 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1354 | // We were missing some default arguments at the end of the list, so remove |
| 1355 | // all of the default arguments. |
| 1356 | if (RemoveDefaultArguments) { |
| 1357 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1358 | NewParamEnd = NewParams->end(); |
| 1359 | NewParam != NewParamEnd; ++NewParam) { |
| 1360 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1361 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1362 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1363 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1364 | NTTP->removeDefaultArgument(); |
| 1365 | else |
| 1366 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1367 | } |
| 1368 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1369 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1370 | return Invalid; |
| 1371 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1372 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1373 | namespace { |
| 1374 | |
| 1375 | /// A class which looks for a use of a certain level of template |
| 1376 | /// parameter. |
| 1377 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1378 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1379 | |
| 1380 | unsigned Depth; |
| 1381 | bool Match; |
| 1382 | |
| 1383 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1384 | NamedDecl *ND = Params->getParam(0); |
| 1385 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1386 | Depth = PD->getDepth(); |
| 1387 | } else if (NonTypeTemplateParmDecl *PD = |
| 1388 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1389 | Depth = PD->getDepth(); |
| 1390 | } else { |
| 1391 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | bool Matches(unsigned ParmDepth) { |
| 1396 | if (ParmDepth >= Depth) { |
| 1397 | Match = true; |
| 1398 | return true; |
| 1399 | } |
| 1400 | return false; |
| 1401 | } |
| 1402 | |
| 1403 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1404 | return !Matches(T->getDepth()); |
| 1405 | } |
| 1406 | |
| 1407 | bool TraverseTemplateName(TemplateName N) { |
| 1408 | if (TemplateTemplateParmDecl *PD = |
| 1409 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1410 | if (Matches(PD->getDepth())) return false; |
| 1411 | return super::TraverseTemplateName(N); |
| 1412 | } |
| 1413 | |
| 1414 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1415 | if (NonTypeTemplateParmDecl *PD = |
| 1416 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1417 | if (PD->getDepth() == Depth) { |
| 1418 | Match = true; |
| 1419 | return false; |
| 1420 | } |
| 1421 | } |
| 1422 | return super::VisitDeclRefExpr(E); |
| 1423 | } |
| 1424 | }; |
| 1425 | } |
| 1426 | |
| 1427 | /// Determines whether a template-id depends on the given parameter |
| 1428 | /// list. |
| 1429 | static bool |
| 1430 | DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId, |
| 1431 | TemplateParameterList *Params) { |
| 1432 | DependencyChecker Checker(Params); |
| 1433 | Checker.TraverseType(QualType(TemplateId, 0)); |
| 1434 | return Checker.Match; |
| 1435 | } |
| 1436 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1437 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1438 | /// specifier, returning the template parameter list that applies to the |
| 1439 | /// name. |
| 1440 | /// |
| 1441 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1442 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1444 | /// \param SS the scope specifier that will be matched to the given template |
| 1445 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1446 | /// being declared. |
| 1447 | /// |
| 1448 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1449 | /// innermost template parameter lists. |
| 1450 | /// |
| 1451 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1452 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1453 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1454 | /// matching template parameters to scope specifiers in friend |
| 1455 | /// declarations. |
| 1456 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1457 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1458 | /// declared is an explicit specialization, false otherwise. |
| 1459 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1460 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1461 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1462 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1464 | /// template specialization), or may be NULL (if what we're declaring isn't |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1465 | /// itself a template). |
| 1466 | TemplateParameterList * |
| 1467 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
| 1468 | const CXXScopeSpec &SS, |
| 1469 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1470 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1471 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1472 | bool &IsExplicitSpecialization, |
| 1473 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1474 | IsExplicitSpecialization = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1475 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1476 | // Find the template-ids that occur within the nested-name-specifier. These |
| 1477 | // template-ids will match up with the template parameter lists. |
| 1478 | llvm::SmallVector<const TemplateSpecializationType *, 4> |
| 1479 | TemplateIdsInSpecifier; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1480 | llvm::SmallVector<ClassTemplateSpecializationDecl *, 4> |
| 1481 | ExplicitSpecializationsInSpecifier; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1482 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 1483 | NNS; NNS = NNS->getPrefix()) { |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1484 | const Type *T = NNS->getAsType(); |
| 1485 | if (!T) break; |
| 1486 | |
| 1487 | // C++0x [temp.expl.spec]p17: |
| 1488 | // A member or a member template may be nested within many |
| 1489 | // enclosing class templates. In an explicit specialization for |
| 1490 | // such a member, the member declaration shall be preceded by a |
| 1491 | // template<> for each enclosing class template that is |
| 1492 | // explicitly specialized. |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1493 | // |
| 1494 | // Following the existing practice of GNU and EDG, we allow a typedef of a |
| 1495 | // template specialization type. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1496 | while (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
| 1497 | T = TT->getDecl()->getUnderlyingType().getTypePtr(); |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1498 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1499 | if (const TemplateSpecializationType *SpecType |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1500 | = dyn_cast<TemplateSpecializationType>(T)) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1501 | TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl(); |
| 1502 | if (!Template) |
| 1503 | continue; // FIXME: should this be an error? probably... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1504 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1505 | if (const RecordType *Record = SpecType->getAs<RecordType>()) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1506 | ClassTemplateSpecializationDecl *SpecDecl |
| 1507 | = cast<ClassTemplateSpecializationDecl>(Record->getDecl()); |
| 1508 | // If the nested name specifier refers to an explicit specialization, |
| 1509 | // we don't need a template<> header. |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1510 | if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1511 | ExplicitSpecializationsInSpecifier.push_back(SpecDecl); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1512 | continue; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1513 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1514 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1516 | TemplateIdsInSpecifier.push_back(SpecType); |
| 1517 | } |
| 1518 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1520 | // Reverse the list of template-ids in the scope specifier, so that we can |
| 1521 | // more easily match up the template-ids and the template parameter lists. |
| 1522 | std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1523 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1524 | SourceLocation FirstTemplateLoc = DeclStartLoc; |
| 1525 | if (NumParamLists) |
| 1526 | FirstTemplateLoc = ParamLists[0]->getTemplateLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1527 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1528 | // Match the template-ids found in the specifier to the template parameter |
| 1529 | // lists. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1530 | unsigned ParamIdx = 0, TemplateIdx = 0; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1531 | for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size(); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1532 | TemplateIdx != NumTemplateIds; ++TemplateIdx) { |
| 1533 | const TemplateSpecializationType *TemplateId |
| 1534 | = TemplateIdsInSpecifier[TemplateIdx]; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1535 | bool DependentTemplateId = TemplateId->isDependentType(); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1536 | |
| 1537 | // In friend declarations we can have template-ids which don't |
| 1538 | // depend on the corresponding template parameter lists. But |
| 1539 | // assume that empty parameter lists are supposed to match this |
| 1540 | // template-id. |
| 1541 | if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) { |
| 1542 | if (!DependentTemplateId || |
| 1543 | !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx])) |
| 1544 | continue; |
| 1545 | } |
| 1546 | |
| 1547 | if (ParamIdx >= NumParamLists) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1548 | // We have a template-id without a corresponding template parameter |
| 1549 | // list. |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1550 | |
| 1551 | // ...which is fine if this is a friend declaration. |
| 1552 | if (IsFriend) { |
| 1553 | IsExplicitSpecialization = true; |
| 1554 | break; |
| 1555 | } |
| 1556 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1557 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | // FIXME: the location information here isn't great. |
| 1559 | Diag(SS.getRange().getBegin(), |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1560 | diag::err_template_spec_needs_template_parameters) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1561 | << QualType(TemplateId, 0) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1562 | << SS.getRange(); |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1563 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1564 | } else { |
| 1565 | Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header) |
| 1566 | << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1567 | << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1568 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1569 | } |
| 1570 | return 0; |
| 1571 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1572 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1573 | // Check the template parameter list against its corresponding template-id. |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1574 | if (DependentTemplateId) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1575 | TemplateParameterList *ExpectedTemplateParams = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1576 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1577 | // Are there cases in (e.g.) friends where this won't match? |
| 1578 | if (const InjectedClassNameType *Injected |
| 1579 | = TemplateId->getAs<InjectedClassNameType>()) { |
| 1580 | CXXRecordDecl *Record = Injected->getDecl(); |
| 1581 | if (ClassTemplatePartialSpecializationDecl *Partial = |
| 1582 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 1583 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1584 | else |
| 1585 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
| 1586 | ->getTemplateParameters(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1587 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1588 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1589 | if (ExpectedTemplateParams) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1590 | TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1591 | ExpectedTemplateParams, |
| 1592 | true, TPL_TemplateMatch); |
| 1593 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1594 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1595 | TPC_ClassTemplateMember); |
| 1596 | } else if (ParamLists[ParamIdx]->size() > 0) |
| 1597 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1598 | diag::err_template_param_list_matches_nontemplate) |
| 1599 | << TemplateId |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1600 | << ParamLists[ParamIdx]->getSourceRange(); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1601 | else |
| 1602 | IsExplicitSpecialization = true; |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1603 | |
| 1604 | ++ParamIdx; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1605 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1607 | // If there were at least as many template-ids as there were template |
| 1608 | // parameter lists, then there are no template parameter lists remaining for |
| 1609 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1610 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1611 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1612 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1613 | // If there were too many template parameter lists, complain about that now. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1614 | if (ParamIdx != NumParamLists - 1) { |
| 1615 | while (ParamIdx < NumParamLists - 1) { |
| 1616 | bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0; |
| 1617 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1618 | isExplicitSpecHeader? diag::warn_template_spec_extra_headers |
| 1619 | : diag::err_template_spec_extra_headers) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1620 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1621 | ParamLists[ParamIdx]->getRAngleLoc()); |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1622 | |
| 1623 | if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) { |
| 1624 | Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(), |
| 1625 | diag::note_explicit_template_spec_does_not_need_header) |
| 1626 | << ExplicitSpecializationsInSpecifier.back(); |
| 1627 | ExplicitSpecializationsInSpecifier.pop_back(); |
| 1628 | } |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1629 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1630 | // We have a template parameter list with no corresponding scope, which |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1631 | // means that the resulting template declaration can't be instantiated |
| 1632 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1633 | if (!isExplicitSpecHeader) |
| 1634 | Invalid = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1635 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1636 | ++ParamIdx; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1637 | } |
| 1638 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1640 | // Return the last template parameter list, which corresponds to the |
| 1641 | // entity being declared. |
| 1642 | return ParamLists[NumParamLists - 1]; |
| 1643 | } |
| 1644 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1645 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1646 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1647 | Diag(Template->getLocation(), diag::note_template_declared_here) |
| 1648 | << (isa<FunctionTemplateDecl>(Template)? 0 |
| 1649 | : isa<ClassTemplateDecl>(Template)? 1 |
| 1650 | : 2) |
| 1651 | << Template->getDeclName(); |
| 1652 | return; |
| 1653 | } |
| 1654 | |
| 1655 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1656 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1657 | IEnd = OST->end(); |
| 1658 | I != IEnd; ++I) |
| 1659 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1660 | << 0 << (*I)->getDeclName(); |
| 1661 | |
| 1662 | return; |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1667 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1668 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1669 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1670 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1671 | if (!Template || isa<FunctionTemplateDecl>(Template)) { |
| 1672 | // We might have a substituted template template parameter pack. If so, |
| 1673 | // build a template specialization type for it. |
| 1674 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 1675 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
| 1676 | |
| 1677 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 1678 | << Name; |
| 1679 | NoteAllFoundTemplates(Name); |
| 1680 | return QualType(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1681 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1682 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1683 | // Check that the template argument list is well-formed for this |
| 1684 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1685 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1686 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1687 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1688 | return QualType(); |
| 1689 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1690 | assert((Converted.size() == Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1691 | "Converted template argument list is too short!"); |
| 1692 | |
| 1693 | QualType CanonType; |
| 1694 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 1695 | if (Name.isDependent() || |
| 1696 | TemplateSpecializationType::anyDependentTemplateArguments( |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1697 | TemplateArgs)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1698 | // This class template specialization is a dependent |
| 1699 | // type. Therefore, its canonical type is another class template |
| 1700 | // specialization type that contains all of the converted |
| 1701 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1702 | // A<T, T> have identical types when A is declared as: |
| 1703 | // |
| 1704 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1705 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1706 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1707 | Converted.data(), |
| 1708 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1709 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1710 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1711 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1712 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1713 | // build the canonical type and return that to us. |
| 1714 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1715 | |
| 1716 | // This might work out to be a current instantiation, in which |
| 1717 | // case the canonical type needs to be the InjectedClassNameType. |
| 1718 | // |
| 1719 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1720 | // changes to CurContext don't change the set of current |
| 1721 | // instantiations. |
| 1722 | if (isa<ClassTemplateDecl>(Template)) { |
| 1723 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1724 | // If we get out to a namespace, we're done. |
| 1725 | if (Ctx->isFileContext()) break; |
| 1726 | |
| 1727 | // If this isn't a record, keep looking. |
| 1728 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1729 | if (!Record) continue; |
| 1730 | |
| 1731 | // Look for one of the two cases with InjectedClassNameTypes |
| 1732 | // and check whether it's the same template. |
| 1733 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1734 | !Record->getDescribedClassTemplate()) |
| 1735 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1736 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1737 | // Fetch the injected class name type and check whether its |
| 1738 | // injected type is equal to the type we just built. |
| 1739 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1740 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 1741 | ->getInjectedSpecializationType(); |
| 1742 | |
| 1743 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 1744 | continue; |
| 1745 | |
| 1746 | // If so, the canonical type of this TST is the injected |
| 1747 | // class name type of the record we just found. |
| 1748 | assert(ICNT.isCanonical()); |
| 1749 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1750 | break; |
| 1751 | } |
| 1752 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1754 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1755 | // Find the class template specialization declaration that |
| 1756 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1757 | void *InsertPos = 0; |
| 1758 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1759 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1760 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1761 | if (!Decl) { |
| 1762 | // This is the first time we have referenced this class template |
| 1763 | // specialization. Create the canonical declaration and add it to |
| 1764 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1765 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1766 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 1767 | ClassTemplate->getDeclContext(), |
| 1768 | ClassTemplate->getLocation(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1769 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1770 | ClassTemplate, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1771 | Converted.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1772 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 1773 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1774 | Decl->setLexicalDeclContext(CurContext); |
| 1775 | } |
| 1776 | |
| 1777 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1778 | assert(isa<RecordType>(CanonType) && |
| 1779 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1780 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1781 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1782 | // Build the fully-sugared type for this class template |
| 1783 | // specialization, which refers back to the class template |
| 1784 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1785 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1788 | TypeResult |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1789 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, |
| 1790 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1792 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1793 | SourceLocation RAngleLoc) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1794 | if (SS.isInvalid()) |
| 1795 | return true; |
| 1796 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1797 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1798 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1799 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1800 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1801 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1802 | |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 1803 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 1804 | QualType T = Context.getDependentTemplateSpecializationType(ETK_None, |
| 1805 | DTN->getQualifier(), |
| 1806 | DTN->getIdentifier(), |
| 1807 | TemplateArgs); |
| 1808 | |
| 1809 | // Build type-source information. |
| 1810 | TypeLocBuilder TLB; |
| 1811 | DependentTemplateSpecializationTypeLoc SpecTL |
| 1812 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1813 | SpecTL.setKeywordLoc(SourceLocation()); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 1814 | SpecTL.setNameLoc(TemplateLoc); |
| 1815 | SpecTL.setLAngleLoc(LAngleLoc); |
| 1816 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 1817 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 1818 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 1819 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 1820 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 1821 | } |
| 1822 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1823 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1824 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1825 | |
| 1826 | if (Result.isNull()) |
| 1827 | return true; |
| 1828 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1829 | // Build type-source information. |
| 1830 | TypeLocBuilder TLB; |
| 1831 | TemplateSpecializationTypeLoc SpecTL |
| 1832 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 1833 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 1834 | SpecTL.setLAngleLoc(LAngleLoc); |
| 1835 | SpecTL.setRAngleLoc(RAngleLoc); |
| 1836 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 1837 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1838 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1839 | if (SS.isNotEmpty()) { |
| 1840 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 1841 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 1842 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 1843 | ElabTL.setKeywordLoc(SourceLocation()); |
| 1844 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 1845 | } |
| 1846 | |
| 1847 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1848 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1849 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1850 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1851 | TypeSpecifierType TagSpec, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1852 | SourceLocation TagLoc, |
| 1853 | CXXScopeSpec &SS, |
| 1854 | TemplateTy TemplateD, |
| 1855 | SourceLocation TemplateLoc, |
| 1856 | SourceLocation LAngleLoc, |
| 1857 | ASTTemplateArgsPtr TemplateArgsIn, |
| 1858 | SourceLocation RAngleLoc) { |
| 1859 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
| 1860 | |
| 1861 | // Translate the parser's template argument list in our AST format. |
| 1862 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 1863 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 1864 | |
| 1865 | // Determine the tag kind |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1866 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1867 | ElaboratedTypeKeyword Keyword |
| 1868 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1869 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1870 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 1871 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 1872 | DTN->getQualifier(), |
| 1873 | DTN->getIdentifier(), |
| 1874 | TemplateArgs); |
| 1875 | |
| 1876 | // Build type-source information. |
| 1877 | TypeLocBuilder TLB; |
| 1878 | DependentTemplateSpecializationTypeLoc SpecTL |
| 1879 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 1880 | SpecTL.setKeywordLoc(TagLoc); |
| 1881 | SpecTL.setNameLoc(TemplateLoc); |
| 1882 | SpecTL.setLAngleLoc(LAngleLoc); |
| 1883 | SpecTL.setRAngleLoc(RAngleLoc); |
| 1884 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 1885 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 1886 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 1887 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 1888 | } |
| 1889 | |
| 1890 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 1891 | if (Result.isNull()) |
| 1892 | return TypeResult(); |
| 1893 | |
| 1894 | // Check the tag kind |
| 1895 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1896 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1897 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1898 | IdentifierInfo *Id = D->getIdentifier(); |
| 1899 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1900 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1901 | if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { |
| 1902 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1903 | << Result |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1904 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1905 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1906 | } |
| 1907 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1908 | |
| 1909 | // Provide source-location information for the template specialization. |
| 1910 | TypeLocBuilder TLB; |
| 1911 | TemplateSpecializationTypeLoc SpecTL |
| 1912 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 1913 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 1914 | SpecTL.setLAngleLoc(LAngleLoc); |
| 1915 | SpecTL.setRAngleLoc(RAngleLoc); |
| 1916 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 1917 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1918 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1919 | // Construct an elaborated type containing the nested-name-specifier (if any) |
| 1920 | // and keyword. |
| 1921 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 1922 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 1923 | ElabTL.setKeywordLoc(TagLoc); |
| 1924 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 1925 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1928 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1929 | LookupResult &R, |
| 1930 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1931 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1932 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 1933 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | // 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] | 1935 | // though. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1936 | // foo<int> could identify a single function unambiguously |
| 1937 | // This approach does NOT work, since f<int>(1); |
| 1938 | // gets resolved prior to resorting to overload resolution |
| 1939 | // i.e., template<class T> void f(double); |
| 1940 | // vs template<class T, class U> void f(U); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1941 | |
| 1942 | // These should be filtered out by our callers. |
| 1943 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 1944 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 1945 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1946 | // We don't want lookup warnings at this point. |
| 1947 | R.suppressDiagnostics(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1948 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1949 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1950 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1951 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1952 | R.getLookupNameInfo(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1953 | RequiresADL, TemplateArgs, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 1954 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1955 | |
| 1956 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1959 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1960 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1961 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1962 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1963 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1964 | DeclContext *DC; |
| 1965 | if (!(DC = computeDeclContext(SS, false)) || |
| 1966 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1967 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1968 | return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1969 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1970 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1971 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1972 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 1973 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1974 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1975 | if (R.isAmbiguous()) |
| 1976 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1977 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1978 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1979 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 1980 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1981 | return ExprError(); |
| 1982 | } |
| 1983 | |
| 1984 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1985 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 1986 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 1987 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1988 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 1989 | return ExprError(); |
| 1990 | } |
| 1991 | |
| 1992 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1995 | /// \brief Form a dependent template name. |
| 1996 | /// |
| 1997 | /// This action forms a dependent template name given the template |
| 1998 | /// name and its (presumably dependent) scope specifier. For |
| 1999 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2000 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2001 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2002 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2003 | SourceLocation TemplateKWLoc, |
| 2004 | CXXScopeSpec &SS, |
| 2005 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2006 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2007 | bool EnteringContext, |
| 2008 | TemplateTy &Result) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 2009 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() && |
| 2010 | !getLangOptions().CPlusPlus0x) |
| 2011 | Diag(TemplateKWLoc, diag::ext_template_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2012 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2013 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2014 | DeclContext *LookupCtx = 0; |
| 2015 | if (SS.isSet()) |
| 2016 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2017 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2018 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2019 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2020 | // C++0x [temp.names]p5: |
| 2021 | // If a name prefixed by the keyword template is not the name of |
| 2022 | // a template, the program is ill-formed. [Note: the keyword |
| 2023 | // template may not be applied to non-template members of class |
| 2024 | // templates. -end note ] [ Note: as is the case with the |
| 2025 | // typename prefix, the template prefix is allowed in cases |
| 2026 | // where it is not strictly necessary; i.e., when the |
| 2027 | // nested-name-specifier or the expression on the left of the -> |
| 2028 | // or . is not dependent on a template-parameter, or the use |
| 2029 | // does not appear in the scope of a template. -end note] |
| 2030 | // |
| 2031 | // Note: C++03 was more strict here, because it banned the use of |
| 2032 | // the "template" keyword prior to a template-name that was not a |
| 2033 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2034 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2035 | // 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] | 2036 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2037 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 2038 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2039 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2040 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2041 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | d078bd2 | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2042 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2043 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2044 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2045 | } else if (TNK == TNK_Non_template) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2046 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2047 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2048 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2049 | << Name.getSourceRange() |
| 2050 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2051 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2052 | } else { |
| 2053 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2054 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2055 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2058 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2059 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2060 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2061 | switch (Name.getKind()) { |
| 2062 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2063 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2064 | Name.Identifier)); |
| 2065 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2066 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2067 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2068 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2069 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2070 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2071 | |
| 2072 | case UnqualifiedId::IK_LiteralOperatorId: |
| 2073 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 2074 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2075 | default: |
| 2076 | break; |
| 2077 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2078 | |
| 2079 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2080 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2081 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2082 | << Name.getSourceRange() |
| 2083 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2084 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2087 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2088 | const TemplateArgumentLoc &AL, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2089 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2090 | const TemplateArgument &Arg = AL.getArgument(); |
| 2091 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2092 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2093 | switch(Arg.getKind()) { |
| 2094 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2095 | // C++ [temp.arg.type]p1: |
| 2096 | // A template-argument for a template-parameter which is a |
| 2097 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2098 | break; |
| 2099 | case TemplateArgument::Template: { |
| 2100 | // We have a template type parameter but the template argument |
| 2101 | // is a template without any arguments. |
| 2102 | SourceRange SR = AL.getSourceRange(); |
| 2103 | TemplateName Name = Arg.getAsTemplate(); |
| 2104 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 2105 | << Name << SR; |
| 2106 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 2107 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2108 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2109 | return true; |
| 2110 | } |
| 2111 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2112 | // We have a template type parameter but the template argument |
| 2113 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 2114 | SourceRange SR = AL.getSourceRange(); |
| 2115 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2116 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2118 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2120 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2121 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2122 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2123 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2124 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2125 | // Add the converted template type argument. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2126 | Converted.push_back( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2127 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2128 | return false; |
| 2129 | } |
| 2130 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2131 | /// \brief Substitute template arguments into the default template argument for |
| 2132 | /// the given template type parameter. |
| 2133 | /// |
| 2134 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2135 | /// the substitution. |
| 2136 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2137 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2138 | /// for. |
| 2139 | /// |
| 2140 | /// \param TemplateLoc the location of the template name that started the |
| 2141 | /// template-id we are checking. |
| 2142 | /// |
| 2143 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2144 | /// terminates the template-id. |
| 2145 | /// |
| 2146 | /// \param Param the template template parameter whose default we are |
| 2147 | /// substituting into. |
| 2148 | /// |
| 2149 | /// \param Converted the list of template arguments provided for template |
| 2150 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2151 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2152 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2153 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2154 | TemplateDecl *Template, |
| 2155 | SourceLocation TemplateLoc, |
| 2156 | SourceLocation RAngleLoc, |
| 2157 | TemplateTypeParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2158 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2159 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2160 | |
| 2161 | // If the argument type is dependent, instantiate it now based |
| 2162 | // on the previously-computed template arguments. |
| 2163 | if (ArgType->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2164 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2165 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2166 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2167 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2168 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2169 | |
| 2170 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2171 | Template, Converted.data(), |
| 2172 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2173 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2174 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2175 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2176 | Param->getDefaultArgumentLoc(), |
| 2177 | Param->getDeclName()); |
| 2178 | } |
| 2179 | |
| 2180 | return ArgType; |
| 2181 | } |
| 2182 | |
| 2183 | /// \brief Substitute template arguments into the default template argument for |
| 2184 | /// the given non-type template parameter. |
| 2185 | /// |
| 2186 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2187 | /// the substitution. |
| 2188 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2189 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2190 | /// for. |
| 2191 | /// |
| 2192 | /// \param TemplateLoc the location of the template name that started the |
| 2193 | /// template-id we are checking. |
| 2194 | /// |
| 2195 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2196 | /// terminates the template-id. |
| 2197 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2198 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2199 | /// substituting into. |
| 2200 | /// |
| 2201 | /// \param Converted the list of template arguments provided for template |
| 2202 | /// parameters that precede \p Param in the template parameter list. |
| 2203 | /// |
| 2204 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2205 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2206 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2207 | TemplateDecl *Template, |
| 2208 | SourceLocation TemplateLoc, |
| 2209 | SourceLocation RAngleLoc, |
| 2210 | NonTypeTemplateParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2211 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2212 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2213 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2214 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2215 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2216 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2217 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2218 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2219 | Template, Converted.data(), |
| 2220 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2221 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2222 | |
| 2223 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2224 | } |
| 2225 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2226 | /// \brief Substitute template arguments into the default template argument for |
| 2227 | /// the given template template parameter. |
| 2228 | /// |
| 2229 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2230 | /// the substitution. |
| 2231 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2232 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2233 | /// for. |
| 2234 | /// |
| 2235 | /// \param TemplateLoc the location of the template name that started the |
| 2236 | /// template-id we are checking. |
| 2237 | /// |
| 2238 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2239 | /// terminates the template-id. |
| 2240 | /// |
| 2241 | /// \param Param the template template parameter whose default we are |
| 2242 | /// substituting into. |
| 2243 | /// |
| 2244 | /// \param Converted the list of template arguments provided for template |
| 2245 | /// parameters that precede \p Param in the template parameter list. |
| 2246 | /// |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2247 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 2248 | /// source-location information) that precedes the template name. |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2249 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2250 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2251 | static TemplateName |
| 2252 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2253 | TemplateDecl *Template, |
| 2254 | SourceLocation TemplateLoc, |
| 2255 | SourceLocation RAngleLoc, |
| 2256 | TemplateTemplateParmDecl *Param, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2257 | llvm::SmallVectorImpl<TemplateArgument> &Converted, |
| 2258 | NestedNameSpecifierLoc &QualifierLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2259 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2260 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2261 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2262 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2263 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2264 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2265 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2266 | Template, Converted.data(), |
| 2267 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2268 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2269 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2270 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2271 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2272 | if (QualifierLoc) { |
| 2273 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2274 | AllTemplateArgs); |
| 2275 | if (!QualifierLoc) |
| 2276 | return TemplateName(); |
| 2277 | } |
| 2278 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2279 | return SemaRef.SubstTemplateName(QualifierLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2280 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2281 | Param->getDefaultArgument().getTemplateNameLoc(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2282 | AllTemplateArgs); |
| 2283 | } |
| 2284 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2285 | /// \brief If the given template parameter has a default template |
| 2286 | /// argument, substitute into that default template argument and |
| 2287 | /// return the corresponding template argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2288 | TemplateArgumentLoc |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2289 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2290 | SourceLocation TemplateLoc, |
| 2291 | SourceLocation RAngleLoc, |
| 2292 | Decl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2293 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 2294 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2295 | if (!TypeParm->hasDefaultArgument()) |
| 2296 | return TemplateArgumentLoc(); |
| 2297 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2298 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2299 | TemplateLoc, |
| 2300 | RAngleLoc, |
| 2301 | TypeParm, |
| 2302 | Converted); |
| 2303 | if (DI) |
| 2304 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2305 | |
| 2306 | return TemplateArgumentLoc(); |
| 2307 | } |
| 2308 | |
| 2309 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2310 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2311 | if (!NonTypeParm->hasDefaultArgument()) |
| 2312 | return TemplateArgumentLoc(); |
| 2313 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2314 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2315 | TemplateLoc, |
| 2316 | RAngleLoc, |
| 2317 | NonTypeParm, |
| 2318 | Converted); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2319 | if (Arg.isInvalid()) |
| 2320 | return TemplateArgumentLoc(); |
| 2321 | |
| 2322 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2323 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2324 | } |
| 2325 | |
| 2326 | TemplateTemplateParmDecl *TempTempParm |
| 2327 | = cast<TemplateTemplateParmDecl>(Param); |
| 2328 | if (!TempTempParm->hasDefaultArgument()) |
| 2329 | return TemplateArgumentLoc(); |
| 2330 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2331 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2332 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2333 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2334 | TemplateLoc, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2335 | RAngleLoc, |
| 2336 | TempTempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2337 | Converted, |
| 2338 | QualifierLoc); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2339 | if (TName.isNull()) |
| 2340 | return TemplateArgumentLoc(); |
| 2341 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2342 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2343 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2344 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2345 | } |
| 2346 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2347 | /// \brief Check that the given template argument corresponds to the given |
| 2348 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2349 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2350 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2351 | /// checked. |
| 2352 | /// |
| 2353 | /// \param Arg The template argument. |
| 2354 | /// |
| 2355 | /// \param Template The template in which the template argument resides. |
| 2356 | /// |
| 2357 | /// \param TemplateLoc The location of the template name for the template |
| 2358 | /// whose argument list we're matching. |
| 2359 | /// |
| 2360 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2361 | /// the template argument list. |
| 2362 | /// |
| 2363 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2364 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2365 | /// |
| 2366 | /// \param Converted The checked, converted argument will be added to the |
| 2367 | /// end of this small vector. |
| 2368 | /// |
| 2369 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2370 | /// explicitly written, deduced, etc. |
| 2371 | /// |
| 2372 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2373 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2374 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2375 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2376 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2377 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2378 | unsigned ArgumentPackIndex, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2379 | llvm::SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2380 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2381 | // Check template type parameters. |
| 2382 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2383 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2384 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2385 | // Check non-type template parameters. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2386 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2387 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2388 | // with the template arguments we've seen thus far. But if the |
| 2389 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2390 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2391 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2392 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2393 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2394 | if (NTTPType->isDependentType() && |
| 2395 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2396 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2397 | // Do substitution on the type of the non-type template parameter. |
| 2398 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2399 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2400 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2401 | |
| 2402 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2403 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2404 | NTTPType = SubstType(NTTPType, |
| 2405 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2406 | NTTP->getLocation(), |
| 2407 | NTTP->getDeclName()); |
| 2408 | // If that worked, check the non-type template parameter type |
| 2409 | // for validity. |
| 2410 | if (!NTTPType.isNull()) |
| 2411 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2412 | NTTP->getLocation()); |
| 2413 | if (NTTPType.isNull()) |
| 2414 | return true; |
| 2415 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2416 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2417 | switch (Arg.getArgument().getKind()) { |
| 2418 | case TemplateArgument::Null: |
| 2419 | assert(false && "Should never see a NULL template argument here"); |
| 2420 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2421 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2422 | case TemplateArgument::Expression: { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2423 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2424 | ExprResult Res = |
| 2425 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 2426 | Result, CTAK); |
| 2427 | if (Res.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2428 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2429 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2430 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2431 | break; |
| 2432 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2433 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2434 | case TemplateArgument::Declaration: |
| 2435 | case TemplateArgument::Integral: |
| 2436 | // We've already checked this template argument, so just copy |
| 2437 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2438 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2439 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2440 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2441 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2442 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2443 | // We were given a template template argument. It may not be ill-formed; |
| 2444 | // see below. |
| 2445 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2446 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2447 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2448 | // We have a template argument such as \c T::template X, which we |
| 2449 | // parsed as a template template argument. However, since we now |
| 2450 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2451 | // template name into an expression. |
| 2452 | |
| 2453 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2454 | Arg.getTemplateNameLoc()); |
| 2455 | |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2456 | CXXScopeSpec SS; |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2457 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2458 | ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2459 | SS.getWithLocInContext(Context), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2460 | NameInfo)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2461 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2462 | // If we parsed the template argument as a pack expansion, create a |
| 2463 | // pack expansion expression. |
| 2464 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2465 | E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc()); |
| 2466 | if (E.isInvalid()) |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2467 | return true; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2468 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2469 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2470 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2471 | E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result); |
| 2472 | if (E.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2473 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2474 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2475 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2476 | break; |
| 2477 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2478 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2479 | // We have a template argument that actually does refer to a class |
| 2480 | // template, template alias, or template template parameter, and |
| 2481 | // therefore cannot be a non-type template argument. |
| 2482 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2483 | << Arg.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2484 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2485 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2486 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2487 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2488 | case TemplateArgument::Type: { |
| 2489 | // We have a non-type template parameter but the template |
| 2490 | // argument is a type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2491 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2492 | // C++ [temp.arg]p2: |
| 2493 | // In a template-argument, an ambiguity between a type-id and |
| 2494 | // an expression is resolved to a type-id, regardless of the |
| 2495 | // form of the corresponding template-parameter. |
| 2496 | // |
| 2497 | // We warn specifically about this case, since it can be rather |
| 2498 | // confusing for users. |
| 2499 | QualType T = Arg.getArgument().getAsType(); |
| 2500 | SourceRange SR = Arg.getSourceRange(); |
| 2501 | if (T->isFunctionType()) |
| 2502 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2503 | else |
| 2504 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2505 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2506 | return true; |
| 2507 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2508 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2509 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2510 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2511 | break; |
| 2512 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2513 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2514 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2518 | // Check template template parameters. |
| 2519 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2520 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2521 | // Substitute into the template parameter list of the template |
| 2522 | // template parameter, since previously-supplied template arguments |
| 2523 | // may appear within the template template parameter. |
| 2524 | { |
| 2525 | // Set up a template instantiation context. |
| 2526 | LocalInstantiationScope Scope(*this); |
| 2527 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2528 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2529 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2530 | |
| 2531 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2532 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2533 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2534 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2535 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2536 | if (!TempParm) |
| 2537 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2538 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2539 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2540 | switch (Arg.getArgument().getKind()) { |
| 2541 | case TemplateArgument::Null: |
| 2542 | assert(false && "Should never see a NULL template argument here"); |
| 2543 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2544 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2545 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2546 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2547 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2548 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2549 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2550 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2551 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2552 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2553 | case TemplateArgument::Expression: |
| 2554 | case TemplateArgument::Type: |
| 2555 | // We have a template template parameter but the template |
| 2556 | // argument does not refer to a template. |
| 2557 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 2558 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2559 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2560 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2561 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2562 | "Declaration argument with template template parameter"); |
| 2563 | break; |
| 2564 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2565 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2566 | "Integral argument with template template parameter"); |
| 2567 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2568 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2569 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2570 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2571 | break; |
| 2572 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2573 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2574 | return false; |
| 2575 | } |
| 2576 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2577 | /// \brief Check that the given template argument list is well-formed |
| 2578 | /// for specializing the given template. |
| 2579 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2580 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2581 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2582 | bool PartialTemplateArgs, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2583 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2584 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2585 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2586 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2587 | bool Invalid = false; |
| 2588 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2589 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2590 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2591 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2592 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2593 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2594 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2595 | (NumArgs < Params->getMinRequiredArguments() && |
| 2596 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2597 | // FIXME: point at either the first arg beyond what we can handle, |
| 2598 | // or the '>', depending on whether we have too many or too few |
| 2599 | // arguments. |
| 2600 | SourceRange Range; |
| 2601 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2602 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2603 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2604 | << (NumArgs > NumParams) |
| 2605 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2606 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2607 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2608 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2609 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2610 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2611 | Invalid = true; |
| 2612 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2613 | |
| 2614 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2615 | // [...] The type and form of each template-argument specified in |
| 2616 | // a template-id shall match the type and form specified for the |
| 2617 | // corresponding parameter declared by the template in its |
| 2618 | // template-parameter-list. |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2619 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2620 | llvm::SmallVector<TemplateArgument, 2> ArgumentPack; |
| 2621 | TemplateParameterList::iterator Param = Params->begin(), |
| 2622 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2623 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 2624 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2625 | while (Param != ParamEnd) { |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2626 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 2627 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2628 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2629 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2630 | // If we have an expanded parameter pack, make sure we don't have too |
| 2631 | // many arguments. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2632 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2633 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2634 | if (NTTP->isExpandedParameterPack() && |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2635 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 2636 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2637 | << true |
| 2638 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2639 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2640 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2641 | << Template; |
| 2642 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2643 | << Params->getSourceRange(); |
| 2644 | return true; |
| 2645 | } |
| 2646 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2647 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2648 | // Check the template argument we were given. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2649 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2650 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2651 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2652 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2653 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2654 | if ((*Param)->isTemplateParameterPack()) { |
| 2655 | // The template parameter was a template parameter pack, so take the |
| 2656 | // deduced argument and place it on the argument pack. Note that we |
| 2657 | // stay on the same template parameter so that we can deduce more |
| 2658 | // arguments. |
| 2659 | ArgumentPack.push_back(Converted.back()); |
| 2660 | Converted.pop_back(); |
| 2661 | } else { |
| 2662 | // Move to the next template parameter. |
| 2663 | ++Param; |
| 2664 | } |
| 2665 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2666 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2667 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2668 | |
| 2669 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2670 | // arguments, just break out now and we'll fill in the argument pack below. |
| 2671 | if ((*Param)->isTemplateParameterPack()) |
| 2672 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2673 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2674 | // We have a default template argument that we will use. |
| 2675 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2676 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2677 | // Retrieve the default template argument from the template |
| 2678 | // parameter. For each kind of template parameter, we substitute the |
| 2679 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2680 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2681 | // the default argument. |
| 2682 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2683 | if (!TTP->hasDefaultArgument()) { |
| 2684 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2685 | break; |
| 2686 | } |
| 2687 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2688 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2689 | Template, |
| 2690 | TemplateLoc, |
| 2691 | RAngleLoc, |
| 2692 | TTP, |
| 2693 | Converted); |
| 2694 | if (!ArgType) |
| 2695 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2696 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2697 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2698 | ArgType); |
| 2699 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2700 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2701 | if (!NTTP->hasDefaultArgument()) { |
| 2702 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2703 | break; |
| 2704 | } |
| 2705 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2706 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2707 | TemplateLoc, |
| 2708 | RAngleLoc, |
| 2709 | NTTP, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2710 | Converted); |
| 2711 | if (E.isInvalid()) |
| 2712 | return true; |
| 2713 | |
| 2714 | Expr *Ex = E.takeAs<Expr>(); |
| 2715 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2716 | } else { |
| 2717 | TemplateTemplateParmDecl *TempParm |
| 2718 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2719 | |
| 2720 | if (!TempParm->hasDefaultArgument()) { |
| 2721 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2722 | break; |
| 2723 | } |
| 2724 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2725 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2726 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2727 | TemplateLoc, |
| 2728 | RAngleLoc, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2729 | TempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2730 | Converted, |
| 2731 | QualifierLoc); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2732 | if (Name.isNull()) |
| 2733 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2734 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2735 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 2736 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2737 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2738 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2739 | // Introduce an instantiation record that describes where we are using |
| 2740 | // the default template argument. |
| 2741 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2742 | Converted.data(), Converted.size(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2743 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2744 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2745 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2746 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2747 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2748 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2749 | |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2750 | // Core issue 150 (assumed resolution): if this is a template template |
| 2751 | // parameter, keep track of the default template arguments from the |
| 2752 | // template definition. |
| 2753 | if (isTemplateTemplateParameter) |
| 2754 | TemplateArgs.addArgument(Arg); |
| 2755 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2756 | // Move to the next template parameter and argument. |
| 2757 | ++Param; |
| 2758 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2759 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2760 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2761 | // Form argument packs for each of the parameter packs remaining. |
| 2762 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2763 | // If we're checking a partial list of template arguments, don't fill |
| 2764 | // in arguments for non-template parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2765 | |
| 2766 | if ((*Param)->isTemplateParameterPack()) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2767 | if (PartialTemplateArgs && ArgumentPack.empty()) { |
| 2768 | Converted.push_back(TemplateArgument()); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2769 | } else if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2770 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2771 | else { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2772 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 2773 | ArgumentPack.data(), |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2774 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2775 | ArgumentPack.clear(); |
| 2776 | } |
| 2777 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2778 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2779 | ++Param; |
| 2780 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2781 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2782 | return Invalid; |
| 2783 | } |
| 2784 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2785 | namespace { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2786 | class UnnamedLocalNoLinkageFinder |
| 2787 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2788 | { |
| 2789 | Sema &S; |
| 2790 | SourceRange SR; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2791 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2792 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2793 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2794 | public: |
| 2795 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 2796 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2797 | bool Visit(QualType T) { |
| 2798 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2799 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2800 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2801 | #define TYPE(Class, Parent) \ |
| 2802 | bool Visit##Class##Type(const Class##Type *); |
| 2803 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 2804 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2805 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 2806 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2807 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2808 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2809 | bool VisitTagDecl(const TagDecl *Tag); |
| 2810 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 2811 | }; |
| 2812 | } |
| 2813 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2814 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2815 | return false; |
| 2816 | } |
| 2817 | |
| 2818 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 2819 | return Visit(T->getElementType()); |
| 2820 | } |
| 2821 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2822 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2823 | return Visit(T->getPointeeType()); |
| 2824 | } |
| 2825 | |
| 2826 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2827 | const BlockPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2828 | return Visit(T->getPointeeType()); |
| 2829 | } |
| 2830 | |
| 2831 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2832 | const LValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2833 | return Visit(T->getPointeeType()); |
| 2834 | } |
| 2835 | |
| 2836 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2837 | const RValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2838 | return Visit(T->getPointeeType()); |
| 2839 | } |
| 2840 | |
| 2841 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2842 | const MemberPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2843 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 2844 | } |
| 2845 | |
| 2846 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2847 | const ConstantArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2848 | return Visit(T->getElementType()); |
| 2849 | } |
| 2850 | |
| 2851 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2852 | const IncompleteArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2853 | return Visit(T->getElementType()); |
| 2854 | } |
| 2855 | |
| 2856 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2857 | const VariableArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2858 | return Visit(T->getElementType()); |
| 2859 | } |
| 2860 | |
| 2861 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2862 | const DependentSizedArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2863 | return Visit(T->getElementType()); |
| 2864 | } |
| 2865 | |
| 2866 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2867 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2868 | return Visit(T->getElementType()); |
| 2869 | } |
| 2870 | |
| 2871 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 2872 | return Visit(T->getElementType()); |
| 2873 | } |
| 2874 | |
| 2875 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 2876 | return Visit(T->getElementType()); |
| 2877 | } |
| 2878 | |
| 2879 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 2880 | const FunctionProtoType* T) { |
| 2881 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2882 | AEnd = T->arg_type_end(); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2883 | A != AEnd; ++A) { |
| 2884 | if (Visit(*A)) |
| 2885 | return true; |
| 2886 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2887 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2888 | return Visit(T->getResultType()); |
| 2889 | } |
| 2890 | |
| 2891 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 2892 | const FunctionNoProtoType* T) { |
| 2893 | return Visit(T->getResultType()); |
| 2894 | } |
| 2895 | |
| 2896 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 2897 | const UnresolvedUsingType*) { |
| 2898 | return false; |
| 2899 | } |
| 2900 | |
| 2901 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 2902 | return false; |
| 2903 | } |
| 2904 | |
| 2905 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 2906 | return Visit(T->getUnderlyingType()); |
| 2907 | } |
| 2908 | |
| 2909 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 2910 | return false; |
| 2911 | } |
| 2912 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2913 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 2914 | return Visit(T->getDeducedType()); |
| 2915 | } |
| 2916 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2917 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 2918 | return VisitTagDecl(T->getDecl()); |
| 2919 | } |
| 2920 | |
| 2921 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 2922 | return VisitTagDecl(T->getDecl()); |
| 2923 | } |
| 2924 | |
| 2925 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 2926 | const TemplateTypeParmType*) { |
| 2927 | return false; |
| 2928 | } |
| 2929 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2930 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 2931 | const SubstTemplateTypeParmPackType *) { |
| 2932 | return false; |
| 2933 | } |
| 2934 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2935 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 2936 | const TemplateSpecializationType*) { |
| 2937 | return false; |
| 2938 | } |
| 2939 | |
| 2940 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 2941 | const InjectedClassNameType* T) { |
| 2942 | return VisitTagDecl(T->getDecl()); |
| 2943 | } |
| 2944 | |
| 2945 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 2946 | const DependentNameType* T) { |
| 2947 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 2948 | } |
| 2949 | |
| 2950 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 2951 | const DependentTemplateSpecializationType* T) { |
| 2952 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 2953 | } |
| 2954 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2955 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 2956 | const PackExpansionType* T) { |
| 2957 | return Visit(T->getPattern()); |
| 2958 | } |
| 2959 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2960 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 2961 | return false; |
| 2962 | } |
| 2963 | |
| 2964 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 2965 | const ObjCInterfaceType *) { |
| 2966 | return false; |
| 2967 | } |
| 2968 | |
| 2969 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 2970 | const ObjCObjectPointerType *) { |
| 2971 | return false; |
| 2972 | } |
| 2973 | |
| 2974 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 2975 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
| 2976 | S.Diag(SR.getBegin(), diag::ext_template_arg_local_type) |
| 2977 | << S.Context.getTypeDeclType(Tag) << SR; |
| 2978 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2981 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2982 | S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR; |
| 2983 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 2984 | return true; |
| 2985 | } |
| 2986 | |
| 2987 | return false; |
| 2988 | } |
| 2989 | |
| 2990 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 2991 | NestedNameSpecifier *NNS) { |
| 2992 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 2993 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2994 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2995 | switch (NNS->getKind()) { |
| 2996 | case NestedNameSpecifier::Identifier: |
| 2997 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 2998 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2999 | case NestedNameSpecifier::Global: |
| 3000 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3001 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3002 | case NestedNameSpecifier::TypeSpec: |
| 3003 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3004 | return Visit(QualType(NNS->getAsType(), 0)); |
| 3005 | } |
Fariborz Jahanian | 7b1ec6c | 2010-10-13 16:19:16 +0000 | [diff] [blame] | 3006 | return false; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
| 3009 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3010 | /// \brief Check a template argument against its corresponding |
| 3011 | /// template type parameter. |
| 3012 | /// |
| 3013 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 3014 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3015 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3016 | TypeSourceInfo *ArgInfo) { |
| 3017 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3018 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 3019 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 3020 | |
| 3021 | if (Arg->isVariablyModifiedType()) { |
| 3022 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3023 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3024 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3027 | // C++03 [temp.arg.type]p2: |
| 3028 | // A local type, a type with no linkage, an unnamed type or a type |
| 3029 | // compounded from any of these types shall not be used as a |
| 3030 | // template-argument for a template type-parameter. |
| 3031 | // |
| 3032 | // C++0x allows these, and even in C++03 we allow them as an extension with |
| 3033 | // a warning. |
Douglas Gregor | db4d4bb | 2010-10-13 18:05:20 +0000 | [diff] [blame] | 3034 | if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3035 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 3036 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 3037 | } |
| 3038 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3039 | return false; |
| 3040 | } |
| 3041 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3042 | /// \brief Checks whether the given template argument is the address |
| 3043 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3044 | static bool |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3045 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 3046 | NonTypeTemplateParmDecl *Param, |
| 3047 | QualType ParamType, |
| 3048 | Expr *ArgIn, |
| 3049 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3050 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3051 | Expr *Arg = ArgIn; |
| 3052 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3053 | |
| 3054 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3055 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3056 | Arg = Cast->getSubExpr(); |
| 3057 | |
| 3058 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3059 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3060 | // A template-argument for a non-type, non-template |
| 3061 | // template-parameter shall be one of: [...] |
| 3062 | // |
| 3063 | // -- the address of an object or function with external |
| 3064 | // linkage, including function templates and function |
| 3065 | // template-ids but excluding non-static class members, |
| 3066 | // expressed as & id-expression where the & is optional if |
| 3067 | // the name refers to a function or array, or if the |
| 3068 | // corresponding template-parameter is a reference; or |
| 3069 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3070 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3071 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3072 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3073 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3074 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3075 | if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3076 | S.Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3077 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3078 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3079 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | Arg = Parens->getSubExpr(); |
| 3083 | } |
| 3084 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3085 | bool AddressTaken = false; |
| 3086 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3087 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3088 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3089 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3090 | AddressTaken = true; |
| 3091 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 3092 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3093 | } else |
| 3094 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 3095 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3096 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3097 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 3098 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3099 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3100 | return true; |
| 3101 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3102 | |
| 3103 | // Stop checking the precise nature of the argument if it is value dependent, |
| 3104 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3105 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3106 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3107 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3108 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3109 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3110 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 3111 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3112 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3113 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3114 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3115 | return true; |
| 3116 | } |
| 3117 | |
| 3118 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3119 | |
| 3120 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3121 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 3122 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3123 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3124 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3125 | return true; |
| 3126 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3127 | |
| 3128 | // Cannot refer to non-static member functions |
| 3129 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3130 | if (!Method->isStatic()) { |
| 3131 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3132 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3133 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3134 | return true; |
| 3135 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3136 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3137 | // Functions must have external linkage. |
| 3138 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3139 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3140 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3141 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3142 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3143 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3144 | << true; |
| 3145 | return true; |
| 3146 | } |
| 3147 | |
| 3148 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3149 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3150 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3151 | // If the template parameter has pointer type, the function decays. |
| 3152 | if (ParamType->isPointerType() && !AddressTaken) |
| 3153 | ArgType = S.Context.getPointerType(Func->getType()); |
| 3154 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 3155 | // If we originally had an address-of operator, but the |
| 3156 | // parameter has reference type, complain and (if things look |
| 3157 | // like they will work) drop the address-of operator. |
| 3158 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 3159 | ParamType.getNonReferenceType())) { |
| 3160 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3161 | << ParamType; |
| 3162 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3163 | return true; |
| 3164 | } |
| 3165 | |
| 3166 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3167 | << ParamType |
| 3168 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3169 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3170 | |
| 3171 | ArgType = Func->getType(); |
| 3172 | } |
| 3173 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3174 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3175 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3176 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3177 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3178 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3179 | << true; |
| 3180 | return true; |
| 3181 | } |
| 3182 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3183 | // A value of reference type is not an object. |
| 3184 | if (Var->getType()->isReferenceType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3185 | S.Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3186 | diag::err_template_arg_reference_var) |
| 3187 | << Var->getType() << Arg->getSourceRange(); |
| 3188 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3189 | return true; |
| 3190 | } |
| 3191 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3192 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3193 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3194 | |
| 3195 | // If the template parameter has pointer type, we must have taken |
| 3196 | // the address of this object. |
| 3197 | if (ParamType->isReferenceType()) { |
| 3198 | if (AddressTaken) { |
| 3199 | // If we originally had an address-of operator, but the |
| 3200 | // parameter has reference type, complain and (if things look |
| 3201 | // like they will work) drop the address-of operator. |
| 3202 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3203 | ParamType.getNonReferenceType())) { |
| 3204 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3205 | << ParamType; |
| 3206 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3207 | return true; |
| 3208 | } |
| 3209 | |
| 3210 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3211 | << ParamType |
| 3212 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3213 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3214 | |
| 3215 | ArgType = Var->getType(); |
| 3216 | } |
| 3217 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3218 | if (Var->getType()->isArrayType()) { |
| 3219 | // Array-to-pointer decay. |
| 3220 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3221 | } else { |
| 3222 | // If the template parameter has pointer type but the address of |
| 3223 | // this object was not taken, complain and (possibly) recover by |
| 3224 | // taking the address of the entity. |
| 3225 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3226 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3227 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3228 | << ParamType; |
| 3229 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3230 | return true; |
| 3231 | } |
| 3232 | |
| 3233 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3234 | << ParamType |
| 3235 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3236 | |
| 3237 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3238 | } |
| 3239 | } |
| 3240 | } else { |
| 3241 | // We found something else, but we don't know specifically what it is. |
| 3242 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3243 | diag::err_template_arg_not_object_or_func) |
| 3244 | << Arg->getSourceRange(); |
| 3245 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3246 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3247 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3248 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3249 | if (ParamType->isPointerType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3250 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3251 | S.IsQualificationConversion(ArgType, ParamType, false)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3252 | // For pointer-to-object types, qualification conversions are |
| 3253 | // permitted. |
| 3254 | } else { |
| 3255 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3256 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3257 | // C++ [temp.arg.nontype]p5b3: |
| 3258 | // For a non-type template-parameter of type reference to |
| 3259 | // object, no conversions apply. The type referred to by the |
| 3260 | // reference may be more cv-qualified than the (otherwise |
| 3261 | // identical) type of the template- argument. The |
| 3262 | // template-parameter is bound directly to the |
| 3263 | // template-argument, which shall be an lvalue. |
| 3264 | |
| 3265 | // FIXME: Other qualifiers? |
| 3266 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3267 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3268 | |
| 3269 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 3270 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3271 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3272 | << ParamType << Arg->getType() |
| 3273 | << Arg->getSourceRange(); |
| 3274 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3275 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3276 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3277 | } |
| 3278 | } |
| 3279 | |
| 3280 | // At this point, the template argument refers to an object or |
| 3281 | // function with external linkage. We now need to check whether the |
| 3282 | // argument and parameter types are compatible. |
| 3283 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3284 | ParamType.getNonReferenceType())) { |
| 3285 | // We can't perform this conversion or binding. |
| 3286 | if (ParamType->isReferenceType()) |
| 3287 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 3288 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 3289 | else |
| 3290 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 3291 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 3292 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3293 | return true; |
| 3294 | } |
| 3295 | } |
| 3296 | |
| 3297 | // Create the template argument. |
| 3298 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 3299 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3300 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
| 3303 | /// \brief Checks whether the given template argument is a pointer to |
| 3304 | /// member constant according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3305 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3306 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3307 | bool Invalid = false; |
| 3308 | |
| 3309 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3310 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3311 | Arg = Cast->getSubExpr(); |
| 3312 | |
| 3313 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3314 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3315 | // A template-argument for a non-type, non-template |
| 3316 | // template-parameter shall be one of: [...] |
| 3317 | // |
| 3318 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3319 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3320 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3321 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3322 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3323 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3324 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3325 | if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3326 | Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3327 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3328 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3329 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | Arg = Parens->getSubExpr(); |
| 3333 | } |
| 3334 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3335 | // A pointer-to-member constant written &Class::member. |
| 3336 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3337 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3338 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3339 | if (DRE && !DRE->getQualifier()) |
| 3340 | DRE = 0; |
| 3341 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3342 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3343 | // A constant of pointer-to-member type. |
| 3344 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3345 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3346 | if (VD->getType()->isMemberPointerType()) { |
| 3347 | if (isa<NonTypeTemplateParmDecl>(VD) || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3348 | (isa<VarDecl>(VD) && |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3349 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3350 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3351 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3352 | else |
| 3353 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3354 | return Invalid; |
| 3355 | } |
| 3356 | } |
| 3357 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3358 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3359 | DRE = 0; |
| 3360 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3361 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3362 | if (!DRE) |
| 3363 | return Diag(Arg->getSourceRange().getBegin(), |
| 3364 | diag::err_template_arg_not_pointer_to_member_form) |
| 3365 | << Arg->getSourceRange(); |
| 3366 | |
| 3367 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3368 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3369 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3370 | "Only non-static member pointers can make it here"); |
| 3371 | |
| 3372 | // Okay: this is the address of a non-static member, and therefore |
| 3373 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3374 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3375 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3376 | else |
| 3377 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3378 | return Invalid; |
| 3379 | } |
| 3380 | |
| 3381 | // 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] | 3382 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3383 | diag::err_template_arg_not_pointer_to_member_form) |
| 3384 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3385 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3386 | diag::note_template_arg_refers_here); |
| 3387 | return true; |
| 3388 | } |
| 3389 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3390 | /// \brief Check a template argument against its corresponding |
| 3391 | /// non-type template parameter. |
| 3392 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3393 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3394 | /// If an error occurred, it returns ExprError(); otherwise, it |
| 3395 | /// returns the converted template argument. \p |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3396 | /// InstantiatedParamType is the type of the non-type template |
| 3397 | /// parameter after it has been instantiated. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3398 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 3399 | QualType InstantiatedParamType, Expr *Arg, |
| 3400 | TemplateArgument &Converted, |
| 3401 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3402 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3403 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3404 | // If either the parameter has a dependent type or the argument is |
| 3405 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3406 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3407 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3408 | Converted = TemplateArgument(Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3409 | return Owned(Arg); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3410 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3411 | |
| 3412 | // C++ [temp.arg.nontype]p5: |
| 3413 | // The following conversions are performed on each expression used |
| 3414 | // as a non-type template-argument. If a non-type |
| 3415 | // template-argument cannot be converted to the type of the |
| 3416 | // corresponding template-parameter then the program is |
| 3417 | // ill-formed. |
| 3418 | // |
| 3419 | // -- for a non-type template-parameter of integral or |
| 3420 | // enumeration type, integral promotions (4.5) and integral |
| 3421 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3422 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3423 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3424 | if (ParamType->isIntegralOrEnumerationType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3425 | // C++ [temp.arg.nontype]p1: |
| 3426 | // A template-argument for a non-type, non-template |
| 3427 | // template-parameter shall be one of: |
| 3428 | // |
| 3429 | // -- an integral constant-expression of integral or enumeration |
| 3430 | // type; or |
| 3431 | // -- the name of a non-type template-parameter; or |
| 3432 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3433 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3434 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3435 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3436 | diag::err_template_arg_not_integral_or_enumeral) |
| 3437 | << ArgType << Arg->getSourceRange(); |
| 3438 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3439 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3440 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3441 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3442 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 3443 | << ArgType << Arg->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3444 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3447 | // From here on out, all we care about are the unqualified forms |
| 3448 | // of the parameter and argument types. |
| 3449 | ParamType = ParamType.getUnqualifiedType(); |
| 3450 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3451 | |
| 3452 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3453 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3454 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3455 | } else if (CTAK == CTAK_Deduced) { |
| 3456 | // C++ [temp.deduct.type]p17: |
| 3457 | // If, in the declaration of a function template with a non-type |
| 3458 | // template-parameter, the non-type template- parameter is used |
| 3459 | // in an expression in the function parameter-list and, if the |
| 3460 | // corresponding template-argument is deduced, the |
| 3461 | // template-argument type shall match the type of the |
| 3462 | // template-parameter exactly, except that a template-argument |
| 3463 | // deduced from an array bound may be of any integral type. |
| 3464 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3465 | << ArgType << ParamType; |
| 3466 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3467 | return ExprError(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3468 | } else if (ParamType->isBooleanType()) { |
| 3469 | // This is an integral-to-boolean conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3470 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3471 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3472 | !ParamType->isEnumeralType()) { |
| 3473 | // This is an integral promotion or conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3474 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3475 | } else { |
| 3476 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3477 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3478 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3479 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3480 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3481 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3482 | } |
| 3483 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3484 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3485 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3486 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3487 | |
| 3488 | if (!Arg->isValueDependent()) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3489 | llvm::APSInt OldValue = Value; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3490 | |
| 3491 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3492 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3493 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3494 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3495 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3496 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3497 | |
| 3498 | // Complain if an unsigned parameter received a negative value. |
| 3499 | if (IntegerType->isUnsignedIntegerType() |
| 3500 | && (OldValue.isSigned() && OldValue.isNegative())) { |
| 3501 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3502 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3503 | << Arg->getSourceRange(); |
| 3504 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3505 | } |
| 3506 | |
| 3507 | // Complain if we overflowed the template parameter's type. |
| 3508 | unsigned RequiredBits; |
| 3509 | if (IntegerType->isUnsignedIntegerType()) |
| 3510 | RequiredBits = OldValue.getActiveBits(); |
| 3511 | else if (OldValue.isUnsigned()) |
| 3512 | RequiredBits = OldValue.getActiveBits() + 1; |
| 3513 | else |
| 3514 | RequiredBits = OldValue.getMinSignedBits(); |
| 3515 | if (RequiredBits > AllowedBits) { |
| 3516 | Diag(Arg->getSourceRange().getBegin(), |
| 3517 | diag::warn_template_arg_too_large) |
| 3518 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3519 | << Arg->getSourceRange(); |
| 3520 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3521 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3522 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3523 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3524 | // Add the value of this argument to the list of converted |
| 3525 | // arguments. We use the bitwidth and signedness of the template |
| 3526 | // parameter. |
| 3527 | if (Arg->isValueDependent()) { |
| 3528 | // The argument is value-dependent. Create a new |
| 3529 | // TemplateArgument with the converted expression. |
| 3530 | Converted = TemplateArgument(Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3531 | return Owned(Arg); |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3532 | } |
| 3533 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3534 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3535 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3536 | : IntegerType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3537 | return Owned(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3538 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3539 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3540 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 3541 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3542 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 3543 | // from a template argument of type std::nullptr_t to a non-type |
| 3544 | // template parameter of type pointer to object, pointer to |
| 3545 | // function, or pointer-to-member, respectively. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3546 | if (ArgType->isNullPtrType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3547 | (ParamType->isPointerType() || ParamType->isMemberPointerType())) { |
| 3548 | Converted = TemplateArgument((NamedDecl *)0); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3549 | return Owned(Arg); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3550 | } |
| 3551 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3552 | // Handle pointer-to-function, reference-to-function, and |
| 3553 | // pointer-to-member-function all in (roughly) the same way. |
| 3554 | if (// -- For a non-type template-parameter of type pointer to |
| 3555 | // function, only the function-to-pointer conversion (4.3) is |
| 3556 | // applied. If the template-argument represents a set of |
| 3557 | // overloaded functions (or a pointer to such), the matching |
| 3558 | // function is selected from the set (13.4). |
| 3559 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3560 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3561 | // -- For a non-type template-parameter of type reference to |
| 3562 | // function, no conversions apply. If the template-argument |
| 3563 | // represents a set of overloaded functions, the matching |
| 3564 | // function is selected from the set (13.4). |
| 3565 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3566 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3567 | // -- For a non-type template-parameter of type pointer to |
| 3568 | // member function, no conversions apply. If the |
| 3569 | // template-argument represents a set of overloaded member |
| 3570 | // functions, the matching member function is selected from |
| 3571 | // the set (13.4). |
| 3572 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3573 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3574 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3575 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3576 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3577 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3578 | true, |
| 3579 | FoundResult)) { |
| 3580 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3581 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3582 | |
| 3583 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3584 | ArgType = Arg->getType(); |
| 3585 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3586 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3587 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3588 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3589 | if (!ParamType->isMemberPointerType()) { |
| 3590 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3591 | ParamType, |
| 3592 | Arg, Converted)) |
| 3593 | return ExprError(); |
| 3594 | return Owned(Arg); |
| 3595 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3596 | |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3597 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(), |
| 3598 | false)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3599 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3600 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 3601 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3602 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3603 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3604 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3605 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3606 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3607 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3608 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3609 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3610 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 3611 | return ExprError(); |
| 3612 | return Owned(Arg); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3613 | } |
| 3614 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 3615 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3616 | // -- for a non-type template-parameter of type pointer to |
| 3617 | // object, qualification conversions (4.4) and the |
| 3618 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3619 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3620 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3621 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3622 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3623 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3624 | ParamType, |
| 3625 | Arg, Converted)) |
| 3626 | return ExprError(); |
| 3627 | return Owned(Arg); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3628 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3629 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3630 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3631 | // -- For a non-type template-parameter of type reference to |
| 3632 | // object, no conversions apply. The type referred to by the |
| 3633 | // reference may be more cv-qualified than the (otherwise |
| 3634 | // identical) type of the template-argument. The |
| 3635 | // template-parameter is bound directly to the |
| 3636 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3637 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3638 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3639 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3640 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3641 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 3642 | ParamRefType->getPointeeType(), |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3643 | true, |
| 3644 | FoundResult)) { |
| 3645 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3646 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3647 | |
| 3648 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3649 | ArgType = Arg->getType(); |
| 3650 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3651 | return ExprError(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3652 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3653 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3654 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3655 | ParamType, |
| 3656 | Arg, Converted)) |
| 3657 | return ExprError(); |
| 3658 | return Owned(Arg); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3659 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3660 | |
| 3661 | // -- For a non-type template-parameter of type pointer to data |
| 3662 | // member, qualification conversions (4.4) are applied. |
| 3663 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 3664 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 3665 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3666 | // Types match exactly: nothing more to do here. |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3667 | } else if (IsQualificationConversion(ArgType, ParamType, false)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3668 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)).take(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3669 | } else { |
| 3670 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3671 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3672 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3673 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3674 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3675 | return ExprError(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3678 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 3679 | return ExprError(); |
| 3680 | return Owned(Arg); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
| 3683 | /// \brief Check a template argument against its corresponding |
| 3684 | /// template template parameter. |
| 3685 | /// |
| 3686 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 3687 | /// It returns true if an error occurred, and false otherwise. |
| 3688 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3689 | const TemplateArgumentLoc &Arg) { |
| 3690 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 3691 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 3692 | if (!Template) { |
| 3693 | // Any dependent template name is fine. |
| 3694 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 3695 | return false; |
| 3696 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3697 | |
| 3698 | // C++ [temp.arg.template]p1: |
| 3699 | // A template-argument for a template template-parameter shall be |
| 3700 | // the name of a class template, expressed as id-expression. Only |
| 3701 | // primary class templates are considered when matching the |
| 3702 | // template template argument with the corresponding parameter; |
| 3703 | // partial specializations are not considered even if their |
| 3704 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3705 | // |
| 3706 | // Note that we also allow template template parameters here, which |
| 3707 | // will happen when we are dealing with, e.g., class template |
| 3708 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3709 | if (!isa<ClassTemplateDecl>(Template) && |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3710 | !isa<TemplateTemplateParmDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3711 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3712 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3713 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3714 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3715 | << Template; |
| 3716 | } |
| 3717 | |
| 3718 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 3719 | Param->getTemplateParameters(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3720 | true, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3721 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3722 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3725 | /// \brief Given a non-type template argument that refers to a |
| 3726 | /// declaration and the type of its corresponding non-type template |
| 3727 | /// parameter, produce an expression that properly refers to that |
| 3728 | /// declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3729 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3730 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 3731 | QualType ParamType, |
| 3732 | SourceLocation Loc) { |
| 3733 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 3734 | "Only declaration template arguments permitted here"); |
| 3735 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 3736 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3737 | if (VD->getDeclContext()->isRecord() && |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3738 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 3739 | // If the value is a class member, we might have a pointer-to-member. |
| 3740 | // Determine whether the non-type template template parameter is of |
| 3741 | // pointer-to-member type. If so, we need to build an appropriate |
| 3742 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 3743 | // would refer to the member itself. |
| 3744 | if (ParamType->isMemberPointerType()) { |
| 3745 | QualType ClassType |
| 3746 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 3747 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3748 | = NestedNameSpecifier::Create(Context, 0, false, |
| 3749 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3750 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 3751 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3752 | |
| 3753 | // The actual value-ness of this is unimportant, but for |
| 3754 | // internal consistency's sake, references to instance methods |
| 3755 | // are r-values. |
| 3756 | ExprValueKind VK = VK_LValue; |
| 3757 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 3758 | VK = VK_RValue; |
| 3759 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3760 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3761 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3762 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3763 | Loc, |
| 3764 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3765 | if (RefExpr.isInvalid()) |
| 3766 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3767 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3768 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3769 | |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3770 | // We might need to perform a trailing qualification conversion, since |
| 3771 | // the element type on the parameter could be more qualified than the |
| 3772 | // element type in the expression we constructed. |
| 3773 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3774 | ParamType.getUnqualifiedType(), false)) |
| 3775 | RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3776 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3777 | assert(!RefExpr.isInvalid() && |
| 3778 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3779 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3780 | return move(RefExpr); |
| 3781 | } |
| 3782 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3783 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3784 | QualType T = VD->getType().getNonReferenceType(); |
| 3785 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3786 | // When the non-type template parameter is a pointer, take the |
| 3787 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3788 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3789 | if (RefExpr.isInvalid()) |
| 3790 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3791 | |
| 3792 | if (T->isFunctionType() || T->isArrayType()) { |
| 3793 | // Decay functions and arrays. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3794 | RefExpr = DefaultFunctionArrayConversion(RefExpr.take()); |
| 3795 | if (RefExpr.isInvalid()) |
| 3796 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3797 | |
| 3798 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3799 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3800 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3801 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3802 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3805 | ExprValueKind VK = VK_RValue; |
| 3806 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3807 | // If the non-type template parameter has reference type, qualify the |
| 3808 | // resulting declaration reference with the extra qualifiers on the |
| 3809 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3810 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 3811 | VK = VK_LValue; |
| 3812 | T = Context.getQualifiedType(T, |
| 3813 | TargetRef->getPointeeType().getQualifiers()); |
| 3814 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3815 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3816 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
| 3819 | /// \brief Construct a new expression that refers to the given |
| 3820 | /// integral template argument with the given source-location |
| 3821 | /// information. |
| 3822 | /// |
| 3823 | /// This routine takes care of the mapping from an integral template |
| 3824 | /// argument (which may have any integral type) to the appropriate |
| 3825 | /// literal value. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3826 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3827 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 3828 | SourceLocation Loc) { |
| 3829 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3830 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3831 | QualType T = Arg.getIntegralType(); |
| 3832 | if (T->isCharType() || T->isWideCharType()) |
| 3833 | return Owned(new (Context) CharacterLiteral( |
| 3834 | Arg.getAsIntegral()->getZExtValue(), |
| 3835 | T->isWideCharType(), |
| 3836 | T, |
| 3837 | Loc)); |
| 3838 | if (T->isBooleanType()) |
| 3839 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 3840 | Arg.getAsIntegral()->getBoolValue(), |
| 3841 | T, |
| 3842 | Loc)); |
| 3843 | |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 3844 | QualType BT; |
| 3845 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 3846 | BT = ET->getDecl()->getPromotionType(); |
| 3847 | else |
| 3848 | BT = T; |
| 3849 | |
| 3850 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
Douglas Gregor | 8f5667d | 2011-02-18 02:12:44 +0000 | [diff] [blame] | 3851 | if (T->isEnumeralType()) { |
| 3852 | // FIXME: This is a hack. We need a better way to handle substituted |
| 3853 | // non-type template parameters. |
| 3854 | E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, |
| 3855 | E, 0, |
| 3856 | Context.getTrivialTypeSourceInfo(T, Loc), |
| 3857 | Loc, Loc); |
| 3858 | } |
| 3859 | |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 3860 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3863 | /// \brief Match two template parameters within template parameter lists. |
| 3864 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 3865 | bool Complain, |
| 3866 | Sema::TemplateParameterListEqualKind Kind, |
| 3867 | SourceLocation TemplateArgLoc) { |
| 3868 | // Check the actual kind (type, non-type, template). |
| 3869 | if (Old->getKind() != New->getKind()) { |
| 3870 | if (Complain) { |
| 3871 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 3872 | if (TemplateArgLoc.isValid()) { |
| 3873 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3874 | NextDiag = diag::note_template_param_different_kind; |
| 3875 | } |
| 3876 | S.Diag(New->getLocation(), NextDiag) |
| 3877 | << (Kind != Sema::TPL_TemplateMatch); |
| 3878 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 3879 | << (Kind != Sema::TPL_TemplateMatch); |
| 3880 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3881 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3882 | return false; |
| 3883 | } |
| 3884 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3885 | // Check that both are parameter packs are neither are parameter packs. |
| 3886 | // However, if we are matching a template template argument to a |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3887 | // template template parameter, the template template parameter can have |
| 3888 | // a parameter pack where the template template argument does not. |
| 3889 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 3890 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 3891 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3892 | if (Complain) { |
| 3893 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 3894 | if (TemplateArgLoc.isValid()) { |
| 3895 | S.Diag(TemplateArgLoc, |
| 3896 | diag::err_template_arg_template_params_mismatch); |
| 3897 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 3898 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3899 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3900 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 3901 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 3902 | : 2; |
| 3903 | S.Diag(New->getLocation(), NextDiag) |
| 3904 | << ParamKind << New->isParameterPack(); |
| 3905 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 3906 | << ParamKind << Old->isParameterPack(); |
| 3907 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3908 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3909 | return false; |
| 3910 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3911 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3912 | // For non-type template parameters, check the type of the parameter. |
| 3913 | if (NonTypeTemplateParmDecl *OldNTTP |
| 3914 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 3915 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3916 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3917 | // If we are matching a template template argument to a template |
| 3918 | // template parameter and one of the non-type template parameter types |
| 3919 | // is dependent, then we must wait until template instantiation time |
| 3920 | // to actually compare the arguments. |
| 3921 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 3922 | (OldNTTP->getType()->isDependentType() || |
| 3923 | NewNTTP->getType()->isDependentType())) |
| 3924 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3925 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3926 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 3927 | if (Complain) { |
| 3928 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 3929 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3930 | S.Diag(TemplateArgLoc, |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3931 | diag::err_template_arg_template_params_mismatch); |
| 3932 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 3933 | } |
| 3934 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 3935 | << NewNTTP->getType() |
| 3936 | << (Kind != Sema::TPL_TemplateMatch); |
| 3937 | S.Diag(OldNTTP->getLocation(), |
| 3938 | diag::note_template_nontype_parm_prev_declaration) |
| 3939 | << OldNTTP->getType(); |
| 3940 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3941 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3942 | return false; |
| 3943 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3944 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3945 | return true; |
| 3946 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3947 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3948 | // For template template parameters, check the template parameter types. |
| 3949 | // The template parameter lists of template template |
| 3950 | // parameters must agree. |
| 3951 | if (TemplateTemplateParmDecl *OldTTP |
| 3952 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3953 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3954 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 3955 | OldTTP->getTemplateParameters(), |
| 3956 | Complain, |
| 3957 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3958 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3959 | : Kind), |
| 3960 | TemplateArgLoc); |
| 3961 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3962 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3963 | return true; |
| 3964 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3965 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3966 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 3967 | /// lists. |
| 3968 | static |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3969 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3970 | TemplateParameterList *New, |
| 3971 | TemplateParameterList *Old, |
| 3972 | Sema::TemplateParameterListEqualKind Kind, |
| 3973 | SourceLocation TemplateArgLoc) { |
| 3974 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 3975 | if (TemplateArgLoc.isValid()) { |
| 3976 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3977 | NextDiag = diag::note_template_param_list_different_arity; |
| 3978 | } |
| 3979 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 3980 | << (New->size() > Old->size()) |
| 3981 | << (Kind != Sema::TPL_TemplateMatch) |
| 3982 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 3983 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 3984 | << (Kind != Sema::TPL_TemplateMatch) |
| 3985 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 3986 | } |
| 3987 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3988 | /// \brief Determine whether the given template parameter lists are |
| 3989 | /// equivalent. |
| 3990 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3991 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3992 | /// source code as part of a new template declaration. |
| 3993 | /// |
| 3994 | /// \param Old The old template parameter list, typically found via |
| 3995 | /// name lookup of the template declared with this template parameter |
| 3996 | /// list. |
| 3997 | /// |
| 3998 | /// \param Complain If true, this routine will produce a diagnostic if |
| 3999 | /// the template parameter lists are not equivalent. |
| 4000 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4001 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4002 | /// |
| 4003 | /// \param TemplateArgLoc If this source location is valid, then we |
| 4004 | /// are actually checking the template parameter list of a template |
| 4005 | /// argument (New) against the template parameter list of its |
| 4006 | /// corresponding template template parameter (Old). We produce |
| 4007 | /// slightly different diagnostics in this scenario. |
| 4008 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4009 | /// \returns True if the template parameter lists are equal, false |
| 4010 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4011 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4012 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 4013 | TemplateParameterList *Old, |
| 4014 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4015 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4016 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4017 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 4018 | if (Complain) |
| 4019 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4020 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4021 | |
| 4022 | return false; |
| 4023 | } |
| 4024 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4025 | // C++0x [temp.arg.template]p3: |
| 4026 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4027 | // when each of the template parameters in the template-parameter-list of |
| 4028 | // the template-argument's corresponding class template or template alias |
| 4029 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4030 | // template-parameter-list of P. [...] |
| 4031 | TemplateParameterList::iterator NewParm = New->begin(); |
| 4032 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4033 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4034 | OldParmEnd = Old->end(); |
| 4035 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 4036 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 4037 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4038 | if (NewParm == NewParmEnd) { |
| 4039 | if (Complain) |
| 4040 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4041 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4042 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4043 | return false; |
| 4044 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4045 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4046 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4047 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4048 | return false; |
| 4049 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4050 | ++NewParm; |
| 4051 | continue; |
| 4052 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4053 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4054 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4055 | // [...] When P's template- parameter-list contains a template parameter |
| 4056 | // pack (14.5.3), the template parameter pack will match zero or more |
| 4057 | // template parameters or template parameter packs in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4058 | // template-parameter-list of A with the same type and form as the |
| 4059 | // template parameter pack in P (ignoring whether those template |
| 4060 | // parameters are template parameter packs). |
| 4061 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 4062 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4063 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4064 | return false; |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4065 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4066 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4067 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4068 | // Make sure we exhausted all of the arguments. |
| 4069 | if (NewParm != NewParmEnd) { |
| 4070 | if (Complain) |
| 4071 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4072 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4073 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4074 | return false; |
| 4075 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4076 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4077 | return true; |
| 4078 | } |
| 4079 | |
| 4080 | /// \brief Check whether a template can be declared within this scope. |
| 4081 | /// |
| 4082 | /// If the template declaration is valid in this scope, returns |
| 4083 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4084 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4085 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4086 | // Find the nearest enclosing declaration scope. |
| 4087 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4088 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4089 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4090 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4091 | // C++ [temp]p2: |
| 4092 | // A template-declaration can appear only as a namespace scope or |
| 4093 | // class scope declaration. |
| 4094 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4095 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 4096 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4097 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4098 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4099 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4100 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4101 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4102 | |
| 4103 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 4104 | return false; |
| 4105 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4106 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4107 | diag::err_template_outside_namespace_or_class_scope) |
| 4108 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4109 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4110 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4111 | /// \brief Determine what kind of template specialization the given declaration |
| 4112 | /// is. |
| 4113 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 4114 | if (!D) |
| 4115 | return TSK_Undeclared; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4116 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4117 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 4118 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4119 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 4120 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4121 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 4122 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4123 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4124 | return TSK_Undeclared; |
| 4125 | } |
| 4126 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4127 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4128 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4129 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4130 | /// This routine determines whether a template specialization can be declared |
| 4131 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4132 | /// |
| 4133 | /// \param S the semantic analysis object for which this check is being |
| 4134 | /// performed. |
| 4135 | /// |
| 4136 | /// \param Specialized the entity being specialized or instantiated, which |
| 4137 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4138 | /// a member of a class template (member function, static data member, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4139 | /// member class). |
| 4140 | /// |
| 4141 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 4142 | /// |
| 4143 | /// \param Loc the location of the explicit specialization or instantiation of |
| 4144 | /// this entity. |
| 4145 | /// |
| 4146 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 4147 | /// a class template. |
| 4148 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4149 | /// \returns true if there was an error that we cannot recover from, false |
| 4150 | /// otherwise. |
| 4151 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 4152 | NamedDecl *Specialized, |
| 4153 | NamedDecl *PrevDecl, |
| 4154 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4155 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4156 | // Keep these "kind" numbers in sync with the %select statements in the |
| 4157 | // various diagnostics emitted by this routine. |
| 4158 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4159 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4160 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4161 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4162 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4163 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4164 | EntityKind = 3; |
| 4165 | else if (isa<VarDecl>(Specialized)) |
| 4166 | EntityKind = 4; |
| 4167 | else if (isa<RecordDecl>(Specialized)) |
| 4168 | EntityKind = 5; |
| 4169 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4170 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 4171 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4172 | return true; |
| 4173 | } |
| 4174 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4175 | // C++ [temp.expl.spec]p2: |
| 4176 | // An explicit specialization shall be declared in the namespace |
| 4177 | // of which the template is a member, or, for member templates, in |
| 4178 | // the namespace of which the enclosing class or enclosing class |
| 4179 | // template is a member. An explicit specialization of a member |
| 4180 | // function, member class or static data member of a class |
| 4181 | // template shall be declared in the namespace of which the class |
| 4182 | // template is a member. Such a declaration may also be a |
| 4183 | // definition. If the declaration is not a definition, the |
| 4184 | // specialization may be defined later in the name- space in which |
| 4185 | // the explicit specialization was declared, or in a namespace |
| 4186 | // that encloses the one in which the explicit specialization was |
| 4187 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4188 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4189 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4190 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4191 | return true; |
| 4192 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4193 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4194 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 4195 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4196 | << Specialized; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4197 | return true; |
| 4198 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4199 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4200 | // C++ [temp.class.spec]p6: |
| 4201 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4202 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4203 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4204 | bool ComplainedAboutScope = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4205 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4206 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4207 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4208 | if ((!PrevDecl || |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4209 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4210 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4211 | // C++ [temp.exp.spec]p2: |
| 4212 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4213 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4214 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4215 | // An explicit specialization of a member function, member class or |
| 4216 | // static data member of a class template shall be declared in the |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4217 | // namespace of which the class template is a member. |
| 4218 | // |
| 4219 | // C++0x [temp.expl.spec]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4220 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4221 | // the specialized template. |
| 4222 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) && |
| 4223 | !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) { |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4224 | bool IsCPlusPlus0xExtension |
| 4225 | = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4226 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4227 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4228 | ? diag::ext_template_spec_decl_out_of_scope_global |
| 4229 | : diag::err_template_spec_decl_out_of_scope_global) |
| 4230 | << EntityKind << Specialized; |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4231 | else if (isa<NamespaceDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4232 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4233 | ? diag::ext_template_spec_decl_out_of_scope |
| 4234 | : diag::err_template_spec_decl_out_of_scope) |
| 4235 | << EntityKind << Specialized |
| 4236 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4237 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4238 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 4239 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4240 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4241 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4242 | |
| 4243 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4244 | // namespace. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4245 | // Note that HandleDeclarator() performs this check for explicit |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4246 | // specializations of function templates, static data members, and member |
| 4247 | // functions, so we skip the check here for those kinds of entities. |
| 4248 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4249 | // Should we refactor that check, so that it occurs later? |
| 4250 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4251 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 4252 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4253 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 4254 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 4255 | << EntityKind << Specialized; |
| 4256 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 4257 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 4258 | << EntityKind << Specialized |
| 4259 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4260 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4261 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4262 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4263 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4264 | // FIXME: check for specialization-after-instantiation errors and such. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4265 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4266 | return false; |
| 4267 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4268 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4269 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 4270 | /// that checks non-type template partial specialization arguments. |
| 4271 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 4272 | NonTypeTemplateParmDecl *Param, |
| 4273 | const TemplateArgument *Args, |
| 4274 | unsigned NumArgs) { |
| 4275 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4276 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 4277 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4278 | Args[I].pack_begin(), |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4279 | Args[I].pack_size())) |
| 4280 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4281 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4282 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4283 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4284 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4285 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4286 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4287 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4288 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4289 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4290 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4291 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4292 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4293 | |
| 4294 | // Strip off any implicit casts we added as part of type checking. |
| 4295 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4296 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4297 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4298 | // C++ [temp.class.spec]p8: |
| 4299 | // A non-type argument is non-specialized if it is the name of a |
| 4300 | // non-type parameter. All other non-type arguments are |
| 4301 | // specialized. |
| 4302 | // |
| 4303 | // Below, we check the two conditions that only apply to |
| 4304 | // specialized non-type arguments, so skip any non-specialized |
| 4305 | // arguments. |
| 4306 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4307 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4308 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4309 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4310 | // C++ [temp.class.spec]p9: |
| 4311 | // Within the argument list of a class template partial |
| 4312 | // specialization, the following restrictions apply: |
| 4313 | // -- A partially specialized non-type argument expression |
| 4314 | // shall not involve a template parameter of the partial |
| 4315 | // specialization except when the argument expression is a |
| 4316 | // simple identifier. |
| 4317 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4318 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4319 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4320 | << ArgExpr->getSourceRange(); |
| 4321 | return true; |
| 4322 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4323 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4324 | // -- The type of a template parameter corresponding to a |
| 4325 | // specialized non-type argument shall not be dependent on a |
| 4326 | // parameter of the specialization. |
| 4327 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4328 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4329 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 4330 | << Param->getType() |
| 4331 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4332 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4333 | return true; |
| 4334 | } |
| 4335 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4336 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4337 | return false; |
| 4338 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4339 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4340 | /// \brief Check the non-type template arguments of a class template |
| 4341 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 4342 | /// |
| 4343 | /// \param TemplateParams the template parameters of the primary class |
| 4344 | /// template. |
| 4345 | /// |
| 4346 | /// \param TemplateArg the template arguments of the class template |
| 4347 | /// partial specialization. |
| 4348 | /// |
| 4349 | /// \returns true if there was an error, false otherwise. |
| 4350 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 4351 | TemplateParameterList *TemplateParams, |
| 4352 | llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
| 4353 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 4354 | |
| 4355 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4356 | NonTypeTemplateParmDecl *Param |
| 4357 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 4358 | if (!Param) |
| 4359 | continue; |
| 4360 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4361 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4362 | &ArgList[I], 1)) |
| 4363 | return true; |
| 4364 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4365 | |
| 4366 | return false; |
| 4367 | } |
| 4368 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4369 | /// \brief Retrieve the previous declaration of the given declaration. |
| 4370 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 4371 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4372 | return VD->getPreviousDeclaration(); |
| 4373 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 4374 | return FD->getPreviousDeclaration(); |
| 4375 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 4376 | return TD->getPreviousDeclaration(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4377 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4378 | return TD->getPreviousDeclaration(); |
| 4379 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 4380 | return FTD->getPreviousDeclaration(); |
| 4381 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 4382 | return CTD->getPreviousDeclaration(); |
| 4383 | return 0; |
| 4384 | } |
| 4385 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4386 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4387 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4388 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4389 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4390 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4391 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4392 | SourceLocation TemplateNameLoc, |
| 4393 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4394 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4395 | SourceLocation RAngleLoc, |
| 4396 | AttributeList *Attr, |
| 4397 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4398 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4399 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4400 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 4401 | // store the location of the outermost template keyword in the declaration. |
| 4402 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
| 4403 | ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation(); |
| 4404 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4405 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4406 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4407 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4408 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4409 | |
| 4410 | if (!ClassTemplate) { |
| 4411 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4412 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4413 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4414 | return true; |
| 4415 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4416 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4417 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4418 | bool isPartialSpecialization = false; |
| 4419 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4420 | // Check the validity of the template headers that introduce this |
| 4421 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4422 | // FIXME: We probably shouldn't complain about these headers for |
| 4423 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4424 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4425 | TemplateParameterList *TemplateParams |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4426 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, |
| 4427 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4428 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4429 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4430 | isExplicitSpecialization, |
| 4431 | Invalid); |
| 4432 | if (Invalid) |
| 4433 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4434 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4435 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4436 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4437 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4438 | if (TUK == TUK_Friend) { |
| 4439 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4440 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4441 | return true; |
| 4442 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4443 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4444 | // C++ [temp.class.spec]p10: |
| 4445 | // The template parameter list of a specialization shall not |
| 4446 | // contain default template argument values. |
| 4447 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4448 | Decl *Param = TemplateParams->getParam(I); |
| 4449 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4450 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4451 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4452 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4453 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4454 | } |
| 4455 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4456 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4457 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4458 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4459 | diag::err_default_arg_in_partial_spec) |
| 4460 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4461 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4462 | } |
| 4463 | } else { |
| 4464 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4465 | if (TTP->hasDefaultArgument()) { |
| 4466 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4467 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4468 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4469 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4470 | } |
| 4471 | } |
| 4472 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4473 | } else if (TemplateParams) { |
| 4474 | if (TUK == TUK_Friend) |
| 4475 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4476 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4477 | SourceRange(TemplateParams->getTemplateLoc(), |
| 4478 | TemplateParams->getRAngleLoc())) |
| 4479 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4480 | else |
| 4481 | isExplicitSpecialization = true; |
| 4482 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4483 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4484 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4485 | isExplicitSpecialization = true; |
| 4486 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4487 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4488 | // Check that the specialization uses the same tag kind as the |
| 4489 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4490 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4491 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4492 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4493 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4494 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4495 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4496 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4497 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4498 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4499 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4500 | diag::note_previous_use); |
| 4501 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4502 | } |
| 4503 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4504 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4505 | TemplateArgumentListInfo TemplateArgs; |
| 4506 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 4507 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4508 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4509 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4510 | // Check for unexpanded parameter packs in any of the template arguments. |
| 4511 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4512 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4513 | UPPC_PartialSpecialization)) |
| 4514 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4515 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4516 | // Check that the template argument list is well-formed for this |
| 4517 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4518 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4519 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4520 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4521 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4522 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4523 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4524 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4525 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4526 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4527 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4528 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4529 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4530 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4531 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4532 | return true; |
| 4533 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4534 | if (!Name.isDependent() && |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4535 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4536 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4537 | TemplateArgs.size())) { |
| 4538 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 4539 | << ClassTemplate->getDeclName(); |
| 4540 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4541 | } |
| 4542 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4543 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4544 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4545 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 4546 | |
| 4547 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4548 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4549 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4550 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 4551 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4552 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4553 | else |
| 4554 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4555 | = ClassTemplate->findSpecialization(Converted.data(), |
| 4556 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4557 | |
| 4558 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4559 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4560 | // Check whether we can declare a class template specialization in |
| 4561 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4562 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4563 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 4564 | TemplateNameLoc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4565 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4566 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4567 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4568 | // The canonical type |
| 4569 | QualType CanonType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4570 | if (PrevDecl && |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4571 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4572 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4573 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4574 | // arguments was referenced but not declared, or we're only |
| 4575 | // referencing this specialization as a friend, reuse that |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4576 | // declaration node as our own, updating its source location and |
| 4577 | // the list of outer template parameters to reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4578 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4579 | Specialization->setLocation(TemplateNameLoc); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4580 | if (TemplateParameterLists.size() > 0) { |
| 4581 | Specialization->setTemplateParameterListsInfo(Context, |
| 4582 | TemplateParameterLists.size(), |
| 4583 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4584 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4585 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4586 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4587 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4588 | // Build the canonical type that describes the converted template |
| 4589 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4590 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4591 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4592 | Converted.data(), |
| 4593 | Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4594 | |
| 4595 | if (Context.hasSameType(CanonType, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4596 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 4597 | // C++ [temp.class.spec]p9b3: |
| 4598 | // |
| 4599 | // -- The argument list of the specialization shall not be identical |
| 4600 | // to the implicit argument list of the primary template. |
| 4601 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 4602 | << (TUK == TUK_Definition) |
| 4603 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 4604 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 4605 | ClassTemplate->getIdentifier(), |
| 4606 | TemplateNameLoc, |
| 4607 | Attr, |
| 4608 | TemplateParams, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 4609 | AS_none, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4610 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 4611 | (TemplateParameterList**) TemplateParameterLists.release()); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4612 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4613 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4614 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4615 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 4616 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4617 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4618 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4619 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4620 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4621 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4622 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 4623 | TemplateParams, |
| 4624 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4625 | Converted.data(), |
| 4626 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4627 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4628 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4629 | PrevPartial, |
| 4630 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4631 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4632 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4633 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4634 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4635 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4636 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4637 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4638 | if (!PrevPartial) |
| 4639 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4640 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4641 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4642 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4643 | // template specialization, make a note of that. |
| 4644 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 4645 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4646 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4647 | // Check that all of the template parameters of the class template |
| 4648 | // partial specialization are deducible from the template |
| 4649 | // arguments. If not, this class template partial specialization |
| 4650 | // will never be used. |
| 4651 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 4652 | DeducibleParams.resize(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4653 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4654 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4655 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4656 | unsigned NumNonDeducible = 0; |
| 4657 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 4658 | if (!DeducibleParams[I]) |
| 4659 | ++NumNonDeducible; |
| 4660 | |
| 4661 | if (NumNonDeducible) { |
| 4662 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 4663 | << (NumNonDeducible > 1) |
| 4664 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 4665 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 4666 | if (!DeducibleParams[I]) { |
| 4667 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 4668 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4669 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4670 | diag::note_partial_spec_unused_parameter) |
| 4671 | << Param->getDeclName(); |
| 4672 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4673 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4674 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 4675 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4676 | } |
| 4677 | } |
| 4678 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4679 | } else { |
| 4680 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4681 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4682 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4683 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4684 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4685 | KWLoc, TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4686 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4687 | Converted.data(), |
| 4688 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4689 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4690 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4691 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4692 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4693 | TemplateParameterLists.size(), |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4694 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4695 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4696 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4697 | if (!PrevDecl) |
| 4698 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4699 | |
| 4700 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4701 | } |
| 4702 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4703 | // C++ [temp.expl.spec]p6: |
| 4704 | // 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] | 4705 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4706 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4707 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4708 | // use occurs; no diagnostic is required. |
| 4709 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4710 | bool Okay = false; |
| 4711 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4712 | // Is there any previous explicit specialization declaration? |
| 4713 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 4714 | Okay = true; |
| 4715 | break; |
| 4716 | } |
| 4717 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4718 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4719 | if (!Okay) { |
| 4720 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 4721 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 4722 | << Context.getTypeDeclType(Specialization) << Range; |
| 4723 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4724 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4725 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4726 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4727 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4728 | return true; |
| 4729 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4730 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4731 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4732 | // If this is not a friend, note that this is an explicit specialization. |
| 4733 | if (TUK != TUK_Friend) |
| 4734 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4735 | |
| 4736 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4737 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4738 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4739 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4740 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4741 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4742 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 4743 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4744 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4745 | } |
| 4746 | } |
| 4747 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 4748 | if (Attr) |
| 4749 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 4750 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 4751 | // Build the fully-sugared type for this class template |
| 4752 | // specialization as the user wrote in the specialization |
| 4753 | // itself. This means that we'll pretty-print the type retrieved |
| 4754 | // from the specialization's declaration the way that the user |
| 4755 | // actually wrote the specialization, rather than formatting the |
| 4756 | // name based on the "canonical" representation used to store the |
| 4757 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4758 | TypeSourceInfo *WrittenTy |
| 4759 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 4760 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4761 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4762 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4763 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4764 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4765 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4766 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4767 | // C++ [temp.expl.spec]p9: |
| 4768 | // A template explicit specialization is in the scope of the |
| 4769 | // namespace in which the template was defined. |
| 4770 | // |
| 4771 | // We actually implement this paragraph where we set the semantic |
| 4772 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 4773 | // but we also maintain the lexical context where the actual |
| 4774 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4775 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4776 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4777 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4778 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4779 | Specialization->startDefinition(); |
| 4780 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4781 | if (TUK == TUK_Friend) { |
| 4782 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 4783 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 4784 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4785 | /*FIXME:*/KWLoc); |
| 4786 | Friend->setAccess(AS_public); |
| 4787 | CurContext->addDecl(Friend); |
| 4788 | } else { |
| 4789 | // Add the specialization into its lexical context, so that it can |
| 4790 | // be seen when iterating through the list of declarations in that |
| 4791 | // context. However, specializations are not found by name lookup. |
| 4792 | CurContext->addDecl(Specialization); |
| 4793 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4794 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4795 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4796 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4797 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4798 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4799 | Declarator &D) { |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4800 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 4801 | } |
| 4802 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4803 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4804 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4805 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4806 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4807 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4808 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4809 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4810 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4811 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4812 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4813 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4814 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4815 | Decl *DP = HandleDeclarator(ParentScope, D, |
| 4816 | move(TemplateParameterLists), |
| 4817 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4818 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4819 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4820 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4821 | FunctionTemplate->getTemplatedDecl()); |
| 4822 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 4823 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 4824 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4825 | } |
| 4826 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4827 | /// \brief Strips various properties off an implicit instantiation |
| 4828 | /// that has just been explicitly specialized. |
| 4829 | static void StripImplicitInstantiation(NamedDecl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4830 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4831 | |
| 4832 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4833 | FD->setInlineSpecified(false); |
| 4834 | } |
| 4835 | } |
| 4836 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4837 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4838 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4839 | /// for those cases where they are required and determining whether the |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4840 | /// new specialization/instantiation will have any effect. |
| 4841 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4842 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4843 | /// instantiation. |
| 4844 | /// |
| 4845 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 4846 | /// |
| 4847 | /// \param PrevDecl the previous declaration of the entity. |
| 4848 | /// |
| 4849 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 4850 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4851 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4852 | /// declaration was instantiated (either implicitly or explicitly). |
| 4853 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4854 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4855 | /// specialization or instantiation has no effect and should be ignored. |
| 4856 | /// |
| 4857 | /// \returns true if there was an error that should prevent the introduction of |
| 4858 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4859 | bool |
| 4860 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 4861 | TemplateSpecializationKind NewTSK, |
| 4862 | NamedDecl *PrevDecl, |
| 4863 | TemplateSpecializationKind PrevTSK, |
| 4864 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4865 | bool &HasNoEffect) { |
| 4866 | HasNoEffect = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4867 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4868 | switch (NewTSK) { |
| 4869 | case TSK_Undeclared: |
| 4870 | case TSK_ImplicitInstantiation: |
| 4871 | assert(false && "Don't check implicit instantiations here"); |
| 4872 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4873 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4874 | case TSK_ExplicitSpecialization: |
| 4875 | switch (PrevTSK) { |
| 4876 | case TSK_Undeclared: |
| 4877 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4878 | // Okay, we're just specializing something that is either already |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4879 | // explicitly specialized or has merely been mentioned without any |
| 4880 | // instantiation. |
| 4881 | return false; |
| 4882 | |
| 4883 | case TSK_ImplicitInstantiation: |
| 4884 | if (PrevPointOfInstantiation.isInvalid()) { |
| 4885 | // The declaration itself has not actually been instantiated, so it is |
| 4886 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4887 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4888 | return false; |
| 4889 | } |
| 4890 | // Fall through |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4891 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4892 | case TSK_ExplicitInstantiationDeclaration: |
| 4893 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4894 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 4895 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4896 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4897 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4898 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4899 | // 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] | 4900 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4901 | // before the first use of that specialization that would cause an |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4902 | // implicit instantiation to take place, in every translation unit in |
| 4903 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4904 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4905 | // Is there any previous explicit specialization declaration? |
| 4906 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 4907 | return false; |
| 4908 | } |
| 4909 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4910 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4911 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4912 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4913 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4914 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4915 | return true; |
| 4916 | } |
| 4917 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4918 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4919 | case TSK_ExplicitInstantiationDeclaration: |
| 4920 | switch (PrevTSK) { |
| 4921 | case TSK_ExplicitInstantiationDeclaration: |
| 4922 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4923 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4924 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4925 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4926 | case TSK_Undeclared: |
| 4927 | case TSK_ImplicitInstantiation: |
| 4928 | // We're explicitly instantiating something that may have already been |
| 4929 | // implicitly instantiated; that's fine. |
| 4930 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4931 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4932 | case TSK_ExplicitSpecialization: |
| 4933 | // C++0x [temp.explicit]p4: |
| 4934 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4935 | // of a template appears after a declaration of an explicit |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4936 | // specialization for that template, the explicit instantiation has no |
| 4937 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4938 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4939 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4940 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4941 | case TSK_ExplicitInstantiationDefinition: |
| 4942 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4943 | // If an entity is the subject of both an explicit instantiation |
| 4944 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4945 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4946 | Diag(NewLoc, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4947 | diag::err_explicit_instantiation_declaration_after_definition); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4948 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4949 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4950 | assert(PrevPointOfInstantiation.isValid() && |
| 4951 | "Explicit instantiation without point of instantiation?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4952 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4953 | return false; |
| 4954 | } |
| 4955 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4956 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4957 | case TSK_ExplicitInstantiationDefinition: |
| 4958 | switch (PrevTSK) { |
| 4959 | case TSK_Undeclared: |
| 4960 | case TSK_ImplicitInstantiation: |
| 4961 | // We're explicitly instantiating something that may have already been |
| 4962 | // implicitly instantiated; that's fine. |
| 4963 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4964 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4965 | case TSK_ExplicitSpecialization: |
| 4966 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 4967 | // For a given set of template parameters, if an explicit |
| 4968 | // instantiation of a template appears after a declaration of |
| 4969 | // an explicit specialization for that template, the explicit |
| 4970 | // instantiation has no effect. |
| 4971 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4972 | // 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] | 4973 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4974 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4975 | if (!getLangOptions().CPlusPlus0x) { |
| 4976 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4977 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4978 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4979 | diag::note_previous_template_specialization); |
| 4980 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4981 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4982 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4983 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4984 | case TSK_ExplicitInstantiationDeclaration: |
| 4985 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4986 | // were previously asked to suppress instantiations. That's fine. |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4987 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4988 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4989 | case TSK_ExplicitInstantiationDefinition: |
| 4990 | // C++0x [temp.spec]p5: |
| 4991 | // For a given template and a given set of template-arguments, |
| 4992 | // - an explicit instantiation definition shall appear at most once |
| 4993 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4994 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4995 | << PrevDecl; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4996 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4997 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4998 | HasNoEffect = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4999 | return false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5000 | } |
| 5001 | break; |
| 5002 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5003 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5004 | assert(false && "Missing specialization/instantiation case?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5005 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5006 | return false; |
| 5007 | } |
| 5008 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5009 | /// \brief Perform semantic analysis for the given dependent function |
| 5010 | /// template specialization. The only possible way to get a dependent |
| 5011 | /// function template specialization is with a friend declaration, |
| 5012 | /// like so: |
| 5013 | /// |
| 5014 | /// template <class T> void foo(T); |
| 5015 | /// template <class T> class A { |
| 5016 | /// friend void foo<>(T); |
| 5017 | /// }; |
| 5018 | /// |
| 5019 | /// There really isn't any useful analysis we can do here, so we |
| 5020 | /// just store the information. |
| 5021 | bool |
| 5022 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 5023 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 5024 | LookupResult &Previous) { |
| 5025 | // Remove anything from Previous that isn't a function template in |
| 5026 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5027 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5028 | LookupResult::Filter F = Previous.makeFilter(); |
| 5029 | while (F.hasNext()) { |
| 5030 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 5031 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5032 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 5033 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5034 | F.erase(); |
| 5035 | } |
| 5036 | F.done(); |
| 5037 | |
| 5038 | // Should this be diagnosed here? |
| 5039 | if (Previous.empty()) return true; |
| 5040 | |
| 5041 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 5042 | ExplicitTemplateArgs); |
| 5043 | return false; |
| 5044 | } |
| 5045 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5046 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5047 | /// specialization. |
| 5048 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5049 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5050 | /// explicit function template specialization. On successful completion, |
| 5051 | /// the function declaration \p FD will become a function template |
| 5052 | /// specialization. |
| 5053 | /// |
| 5054 | /// \param FD the function declaration, which will be updated to become a |
| 5055 | /// function template specialization. |
| 5056 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5057 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 5058 | /// if any. Note that this may be valid info even when 0 arguments are |
| 5059 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 5060 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5061 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5062 | /// \param PrevDecl the set of declarations that may be specialized by |
| 5063 | /// this function specialization. |
| 5064 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5065 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5066 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5067 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5068 | // The set of function template specializations that could match this |
| 5069 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5070 | UnresolvedSet<8> Candidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5071 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5072 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5073 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5074 | I != E; ++I) { |
| 5075 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 5076 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5077 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5078 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5079 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 5080 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5081 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5082 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5083 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5084 | // A trailing template-argument can be left unspecified in the |
| 5085 | // template-id naming an explicit function template specialization |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5086 | // provided it can be deduced from the function argument type. |
| 5087 | // Perform template argument deduction to determine whether we may be |
| 5088 | // specializing this template. |
| 5089 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5090 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5091 | FunctionDecl *Specialization = 0; |
| 5092 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5093 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5094 | FD->getType(), |
| 5095 | Specialization, |
| 5096 | Info)) { |
| 5097 | // FIXME: Template argument deduction failed; record why it failed, so |
| 5098 | // that we can provide nifty diagnostics. |
| 5099 | (void)TDK; |
| 5100 | continue; |
| 5101 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5102 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5103 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5104 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5105 | } |
| 5106 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5107 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5108 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5109 | UnresolvedSetIterator Result |
| 5110 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5111 | TPOC_Other, 0, FD->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5112 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5113 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5114 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5115 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5116 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5117 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5118 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5119 | |
| 5120 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 5121 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | abfb405 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 5122 | |
| 5123 | FunctionTemplateSpecializationInfo *SpecInfo |
| 5124 | = Specialization->getTemplateSpecializationInfo(); |
| 5125 | assert(SpecInfo && "Function template specialization info missing?"); |
| 5126 | { |
| 5127 | // Note: do not overwrite location info if previous template |
| 5128 | // specialization kind was explicit. |
| 5129 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
| 5130 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) |
| 5131 | Specialization->setLocation(FD->getLocation()); |
| 5132 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5133 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5134 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5135 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5136 | |
| 5137 | // If this is a friend declaration, then we're not really declaring |
| 5138 | // an explicit specialization. |
| 5139 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5140 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5141 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5142 | if (!isFriend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5143 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5144 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5145 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5146 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5147 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5148 | |
| 5149 | // C++ [temp.expl.spec]p6: |
| 5150 | // 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] | 5151 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5152 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5153 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5154 | // use occurs; no diagnostic is required. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5155 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5156 | if (!isFriend && |
| 5157 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5158 | TSK_ExplicitSpecialization, |
| 5159 | Specialization, |
| 5160 | SpecInfo->getTemplateSpecializationKind(), |
| 5161 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5162 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5163 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5164 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5165 | // Mark the prior declaration as an explicit specialization, so that later |
| 5166 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5167 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5168 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5169 | MarkUnusedFileScopedDecl(Specialization); |
| 5170 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5171 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5172 | // Turn the given function declaration into a function template |
| 5173 | // specialization, with the template arguments from the previous |
| 5174 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5175 | // Take copies of (semantic and syntactic) template argument lists. |
| 5176 | const TemplateArgumentList* TemplArgs = new (Context) |
| 5177 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
| 5178 | const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs |
| 5179 | ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5180 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5181 | TemplArgs, /*InsertPos=*/0, |
| 5182 | SpecInfo->getTemplateSpecializationKind(), |
| 5183 | TemplArgsAsWritten); |
| 5184 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5185 | // The "previous declaration" for this function template specialization is |
| 5186 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5187 | Previous.clear(); |
| 5188 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5189 | return false; |
| 5190 | } |
| 5191 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5192 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5193 | /// specialization. |
| 5194 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5195 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5196 | /// explicit member function specialization. On successful completion, |
| 5197 | /// the function declaration \p FD will become a member function |
| 5198 | /// specialization. |
| 5199 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5200 | /// \param Member the member declaration, which will be updated to become a |
| 5201 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5202 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5203 | /// \param Previous the set of declarations, one of which may be specialized |
| 5204 | /// by this function specialization; the set will be modified to contain the |
| 5205 | /// redeclared member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5206 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5207 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5208 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5209 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5210 | // Try to find the member we are instantiating. |
| 5211 | NamedDecl *Instantiation = 0; |
| 5212 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5213 | MemberSpecializationInfo *MSInfo = 0; |
| 5214 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5215 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5216 | // Nowhere to look anyway. |
| 5217 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5218 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5219 | I != E; ++I) { |
| 5220 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5221 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5222 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 5223 | Instantiation = Method; |
| 5224 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5225 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5226 | break; |
| 5227 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5228 | } |
| 5229 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5230 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5231 | VarDecl *PrevVar; |
| 5232 | if (Previous.isSingleResult() && |
| 5233 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5234 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5235 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5236 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5237 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5238 | } |
| 5239 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5240 | CXXRecordDecl *PrevRecord; |
| 5241 | if (Previous.isSingleResult() && |
| 5242 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 5243 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5244 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5245 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5246 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5247 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5248 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5249 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5250 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5251 | // specializations are always out-of-line, the caller will complain about |
| 5252 | // this mismatch later. |
| 5253 | return false; |
| 5254 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5255 | |
| 5256 | // If this is a friend, just bail out here before we start turning |
| 5257 | // things into explicit specializations. |
| 5258 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 5259 | // Preserve instantiation information. |
| 5260 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 5261 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 5262 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5263 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5264 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 5265 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 5266 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5267 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5268 | } |
| 5269 | |
| 5270 | Previous.clear(); |
| 5271 | Previous.addDecl(Instantiation); |
| 5272 | return false; |
| 5273 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5274 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5275 | // Make sure that this is a specialization of a member. |
| 5276 | if (!InstantiatedFrom) { |
| 5277 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 5278 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5279 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 5280 | return true; |
| 5281 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5282 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5283 | // C++ [temp.expl.spec]p6: |
| 5284 | // 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] | 5285 | // explicitly specialized then that spe- cialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5286 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5287 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5288 | // use occurs; no diagnostic is required. |
| 5289 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5290 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5291 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5292 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 5293 | TSK_ExplicitSpecialization, |
| 5294 | Instantiation, |
| 5295 | MSInfo->getTemplateSpecializationKind(), |
| 5296 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5297 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5298 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5299 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5300 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5301 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5302 | InstantiatedFrom, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5303 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5304 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5305 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 5306 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5307 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5308 | // the original declaration to note that it is an explicit specialization |
| 5309 | // (if it was previously an implicit instantiation). This latter step |
| 5310 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5311 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5312 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 5313 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 5314 | TSK_ImplicitInstantiation) { |
| 5315 | InstantiationFunction->setTemplateSpecializationKind( |
| 5316 | TSK_ExplicitSpecialization); |
| 5317 | InstantiationFunction->setLocation(Member->getLocation()); |
| 5318 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5319 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5320 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 5321 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5322 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5323 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5324 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5325 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 5326 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 5327 | TSK_ImplicitInstantiation) { |
| 5328 | InstantiationVar->setTemplateSpecializationKind( |
| 5329 | TSK_ExplicitSpecialization); |
| 5330 | InstantiationVar->setLocation(Member->getLocation()); |
| 5331 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5332 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5333 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 5334 | cast<VarDecl>(InstantiatedFrom), |
| 5335 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5336 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5337 | } else { |
| 5338 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5339 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 5340 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 5341 | TSK_ImplicitInstantiation) { |
| 5342 | InstantiationClass->setTemplateSpecializationKind( |
| 5343 | TSK_ExplicitSpecialization); |
| 5344 | InstantiationClass->setLocation(Member->getLocation()); |
| 5345 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5346 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5347 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5348 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5349 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5350 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5351 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5352 | // Save the caller the trouble of having to figure out which declaration |
| 5353 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5354 | Previous.clear(); |
| 5355 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5356 | return false; |
| 5357 | } |
| 5358 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5359 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5360 | /// |
| 5361 | /// \returns true if a serious error occurs, false otherwise. |
| 5362 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5363 | SourceLocation InstLoc, |
| 5364 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5365 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5366 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5367 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5368 | if (CurContext->isRecord()) { |
| 5369 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5370 | << D; |
| 5371 | return true; |
| 5372 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5373 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5374 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5375 | // An explicit instantiation shall appear in an enclosing namespace of its |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5376 | // template. |
| 5377 | // |
| 5378 | // 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] | 5379 | if (S.getLangOptions().CPlusPlus0x && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5380 | !CurContext->Encloses(OrigContext)) { |
| 5381 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5382 | S.Diag(InstLoc, |
| 5383 | S.getLangOptions().CPlusPlus0x? |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5384 | diag::err_explicit_instantiation_out_of_scope |
| 5385 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5386 | << D << NS; |
| 5387 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5388 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5389 | S.getLangOptions().CPlusPlus0x? |
| 5390 | diag::err_explicit_instantiation_must_be_global |
| 5391 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5392 | << D; |
| 5393 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5394 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5395 | } |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5396 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5397 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5398 | // If the name declared in the explicit instantiation is an unqualified |
| 5399 | // name, the explicit instantiation shall appear in the namespace where |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5400 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 5401 | // namespace from its enclosing namespace set. |
| 5402 | if (WasQualifiedName) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5403 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5404 | |
| 5405 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5406 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5407 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5408 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5409 | S.getLangOptions().CPlusPlus0x? |
| 5410 | diag::err_explicit_instantiation_unqualified_wrong_namespace |
| 5411 | : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5412 | << D << OrigContext; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5413 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5414 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5415 | } |
| 5416 | |
| 5417 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5418 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5419 | if (!SS.isSet()) |
| 5420 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5421 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5422 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5423 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5424 | // or a static data member of a class template specialization, the name of |
| 5425 | // the class template specialization in the qualified-id for the member |
| 5426 | // name shall be a simple-template-id. |
| 5427 | // |
| 5428 | // C++98 has the same restriction, just worded differently. |
| 5429 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5430 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5431 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5432 | if (isa<TemplateSpecializationType>(T)) |
| 5433 | return true; |
| 5434 | |
| 5435 | return false; |
| 5436 | } |
| 5437 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5438 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5439 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5440 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5441 | SourceLocation ExternLoc, |
| 5442 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5443 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5444 | SourceLocation KWLoc, |
| 5445 | const CXXScopeSpec &SS, |
| 5446 | TemplateTy TemplateD, |
| 5447 | SourceLocation TemplateNameLoc, |
| 5448 | SourceLocation LAngleLoc, |
| 5449 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5450 | SourceLocation RAngleLoc, |
| 5451 | AttributeList *Attr) { |
| 5452 | // Find the class template we're specializing |
| 5453 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5454 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5455 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5456 | |
| 5457 | // Check that the specialization uses the same tag kind as the |
| 5458 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5459 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5460 | assert(Kind != TTK_Enum && |
| 5461 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5462 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5463 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5464 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5465 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5466 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5467 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5468 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5469 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5470 | diag::note_previous_use); |
| 5471 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5472 | } |
| 5473 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5474 | // C++0x [temp.explicit]p2: |
| 5475 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5476 | // definition and an explicit instantiation declaration. An explicit |
| 5477 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5478 | TemplateSpecializationKind TSK |
| 5479 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5480 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5481 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5482 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5483 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5484 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5485 | |
| 5486 | // Check that the template argument list is well-formed for this |
| 5487 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5488 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5489 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5490 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5491 | return true; |
| 5492 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5493 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5494 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5495 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5496 | // Find the class template specialization declaration that |
| 5497 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5498 | void *InsertPos = 0; |
| 5499 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5500 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5501 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5502 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5503 | TemplateSpecializationKind PrevDecl_TSK |
| 5504 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 5505 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5506 | // C++0x [temp.explicit]p2: |
| 5507 | // [...] An explicit instantiation shall appear in an enclosing |
| 5508 | // namespace of its template. [...] |
| 5509 | // |
| 5510 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5511 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 5512 | SS.isSet())) |
| 5513 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5514 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5515 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5516 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5517 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5518 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5519 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5520 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5521 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5522 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5523 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5524 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5525 | // Even though HasNoEffect == true means that this explicit instantiation |
| 5526 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 5527 | |
| 5528 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 5529 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5530 | // Since the only prior class template specialization with these |
| 5531 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5532 | // declaration node as our own, updating the source location |
| 5533 | // for the template name to reflect our new declaration. |
| 5534 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5535 | Specialization = PrevDecl; |
| 5536 | Specialization->setLocation(TemplateNameLoc); |
| 5537 | PrevDecl = 0; |
| 5538 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5539 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5540 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5541 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5542 | // Create a new class template specialization declaration node for |
| 5543 | // this explicit specialization. |
| 5544 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5545 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5546 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5547 | KWLoc, TemplateNameLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5548 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5549 | Converted.data(), |
| 5550 | Converted.size(), |
| 5551 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5552 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5553 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5554 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5555 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5556 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5557 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5558 | } |
| 5559 | |
| 5560 | // Build the fully-sugared type for this explicit instantiation as |
| 5561 | // the user wrote in the explicit instantiation itself. This means |
| 5562 | // that we'll pretty-print the type retrieved from the |
| 5563 | // specialization's declaration the way that the user actually wrote |
| 5564 | // the explicit instantiation, rather than formatting the name based |
| 5565 | // on the "canonical" representation used to store the template |
| 5566 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5567 | TypeSourceInfo *WrittenTy |
| 5568 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5569 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5570 | Context.getTypeDeclType(Specialization)); |
| 5571 | Specialization->setTypeAsWritten(WrittenTy); |
| 5572 | TemplateArgsIn.release(); |
| 5573 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5574 | // Set source locations for keywords. |
| 5575 | Specialization->setExternLoc(ExternLoc); |
| 5576 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 5577 | |
| 5578 | // Add the explicit instantiation into its lexical context. However, |
| 5579 | // since explicit instantiations are never found by name lookup, we |
| 5580 | // just put it into the declaration context directly. |
| 5581 | Specialization->setLexicalDeclContext(CurContext); |
| 5582 | CurContext->addDecl(Specialization); |
| 5583 | |
| 5584 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 5585 | if (HasNoEffect) { |
| 5586 | // Set the template specialization kind. |
| 5587 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5588 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5589 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5590 | |
| 5591 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5592 | // A definition of a class template or class member template |
| 5593 | // shall be in scope at the point of the explicit instantiation of |
| 5594 | // the class template or class member template. |
| 5595 | // |
| 5596 | // This check comes when we actually try to perform the |
| 5597 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5598 | ClassTemplateSpecializationDecl *Def |
| 5599 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5600 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5601 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5602 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5603 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5604 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5605 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 5606 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5607 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5608 | // Instantiate the members of this class template specialization. |
| 5609 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5610 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5611 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 5612 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 5613 | |
| 5614 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 5615 | // TSK_ExplicitInstantiationDefinition |
| 5616 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 5617 | TSK == TSK_ExplicitInstantiationDefinition) |
| 5618 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5619 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5620 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5621 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5622 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5623 | // Set the template specialization kind. |
| 5624 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5625 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5626 | } |
| 5627 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5628 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5629 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5630 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5631 | SourceLocation ExternLoc, |
| 5632 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5633 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5634 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5635 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5636 | IdentifierInfo *Name, |
| 5637 | SourceLocation NameLoc, |
| 5638 | AttributeList *Attr) { |
| 5639 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 5640 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5641 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5642 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5643 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
| 5644 | MultiTemplateParamsArg(*this, 0, 0), |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 5645 | Owned, IsDependent, false, false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5646 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5647 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 5648 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5649 | if (!TagD) |
| 5650 | return true; |
| 5651 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5652 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5653 | if (Tag->isEnum()) { |
| 5654 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 5655 | << Context.getTypeDeclType(Tag); |
| 5656 | return true; |
| 5657 | } |
| 5658 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 5659 | if (Tag->isInvalidDecl()) |
| 5660 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5661 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5662 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 5663 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 5664 | if (!Pattern) { |
| 5665 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 5666 | << Context.getTypeDeclType(Record); |
| 5667 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 5668 | return true; |
| 5669 | } |
| 5670 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5671 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5672 | // If the explicit instantiation is for a class or member class, the |
| 5673 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5674 | // simple-template-id. |
| 5675 | // |
| 5676 | // C++98 has the same restriction, just worded differently. |
| 5677 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5678 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5679 | << Record << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5680 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5681 | // C++0x [temp.explicit]p2: |
| 5682 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5683 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5684 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 5685 | TemplateSpecializationKind TSK |
| 5686 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5687 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5688 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5689 | // C++0x [temp.explicit]p2: |
| 5690 | // [...] An explicit instantiation shall appear in an enclosing |
| 5691 | // namespace of its template. [...] |
| 5692 | // |
| 5693 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5694 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5695 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5696 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5697 | CXXRecordDecl *PrevDecl |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5698 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5699 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5700 | PrevDecl = Record; |
| 5701 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5702 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5703 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5704 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5705 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5706 | PrevDecl, |
| 5707 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5708 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5709 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5710 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5711 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5712 | return TagD; |
| 5713 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5714 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5715 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5716 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5717 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5718 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5719 | // 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] | 5720 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5721 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5722 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5723 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 5724 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 5725 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5726 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 5727 | << Pattern; |
| 5728 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5729 | } else { |
| 5730 | if (InstantiateClass(NameLoc, Record, Def, |
| 5731 | getTemplateInstantiationArgs(Record), |
| 5732 | TSK)) |
| 5733 | return true; |
| 5734 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5735 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5736 | if (!RecordDef) |
| 5737 | return true; |
| 5738 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5739 | } |
| 5740 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5741 | // Instantiate all of the members of the class. |
| 5742 | InstantiateClassMembers(NameLoc, RecordDef, |
| 5743 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5744 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5745 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 5746 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 5747 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5748 | // FIXME: We don't have any representation for explicit instantiations of |
| 5749 | // member classes. Such a representation is not needed for compilation, but it |
| 5750 | // should be available for clients that want to see all of the declarations in |
| 5751 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5752 | return TagD; |
| 5753 | } |
| 5754 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5755 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 5756 | SourceLocation ExternLoc, |
| 5757 | SourceLocation TemplateLoc, |
| 5758 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5759 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5760 | // TODO: check if/when DNInfo should replace Name. |
| 5761 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 5762 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5763 | if (!Name) { |
| 5764 | if (!D.isInvalidType()) |
| 5765 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 5766 | diag::err_explicit_instantiation_requires_name) |
| 5767 | << D.getDeclSpec().getSourceRange() |
| 5768 | << D.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5769 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5770 | return true; |
| 5771 | } |
| 5772 | |
| 5773 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 5774 | // we find one that is. |
| 5775 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5776 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5777 | S = S->getParent(); |
| 5778 | |
| 5779 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 5780 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 5781 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5782 | if (R.isNull()) |
| 5783 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5784 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5785 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 5786 | // Cannot explicitly instantiate a typedef. |
| 5787 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 5788 | << Name; |
| 5789 | return true; |
| 5790 | } |
| 5791 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5792 | // C++0x [temp.explicit]p1: |
| 5793 | // [...] An explicit instantiation of a function template shall not use the |
| 5794 | // inline or constexpr specifiers. |
| 5795 | // Presumably, this also applies to member functions of class templates as |
| 5796 | // well. |
| 5797 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5798 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5799 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5800 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5801 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5802 | // FIXME: check for constexpr specifier. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5803 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5804 | // C++0x [temp.explicit]p2: |
| 5805 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5806 | // definition and an explicit instantiation declaration. An explicit |
| 5807 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5808 | TemplateSpecializationKind TSK |
| 5809 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5810 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5811 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5812 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5813 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5814 | |
| 5815 | if (!R->isFunctionType()) { |
| 5816 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5817 | // A [...] static data member of a class template can be explicitly |
| 5818 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5819 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5820 | if (Previous.isAmbiguous()) |
| 5821 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5822 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 5823 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5824 | if (!Prev || !Prev->isStaticDataMember()) { |
| 5825 | // We expect to see a data data member here. |
| 5826 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 5827 | << Name; |
| 5828 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5829 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5830 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5831 | return true; |
| 5832 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5833 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5834 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 5835 | // FIXME: Check for explicit specialization? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5836 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5837 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 5838 | << Prev; |
| 5839 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 5840 | // FIXME: Can we provide a note showing where this was declared? |
| 5841 | return true; |
| 5842 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5843 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5844 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5845 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5846 | // or a static data member of a class template specialization, the name of |
| 5847 | // the class template specialization in the qualified-id for the member |
| 5848 | // name shall be a simple-template-id. |
| 5849 | // |
| 5850 | // C++98 has the same restriction, just worded differently. |
| 5851 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5852 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5853 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5854 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5855 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5856 | // Check the scope of this explicit instantiation. |
| 5857 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5858 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5859 | // Verify that it is okay to explicitly instantiate here. |
| 5860 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 5861 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5862 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5863 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5864 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5865 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5866 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5867 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5868 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5869 | return (Decl*) 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5870 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5871 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5872 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5873 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5874 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5875 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5876 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5877 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5878 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5879 | |
| 5880 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5881 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5882 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5883 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5884 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 5885 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5886 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 5887 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5888 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 5889 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5890 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5891 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5892 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 5893 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5894 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5895 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5896 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5897 | // A [...] function [...] can be explicitly instantiated from its template. |
| 5898 | // A member function [...] of a class template can be explicitly |
| 5899 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5900 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5901 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5902 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5903 | P != PEnd; ++P) { |
| 5904 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5905 | if (!HasExplicitTemplateArgs) { |
| 5906 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 5907 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 5908 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5909 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5910 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5911 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 5912 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5913 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5914 | } |
| 5915 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5916 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5917 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 5918 | if (!FunTmpl) |
| 5919 | continue; |
| 5920 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5921 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5922 | FunctionDecl *Specialization = 0; |
| 5923 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5924 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5925 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5926 | R, Specialization, Info)) { |
| 5927 | // FIXME: Keep track of almost-matches? |
| 5928 | (void)TDK; |
| 5929 | continue; |
| 5930 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5931 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5932 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5933 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5934 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5935 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5936 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5937 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5938 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5939 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 5940 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 5941 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5942 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5943 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5944 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5945 | |
| 5946 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 5947 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5948 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5949 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5950 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5951 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 5952 | << Specialization |
| 5953 | << (Specialization->getTemplateSpecializationKind() == |
| 5954 | TSK_ExplicitSpecialization); |
| 5955 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 5956 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5957 | } |
| 5958 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5959 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5960 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 5961 | PrevDecl = Specialization; |
| 5962 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5963 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5964 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5965 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5966 | PrevDecl, |
| 5967 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5968 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5969 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5970 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5971 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5972 | // FIXME: We may still want to build some representation of this |
| 5973 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5974 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5975 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5976 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 5977 | |
| 5978 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5979 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5980 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5981 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5982 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5983 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5984 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5985 | // or a static data member of a class template specialization, the name of |
| 5986 | // the class template specialization in the qualified-id for the member |
| 5987 | // name shall be a simple-template-id. |
| 5988 | // |
| 5989 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5990 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5991 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5992 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5993 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5994 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5995 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5996 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5997 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5998 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5999 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6000 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6001 | D.getIdentifierLoc(), |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6002 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6003 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6004 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6005 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6006 | } |
| 6007 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6008 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6009 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 6010 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 6011 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 6012 | // This has to hold, because SS is expected to be defined. |
| 6013 | assert(Name && "Expected a name in a dependent tag"); |
| 6014 | |
| 6015 | NestedNameSpecifier *NNS |
| 6016 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6017 | if (!NNS) |
| 6018 | return true; |
| 6019 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6020 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 6021 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6022 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 6023 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6024 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6025 | return true; |
| 6026 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6027 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6028 | // Create the resulting type. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6029 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6030 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 6031 | |
| 6032 | // Create type-source location information for this type. |
| 6033 | TypeLocBuilder TLB; |
| 6034 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
| 6035 | TL.setKeywordLoc(TagLoc); |
| 6036 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6037 | TL.setNameLoc(NameLoc); |
| 6038 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6039 | } |
| 6040 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6041 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6042 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6043 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6044 | SourceLocation IdLoc) { |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6045 | if (SS.isInvalid()) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6046 | return true; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6047 | |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6048 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 6049 | !getLangOptions().CPlusPlus0x) |
| 6050 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6051 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6052 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6053 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6054 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 6055 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 6056 | if (T.isNull()) |
| 6057 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6058 | |
| 6059 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6060 | if (isa<DependentNameType>(T)) { |
| 6061 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6062 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6063 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6064 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6065 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6066 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6067 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6068 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6069 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6070 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6071 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6072 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6073 | } |
| 6074 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6075 | TypeResult |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6076 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6077 | const CXXScopeSpec &SS, |
| 6078 | SourceLocation TemplateLoc, |
| 6079 | TemplateTy TemplateIn, |
| 6080 | SourceLocation TemplateNameLoc, |
| 6081 | SourceLocation LAngleLoc, |
| 6082 | ASTTemplateArgsPtr TemplateArgsIn, |
| 6083 | SourceLocation RAngleLoc) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6084 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 6085 | !getLangOptions().CPlusPlus0x) |
| 6086 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6087 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6088 | |
| 6089 | // Translate the parser's template argument list in our AST format. |
| 6090 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 6091 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 6092 | |
| 6093 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6094 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 6095 | // Construct a dependent template specialization type. |
| 6096 | assert(DTN && "dependent template has non-dependent name?"); |
| 6097 | assert(DTN->getQualifier() |
| 6098 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 6099 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 6100 | DTN->getQualifier(), |
| 6101 | DTN->getIdentifier(), |
| 6102 | TemplateArgs); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6103 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6104 | // Create source-location information for this type. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6105 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6106 | DependentTemplateSpecializationTypeLoc SpecTL |
| 6107 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6108 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6109 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6110 | SpecTL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 6111 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6112 | SpecTL.setNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6113 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6114 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6115 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 6116 | } |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6117 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6118 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 6119 | if (T.isNull()) |
| 6120 | return true; |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6121 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6122 | // Provide source-location information for the template specialization |
| 6123 | // type. |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6124 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6125 | TemplateSpecializationTypeLoc SpecTL |
| 6126 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
| 6127 | |
| 6128 | // FIXME: No place to set the location of the 'template' keyword! |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6129 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6130 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6131 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6132 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6133 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 6134 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6135 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 6136 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 6137 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6138 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6139 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6140 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 6141 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 6142 | } |
| 6143 | |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6144 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6145 | /// \brief Build the type that describes a C++ typename specifier, |
| 6146 | /// e.g., "typename T::type". |
| 6147 | QualType |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6148 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 6149 | SourceLocation KeywordLoc, |
| 6150 | NestedNameSpecifierLoc QualifierLoc, |
| 6151 | const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6152 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6153 | CXXScopeSpec SS; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6154 | SS.Adopt(QualifierLoc); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6155 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6156 | DeclContext *Ctx = computeDeclContext(SS); |
| 6157 | if (!Ctx) { |
| 6158 | // If the nested-name-specifier is dependent and couldn't be |
| 6159 | // resolved to a type, build a typename type. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6160 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 6161 | return Context.getDependentNameType(Keyword, |
| 6162 | QualifierLoc.getNestedNameSpecifier(), |
| 6163 | &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 6164 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6165 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6166 | // If the nested-name-specifier refers to the current instantiation, |
| 6167 | // the "typename" keyword itself is superfluous. In C++03, the |
| 6168 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 6169 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 6170 | // 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] | 6171 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6172 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 6173 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6174 | |
| 6175 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6176 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6177 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6178 | unsigned DiagID = 0; |
| 6179 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6180 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6181 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6182 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6183 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6184 | |
| 6185 | case LookupResult::FoundUnresolvedValue: { |
| 6186 | // We found a using declaration that is a value. Most likely, the using |
| 6187 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6188 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6189 | IILoc); |
| 6190 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 6191 | << Name << Ctx << FullRange; |
| 6192 | if (UnresolvedUsingValueDecl *Using |
| 6193 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6194 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6195 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 6196 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 6197 | } |
| 6198 | } |
| 6199 | // Fall through to create a dependent typename type, from which we can recover |
| 6200 | // better. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6201 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 6202 | case LookupResult::NotFoundInCurrentInstantiation: |
| 6203 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6204 | return Context.getDependentNameType(Keyword, |
| 6205 | QualifierLoc.getNestedNameSpecifier(), |
| 6206 | &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6207 | |
| 6208 | case LookupResult::Found: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6209 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6210 | // We found a type. Build an ElaboratedType, since the |
| 6211 | // typename-specifier was just sugar. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6212 | return Context.getElaboratedType(ETK_Typename, |
| 6213 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6214 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6215 | } |
| 6216 | |
| 6217 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6218 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6219 | break; |
| 6220 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6221 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6222 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6223 | return QualType(); |
| 6224 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6225 | case LookupResult::FoundOverloaded: |
| 6226 | DiagID = diag::err_typename_nested_not_type; |
| 6227 | Referenced = *Result.begin(); |
| 6228 | break; |
| 6229 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 6230 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6231 | return QualType(); |
| 6232 | } |
| 6233 | |
| 6234 | // If we get here, it's because name lookup did not find a |
| 6235 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6236 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6237 | IILoc); |
| 6238 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6239 | if (Referenced) |
| 6240 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 6241 | << Name; |
| 6242 | return QualType(); |
| 6243 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6244 | |
| 6245 | namespace { |
| 6246 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 6247 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6248 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6249 | SourceLocation Loc; |
| 6250 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6251 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6252 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 6253 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6254 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6255 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6256 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6257 | DeclarationName Entity) |
| 6258 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6259 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6260 | |
| 6261 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6262 | /// transformed. |
| 6263 | /// |
| 6264 | /// For the purposes of type reconstruction, a type has already been |
| 6265 | /// transformed if it is NULL or if it is not dependent. |
| 6266 | bool AlreadyTransformed(QualType T) { |
| 6267 | return T.isNull() || !T->isDependentType(); |
| 6268 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6269 | |
| 6270 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6271 | /// rebuilt. |
| 6272 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6273 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6274 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 6275 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6276 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6277 | /// \brief Sets the "base" location and entity when that |
| 6278 | /// information is known based on another transformation. |
| 6279 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 6280 | this->Loc = Loc; |
| 6281 | this->Entity = Entity; |
| 6282 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6283 | }; |
| 6284 | } |
| 6285 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6286 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 6287 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6288 | /// 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] | 6289 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6290 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6291 | /// partial specialization thereof). This routine will rebuild that type now |
| 6292 | /// that we have entered the declarator's scope, which may produce different |
| 6293 | /// canonical types, e.g., |
| 6294 | /// |
| 6295 | /// \code |
| 6296 | /// template<typename T> |
| 6297 | /// struct X { |
| 6298 | /// typedef T* pointer; |
| 6299 | /// pointer data(); |
| 6300 | /// }; |
| 6301 | /// |
| 6302 | /// template<typename T> |
| 6303 | /// typename X<T>::pointer X<T>::data() { ... } |
| 6304 | /// \endcode |
| 6305 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 6306 | /// 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] | 6307 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 6308 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6309 | /// 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] | 6310 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 6311 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6312 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 6313 | SourceLocation Loc, |
| 6314 | DeclarationName Name) { |
| 6315 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6316 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6317 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6318 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 6319 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 6320 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6321 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6322 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6323 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 6324 | DeclarationName()); |
| 6325 | return Rebuilder.TransformExpr(E); |
| 6326 | } |
| 6327 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6328 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6329 | if (SS.isInvalid()) |
| 6330 | return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6331 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6332 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6333 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 6334 | DeclarationName()); |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6335 | NestedNameSpecifierLoc Rebuilt |
| 6336 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 6337 | if (!Rebuilt) |
| 6338 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6339 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6340 | SS.Adopt(Rebuilt); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6341 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6342 | } |
| 6343 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6344 | /// \brief Produces a formatted string that describes the binding of |
| 6345 | /// template parameters to template arguments. |
| 6346 | std::string |
| 6347 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6348 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6349 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6350 | } |
| 6351 | |
| 6352 | std::string |
| 6353 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6354 | const TemplateArgument *Args, |
| 6355 | unsigned NumArgs) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6356 | llvm::SmallString<128> Str; |
| 6357 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6358 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6359 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6360 | return std::string(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6361 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6362 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6363 | if (I >= NumArgs) |
| 6364 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6365 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6366 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6367 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6368 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6369 | Out << ", "; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6370 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6371 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6372 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6373 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6374 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6375 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6376 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6377 | Out << " = "; |
| 6378 | Args[I].print(Context.PrintingPolicy, Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6379 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6380 | |
| 6381 | Out << ']'; |
| 6382 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6383 | } |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 6384 | |
| 6385 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) { |
| 6386 | if (!FD) |
| 6387 | return; |
| 6388 | FD->setLateTemplateParsed(Flag); |
| 6389 | } |
| 6390 | |
| 6391 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 6392 | DeclContext *DC = CurContext; |
| 6393 | |
| 6394 | while (DC) { |
| 6395 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 6396 | const FunctionDecl *FD = RD->isLocalClass(); |
| 6397 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 6398 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 6399 | return false; |
| 6400 | |
| 6401 | DC = DC->getParent(); |
| 6402 | } |
| 6403 | return false; |
| 6404 | } |